USE JAVA Language USE JAVA Language Please write code that meets the following r
ID: 3854462 • 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 you go champ :) happy coding... Let me know if you face any trouble understanding these
/**
*
* @author Sam
*/
abstract class Student {
String name;
boolean mayLiveOnCampus;
public Student(String name, boolean mayLiveOnCampus) {
this.name = name;
this.mayLiveOnCampus = mayLiveOnCampus;
}
public String getName() {
return name;
}
public boolean isMayLiveOnCampus() {
return mayLiveOnCampus;
}
@Override
public String toString() {
return "student{" + "name=" + name + ", mayLiveOnCampus=" + mayLiveOnCampus + '}';
}
public abstract boolean hasPriorityEnrollment();
}
/**
*
* @author Sam
*/
class MatriculatedStudent extends Student {
String studentId;
public MatriculatedStudent(String studentId, String name) {
super(name, true);
this.studentId = studentId;
}
@Override
public boolean hasPriorityEnrollment() {
return true;
}
}
/**
*
* @author Sam
*/
class NonMatriculatedStudent extends Student {
String ssn;
public NonMatriculatedStudent(String ssn, String name) {
super(name, false);
this.ssn = ssn;
}
@Override
public boolean hasPriorityEnrollment() {
return false;
}
}
/**
*
* @author Sam
*/
public class StudentDriver {
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);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.