In: Computer Science
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.
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.