Part 1:
Your supervisor, Lester Zamboni, has been approached by a member of the team responsible for building separate types of characters in Legendary Epics. Knowing that you are his ace programmer on the team, he volunteered you to help build new features in the character creation routine:
Background: Most combat-based games have different types of character classes that fulfill different types of roles. In Legendary Epics, there are four roles: Barbarian, Magician, Cleric, and Rogue. Each has their own strengths and weaknesses, but after entering a character name, the user must choose one of the four roles. Present the user with an input box that takes their choice. Note: The input box needs to expect a 1, 2, 3, or 4. If anything different is entered, you should automatically default the role to Barbarian.
You will need to add this code to Character.java:
A new class instance variable that is a string, called charRole.
A new setter and getter (setCharRole() and getCharRole()). Notice that setCharRole() accepts a string argument.
You will need to add this code to Charactergen.java:
Create a method called roleSelect() that accepts a single string as input parameter. roleSelect() should do these things:
Convert the string input variable to integer through Integer.ParseInt, because data entered in an input box is stored as a string, so you have to convert the data type to integer for processing your switch statement.
Declare a string that will be the string variable that will be returned from this method.
Create a switch statement with 4 cases, each one corresponding with the 4 possible roles in the game. If the user enters anything else, the default case should be Barbarian.
Return the resulting role from the method.
In the main method, make a call to human.setCharRole, using a call to roleSelect() as a parameter.
In the main method, when you call displayOutput(), add getCharRole() as a fourth variable to pass.
In the displayOutput() method, make sure that you add the fourth parameter that represents the user’s role.
Modify the output text, so that it passes the string value of the player’s Role.
Part 2:
At lunch on Friday, a fellow coder who is working on the vendor modules in Legendary Epics needs you to help with a few lines of the code. The vendor code is a bit dynamic, as an item has an advertised price, as well as an acceptable minimum price, which the player cannot see. Write a brief program based on this scenario:
Create an integer CONSTANT that represents named ADVERTISEDPRICE equal to 15.
Create an integer CONSTANT that represents named MINIMUMPRICE equal to 12.
Prompt the user with this input box: “The vendor is offering this item at 15, but you might be able to bargain with him. How much do you wish to offer?”
Evaluate the offer in this manner, using if…else:
If the user offers 11 or less or 16 or more, show the text box below:
If the user offers 12 to 15, show the text box below:
and this is the codes
character.java
// This is a class definition for the Character class.
public class Character
{
// Declare instance variables
private String charName;
private int startHealth;
private double startCurrency;
// **** Add a string variable named charRole
// Declare object methods, getters and setters for character name, health, and currency
// ****** Add a setter named setCharRole
// ****** Add a getter named getCharRole
public void setCharName(String cName)
{
charName = cName;
}
public String getCharName()
{
return charName;
}
public void setHealth(int health)
{
startHealth = health;
}
public int getHealth()
{
return startHealth;
}
public void setCurrency(double currency)
{
startCurrency = currency;
}
public double getCurrency()
{
return startCurrency;
}
}
2) Charctergen.java
import javax.swing.JOptionPane;
import java.text.*;
public class Charactergen {
// This is the main method
public static void main(String[] args)
{
// Declare variables
final double BASE_CURRENCY = 10.55;
final int BASE_HEALTH = 50;
int minHealthBonus = 1;
int maxHealthBonus = 5;
String cName;
// Create a new character object named human
Character human = new Character();
// Use the Character class method to SET the character’s name by prompting the user to enter a character name
human.setCharName(JOptionPane.showInputDialog(null,”Enter your character’s name: “, “Welcome to Legendary Epics!”, JOptionPane.QUESTION_MESSAGE));
// ***** Use the Character class method to SET the character’s role by calling the roleSelect method, passing an integer that returns a string)
// Use the Character class method to SET the character’s health by calling the calculateHealth method, passing BASE_HEALTH, minHealthBonus, and maxHealthBonus as parameters
human.setHealth(calculateHealth(BASE_HEALTH, minHealthBonus, maxHealthBonus));
// Use the Character class method to SET the character’s currency by calling the setCurrency method, passing BASE_CURRENCY as a parameter
human.setCurrency(calculateCurrency(BASE_CURRENCY));
// Call the displayOutput Method by passing the values of character name, role, starting health, and starting currency through the Character class GETTER methods
// ***** Modify the line below to add the role.
displayOutput(human.getCharName(), human.getHealth(), human.getCurrency());
}
// ***** Create a Method called roleSelect() here that sets the character’s role, using a switch statement.
// This method calculates the starting health of the character by accepting 3 parameters
public static int calculateHealth(int bh, int minhb, int maxhb)
{
int health = bh + minhb + (int)(Math.random() * maxhb);
return health;
}
// This method calculates the starting credits of the character by accepting one parameter
public static double calculateCurrency(double cash)
{
//Compute Currency
double startCurrency = cash – Math.random();
return startCurrency;
}
// This method displays the output to the user
// ****** Modify the method header below so that it also accepts the Character’s role as an input parameter.
public static void displayOutput(String charName, int startHealth, double startCurrency)
{
// Create a new decimal format to truncate double output
DecimalFormat df = new DecimalFormat(“#.##”);
// Display output to the user
// ***** Modify the code below so that it also display’s the character’s class.
JOptionPane.showMessageDialog(null,”Your name is ” + charName + “, your starting health is ” + startHealth+ “, and your starting credits are ” + Double.valueOf(df.format(startCurrency))+ “.”, “Your Character Stats”, JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Show transcribed image textWelcome to Legendary Epics! Enter a number that represents your desired character class. 1-Barbarian, 2-Magician, 3-Cleric, 4-Rogue: OK Cancel