Uploaded by Salem Saeed

A4.docx

advertisement
Spring 2020
Object Oriented Programming
Assignment 4
Name
Sultan Al darei
ID
201609665
Section
Exercise 1:
1. Use the following UML class diagram to create Java classes:
Person
- name: String
+Person()
+Person(String)
+setName(String) : void
+getName(): String
+display(): void
+hasSameName(Person): boolean
Employee
- salary: double
+Employee(String, double)
+setSalary(double) : void
+getSalary(): double
+display(): void
Student
- studentID: int
+Student()
+Student(String, int)
+setID(int) : void
+getID(): int
+display(): void
+isEqual(Student): boolean
Note that:
a. Method display() in Person prints out the name of the person.
b. Method display() in Student prints out the name and the ID of the student.
c. Method display() in Employee prints out the name and the salary of the employee.
d. Method hasSameName(Person) compares the names.
e. Method isEqual(Student) compares the names and ID. It should use the method
hasSameName(Person).
2. Create a Java program to do the following:
a. Read from file information about 10 Students and and from another file information about
10 Employees (Hint: use arrays of objects)
b. Write code to check if there is a duplicate records of students and print them out
c. Write code to check if there is a student and employee who have the same name and print
them out.
d. Write code to print out the 3 employee with the highest salary
1/5
Example input files
Students file:
Saeid 312
Mohammed 124
Salem 751
Nasser 613
Saeed 312
Mohammad 813
Mariam 543
Shamma 712
Naser 784
Salim 751
Example input files
Students file:
Mansoor 12300
Mohammed 11000
Zayed 7500
Nasser 6900
Saeed 8000
Maryam 8100
Fatema 15000
Asma 7100
Khaled 12800
Khalifah 9600
Person Class;
package assignment4;
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public Person() {
this.name = "";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void display() {
System.out.print("Name: " + this.name+"\n");
}
public boolean hasSameName(Person other) {
return (this.name.equals(other.name));
}
}
2/5
Employee Class
package assignment4;
public class Employee extends Person {
private double salary;
public Employee(String name, double i) {
super(name);
this.salary = i;
}
public Employee() {
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void display() {
super.display();
System.out.println("Salary: " + this.salary+"\n");
}
}
Student Class
package assignment4;
public class Student extends Person {
private int studentID;
public Student(int studentID, String name) {
super(name);
this.studentID = studentID;
}
public Student() {
super();
this.studentID = 0;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public int getStudentID() {
return studentID;
}
public void display() {
super.display();
System.out.print("Student ID : " + this.studentID+"\n");
}
public boolean isEqual(Student i ){
return (i.hasSameName(this)&& i.studentID==this.studentID);
}
}
3/5
Test
package assignment4;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Testing {
public static void main(String[] args) throws FileNotFoundException {
Scanner inFile1 = new Scanner(new FileReader("Student"));
Scanner inFile2 = new Scanner(new FileReader("Employee.t"));
Student Stuarray[] = new Student[10];
Employee Emparray[] = new Employee[10];
String name;
int ID;
int Salary;
//Read from file information about 10 Students and and from another file information about 10 Employees
for (int i = 0; i < Stuarray.length; i++) {
name = inFile1.next();
ID = inFile1.nextInt();
Stuarray[i] = new Student(ID, name);
}
for (int i = 0; i < Emparray.length; i++) {
name = inFile2.next();
Salary = inFile2.nextInt();
Emparray[i] = new Employee(name, Salary);
}
boolean same = false;
for (int i = 0; i < Stuarray.length; i++) {
Student student1 = Stuarray[i];
for (int j = i+1; j < Stuarray.length; j++) {
Student student2 =Stuarray[j];
//using isEqual() method - to check the (same name)/(same id)
same= student1.isEqual(student2);
//b) 2-print out the dublicated students records:
if(same == true){
System.out.println("these students have duplicated records: ");
student1.display();
student2.display();
}
}
}
Person []names =new Person[10];
for (int i = 0; i < Stuarray.length; i++) {
for (int j = 0; j < Emparray.length; j++) {
if(Stuarray[i].hasSameName(Emparray[j])){
names[i]=Stuarray[i];
System.out.println("The Same Names Of Students & Employees: ");
4/5
System.out.println(names[i].getName());
}
}
}
Write code to print out the 3 employee with the highest salary:
System.out.println("");
double first, second, third;
Person firstP = new Employee();
Person secP = new Employee();
Person thirdP = new Employee();
third = first = second = 0;
for (int i = 0; i < Emparray.length; i++) {
/* If current element is greater than
first*/
if (Emparray[i].getSalary() > first) {
third = second;
second = first;
first = Emparray[i].getSalary();
firstP = Emparray[i];
} else if (Emparray[i].getSalary() > second) {
third = second;
second = Emparray[i].getSalary();
secP = Emparray[i];
} else if (Emparray[i].getSalary() > third) {
third = Emparray[i].getSalary();
thirdP = Emparray[i];
}
}
System.out.println("Three highest salary are " + first + " , " + second + " , " + third + "\n");
System.out.println("Thier info are :");
firstP.display();
secP.display();
thirdP.display();
}
}
5/5
Download