USE JAVA LANGUAGE USE JAVA LANGUAGE Please write code that meets the following r
ID: 3854467 • Letter: U
Question
USE JAVA LANGUAGE
USE JAVA LANGUAGE
Please write code that meets the following requirements:
Create an abstract class called Student
Has private attributes name (String), mayLiveOnCampus (boolean)
Create a constructor that requires both parameters
Has getters for name and mayLiveOnCampus (but not setters)
Has a toString () method implemented (use NetBeans for this)
Has a public abstract boolean method called hasPriorityEnrollment ()
Create a concrete class called MatriculatedStudent
Inherits from Student
Has a private attribute for Student ID named studentId (string)
Implements only one constructor that requires BOTH name and student
ID, and calls the superclass constructor with the name plus true for
mayLiveOnCampus
Implements hasPriorityEnrollment (), returns true
Create a concrete class called NonMatriculatedStudent
Inherits from Student
Has a private attribute for Social Security Number named ssn (string)
Implements only one constructor that requires BOTH name and SSN, and
calls the superclass constructor with the name plus false for
mayLiveOnCampus
Implements hasPriorityEnrollment (), returns false
Create a class Main which has a main method like this:
}
Explanation / Answer
Here is the code for the question with output. Post a comment in case of any issues, I will respond. Please rate the answer if happy with it. Thank you.
Student.java
public abstract class Student {
private String name;
private boolean mayLiveOnCampus;
//construct with parameters
public Student(String name, boolean liveOnCampus)
{
this.name = name;
this.mayLiveOnCampus = liveOnCampus;
}
//getter
public String getName() {
return name;
}
public boolean isLiveOnCampus() {
return mayLiveOnCampus;
}
@Override
public String toString() {
return "Student [name=" + name + ", mayLiveOnCampus=" + mayLiveOnCampus + "]";
}
//abstract method to be implemented in inheriting classe
public abstract boolean hasPriorityEnrollment();
}
MatriculatedStudent.java
public class MatriculatedStudent extends Student {
private String studentId;
public MatriculatedStudent( String name, String id)
{
//call super constructor with true for mayLiveOnCampus
super(name, true);
this.studentId = id;
}
@Override
public boolean hasPriorityEnrollment() {
return true;
}
}
NonMetriculatedStudent.java
public class NonMatriculatedStudent extends Student {
private String ssn;
public NonMatriculatedStudent(String name, String SSN) {
//call super constructor with false for mayLiveOnCampus
super(name, false);
this.ssn = SSN;
}
@Override
public boolean hasPriorityEnrollment() {
return false;
}
}
StudentTest.java
public class StudentTest {
public static void main(String[] args) {
Student s1 = new MatriculatedStudent("Deena", "SB158320");
Student s2 = new NonMatriculatedStudent("Edward",
"123-45-6789" );
System.out.println(s1);
System.out.println(s2);
}
}
output
Student [name=Deena, mayLiveOnCampus=true]
Student [name=Edward, mayLiveOnCampus=false]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.