+ 1

Practice 42.1 Encapsulation (Welcome)

You need a program to manage admissions for an art school. Pupils can be admitted to the school if they are over 6 years of age. You're given a program which declares a Pupil class. Task Complete the setAge method of the Pupil class. If the value of parameter a is over 6, assign it to age attribute and output "Welcome". Output "Sorry" otherwise. Sample Input 7 Sample Output Welcome *Use an if statement to check the mentioned condition.

21st Sep 2021, 2:40 AM
Chin Eu
Chin Eu - avatar
13 Antworten
+ 11
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 a){ age =a; if (age > 6 ){ System.out.println("Welcome"); } else { System.out.println("Sorry"); } } public int getAge(){ return age; } }
3rd Dec 2021, 12:12 AM
Ahmad Ziadat
Ahmad Ziadat - avatar
+ 6
Mistakes : 1. use public void setAge(int age){ } instead of public void setAge(int a){} not' int a 'it should ' int age '
21st Sep 2021, 3:13 AM
Pariket Thakur
Pariket Thakur - avatar
+ 2
Thanks HrCoder !
21st Sep 2021, 4:39 AM
Chin Eu
Chin Eu - avatar
+ 2
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 a){ age=a; if(age>6){ this.age=age; System .out .println ("Welcome"); } else { System .out .println ("Sorry"); } } public int getAge(){ return age; } }
10th Jan 2022, 2:38 PM
Srinivas vedullapalli
Srinivas vedullapalli - avatar
+ 2
@Srinivas Vedullapalli Your answer worked out. Thank you so much
13th Oct 2022, 2:52 PM
Kanagaraj Arunthadhy
Kanagaraj Arunthadhy - avatar
+ 1
Maybe try age > 6 as the condition?
21st Sep 2021, 2:58 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
First show your attempt
21st Sep 2021, 2:41 AM
Pariket Thakur
Pariket Thakur - avatar
0
CarrieForle tried and it is still the same
21st Sep 2021, 3:02 AM
Chin Eu
Chin Eu - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int a =7; 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; } }
21st Sep 2021, 3:11 AM
Pariket Thakur
Pariket Thakur - avatar
0
This should help out... First create a class to hide the varaible public class Pupli{ private int age; public void setAge(int age) { this.age = age; } public int getAge() { return age; } } Secondly Create a main and and call in your pupils import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Pupil pupil= new Pupil(); int a ; System.out.println("Enter Age Value"); a = input.nextInt(); if (a > 6){ System.out.println("Welcome"); } else { System.out.println("Sorry"); } } }
13th Oct 2021, 5:25 AM
Peter Aro
Peter Aro - 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 a){ age =a; if (age > 6 ){ System.out.println("Welcome"); } else { System.out.println("Sorry"); } } public int getAge(){ return age; } }
15th Nov 2022, 7:36 AM
Pooja Patel
Pooja Patel - avatar
0
/* You need a program to manage admissions for an art school. Pupils can be admitted to the school if they are over 6 years of age. You're given a program which declares a Pupil class. Task Complete the setAge method of the Pupil class. If the value of parameter a is over 6, assign it to age attribute and output "Welcome". Output "Sorry" otherwise. Sample Input 7 Sample Output Welcome */ 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 a){ if(a > 6) { this.age = a; System.out.println("Welcome"); } else { System.out.println("Sorry"); } } public int getAge(){ return age; } }
22nd May 2023, 6:56 AM
Esteban Perez-Ortiz
Esteban Perez-Ortiz - avatar
- 1
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 a){ if(age >= 6) { this.age = age; System.out.println(“Welcome”); } else { System.out.println(“Sorry”); } } public int getAge(){ return age; } } This is my code but it isn’t correct.
21st Sep 2021, 2:42 AM
Chin Eu
Chin Eu - avatar