Java Creating Classes I already submitted this once, but there are a lot of corr
ID: 3916433 • Letter: J
Question
Java Creating Classes I already submitted this once, but there are a lot of corrections needed to be made in my code Below are the instructions, my wrong code, the bold comments stating what needs to be fixed, and my attempt at fixing the code
Instructions: Hero Class Properties: name: String int: rupees Constructors: Working constructor: sets both Working constructor: sets name, rupees default to 0 Default constructor: sets name to empty string, and rupees to 0 Getters/Setters: total of 4 methods Methods: findRupees: int - uses helper method to increment value spendRupees: int - uses helper method to decrement value isEmpty: boolean - returns true if name is empty toString: String - holds name and rupees Helper methods: adjustRupees: void - method adjusts the total rupee value What to submit: Hero.java and main.java - this file tests your hero class
**************************My Code 1st attempt with what needs to be corrected in Bold:**********************
public class Hero
{
//Properties <--------- These MUST be private
int Rupees;
String name;
//Constructor
public Hero() <--------- This is not the proper way, you have a working constructor. this("", 0);
{
name = "";
Rupees = 0;
}
public Hero(String name) <--------- This is incorrect, it doesn't initialize the number of rupees, and you were supposed to use the other constructor this(name)
{
this.name = name;
}
//Overloaded constructor
public Hero(String name, int Rupees)
{
this.name = name;
this.Rupees = Rupees;
}
//Methods
public int findRupees() <--------- NO these are void functions, why would you return a value? it defeats the purpose of the object
{
Rupees++;
return Rupees;
}
public int spendRupees() <--------- NO these are void functions, why would you return a value? it defeats the purpose of the object
{
Rupees--;
return Rupees;
}
public boolean isEmpty(String name) <--------- ? NO, this defeats the purpose of the object
{
if (name.isEmpty()) <--------- I also corrected this in your other program return name.isEmpty();
{
return true;
} else
{
return false;
}
}
public String toString()
{
String result;
result = this.name + ": " + this.Rupees;
return result;
}
//Helper method
public void adjustRupees(int R) <---------This is a helper method, they are private functions, and this is not adjusting rupees, it is setting rupees
{
Rupees = R;
}
}
******************************My code #2 attempt at correcting:*******************************
public class Hero
{
//Private Properties
private int Rupees;
private String name;
//Constructor
public Hero()
{
}
public Hero(String name)
{
name = " ";
Rupees = 0;
}
//Overloaded constructor
public Hero(String name, int Rupees)
{
this.name = name;
this.Rupees = Rupees;
}
//Methods
public findRupees()
{
Rupees++;
}
public int spendRupees()
{
Rupees--;
}
public boolean isEmpty()
{
return name.isEmpty();
}
public String toString()
{
String result;
result = this.name + ": " + this.Rupees;
return result;
}
//Helper method ?? used in main??
public void adjustRupees()
{
}
}
Main.Java
public class Joural_6b
{
public static void main(String[] args)
{
//Object
Hero her = new Hero("Heroes Names");
System.out.println(her);
//Call Method
her.findRupees();
System.out.println(eer);
//Call Method her.findRupees();
//Print System.out.println(Her);
}
public class Hero //Properties int Rupees String name: //Constructor ic Hero ( name Rupees 0 public Hero (String name) his-name = name ; //overloaded constructor public Hero (String name, int Rupees) this.name = name ; this Rupees -Rupees; //Methods public int findRupees () Rupees++ public int spendRupees () Rupees-m retur public boolean isEmpty (String rame)Explanation / Answer
public class Hero
{
private int Rupees;
private String name;
//Constructor
public Hero()
{
name = "";
Rupees = 0;
}
public Hero(String name)
{
this.name = name;
this.Rupees = 0;
}
//Overloaded constructor
public Hero(String name, int Rupees)
{
this.name = name;
this.Rupees = Rupees;
}
//Methods
public void findRupees()
{
Rupees++;
}
public int spendRupees()
{
Rupees--;
}
public boolean isEmpty()
{
return name.isEmpty();
}
public String toString()
{
String result;
result = this.name + ": " + this.Rupees;
return result;
}
//Helper method
private void adjustRupees(int R)
{
//you did not mention what this method does.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.