use java portAssist Slayaway Camp Lec3 InheriP Assignment4Fall2018- Class Init C
ID: 3755080 • Letter: U
Question
use java
portAssist Slayaway Camp Lec3 InheriP Assignment4Fall2018- Class Init Construct Polym (Protected View Microsoft Wor _Paget ivoir_.. References -… pse- File R me- Trisert Mailings Review Vie Nav (5) (15 points) Polymorphism 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(), andsleep)) that are common to all students. The derived classes (Grad, UnderGrad, PartTime ) override these methods to perform different behaviors dependingon the specific type of students. For this exercise, you are using the following 3 habits for each type of students ary aaSE Grad Do Research Sometimes Not that much Undergrad Sometimes Party hard Sleep a lot Partime Make $5 and study Party at work Sleep at work Student kage) Study Part Slee va ctorTest.java eak java ava Again no if then-else, or switch case for student selections. In your main () program, build an array of Students [ 1 and fill it with the three different subtype types. Use the for enhanced loop (for (Studentsx: stud) to walk the Students data type Your final results should look like the following: 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 Wo PartTime: Ther Uranium Fightin Page: 2 of 4 Words: 997 ALIENWAREExplanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Student.java
--------
public class Student {
public Student(){
}
public String getType(){
return "Student";
}
public void study(){
}
public void party(){
}
public void sleep(){
}
}
Grad.java
-------
public class Grad extends Student {
@Override
public String getType() {
return "Grad";
}
@Override
public void study() {
System.out.println("Grad student do research");
}
@Override
public void party() {
System.out.println("Grad parties sometime");
}
@Override
public void sleep() {
System.out.println("Grad students don't sleep that much");
}
}
UnderGrad.java
------------
public class UnderGrad extends Student {
@Override
public String getType() {
return "UnderGrad";
}
@Override
public void study() {
System.out.println("UnderGrad studies sometime");
}
@Override
public void party() {
System.out.println("UnderGrad tends to party hard");
}
@Override
public void sleep() {
System.out.println("UnderGrad tends to sleep a lot");
}
}
PartTime.java
---------
public class PartTime extends Student {
@Override
public String getType() {
return "PartTime";
}
@Override
public void study() {
System.out.println("ParTime students make $$ and study");
}
@Override
public void party() {
System.out.println("PartTime students party at work");
}
@Override
public void sleep() {
System.out.println("PartTime students sleep at work");
}
}
TestStudents.java
----------
public class TestStudents {
public static void main(String[] args) {
Student[] studs = new Student[3];
studs[0] = new Grad();
studs[1] = new UnderGrad();
studs[2] = new PartTime();
for(Student s: studs){
System.out.println(s.getType()+":");
s.study();
s.party();
s.sleep();
}
}
}
output
-------
Grad:
Grad student 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:
ParTime students make $$ and study
PartTime students party at work
PartTime students sleep at work
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.