0

Java Encapsulation welcome challenge

Test 5 fails no matter how I set the variables. I've swapped the age variables in the setAge class from a to age and back again one by one and test 5 never passes no matter which way I try it. Is this a bug? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int a = read.nextInt(); Pupil pupil = new Pupil(); pupil.setAge(a); } } class Pupil{ private int age; //complete setter method public void setAge(int age){ if (age >= 6){ this.age = age; System.out.println("Welcome"); } else { System.out.println("Sorry"); } } public int getAge(){ return age; } }

25th Nov 2021, 8:37 AM
Paul McPhillips
Paul McPhillips - avatar
3 Respostas
+ 3
I think Over 6 means if age > 6 not age>=6. Have you tryed it this way?
25th Nov 2021, 9:33 AM
Coding Cat
Coding Cat - avatar
0
Yeah that was my problem 🤦. I knew it had to be something small I was overlooking. I need to learn to read the problem better. Thanks for the help
25th Nov 2021, 12:24 PM
Paul McPhillips
Paul McPhillips - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int a = read.nextInt(); Pupil pupil = new Pupil(); pupil.setAge(a); } } class Pupil{ private int age; //complete setter method public void setAge(int age){ if (age > 6){ //age is over 6 not age>=6 this.age = age; System.out.println("Welcome"); } else { System.out.println("Sorry"); } } public int getAge(){ return age; } }
17th Jan 2023, 7:06 AM
Harshika Madhushani
Harshika Madhushani - avatar