Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

As a junior programmer, your work last week on “Legendary Epics” is earning you

ID: 2247193 • Letter: A

Question

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. Your class definition and executable class should be zipped up in a folder named FirstLast_HW3. Submit the zip archive in Blackboard under the assignments area.

Explanation / Answer

Content of class Character.java

Directions:-

1.) Make an text file rename it with extension to Character.java

2.) Paste the following content in it.

package CharacterTest;

public class Character {

private String charName;

private int startHealth;

private double startCurrency;

public int getHealth() {

return startHealth;

}

public void setHealth(int startHealth) {

this.startHealth = startHealth;

}

public String getCharName() {

return charName;

}

public void setCharName(String charName) {

this.charName = charName;

}

public double getCurrency() {

return startCurrency;

}

public void setCurrency(double startCurrency) {

this.startCurrency = startCurrency;

}

}

Content of class Character.java

Directions:-

1.) Make an text file rename it with extension to Charactergen.java

2.) Paste the following content in it.

package CharacterTest;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Random;

import java.util.Scanner;

public class Charactergen {

// main class to execute the code

public static void main(String args[]) {

final double BASE_CURRENCY = 10.55;

final int BASE_HEALTH = 50;

int minHealthBonus = 1;

int maxHealthBonus = 5;

String cName;

// initialize of the reference variable for Character class

Character human = new Character();

System.out.println("Enter a character name : ");

// Taking input from user with buffered reader

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try {

String s = br.readLine();

// setting values in reference variable as directed

human.setCharName(s);

human.setHealth(calculateHealth(BASE_HEALTH, minHealthBonus, maxHealthBonus));

human.setCurrency(calculateCurrency(BASE_CURRENCY));

// displaying output from Character class object

displayOutput(human.getCharName(), human.getHealth(), human.getCurrency());

} catch (IOException e) {

e.printStackTrace();

}

}

/**This method calculates the health where variability is between the min and max values provided

* @param BASE_HEALTH -- base value for health

* @param minHealthBonus -- minimum value for variability

* @param maxHealthBonus -- maximum value for variability

* @return integer value for calculated health

* */

private static int calculateHealth(int BASE_HEALTH, int minHealthBonus, int maxHealthBonus) {

Random r = new Random();

int Result = r.nextInt(maxHealthBonus - minHealthBonus) + minHealthBonus;

return BASE_HEALTH + Result;

}

/**This method calculates the currency where variability is between 0.0 - 1.0 less from

* BASE_CURRENCY with decimal limit till two decimal places

*

* @param BASE_CURRENCY - base value for currency

* @return double value for calculated currency

*

* */

private static double calculateCurrency(double BASE_CURRENCY) {

Random r = new Random();

double result = r.nextDouble();

result = BASE_CURRENCY - result;

return Math.round(result * 100.0) / 100.0;

}

/**This method displays the content

* @param charName - String

* @param startHealth - int

* @param startCurrency - double

* */

private static void displayOutput(String charName, int startHealth, double startCurrency) {

System.out.println("Character name : " + charName);

System.out.println("Starting health : " + startHealth);

System.out.println("Starting currency : " + startCurrency);

}

}

P.S.

*Save these two java files in same directory

*This has been done in core java as none of the other stuff is mentioned in the question

*Making dialog as mentioned in betwwen question is not allowed in core java and is not properly mentioned in question as it states "dialog box, identical to last week’s assignment.", which we are not aware about.

* You can change package name according to your requirement and you name and personal details by own at the top of both of the files.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote