Question

In: Computer Science

Q1. Write an application that performs each of the following tasks: a) Specify that class “Pieceworker”...

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.

  • Use Student as the superclass of the hierarchy, and then extend Student with classes Undergraduate Student and Graduate Student.
  • Continue to extend the hierarchy as deep (i.e., as many levels) as possible. For example, freshman, sophomore, Junior and Senior might extend undergraduate Student, and Doctoral Student and Masters Student might be subclasses of graduate Student.
  • After drawing the hierarchy, discuss the relationships that exist between the classes.
  • Write a program that illustrates the above idea of hierarchy.

Solutions

Expert Solution

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!


Related Solutions

Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
Write a program that performs the following two tasks in java Reads an arithmetic expression in...
Write a program that performs the following two tasks in java Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES
Write a program that performs the following two tasks: Reads an arithmetic expression in an infix...
Write a program that performs the following two tasks: Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use the algorithms described in class/ lecture notes. Use linked lists to implement the Queue and Stack ADTs. Using Java built-in classes will result in 0 points. You must use your own Stack and Queue classes (my code is a...
You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
Write an array method to carry out each of the following tasks for an array of...
Write an array method to carry out each of the following tasks for an array of integers. Note: you can think of each as a separate problem independent of the others. a) Swap the first and last elements in the array. b) Shi< all elements by one to the right and move the last element into the first posi>on. For example, 1 4 9 16 25 would be transformed into 25 1 4 9 16. c) Replace all even elements...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Write code in your “main” function that performs the following: a) For each n є {10^3,...
Write code in your “main” function that performs the following: a) For each n є {10^3, 5x10^3, 10^4, 5x10^4, 10^5}, randomly generate 5 integer arrays of length n. b) Run each of the five comparison sorts you implemented in Step 1 on all the five arrays generated in Step 2.a and record the worst-case actual running time and number of comparisons performed among elements in the input array. #include<iostream> #include<cmath> using namespace std; void displayArray(int a[], int n) { cout<<"\nArray...
3. For each case, write the degrees of freedom (F) and specify the state of the...
3. For each case, write the degrees of freedom (F) and specify the state of the water. Calculate quality or degrees of superheat whenever applicable. a) What is the quality of wet steam if V= 8.35 cm3 /g at 10,703 kPa? What is the specific internal Energy? What is the specific entropy? b) What is the specific enthalpy of steam at T= 200°C and P=525 kPa? c) What is the specific entropy of steam at 9100 kPa and 560° C?...
6.         One of the tasks capital performs is regulator of growth. This means: Geographic and portfolio...
6.         One of the tasks capital performs is regulator of growth. This means: Geographic and portfolio diversification. Growth and development of new services. The regulator could limit a financial institution from expanding its loans and deposits too fast, or from acquiring other institutions if the financial institution is deemed to be undercapitalized. All of the above.
Work standards specify time, cost, and efficiency norms for the performance of work tasks. They are...
Work standards specify time, cost, and efficiency norms for the performance of work tasks. They are typically used to monitor job performance. In one distribution center, data were collected to develop work standards for the time to assemble or fill customer orders. The table below contains data for a random sample of 9 orders. Time (mins.) Order Size 27 36 15 34 71 255 35 103 8 4 60 555 3 6 10 60 10 96 Construct a simple linear...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT