Uploaded by Pritam Das

CSC207A Lab 4 and 5

advertisement
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Laboratory 4
Registration Number: 17ETCS002130
Title of the Laboratory Exercise: Overloading, Inheritance and Overriding
1. Introduction and Purpose of Experiment
Students apply object oriented programming concepts including Overloading, Inheritance
and Overriding to solve problems.
2. Aim and Objectives
Aim
To apply object oriented programming concepts including Overloading, Inheritance and
Overriding to solve problems
Objectives
At the end of this lab, the student will be able to
●
Apply Overloading, Inheritance and Overriding for solving problems
●
Express solutions in Java language
●
Use Netbeans IDE
3. Experimental Procedure
For the problems listed below, design the data structures, algorithm(s) and write the program(s).
Tabulate the output for various inputs and verify against expected values. Compare the
programming method in Java with C programming languages. Describe your learning along with
the limitations of overall approach if any. Suggest how these can be overcome.
a. Write a program to develop a game for the scenario posed:
In ACME organization, there are many employees. All employees have first name, last name and
Aadhaar number. ACME organization creates its own products and sells them. There are two
types of sales employees: Commission employee are paid a percentage share (known as
commission rate) of their gross sales. Base plus commission employee is a second type of sales
employee who is paid a basic salary along with the commission. Other types of Employees
include salaried employees who get paid a fixed weekly salary, piece workers who get paid a
preset per piece amount based on the number of pieces they produce and hourly wage
employees who get paid an hourly wage. Hourly wage employees also get 1.5 times the hourly
wage for hours worked over 40 hours. Create a Java program to calculate salary of an employee
in ACME organization.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
Documentation:
a.
Procedure
Algorithm(s):
and Procedure:
To create the application required, I have followed the procedure
below:
1. Create Java application project
Open NetBeans IDE and create a new project. Select project type
as Java and sub type as Java Application. Provide name of
application and class name when prompted.
2. Create new Parent class
We create a new Java class and enter the details like the first Name,
last Name and the Aadhaar Number along with the required
constructors and getters for the above-mentioned field.
3. Create new Sub-Classes
Then we make new Java classes for the required employee type and
extend them to the main parent class. This lets us share the
components with the parent class.
4. Create a Main Class
This class contains the scanner class which takes input from the user
and displays it according to sub-class selected for the employee type
selected. Further there are operation to calculate the salary for the
employee and the employee type entered.
4. Execute and debug
Close all files. Press clean and build button. If build is successful,
run the application. Debug errors if errors exist.
Algorithms:
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
Employee
Information
Salary
Employee
PieceWise
Employee
Hourly
Employee
Commission
Class
Commission
Employee
Base plus
Commission
Employee
Algorithm for parent class “employeeInf”
Step 1: Start
Step 2: Initialize private and final methods for firstName,
lastName and aadhaarNumber
Step 3: Abstract the parent class.
Step 4: Create the required constructor and getter for the
implemented variables
Step 5: Initialize an abstract method for calculation of salary
Step 6: Stop
Algorithm for “commissionClass”
Step 1: Start
Step 2: Extend the Java Class to main parent class
Step 3: Implement all abstract method
Step 4: Initialize private methods for commisssionRate and
totalSales.
Step 5: Create the required constructor, getter and setter for the
implemented variables
Step 6: Calculate the salary of the class in function under @override
method
Step 7: Stop
Algorithm for “baseCommissionClass”
Step 1: Start
Step 2: Extend the Java Class to main sub-class “commissionClass”.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
Step 3: Initialize private methods for baseRate
Step 4: Create the required constructor, getter and setter for the
implemented variables
Step 5: Calculate the salary of the class in function
Step 7: Stop
Algorithm for “houlyWage”
Step 1: Start
Step 2: Extend the Java Class to main parent class
Step 3: Implement all abstract method
Step
4:
Initialize
private
methods
for
noOfHours
and
wagePerHour.
Step 5: Create the required constructor, getter and setter for the
implemented variables
Step 6: Calculate the salary of the class in function under @override
method
Step 7: The Salary of the employee depends upon how much hour has
the employee worked.
Step 8: Stop
Algorithm for “piecewise”
Step 1: Start
Step 2: Extend the Java Class to main parent class
Step 3: Implement all abstract method
Step
4:
Initialize
private
methods
for
noOfPieces
and
pricePerPiece.
Step 5: Create the required constructor, getter and setter for the
implemented variables
Step 6: Calculate the salary of the class in function under @override
method
Step 7: Stop
Algorithm for “salary”
Step 1: Start
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
Step 2: Extend the Java Class to main parent class
Step 3: Implement all abstract method
Step 4: Initialize private methods for totalSalary.
Step 5: Create the required constructor, getter and setter for the
implemented variables
Step 6: Calculate the salary of the class in function under @override
method
Step 7: Stop
Algorithm for Main.java
Step 1: Start
Step 2: We display a series of option containing different classes.
Step 3: We implement scanner class and import the class
requirements
Step 4: We create a variable to select an option.
Step 5: We create a new entry for the Java class employeeInf and
Nullify it.
Step 6: The we enter into the switch case for the option selected
Step 7: Enter required cases for the selected option by using the
constructor class of the respective sub-classes
Step 8: For the selection made we display the calculated salary by
calling the function which was abstracted in parent class and was
implemented in each sub classes
Step 9: Stop
b. Conclusions:
I have learnt Java Application and I am able to create a salary
calculator with the minimal knowledge of inheritance. The above code
could have been made more efficiently since I am new to this language
it my first effort to handle a java application using the concept of
Inheritance. To handle any such program which works with the
concept of inheritance java application is the best option to execute
such problems. Splitting code in different classes and encapsulating all
in one place and displaying results makes coding very easy and also
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
debugging or making changes is also easier compared to doing in a
single class.
Results and Discussions:
Screenshot:
Discussion:
This is the main parent class which contains all the specific information for all the types employees like the
first name, last name and Aadhaar number which will common for every employee in every sub class. This
main class even has an abstract method for calculating salary which will get automatically implemented
when a sub-class extends it.
Screenshot:
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
Discussion:
This screenshot is an example of a sub-class which extends the main class by abstracting all the override
methods into it for the calculation of salary for the sub class respectively.
Screenshot:
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Discussion:
Registration Number: 17ETCS002130
This screenshot is the main class which consist of several switch cases and we use scanner class to take
input from the user. In this class each case has their respective constructor which calls the respective Java
class to take input from the user. At the end of the main class we call the salary calculation function to
calculate the salary of each class.
Screenshot:
Discussion:
The program was successfully developed and executed. This screenshot displays the result of adding a single
data of an employee with the required personal details and the salary he should be paid. When executed,
the Java language first approaches to the main parent class to enter the personal details then investigates
to which class has the employee must be entered. When the above method is satisfied it calls the salary
calculation function to calculate the required salary to be paid.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Laboratory 5
Registration Number: 17ETCS002130
Title of the Laboratory Exercise: Generic classes, Collections and their uses
1. Introduction and Purpose of Experiment
Students apply object oriented programming concepts Generic classes and Collections to
solve problems.
2. Aim and Objectives
Aim
To apply object oriented programming concepts Generic classes and Collections to solve
problems
Objectives
At the end of this lab, the student will be able to
●
Apply Generic classes and Collections for solving simple problems
●
Express generic solutions in Java language
3. Experimental Procedure
For the problems listed below, design the data structures, algorithm(s) and write the program(s).
Tabulate the output for various inputs and verify against expected values. Compare the
programming method in Java with C programming languages. Describe your learning along with
the limitations of overall approach if any. Suggest how these can be overcome.
a. Write a program to solve for the following scenario:
Extend the program created in laboratory 2 to include logic for input of multiple employees.
These employees should be stored in a collection such as ArrayList. The program should display
the salaries of each employee as output, after all the employee details are entered. Extend the
program further to display the employee details in ascending order of their salaries.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
Documentation:
a.
Procedure
Algorithm(s):
and Procedure:
To create the application required, I have followed the procedure
below:
1. Create Java application project
Open NetBeans IDE and create a new project. Select project type
as Java and sub type as Java Application. Provide name of
application and class name when prompted.
2. Create new Parent class
We create a new Java class and enter the details like the first Name,
last Name and the Aadhaar Number along with the required
constructors and getters for the above-mentioned field.
3. Create new Sub-Classes
Then we make new Java classes for the required employee type and
extend them to the main parent class. This lets us share the
components with the parent class.
4. Create a Main Class
This class contains the scanner class and ArrayList which takes input
from the user and displays it according to sub-class selected for the
employee type selected. Further there are operation to calculate the
salary for the employee and the employee type entered. ArrayList is
used to enter multiple employee data and view all at once.
4. Execute and debug
Close all files. Press clean and build button. If build is successful,
run the application. Debug errors if errors exist.
Algorithms:
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
Employee
Information
Salary
Employee
PieceWise
Employee
Hourly
Employee
Commission
Class
Commission
Employee
Base plus
Commission
Employee
Algorithm for parent class “employeeInf”
Step 1: Start
Step 2: Initialize private and final methods for firstName,
lastName and aadhaarNumber
Step 3: Abstract the parent class.
Step 4: Create the required constructor and getter for the
implemented variables
Step 5: Initialize an abstract method for calculation of salary
Step 6: Stop
Algorithm for “commissionClass”
Step 1: Start
Step 2: Extend the Java Class to main parent class
Step 3: Implement all abstract method
Step 4: Initialize private methods for commisssionRate and
totalSales.
Step 5: Create the required constructor, getter and setter for the
implemented variables
Step 6: Calculate the salary of the class in function under @override
method
Step 7: Stop
Algorithm for “baseCommissionClass”
Step 1: Start
Step 2: Extend the Java Class to main sub-class “commissionClass”.
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
Step 3: Initialize private methods for baseRate
Step 4: Create the required constructor, getter and setter for the
implemented variables
Step 5: Calculate the salary of the class in function
Step 7: Stop
Algorithm for “houlyWage”
Step 1: Start
Step 2: Extend the Java Class to main parent class
Step 3: Implement all abstract method
Step
4:
Initialize
private
methods
for
noOfHours
and
wagePerHour.
Step 5: Create the required constructor, getter and setter for the
implemented variables
Step 6: Calculate the salary of the class in function under @override
method
Step 7: The Salary of the employee depends upon how much hour has
the employee worked.
Step 8: Stop
Algorithm for “piecewise”
Step 1: Start
Step 2: Extend the Java Class to main parent class
Step 3: Implement all abstract method
Step
4:
Initialize
private
methods
for
noOfPieces
and
pricePerPiece.
Step 5: Create the required constructor, getter and setter for the
implemented variables
Step 6: Calculate the salary of the class in function under @override
method
Step 7: Stop
Algorithm for “salary”
Step 1: Start
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
Step 2: Extend the Java Class to main parent class
Step 3: Implement all abstract method
Step 4: Initialize private methods for totalSalary.
Step 5: Create the required constructor, getter and setter for the
implemented variables
Step 6: Calculate the salary of the class in function under @override
method
Step 7: Stop
Algorithm for Main.java
Step 1: Start
Step 2: Create a vector of employees as ArrayList.
Step 3: We enter into a while loop
Step 4: We display a series of option containing option for adding
multiple entries, displaying it and exiting the application.
Step 5: We implement scanner class and import the class
requirements
Step 6: The we enter into the switch case for the option selected
Step 7: Enter required cases for the selected option
Step 8: We display a series of option containing different classes.
Step 9: We implement scanner class and import the class
requirements
Step 10: We create a variable to select an option.
Step 11: We create a new entry for the Java class employeeInf
and Nullify it.
Step 12: The we enter into the switch case for the option selected
Step 13: Enter required cases for the selected option by using the
constructor class of the respective sub-classes
Step 14: We add the added data to ArrayList.
Step 15: Run a for loop to display the salaries.
Step 16: For the selection made we display the calculated salary by
calling the function which was abstracted in parent class
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
and was implemented in each sub classes
Step 17: We sort the data in the ArrayList according to the
salary to be paid to the employee.
Step 18: We create the switch case for terminating the program.
Step 19: Stop
b. Conclusions:
I have learnt Java Application and I am able to create a salary
calculator with the minimal knowledge of inheritance along with
inserting multiple entries using ArrayList. The above code could
have been made more efficiently since I am new to this language it my
first effort to handle a java application using the concept of
Inheritance and working with vectors like ArrayList. Splitting code in
different classes and encapsulating all in one place and displaying
multiple results using ArrayList makes coding very easy and also
debugging or making changes is also easier compared to doing in a
single class.
Results and Discussions:
Screenshot:
Discussion:
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
This is the main parent class which contains all the specific information for all the types employees like the
first name, last name and Aadhaar number which will common for every employee in every sub class. This
main class even has an abstract method for calculating salary which will get automatically implemented
when a sub-class extends it.
Screenshot:
Discussion:
This screenshot is an example of a sub-class which extends the main class by abstracting all the override
methods into it for the calculation of salary for the sub class respectively.
Screenshot:
Discussion:
This screenshot is the main class mainly consist of a vector called ArrayList which works with multiple
entries of employees. The program displays the salaries of each employee as output, after all the employee
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
Name: Pritam Das
Registration Number: 17ETCS002130
details are entered. The entered data is stored in a collection like ArrayList. This makes program highly
efficient and user friendly. Using Collections.sort() we sort the salary of the employee in ascending
order.
Screenshot:
Discussion:
The program was successfully developed and executed. This screenshot displays the result of adding
multiple data of an employee with the required personal details and the salary he should be paid. When
executed, the Java language first approaches to the main parent class to enter the personal details then
investigates to which class has the employee must be entered. When the above method is satisfied it calls
the salary calculation function to calculate the required salary to be paid. Once one data is entered it stores
in the array. Similarly, many data are stored in the array and is displayed at once when it is commanded to
be displayed.
Download