In: Computer Science
Q1. Write an application that performs each of the following tasks:
a) Specify that class “Pieceworker” inherits from class Employee.
b) Create instance variables “Firstname” and “ID” in superclass and a toString to display.
c) Add “branch” and toString in PieceWorker.
d) Show overriding to display the variables of the subclass.
Q2 Draw an inheritance hierarchy for students at a university.
Q1
class Employee
{
String Firstname = "ABC";
int ID = 123; //values hadcoded here, can be used
through constructor as well.
void show()
{
System.out.println(Firstname +
ID);
}
@Override
public String toString() {
return "Employee [Firstname=" +
Firstname + ", ID=" + ID + "]";
}
}
class PieceWorker extends Employee
{
String branch = "CSE";
Employee emp = new Employee();
@Override
void show()
{
//first way of overriding, and showing superclass variables
here. We can reassign the value of one of the variables //of
superclass as well, for eg String Firstname="DEF";
System.out.println(Firstname + ID +
branch);
}
@Override //overriding and using the employee
variables in the subclass
public String toString() {
return "PieceWorker [branch=" +
branch + "]" + "Employee [Firstname=" + emp.Firstname + ", ID=" +
emp.ID + "]";
}
}
public class Driver_Class
{
public static void main(String[]
args)
{
PieceWorker pw = new PieceWorker();
System.out.println(pw.varValues());
}
}
Q2
//Please feel free to add more variables wherever necessary, a basic template implementation has been shown //hereunder with basic variables relevant to each subclass. The hierarchy can be drawn as a simple multiple //inheritance, the relation being that Student is the super class to both UG and Graduate class, and has the basic //information of a student, building on top of which each child class is adding more relevant data. Please feel free to //add in comments if any extra information is needed.
class Student //superclass
{
String Name, Address;
int phone, zip;
}
class Undergraduate_Student extends Student
{
String course; //all variables of Student class are
already inherited, hence we only build on top of that
int duration;
double current_CGPA;
}
class Graduate_Student extends Student
{
double grad_CGPA;
String grad_College;
}
class Freshman extends Undergraduate_Student
{
String school_passed_from;
}
class Sophomore extends Undergraduate_Student
{
double CGPA_first_year;
}
class Junior extends Undergraduate_Student
{
String[] junior_Subjects;
String junior_College_Name;
}
class Senior extends Undergraduate_Student
{
double current_CGPA;
String[] senior_subjects;
String senior_College_Name;
}
class Masters_Student extends Graduate_Student
{
String masters_course;
double masters_Current_CGPA;
}
class Doctorals_Student extends Graduate_Student
{
String[] doctoral_theme;
double post_grad_CGPA;
String PG_College;
}
public class Driver_Class
{
public static void main(String[]
args)
{
Doctorals_Student doct_stu - new Doctorals_Student
();
//can instantiate further, and if
required can use constructors to load data from the driver
class
}
}
If answer is satisfactory do give a thumbs up! Otherwise you can add the query in the comment thread for this post and we can try and get back to you as early as possible!