continue to compile/run our Java programs from the command line. Create a progra
ID: 3784805 • Letter: C
Question
continue to compile/run our Java programs from the command line.
Create a program that shows the following
We'll need to know a few things about our knight such as his name(hint:String), health(hint:int), number of battles, age, and amount of gold procured from pillaging.
To keep track of all of these attributes, we'll need to create a container to hold each of the specified pieces of information above. To populate each of these containers, we'll need to have a way of entering data from the user.
To get an overall picture of your knight, we'll need a way to export all of these attributes to the screen. Hint: You can use Scanner's next() method to gather the String name.
Acceptance Criteria:
1. Ability to enter Sir Jav-a-lot's attributes.
2. Sir Jav-a-lot's information will be shown on the screen.
a. In addition to showing each of Sir Jav-a-lot's attributes, we need to know the average gold accumulated per battle.
3. This is to be a command line interface only.
4. . Output must be displayed using the printf method.
Explanation / Answer
Hi Friend, Please find my implementation.
Please let me know in case of any issue.
import java.io.PrintWriter;
import java.util.Scanner;
public class Knight {
// attribute of Knight
private String name;
private int health;
private int no_of_battles;
private int age;
private double amount_of_gold;
// constructor
public Knight(String name, int health, int no_of_battles, int age, double amount_of_gold) {
this.name = name;
this.health = health;
this.no_of_battles = no_of_battles;
this.age = age;
this.amount_of_gold = amount_of_gold;
}
// setters and getters of attributes
public String getName() {
return name;
}
public int getHealth() {
return health;
}
public int getNo_of_battles() {
return no_of_battles;
}
public int getAge() {
return age;
}
public double getAmount_of_gold() {
return amount_of_gold;
}
public void setName(String name) {
this.name = name;
}
public void setHealth(int health) {
this.health = health;
}
public void setNo_of_battles(int no_of_battles) {
this.no_of_battles = no_of_battles;
}
public void setAge(int age) {
this.age = age;
}
public void setAmount_of_gold(double amount_of_gold) {
this.amount_of_gold = amount_of_gold;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.next();
System.out.print("Enter health: ");
int health = sc.nextInt();
System.out.print("Enter number of battels: ");
int no_of_battles = sc.nextInt();
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.print("Enter amount of gold: ");
double amount_of_gold = sc.nextDouble();
// creating Knight object
Knight knight = new Knight(name, health, no_of_battles, age, amount_of_gold);
// printing information
PrintWriter pw = new PrintWriter(System.out);
pw.printf("Name of knight : %s ", knight.getName());
pw.printf("Number of batles: %d ", knight.getNo_of_battles());
pw.printf("Health : %d ", knight.getHealth());
pw.printf("Age : %d ", knight.getAge());
pw.printf("Amount of gold : %f ", knight.getAmount_of_gold());
pw.printf("Average gold per battle: %f ", knight.getAmount_of_gold()/knight.getNo_of_battles());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.