JAVA CODING: (5) (15 points) Polymorphisnm This exercise is another demonstratio
ID: 3753681 • Letter: J
Question
JAVA CODING:
(5) (15 points) Polymorphisnm This exercise is another demonstration of polymorphism via inheritance. Note polymorphism can also be done using Composition and Interface approaches. We will cover this later after the Abstraction and Interface lecture (next lecture). The base class Student provides methods (study(), party(), and sleep()) that are common to all students. The derived classes (Grad, UnderGrad, PartTime) override these methods to perform different behaviors depending on the specific type of students. For this exercise, you are using the following 3 habits for each type of students: Student Grad Do Research Sometimes Not that much Undergrad Sometimes Party hard Sleep a lot Partime Make $$ and study Party at work Part Slee Sleep at work Again no if-then-else, or switch case for student selections. In your main( program, build an array of Students [ and fill it with the three different subtype types. Use the for enhanced loop (for (Students x : stud) to walk the Students data type Your final results should look like the following: Grad: Grad students do researckh Grad parties sometime Grad students don't sleep that much UnderGrad: UnderGrad studies sometime UnderGrad tends to party hard UnderGrad tends to sleep a lot PartTime: PartTime students make $$ and study PartTime students party at work PartTime students sleep at workExplanation / Answer
Students.java
public class Students {
private String name="Students";
protected void study()
{
System.out.println("Students.study()");
}
protected void party()
{
System.out.println("Students.party()");
}
protected void sleep()
{
System.out.println("Students.sleep()");
}
public String toString()
{
return name;
}
}
_________________
Grad.java
public class Grad extends Students {
private String name="Grad";
protected void study()
{
System.out.println("Grad students do research");
}
protected void party()
{
System.out.println("Grad parties sometime");
}
protected void sleep()
{
System.out.println("Grad students don't sleep that much");
}
public String toString()
{
return name;
}
}
__________________
UnderGrad.java
public class UnderGrad extends Students {
private String name="UnderGrad";
protected void study()
{
System.out.println("UnderGrad studies sometime");
}
protected void party()
{
System.out.println("UnderGrad tends to party hard");
}
protected void sleep()
{
System.out.println("UnderGrad tends to sleep a lot");
}
public String toString()
{
return name;
}
}
____________________
PartTime.java
public class PartTime extends Students {
private String name="PartTime";
protected void study()
{
System.out.println("PartTime students make $$ and study");
}
protected void party()
{
System.out.println("PartTime students party at work");
}
protected void sleep()
{
System.out.println("PartTime students sleep at work");
}
public String toString()
{
return name;
}
}
____________________
Test.java
public class Test {
public static void main(String[] args) {
Students[] stds={new Grad(),new UnderGrad(),new PartTime()};
for(Students x: stds)
{
System.out.println(x.toString()+":");
x.study();
x.party();
x.sleep();
}
}
}
_________________
Output:
Grad:
Grad students do research
Grad parties sometime
Grad students don't sleep that much
UnderGrad:
UnderGrad studies sometime
UnderGrad tends to party hard
UnderGrad tends to sleep a lot
PartTime:
PartTime students make $$ and study
PartTime students party at work
PartTime students sleep at work
_______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.