0% found this document useful (0 votes)
15 views

Java

This document defines a Teacher class with attributes like name, degree, salary, courses taught, and methods like printing courses, checking if a course is taught, and calculating tax. It also has a Test class with methods to find the teacher with the highest salary and count the number of teachers teaching a particular course by passing an array of Teacher objects.

Uploaded by

raaed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java

This document defines a Teacher class with attributes like name, degree, salary, courses taught, and methods like printing courses, checking if a course is taught, and calculating tax. It also has a Test class with methods to find the teacher with the highest salary and count the number of teachers teaching a particular course by passing an array of Teacher objects.

Uploaded by

raaed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;

class Teacher {
private String name;
private String degree;
private double salary;
private int hours;
private String course1;
private String course2;
private String course3;

public Teacher() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
name = scanner.nextLine();
System.out.print("Enter degree: ");
degree = scanner.nextLine();
System.out.print("Enter salary: ");
salary = scanner.nextDouble();
System.out.print("Enter number of hours: ");
hours = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter course 1: ");
course1 = scanner.nextLine();
System.out.print("Enter course 2: ");
course2 = scanner.nextLine();
System.out.print("Enter course 3: ");
course3 = scanner.nextLine();
}

public void printCourses() {


System.out.println("Courses: ");
System.out.println(course1);
System.out.println(course2);
System.out.println(course3);
}

public boolean findCS101() {


return course1.equalsIgnoreCase("CS101") ||
course2.equalsIgnoreCase("CS101") ||
course3.equalsIgnoreCase("CS101");
}

public double tax() {


return salary * 0.01;
}

// Getters and setters


public String getName() {
return name;
}

public String getDegree() {


return degree;
}
public double getSalary() {
return salary;
}

public void setSalary(double salary) {


this.salary = salary;
}

public int getHours() {


return hours;
}

public void setHours(int hours) {


this.hours = hours;
}
}

public class Test {


public static int maxSalary(Teacher[] teachers) {
int maxIndex = 0;
double maxSalary = teachers[0].getSalary();

for (int i = 1; i < teachers.length; i++) {


if (teachers[i].getSalary() > maxSalary) {
maxSalary = teachers[i].getSalary();
maxIndex = i;
}
}

return maxIndex;
}

public static void printCS101(Teacher[] teachers) {


int count = 0;
for (Teacher teacher : teachers) {
if (teacher.findCS101()) {
count++;
}
}
System.out.println("Number of lecturers teaching CS101: " + count);
}

public static void main(String[] args) {


Teacher[] allTeachers = new Teacher[5];

for (int i = 0; i < allTeachers.length; i++) {


System.out.println("Enter details for Teacher " + (i + 1) +
":");
allTeachers[i] = new Teacher();
System.out.println();
}

int maxSalaryIndex = maxSalary(allTeachers);


Teacher maxSalaryTeacher = allTeachers[maxSalaryIndex];
double taxAmount = maxSalaryTeacher.tax();

System.out.println("Teacher with highest salary:");


System.out.println("Name: " + maxSalaryTeacher.getName());
System.out.println("Degree: " + maxSalaryTeacher.getDegree());
System.out.println("Salary: " + maxSalaryTeacher.getSalary());
System.out.println("Tax amount: " + taxAmount);

System.out.println();

printCS101(allTeachers);
}
}

You might also like