• Post Reply Bookmark Topic Watch Topic
  • New Topic

using setter and getter  RSS feed

 
maria sanchez
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to write a Resistor class tells the resistance and type. A Resistor object has a resistance that can vary from 1 to 100 ohms, and a type that is either "film", "carbon", or "wirewound". The resistance only comes in steps, like 1, 2, etc. I must use the method name setResistance, setType, getResistance and getType. When a Resistor object is first instantiated, its default resistance is 1 ohm, and its default type is "film". here is what I have so far.

 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You used int for the type but the description says that 'type that is either "film", "carbon", or "wirewound".' You can't store those values in an int.
Where are the setters?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Don't use numbers for types. You can create an enum:-Read about enums in the Java Tutorials.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like others said, type should not be an int.
you could make it a String, or like Campbell said you can make an array of Strings or an enum
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote: . . . like Campbell said you can make . . .
  • Rule 1: use enums.
  • Rule 2: use enums.
  • Rule 3: there is no rule 3


  • Joking apart, that is exactly what enums were designed for.
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    maria sanchez wrote: . . . When a Resistor object is first instantiated, its default resistance is 1 ohm, and its default type is "film". . . .
    I would implement that as
    If no other values are passed, it defaults to a 1Ω film resistor.
    I would implement that by overloading the constructor. But that can probably wait until you get your enum sorted out.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    maria sanchez wrote:When a Resistor object is first instantiated, its default resistance is 1 ohm, and its default type is "film".

    Why? An object should be instantiated with whatever values make sense.

    Indeed, unless a resistor in real life can actually change, I wonder if there's any need to make your class mutable to being with. And if it isn't, you don't have to worry about setters at all.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    maria sanchez wrote:I must use the method name setResistance, setType, getResistance and getType.

    Well it looks like you've got the getter part down. It was also pointed out to me that, since this is a homework assignment, maybe I shouldn't be so pedantic about why you might want to do this.

    Anyway, the general signature for a setter is:
    public final void setSomething(sometype newValue) { ...
    and the main thing to think about is what you want to do if newValue is invalid. For example a call like:
    resistor.setResistance(999);

    ie, the method isn't just there to set a value; it's there to ensure that it's set correctly.

    HIH

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!