Part a: Write a class called Geniegotchi with: 1. Private data fields: name (of
ID: 3762952 • Letter: P
Question
Part a: Write a class called Geniegotchi with: 1. Private data fields: 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 confirmation to screen;
• void genieInfo() : prints to screen the Genie’s current name, current endurance 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 file; (The fortunes.txt file include 100 sentences in each line.)
1 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 happiness by 1.
Below is what I try to do:
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Geniegotchi
{
private String name = "Bob";
private int endurance = 4;
private int happiness = 3;
private int luckyNumber = 0;
public void setName(String newName)
{
name = newName;
}
public void setEndurance(int newEndurance)
{
endurance = newEndurance;
}
public void setHappiness (int newHappiness)
{
happiness = newHappiness;
}
public String getName()
{
return name;
}
public int getEndurance ()
{
return endurance;
}
public int getHappiness ()
{
return happiness;
}
public void genieInfo()
{
System.out.println( "current name: "+this.getName());
System.out.println( "current happiness level: "+this.getHappiness());
System.out.println( "current endurance level: "+this.getEndurance());
}
public void feed()
{
if (this.getEndurance() <= 10)
{
setEndurance(1+getEndurance());
}
else
System.out.println("No, thanks...");
}
public void play()
{
if (this.getHappiness()<10)
{
setHappiness(1+getHappiness());
setEndurance(getEndurance()-2);
}
else
System.out.println("No, thanks");
}
public void askFortune()
{
if (this.getHappiness() >6 && this.getEndurance() >5)
{
int luckyNumber = (int) (Math.random()*100)+1;
}
Scanner inputStream = null;
try
{
inputStream = new Scanner (new FileInputStream("fortunes.txt."));
}
catch (FileNotFoundException e)
{
System.out.println("File not found");
System.exit(0);
}
while (inputStream.hasNextLine())
{
int count = 1;
String line = inputStream.nextLine();
if ( count == get_fortuneNumber())
{
System.out.println(line);
}
count++;
}
inputStream.close();
}
public int get_fortuneNumber()
{
return luckyNumber;
}
public static void main(String[]args)
{
Geniegotchi a = new Geniegotchi();
a.setName("Chris");
a.setHappiness(10);
a.setEndurance(5);
a.feed();
a.play();
a.genieInfo();
a.askFortune();
}
}
Explanation / Answer
If you have any further queries, just get back to me.
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Geniegotchi
{
private String name = "Bob";
private int endurance = 4;
private int happiness = 3;
//Sets the name of the Genie.
public void setName(String newName)
{
name = newName;
System.out.println("From now Genie is called: "+name);
}
//Sets the endurance of Genie.
public void setEndurance(int newEndurance)
{
endurance = newEndurance;
}
//Sets the happiness of Genie.
public void setHappiness (int newHappiness)
{
happiness = newHappiness;
}
//Accesses the name of the Genie.
public String getName()
{
return name;
}
//Accesses the endurance level of Genie.
public int getEndurance ()
{
return endurance;
}
//Accesses the happiness level of Genie.
public int getHappiness ()
{
return happiness;
}
//Prints the Genie information to screen.
public void genieInfo()
{
System.out.println( "Current name: "+this.getName());
System.out.println( "Current happiness level: "+this.getHappiness());
System.out.println( "Current endurance level: "+this.getEndurance());
}
//Feeds Genie. Increases the endurance by 1.
public void feed()
{
if (this.getEndurance() < 10)
{
this.setEndurance(1+getEndurance());
}
else
System.out.println("No, thanks...");
}
//Plays with genie. Increases the happiness by 1, at the cost of 2 endurance levels.
public void play()
{
if (this.getHappiness()<10)
{
this.setHappiness(1+this.getHappiness());
this.setEndurance(this.getEndurance()-2);
}
else
System.out.println("No, thanks");
}
public void askFortune()
{
if (this.getHappiness() >6 && this.getEndurance() >5)
{
int luckyNumber = (int)(Math.random()/100)+1;
Scanner inputStream = null;
try
{
inputStream = new Scanner (new FileInputStream("fortunes.txt."));
}
catch (FileNotFoundException e)
{
System.out.println("File not found");
System.exit(0);
}
int count = 1;
while (inputStream.hasNextLine())
{
String line = inputStream.nextLine();
if ( count == luckyNumber)
{
System.out.println(line);
break;
}
count++;
}
inputStream.close();
}
else if(this.getHappiness() > 6 && this.getEndurance() <= 5)
System.out.println("Low Endurance levels. Can't speculate fortune.");
else if(this.getHappiness() <= 6 && this.getEndurance() > 5)
System.out.println("Low in happiness. Can't speculate fortune.");
else
System.out.println("Please feed and play with Geniegotchi to predict the fortune.");
this.setHappiness(this.getHappiness() - 1);
this.setEndurance(this.getEndurance() - 1);
}
public static void main(String[] args)
{
Geniegotchi a = new Geniegotchi();
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("Genie Gotchi.");
System.out.println("1. Change the name of the Genie.");
System.out.println("2. Feed Genie.");
System.out.println("3. Play with Genie.");
System.out.println("4. Get Genie info.");
System.out.println("5. Listen your fortune from Genie.");
System.out.println("6. Exit.");
System.out.println("Enter your choice: ");
int choice = sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the new name for Genie:");
String name = sc.next();
a.setName(name);
break;
case 2:
a.feed();
break;
case 3:
a.play();
break;
case 4:
a.genieInfo();
break;
case 5:
a.askFortune();
break;
case 6:
System.exit(0);
default: System.out.println("Invalid Choice. Try Again.");
}
}
}
}