Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

OOP concept practice with array

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I randomly practicing my OOP logic fundamental.
In this case, I was creating a class object contains of Phone features. I want to determine the maximum volume until 5. I was trying to include array for this matter as follows:



and the method for volume up



There certainly red line on the method up above. I was not quite sure with the methods I used, but I know that array is one the solution for this case. I need advice.
 
Saloon Keeper
Posts: 8008
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Computing the max value in an array involves a simple for loop to visit each element. Show us what you've tried so far.

Not sure why you tossed in volumeUp() method. Where does that fit in to your requirements? I'm surprised to see this method implemented as "static". Is this a  "master" volume control?
 
Emir Rayhan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Computing the max value in an array involves a simple for loop to visit each element. Show us what you've tried so far.

Not sure why you tossed in volumeUp() method. Where does that fit in to your requirements? I'm surprised to see this method implemented as "static". Is this a  "master" volume control?



I have just created this line of codes so far

As expected, the value of n does not added up because of not using loop. I was thinking to use loop, but how do I print the value one by one each time the program is run?
And what about the static method? Should i just ignore it?
 
Carey Brown
Saloon Keeper
Posts: 8008
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emir Rayhan wrote:

This means you have an array of 5 volumeControls. Do you want to loop through these to find out the max volume that is set?

Not sure what your intentions are here. It's "static" so there's only one of them, and it's incrementing "volumeeControl" which if it's the same "volumeControl" variable above, it's an array and you can't increment an array.

Can you explain what you are trying to do with volumeUp() ?
 
Emir Rayhan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Emir Rayhan wrote:

This means you have an array of 5 volumeControls. Do you want to loop through these to find out the max volume that is set?


Can you explain what you are trying to do with volumeUp() ?



Yes I wanted to maximize the volume until 5 maximum, but I am not sure if it is the right statement.

Apologize for not informed it before that there are 2 separate classes at the moment, volumeUp() will be declared in Main class.
 
Marshal
Posts: 72441
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emir Rayhan wrote:. . . volumeUp() will be declared in Main class.

Afraid that is a bad idea. The volume isn't anything to do with the Main class (I think that is a bad name, but that't something unrelated), nor is it a feature of a Radio class. It should be a feature of a Radio object, so it shouldn't be static.
 
Emir Rayhan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Emir Rayhan wrote:. . . volumeUp() will be declared in Main class.

Afraid that is a bad idea. The volume isn't anything to do with the Main class (I think that is a bad name, but that't something unrelated), nor is it a feature of a Radio class. It should be a feature of a Radio object, so it shouldn't be static.



Thank for the tip Campbell. This is totally for practicing purpose so I wasn't thinking that far.
 
Carey Brown
Saloon Keeper
Posts: 8008
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emir Rayhan wrote:Yes I wanted to maximize THE volume until 5 maximum, but I am not sure if it is the right statement.

You say this like there's only one volume but you have an array of 5 volumes.
 
Sheriff
Posts: 16106
268
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my guess on why OP is thinking of using an array: He's envisioning the volume to be depicted as a series of squares. As the volume increases, more squares will light up. Setting to max volume, there would be five little squares lit up. This, of course, is not a good way to think about volume.

@OP, think about this way instead: If you were to represent a Person's age, you'd use a single-value variable because age is a discrete property. That is, you can't be both 3 years old and 52 years old at the same time. Similarly, volume will have a discrete value because it can't be both 2 and 5 at the same time.

An array is more suitable for situations where you can have multiple values at the same time, like say counting how many of each denomination of coins you have in your pocket. In this case, coins[0] might represent the number of 1-cent coins you have, coins[1] the number of 5-cent coins, coins[2] the number of 10-cent coins, and coins[3] the number of 25-cent coins.
 
Junilu Lacar
Sheriff
Posts: 16106
268
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emir Rayhan wrote:This is totally for practicing purpose so I wasn't thinking that far.


Maybe you should be thinking beyond what you're thinking now since when you started this topic you said you were "practicing my OOP logic fundamental." What Campbell suggested is fundamental OOP logic. If you put the logic in main() then you are definitely falling short of OOP.

If you want to get better, don't practice by doing things the wrong way.
 
Always look on the bright side of life. At least this ad is really tiny:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic