Your homework assignment builds on the concepts from last week’s homework. Some
ID: 3888079 • Letter: Y
Question
Your homework assignment builds on the concepts from last week’s homework. Some of the code and variables should be re-usable.
As a junior programmer, your work last week on “Legendary Epics” is earning you some kudos from your fellow coders; however, your overbearing supervisor, Lester Zamboni, wants you to re-write your code from the prior week to make it a little more “efficient, re-usable, and modular.”
Here are his directives:
You need to write a class definition named Character. In that class definition, you will need these PRIVATE instance variables:
A string called charName
An integer called startHealth
A double called startCurrency
Also in that class definition, you will need a setter and a getter method for each instance variable, totaling 6 methods in all:
setCharName; getCharName; setHealth; getHealth; setCurrency; getCurrency
This class needs to be named Character.java.
Your executable class will still need to be called Charactergen.java. You believe that you will still need these variables in your main method:
final double BASE_CURRENCY = 10.55;
final int BASE_HEALTH = 50;
int minHealthBonus = 1;
int maxHealthBonus = 5;
String cName;
In pseudocode, your main method MUST do 5 things:
// Create a new character object named human
// Use the Character class method to SET the character's name object data field by prompting the user to enter a character name
// Use the Character class method to SET the character's health object data field by calling the calculateHealth method
// Use the Character class method to SET the character's currency object data field by calling the calculateCurrency method
// Call the displayOutput Method by passing the set object data field values of character name, starting health, and starting currency through the Character class GETTER methods
In order to be more modular, you decide that you need three additional methods besides the main method:
calculateHealth(), which accepts passing BASE_HEALTH, minHealthBonus, and maxHealthBonus as parameters, and performs the calculation as done in last week’s assignment. Remember that starting health is a random number between 50 and 55.
calculateCurrency(), which accepts BASE_CURRENCY as a parameter, performs the calculation to generate starting currency, which is always 10.55 minus a random number between 0 and 1.
displayOutput(), which accepts the values of character name, starting health, and starting currency as parameters and displays them to the end user in a message dialog box, identical to last week’s assignment. You MUST use the 3 getter methods to get the human object’s data field values.
Please make sure that you follow the requirements precisely as outlined above. Failure to do so will result in deductions. If you have some doubt regarding the clarity of the instructions, please feel free to contact me.
Don’t forget to document your code at the top by identifying yourself, the date, and the assignment name. Also intersperse your code with comments that describe what your code is doing.
You should end up with 2 classes (separate .java files), Charactergen.java and Character.java.
Explanation / Answer
import java.util.Scanner;
class Character
{
private String charName;
private int startHealth;
private double startCurrency;
void setCharName(String s)
{
charName=s;
}
void setHealth(int h)
{
startHealth=h;
}
void setCurrency(double c)
{
startCurrency=c;
}
String getCharName()
{
return charName;
}
int getHealth()
{
return startHealth;
}
double getCurrency()
{
return startCurrency;
}
}
class Charactergen extends Character
{
final double BASE_CURRENCY = 10.55;
final int BASE_HEALTH = 50;
int minHealthBonus = 1;
int maxHealthBonus = 5;
String cName;
void calculateHealth()
{
System.out.println(" Base Health : "+BASE_HEALTH);
System.out.println(" Minimum Health : "+minHealthBonus);
System.out.println(" Maximum Health : "+maxHealthBonus);
}
void calculateCurrency()
{
System.out.println(" Base Currency : "+BASE_CURRENCY);
}
void displayOutput()
{
System.out.println("Name : "+getCharName());
System.out.println("Start Health : "+getHealth());
System.out.println("Start Currency : "+getCurrency());
}
public static void main(String args[])
{
String s1;
int h1;
double c1;
Scanner sc=new Scanner(System.in);
Character obj=new Character();
System.out.println("Enter the Name");
s1=sc.nextLine();
System.out.println("Enter the start Health");
h1=sc.nextInt();
System.out.println("Enter the start Currency");
c1=sc.nextDouble();
obj.setCharName(s1);
obj.setHealth(h1);
obj.setCurrency(c1);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.