I don’t know what he trying to tell me Don’t modify values of the attributes dir
ID: 3916659 • Letter: I
Question
I don’t know what he trying to tell me
Don’t modify values of the attributes directly in the constructor with arguments,
Please uses the set methods. We do this to "encapsulate" the variables and offer limited points for modification in the program. The default constructor should also give initial values for all attributes. Do not leave it blank, to let the Java interpreter to fill it. It is best to have initial values given by the programmer.
Please how me where my issues is
The equals method does not need the if statement, just return the result of the comparisons made as shown in the example above.
**************
import java.util.Scanner;
public class DudeAMonsterDriver
{ //Start DudeAMonsterDriver Class
public static void main (String[]args)
{ // Start Static Void Main
DudeAMonster firstDudeAMonster; // This is to declare the first monster
DudeAMonster secondDudeAMonster; // This is to declare the second monster
DudeAMonster thirdDudeAMonster; // This is to declare the third monster
// Create the Scary Monsters three times
firstDudeAMonster = new DudeAMonster();
secondDudeAMonster = new DudeAMonster();
thirdDudeAMonster = new DudeAMonster();
//thirdDudeAMonster = new DudeAMonster // for testing of parameterized constructor
// Set the monster Name
firstDudeAMonster.setname("Small Hairy Scary Monster");
secondDudeAMonster.setname("Small Hairy Scary Monster");
thirdDudeAMonster.setname("Large Hairy Scary Monster");
// Set the monster Hitpoints
firstDudeAMonster.sethitPoints(100);
secondDudeAMonster.sethitPoints(100);
thirdDudeAMonster.sethitPoints(300);
// Set the monster currency level
firstDudeAMonster.setgoldInPouch(20);
secondDudeAMonster.setgoldInPouch(20);
thirdDudeAMonster.setgoldInPouch(50);
System.out.println("First monster is a " + firstDudeAMonster.getname()+" with hit points "+firstDudeAMonster.gethitPoints()+" and with gold = "+firstDudeAMonster.getgoldInPouch()); // prints out monster name, hitpoint and gold
System.out.println("Second monster is a " + secondDudeAMonster.getname()+" with hit points "+secondDudeAMonster.gethitPoints()+" and with gold = "+secondDudeAMonster.getgoldInPouch()); // prints out monster name, hitpoint and gold
System.out.println("Third monster is a " + thirdDudeAMonster.getname()+" with hit points "+thirdDudeAMonster.gethitPoints()+" and with gold = "+thirdDudeAMonster.getgoldInPouch()); // prints out monster name, hitpoint and gold
if (firstDudeAMonster.equals(secondDudeAMonster))
{
System.out.println("these monsters are the same");
}
else
System.out.println("these monsters are not the same");
} // End Static Void Main
} //End DudeAMonsterDriver Class
*************
/*******************************************************************************
*DudeAMonster.java
*Richard Yaple
*
*This program will creates monsters objects and compares them
********************************************************************************/
public class DudeAMonster
{ // Start of public class
private String name; // instance variable for name of monster
private int HP; // instance variable to hold the number of hitpoints monster have
private int gIP; // instance veriable to hold how much gold monster has
// constructor
public DudeAMonster(String name, int HP, int gIP)
{
this.name = name;
this.HP = HP;
this.gIP = gIP;
}
// Accessor Methods
//default constructor
public DudeAMonster() {
}
public String getname() { // method to return the current MonsterName
return this.name;
}
public int getHP() { // method to return the current number of hitpoints
return this.HP;
}
public int getgIP() { // method to return the current number of gold in pouch
return this.gIP;
}
// Mutator Methods
public void setname(String newname)
{ // method to change monsters name
this.name = newname;
}
public void setHP(int HitPoints)
{ // method to change # of legs
this.HP = HitPoints;
}
public void setgIP(int gIP)
{ // method to change # of legs
this.gIP = gIP;
}
public boolean equals(DudeAMonster secondDudeAMonster)
{
if ((this.getHP()==(secondDudeAMonster.getHP())) && ((this.getgIP()== secondDudeAMonster.getgIP())) &&
(this.name.equals(secondDudeAMonster.name)))
{
return true;
}
else
{
return false;
}
}
} // End of public class
Explanation / Answer
There are some problems in the code you provided lets at them one by one.
************************************************************************************************************************
1) Problem :: requirement was "
Don’t modify values of the attributes directly in the constructor with arguments,
Please uses the set methods"
But in your code constructer modifies the value directly, here use set methods which you have already made.
So code look's like below ::
***********************************************************************************************************************
2) Problem :: The default constructor should also give initial values for all attributes. but in the code provided there is empty body for constructer with no arguments
It should assign some initial values.
Code looks like below
What default values are required is up to the programmer.
***********************************************************************************************************************
3) Problem :: Equals method implemented uses if else
Here I will propose two solutions any can be used.
Solution 1)
Solution 2)
Note in a solution I have not used the variable to store the result and directly returned the computed value.
***********************************************************************************************************************
4) Here's something extra which was not asked in question
There will be compilation error because methods name mismatch.
Defined getter methods are not used and some other names are used. Check your code once.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.