Create a class Teacher(Tid, Tname, Designation, Salary, Subject). Write a Java program to accept the details of ‘n’ teachers and display the details of teacher who is teaching Java Subject.(Use array of Object)
import java.util.Scanner;
class Teacher {
private int Tid;
private String Tname;
private String Designation;
private float Salary;
private String Subject;
public Teacher(int Tid, String Tname, String Designation, float Salary, String Subject) {
this.Tid = Tid;
this.Tname = Tname;
this.Designation = Designation;
this.Salary = Salary;
this.Subject = Subject;
}
public String toString() {
return "Teacher id: " + Tid + "\n" + "Teacher name: " + Tname + "\n" + "Teacher designation: " + Designation
+ "\n" + "Teacher salary: " + Salary + "\n" + "Teacher subject: " + Subject + "\n";
}
/**
* @return the subject
*/
public String getSubject() {
return Subject;
}
}
class App {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Ente the number of teachers: ");
int n = keyBoard.nextInt();
Teacher[] teachers = new Teacher[n];
for (int i = 0; i < n; i++) {
System.out.print("Ente the teacher's id: ");
int Tid = keyBoard.nextInt();
keyBoard.nextLine();
System.out.print("Ente the teacher's name: ");
String Tname = keyBoard.nextLine();
System.out.print("Ente the teacher's designation: ");
String Designation = keyBoard.nextLine();
System.out.print("Ente the teacher's salary: ");
float Salary = keyBoard.nextFloat();
keyBoard.nextLine();
System.out.print("Ente the teacher's subject: ");
String Subject = keyBoard.nextLine();
teachers[i] = new Teacher(Tid, Tname, Designation, Salary, Subject);
}
for (int i = 0; i < n; i++) {
if (teachers[i].getSubject().compareToIgnoreCase("Java") == 0) {
System.out.println(teachers[i].toString());
}
}
keyBoard.close();
}
}
Comments
Leave a comment