A) Java Program Write a class called Geniegotchi with: 1. Private data elds: nam
ID: 3763561 • Letter: A
Question
A) Java Program
Write a class called Geniegotchi with: 1. Private data elds: name (of String type, initialized to “Bob”), endurance (of int
type, initialized to 4) and happiness (of int type, initialized to 3); 2. Public methods: • void setName(String newName) : renames Genie with newName, prints
newName conrmation to screen;
• void genieInfo() : prints to screen the Genie’s current name, current en-durance level and current happiness level;
• int getEndurance() : returns current endurance; • int getHappiness() : returns current happiness; • void feed() : this method increases current endurance by 1 if endurance is
less than 10, otherwise it prints a “No, thanks...” message to the screen;
• void play() : if happiness is less than 10, then this method increases current happiness by 1 and decreases current endurance by 2, otherwise it prints a “No, thanks...” message to the screen;
• void askFortune() :
if happiness is greater than 6 and endurance is greater than 5 (that is, if your Genie is happy and healthy enough to predict your fortune...), then:
– using Math.random(), pick a random number ran between 1 and 100
(inclusive);
– display to the screen the fortune from line number ran in the fortunes.txt
le;
if happiness is greater than 6 but endurance is at most 5, then: – display message to screen regarding low endurance;
if endurance greater than 5 but happiness is at most 6, then: – display message to screen regarding low happiness;
if both endurance and happiness are at most 5 and 6 respectively, then: – display message telling user to feed and play with the Geniegotchi.
Regardless of branch, this method should reduce both endurance and hap-piness by 1.
Explanation / Answer
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Geniegotchi {
private String name;
private int endurance;
private int happiness;
public Geniegotchi() {
name="Bob";
endurance=4;
happiness=3;
}
public void setName(String newName) {
name=newName;
System.out.println("Genie's name is: "+name);
}
public void genieInfo() {
System.out.println("Genie's name: "+name);
System.out.println("Endurance level: "+endurance);
System.out.println("Happiness level: "+happiness);
}
public int getEndurance() {
return endurance;
}
public int getHappiness() {
return happiness;
}
public void feed() {
if (endurance<10) {
endurance++;
} else {
System.out.println("No, Thanks...");
}
}
public void play() {
if (happiness<10) {
happiness++;
endurance-=2;
} else {
{
System.out.println("No., thanks...");
}
}
}
public void askFortune() throws IOException {
if (happiness>6 && endurance>5) {
int random = (((int)Math.random()%101) + 1);
String line = Files.readAllLines(Paths.get("fortunes.txt"), null).get(random);
System.out.println(line);
} else if(happiness>6 && endurance<=5) {
System.out.println("Genie has low endurance!!");
} else if(happiness<=6 && endurance>5) {
System.out.println("Genie has low happiness!!");
} else {
System.out.println("Please feed and play with the Geniegotchi");
}
endurance--;
happiness--;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.