Question

In: Computer Science

Question1: Define a class Human that contains: - The data fields "first name" and "last name"...

Question1: Define a class Human that contains: - The data fields "first name" and "last name" - A constructor that sets the first and last name to the instance variables. Create also a no-argument constructor - A getName() method which prints the first and last name Define the class Student which inherits Human and contains: - Private data field "mark" of type integer and a constructor which calls the superclass constructor Define the class Worker which inherits Human and contains: - Private date fields "wage" and "hours worked", and a constructor which calls the superclass constructor - A setWageInfo() method which sets the wage information to the instance variables - A calculateWage() method which calculates a worker’s salary based on wage and hours worked Write a program called HumanTest that instantiates two Human objects (one using the noargument constructor and another using the constructor with parameters). Call the method getName(). Create a Student object which calls the getName() method. Create a Worker object which calls the methods getName(), setWageInfo(), and claculateWage().

Solutions

Expert Solution

Default Constructor(No arg. Constructor):

A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class.

Parameterized Constructor(Constructor With Arguments):

A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with your own values, then use a parameterized constructor.

Note:-

To call any constructor or make use of constructor in Derived Class we make use of SUPER() keyword in a program where if there is an parameterized constructor. pass arguments first to the Base Class Constructor as per sequence and then to the same class constructor.

Solution to the above example is shown below.....

class Human//parent class
{
   String first_name;
   String last_name;
   Human()//Default Constructor also called No arg. Constructor
   {
       first_name="NULL";
       last_name="NuLL";
   }
   Human(String f,String l)//Parameterised Constructor
   {
       first_name=f;
       last_name=l;
   }
   void getName()
   {
       System.out.println("first_name :"+first_name);
       System.out.println("last_name :"+last_name);
   }
}
class Student extends Human
{
   private int mark;
  
   Student()
   {
       super();//calls Default Constructor
       mark=0;
   }
   Student(String f,String l,int m)//here we pass argument first to parent class and last for this class
   {
       super(f,l);//calls Parameterised Constructor
       mark=m;
   }
}
class Worker extends Human
{
   private int wages, hours;
   Worker()
   {
       super();
       wages=0;
       hours=0;
   }
   void setWage(int w,int h)
   {
       wages=w;
       hours=h;
   }
   void calwages()
   {
       int sal=wages*hours;
       System.out.println("Salary :"+sal);
   }
}
class HumanWages
{
   public static void main(String args[])
   {
       Human h1=new Human();
       Human h2=new Human("Shan","Naik");
       h2.getName();
       Student s=new Student("Vish","Sharma",100);
       s.getName();
       Worker w =new Worker();
       w.getName();
       w.setWage(1000,10);
       w.calwages();
   }
}
      


Related Solutions

C# (Thank you in advance) Create an Employee class with five fields: first name, last name,...
C# (Thank you in advance) Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and initSalary. It includes constructor(s) and properties to initialize values for all fields. Create an interface, SalaryCalculate, class that includes two functions: first,CalcYearWorked() function, it takes one parameter (currentyear) and calculates the number of year the worker has been working. The second function, CalcCurSalary() function that calculates the current year salary. Create a Worker classes that is derived from Employee and SalaryCalculate...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Write a class named Person with data attributes for a person’s first name, last name, and...
Write a class named Person with data attributes for a person’s first name, last name, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a calling list. Demonstrate an instance of the Customer class in a simple program. Using python
java Define the Circle2D class that contains: • Two double data fields named x and y...
java Define the Circle2D class that contains: • Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. • A data field radius with a get method. • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea()...
Define the circle2d class that contains: Two double data fields named x and y that specify...
Define the circle2d class that contains: Two double data fields named x and y that specify the center of the circle with getter methods A data field radius with a getter method A no arg constructor that creates a default circle with 0,0 for x,y and 1 for the radius A constructor that creates a circle with the specified x,y of the the circle A method getArea() that returns the area of the circle A method Contains(Double x, Double y)...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name,...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name, last name, and hourly pay rate (use of string datatype is not allowed). Create an array of five Employee objects. Input the data in the employee array. Write a searchById() function, that ask the user to enter an employee id, if its present, your program should display the employee record, if it isn’t there, simply print the message ”Employee with this ID doesn’t exist”....
//Question1: What is the name of the following lines in the data type class? private int...
//Question1: What is the name of the following lines in the data type class? private int intVariable; private float floatVariable; private String stringVariable; //Question2: What is the name of the following lines in the data type class? public DataTypeClass_Smith() { intVariable = 0; floatVariable = 0.0; stringVariable = “aString”; } //Question3: What is the name of the following lines in the data type class? public DataTypeClass_Smith( int intVar, float floatVar, String stringVar) { intVariable = intVar; floatVariable = floatVar; stringVariable...
Design a ship class that has the following data fields: A data field for the name...
Design a ship class that has the following data fields: A data field for the name of the ship (a string). A data field for the year that the ship was built (a String). A constructor and appropriate accessors and mutators. A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following member: A field for the maximum number of passengers...
Create a table ‘StudentInfo’ with following fields: ID First Name Last Name SSN Date of Birth...
Create a table ‘StudentInfo’ with following fields: ID First Name Last Name SSN Date of Birth Create a table ‘ClassInfo’ table: ID Class Name Class Description Create a table ‘RegisteredClasses’ table: StudentID ClassID The RegisteredClasses table should have a foreign key relationship to StudentInfo and ClassInfo tables for the respective IDs. Also the IDs in StudentInfo and ClassInfo need to be primary keys. When you submit the file your email should also contain the following SQL Queries: Query to show...
Define and implement class Course. This class should contain the following fields: course name, course description,...
Define and implement class Course. This class should contain the following fields: course name, course description, department, time the course starts, weekday the course is held on (for simplicity, let us assume the course only meets once a week). This class should contain getters and setters for all its attributes. This class also needs at least one constructor. Save this class and its definition into a file named Course.java. Define and implement class Student. This class should contain the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT