import
java.util.*;
class
Teacher {
privateintamount;
private String tName;
privateinttestNum;
publicvoid getData(){
Scanner indata = new Scanner(System.in);
System.out.print("Enter the amount: ");
amount = indata.nextInt();
for(int num = 1; num <= amount; num++){
System.out.print("Enter the name of teacher " +(num)+": ");
tName = indata.nextLine();
indata.nextLine();
System.out.print("Enter the amount of test: ");
testNum = indata.nextInt();
}
}
//Return the teacher name
public String getName(){
returntName;
}
//Return the test Amount
publicint getTest(){
returntestNum;
}
}
class 2:
import
java.util.*;
class
School {
//Array List of teachers
static ArrayList<Teacher> teachers = new ArrayList<Teacher>();
//Adds a teacher to the Array List
publicvoid addTeacher(){
Teacher teacher = new Teacher();
teacher.getData();
teachers.add(teacher);
}
//Returns the amount of test number of a certain teacher
publicint getTeacherTest(String teachName){
Teacher teacher = new Teacher();
int value = 0;
for(int i = 0; i < teachers.size(); i++){
teacher = teachers.get(i);
if(teacher.getName().equals(teachName))
value = teacher.getTest();
}
return value;
}
}
Class 3:
import
java.util.*;
public
class Application {
publicstaticvoid main(String[] arguments){
int choice = 0;
while(choice !=3){
System.out.println("1. Add a teacher");
System.out.println("2. Get the test number");
System.out.println("3. Quit");
System.out.print("Choose an option: ");
Scanner indata = new Scanner(System.in);
choice = indata.nextInt();
//Creates a new school object
School school =
new School();
switch(choice){
case 1:
school.addTeacher();
System.out.println("Teacher added.");
System.out.println();
break;
case 2:
System.out.print("Enter the teacher's name: ");
indata.nextLine();
String teaName = indata.nextLine();
int test = school.getTeacherTest(teaName);
System.out.println("The test amount is: " +test);
System.out.println();
break;
}
}
}
}
Output:
1. Add a teacher
2. Get the test number
3. Quit
Choose an option: 1
Enter the amount: 1
Enter the name of teacher 1:
John
Enter the amount of test:
4
Teacher added.
1. Add a teacher
2. Get the test number
3. Quit
Choose an option: 2
Enter the teacher's name: John
The test amount is: 0
1. Add a teacher
2. Get the test number
3. Quit
Choose an option: 3
Why is it saying "The test amount is: 0" in the output?
When I used the method getTeacherTest() from the school class?
Shouldn't it return the value "4" instead of "0"?
Is there a way to compare the actual values of Strings?
If so, can you please show me how?
Thanks.


Sign In
Create Account


Back to top









