Question

In: Computer Science

Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑...

Java programming year 2

Polymorphism

superclass variables and subclass objects

polymorphic code

-Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name;

Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for taxes (we will do a flat 17%), and the final amount (for basic employees this will just be a lot of 0's but make sure to do the math based on what paycheck returns).

Salaried employees are employed either for 10 or 12 months of the year for some salary. Their paycheck is their salary, divided up into two pays a month for however many months they are employed.

Using inheritance, create a class for this type of employee (more will be added in part B). Don't override reportDeposit. Give a parameterized constructor for name and salary that chains back to the parameterized superclass constructor.

In a main, create an array of employees, and set it up with some of each kind, some with different values. (hint: you've got parameterized constructors...) Loop through the list and call reportDeposit for each employee, also find the employee with the highest paycheck.

-Part B Hourly employees have an hourly wage, and a number of hours they are scheduled to work per week,. One paycheck is based on two weeks of work (they are guaranteed to have the same hours for both weeks). They are paid an extra 5% of their hourly rate for each hour over 40 hours per week they are scheduled.

Adjunct employees may teach up to 18 credits, and are paid $827 per credit, with the total spread across 2 paychecks a month for 4 months.

Add classes for these types of employee.

Add two hourly and two adjunct employees to your array in main. You shouldn't have to change anything else in main.

Solutions

Expert Solution

Answer:- Types of polymorphisms in Java
There are four types of polymorphisms in Java:

1. Coercion is an operation that implements several types of implicit type conversion. For example, you divide an integer by another integer or a floating point value by another floating point value. If an operant is an integer and another operant is a floating point value, the compiler forces (implicitly) converts the integer to the floating point value to prevent type errors. (There are no division operations that support integer operand and point-floating operand.) Another example is passing subclass objects into method superclass parameters. The compiler trains subclass types into superclass types to limit operations to superclass.

2.Overloading refers to using the same operator symbol or method name in different contexts. For example, you might consider using + to perform the addition of integers, the addition of dots, or the connection of strings, depending on the type of operant. Also, several ways to get the same name can appear in the class (via declaration and / or inheritance).

3. Parametric polymorphism states that in class declarations, field names can be associated with different types and method names can be associated with different parameters and return types. Fields and methods can then take several types in each class instance (object). For example, fields can be of the Double type (a standard Java class library member that wraps a double value) and a way that can return Double on one object, and the same field can be of the same String type and method can return a String on another object. Java supports parametric parametric polymorphisms through generics, which I will discuss in a future article.

4.Subtype means that sex can be made into another type of sex. When a subtype instance appears in the supertype context, executing a supertype operation on a subtype instance produces a subtype version of the executing operation. For example, consider a piece of code that draws an arbitrary shape. You can express this image code more concisely by introducing the Shape class with the draw () method; by introducing Circle, Rectangle, and other subclasses that override draw (); by introducing various types of Shapes whose elements store references to Subclass class forms; and by calling the Shape's draw () method in each instance. When you call draw (), the draw method (Circle's, Rectangle's) or Shape instance is otherwise called. We say that there are many forms of the Shape's draw () method.

Parameterized constructor

Constructor with arguments(or you can say parameters) is known as Parameterized constructor.

Example: parameterized constructor

In this example we have a parameterized constructor with two parameters id and name. While creating the objects obj1 and obj2 I have passed two arguments so that this constructor gets invoked after creation of obj1 and obj2.

public class Employee {

   int empId;  
   String empName;  
            
   //parameterized constructor with two parameters
   Employee(int id, String name){  
       this.empId = id;  
       this.empName = name;  
   }  
   void info(){
        System.out.println("Id: "+empId+" Name: "+empName);
   }  
           
   public static void main(String args[]){  
        Employee obj1 = new Employee(10245,"Chaitanya");  
        Employee obj2 = new Employee(92232,"Negan");  
        obj1.info();  
        obj2.info();  
   }  
}

Output:

Id: 10245 Name: Chaitanya
Id: 92232 Name: Negan

Example2: parameterized constructor

In this example, we have two constructors, a default constructor and a parameterized constructor. When we do not pass any parameter while creating the object using new keyword then default constructor is invoked, however when you pass a parameter then parameterized constructor that matches with the passed parameters list gets invoked.

class Example2
{
      private int var;
      //default constructor
      public Example2()
      {
             this.var = 10;
      }
      //parameterized constructor
      public Example2(int num)
      {
             this.var = num;
      }
      public int getValue()
      {
              return var;
      }
      public static void main(String args[])
      {
              Example2 obj = new Example2();
              Example2 obj2 = new Example2(100);
              System.out.println("var is: "+obj.getValue());
              System.out.println("var is: "+obj2.getValue());
      }
}

Output:

var is: 10
var is: 100

What if you implement only parameterized constructor in class

class Example3
{
      private int var;
      public Example3(int num)
      {
             var=num;
      }
      public int getValue()
      {
              return var;
      }
      public static void main(String args[])
      {
              Example3 myobj = new Example3();
              System.out.println("value of var is: "+myobj.getValue());
      }
}

Output: It will throw a compilation error. The reason is, the statement Example3 myobj = new Example3() is invoking a default constructor which we don’t have in our program. when you don’t implement any constructor in your class, compiler inserts the default constructor into your code, however when you implement any constructor (in above example I have implemented parameterized constructor with int parameter), then you don’t receive the default constructor by compiler into your code.

If we remove the parameterized constructor from the above code then the program would run fine, because then compiler would insert the default constructor into your code.

Python Program for simple interest

-Part B Hourly employees have an hourly wage, and a number of hours they are scheduled to work per week,. One paycheck is based on two weeks of work (they are guaranteed to have the same hours for both weeks). They are paid an extra 5% of their hourly rate for each hour over 40 hours per week they are scheduled.

Adjunct employees may teach up to 18 credits, and are paid $827 per credit, with the total spread across 2 paychecks a month for 4 months.

Add classes for these types of employee.

Add two hourly and two adjunct employees to your array in main. You shouldn't have to change anything else in main.

Below is the few examples:-

Simple interest formula is given by:
Simple Interest = (P x T x R)/100
Where,
P is the principle amount
T is the time and
R is the rate

Examples:

EXAMPLE1:
Input : P = 10000
        R = 5
        T = 5
Output :2500
We need to find simple interest on 
Rs. 10,000 at the rate of 5% for 5 
units of time.

EXAMPLE2:
Input : P = 3000
        R = 7
        T = 1
Output :210

Another Example:- Python3

# Python3 program to find simple interest
# for given principal amount, time and
# rate of interest.


def simple_interest(p,t,r):
   print('The principal is', p)
   print('The time period is', t)
   print('The rate of interest is',r)
  
   si = (p * t * r)/100
  
   print('The Simple Interest is', si)
   return si
  
# Driver code
simple_interest(8, 6, 8)

Output:

The principal is 8
The time period is 6
The rate of interest is 8
The Simple Interest is 3.84

I Hope This Explanation Will Help You.this is the best solution right now Kindly Rate Me Thumbs Up for My Effort for this solution Right Now, if You Are not satisfied for this solution I'll check another method for You & provide another solution ASAP.


Related Solutions

Programming Questions: 1) How do you build an array of objects of a superclass with its...
Programming Questions: 1) How do you build an array of objects of a superclass with its subclass objects? 2) How do you use an enhanced for-loop to traverse through an array while calling a static method(superclass x)? 3) How do you create a static method for a class that has the parent class reference variable? **Include java coding examples for each question if possible
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a)...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a) a TextField b) a Button c) a Text d) a Label 3. create the following container and put them the above controls: a) a VBox controller b) a BorderPane controller c) a GridPane
Java Part 2 of 4 - Amusement Park Programming Project MUST BE COMPATIBLE WITH PART 1...
Java Part 2 of 4 - Amusement Park Programming Project MUST BE COMPATIBLE WITH PART 1 https://www.chegg.com/homework-help/questions-and-answers/java-part-1-4-amusement-park-programming-project-requirements-use-java-selection-construct-q40170145?trackid=ERIFssNL Requirements: Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Proper error handling. Class: Merchandise – models merchandise available in the gift shop such as t-shirts, sweatshirts, and stuffed animals. Instance Fields: id : long – to identify the specific merchandise item. category : String – to...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This lab maps to learning the following objectives: Write a working program that creates a Class, Instances of Objects from that Class, and Functions that use Objects as parameters. For this portion of the lab, you will create a new program for your Professor. Create a class named Student that holds the following data about a student: 1. Name 2. Student ID number 3. GPA...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code outputs the programmer’s name , prompts the user to enter two numbers (integers) and outputs their sum. Note: When you run the program, it expects you to enter two integer values (a counting or whole number like 1, 216, -35, or 0) Make the following changes/additions to the code: On line 11, change the String myName from “your full name goes here!!!” to your...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
How do we change source code on java programming? Thanks
How do we change source code on java programming? Thanks
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
Why is java programming better than bash scripting. Why should programmers write in java code ?...
Why is java programming better than bash scripting. Why should programmers write in java code ? Come up with situations where java is a better option.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT