0

setters & getters Quesition about problem

While solving the problem, Test Case 2 fails. The code seems to match the solution, what should I do? Test case 2 : Input Lora -18 Your Output Invaild age Name: Lora Age: 0 Expected Output Invalid age Name: Lora Age: 0 import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String name = read.nextLine(); int age = read.nextInt(); Student student = new Student(); student.name = name; student.setAge(age); //set the age via Setter System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; private int age; public int getAge() { return age; //complete Getter } public void setAge(int age) { //complete Setter if(age<0) { System.out.println("Invaild age"); this.age = 0; } else { this.age = age; } } }

13th Nov 2022, 11:09 AM
알놀자 (알놀자)
알놀자 (알놀자) - avatar
1 Answer
+ 2
Typo You wrote "Invaild", fix to "Invalid" 😊
13th Nov 2022, 11:23 AM
Tibor Santa
Tibor Santa - avatar