Answer to Question #299860 in Java | JSP | JSF for Jack

Question #299860

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)


1
Expert's answer
2022-02-19T16:56:47-0500
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();
	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS