• Post Reply Bookmark Topic Watch Topic
  • New Topic

Some questions on a program  RSS feed

 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I again have some questions on a code that my teacher wrote.



Thanks a lot for clarification.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the class, pvalue is declared private. Only way, the value of this variable can be changed/mutated from outside the class is through the mutator methods. If you wish the value to remain unchanged, you can simply choose not to provide such a method. Think of a class called user, which encapsulates the birthdate. Using a method, I can change the birthdate, make me born earlier so I am of a legal drinking age!


Only if someone invokes the deliberate(). Notice it has a public access modifier. Actually what this method returns, is the current value of isGuilty.


Your thinking is correct indeed. The object here is this. Meaning the instance on which toString is invoked.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Another point worth mentioning is that all classes in Java inherit the characteristics of Java's root class, Object, which includes the method toString(). The above toString() method overrides the toString() method that the class Juror inherited from the Object class, providing specific functionality desired for the Juror class.
 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Maneesh. However, I still have few questions regarding your explanations.

Maneesh Godbole wrote:
In the class, pvalue is declared private. Only way, the value of this variable can be changed/mutated from outside the class is through the mutator methods. If you wish the value to remain unchanged, you can simply choose not to provide such a method. Think of a class called user, which encapsulates the birthdate. Using a method, I can change the birthdate, make me born earlier so I am of a legal drinking age!

Since in the constructor, we have pvalue=p, so if we want to change pvalue, we can just change the value of p in the main class.
For example: Jury myJury = new Jury(.95); if we want to change pvalue, then we can have: Jury myJury = new Jury(.80). Do you get my point? So why do we need another public method to specifically change it?

Maneesh Godbole wrote:
Only if someone invokes the deliberate(). Notice it has a public access modifier. Actually what this method returns, is the current value of isGuilty.

So if no one invokes the deliberate(), then the current value of isGuilty will just be false, and getVerdict() will return false?
Thanks!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since in the constructor, we have pvalue=p, so if we want to change pvalue, we can just change the value of p in the main class.
For example: Jury myJury = new Jury(.95); if we want to change pvalue, then we can have: Jury myJury = new Jury(.80). Do you get my point? So why do we need another public method to specifically change it?

Sit back and think, what the new really does. In this case, would it change the value or would you get a new object with a different value?


So if no one invokes the deliberate(), then the current value of isGuilty will just be false, and getVerdict() will return false?

Absolutely correct.
 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Since in the constructor, we have pvalue=p, so if we want to change pvalue, we can just change the value of p in the main class.
For example: Jury myJury = new Jury(.95); if we want to change pvalue, then we can have: Jury myJury = new Jury(.80). Do you get my point? So why do we need another public method to specifically change it?

Sit back and think, what the new really does. In this case, would it change the value or would you get a new object with a different value?


Oh! I would get a new object with a different value! The reason I was confused was that I didn't quite get the purpose of pvalue. If I originally have pvalue=95% when I first construct the object, then if I invoke the setPvalue method, the object will have 80% instead, but still the same object? only with a different state? If this is the case, then to me, it doesn't really make a difference if I get a new object with a different value.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I was confused was that I didn't quite get the purpose of pvalue. If I originally have pvalue=95% when I first construct the object, then if I invoke the setPvalue method, the object will have 80% instead, but still the same object? only with a different state?

Yes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!