JAVA There are two types of Students: Sleepy, Grumpy. Students are mostly simila
ID: 3582008 • Letter: J
Question
JAVA
There are two types of Students: Sleepy, Grumpy. Students are mostly similar, but a little bit different. All students have a current happiness factor which is between in 10 and -100 (initially 0). They all respond to the flame() message when they are flamed by e-mail. Here's what they do when flamed:
1. Print "ouch" and decrement happiness by 1, unless it is already at -100. Sleepy grad students are a little different— they go through the above process twice.
2. Then they read their favorite newsgroup to relax (print "reading"). This causes the happiness factor to go up by 4, although it never goes above 10. In addition, the Grumpy grad student posts to the newsgroup after reading (print "posting").
Write the code for Sleepy, Grumpy, and any support classes. You may omit constructors, and don't worry about the public/private/etc. keywords. Just write the code required by the flame() message, and use inheritance to minimize code repetition. The goal is to demonstrate design so please be careful about what classes/methods you choose to implement
HTML EditorKeyboard Shortcuts
Explanation / Answer
//This class acts as super class
//for Sleepy and Grumpy classes
//Student.java
public abstract class Student {
private int happiness;
public Student() {
//set happiness=0
happiness=0;
}
//Students responds to flame
public void flame()
{
ouch();
read();
}
//wrire ouch tha prints reading
//and decrements hapiness by 1
public void ouch() {
System.out.println("ouch");
happiness=happiness-1;
if(happiness<-100)
happiness=-100;
}
//wrire read tha prints reading
//and increments hapiness by 4
public void read() {
System.out.println("reading");
happiness=happiness+4;
if(happiness>10)
happiness=10;
}
}
------------------------------------------------------------------------------
//Sleepy.java
//The class Sleepy inherites from Student
public class Sleepy extends Student
{
public void ouch()
{
//calling ouch two times
super.ouch();
super.ouch();
}
}
------------------------------------------------------------------------------
//Grumpy.java extends the student class
public class Grumpy extends Student
{
public void read()
{
//calling read one time
super.read();
//print posting
System.out.println("posting");
}
}
------------------------------------------------------------------------------
//Tester.java
public class Tester
{
public static void main(String[] args)
{
//Create an array of Student of type abstract dclass
Student[] std=new Student[2];
//Set Sleepy object
std[0]=new Sleepy();
//Set Grummy object
std[1]=new Grumpy();
System.out.println("Sleepy responding to flame");
//call flame method for sleepy
std[0].flame();
System.out.println("Grumpy responding to flame");
//call flame method for grummy
std[1].flame();
}
}
------------------------------------------------------------------------------
Sample output:
Sleepy responding to flame
ouch
ouch
reading
Grumpy responding to flame
ouch
reading
posting
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.