+ 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.
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;
}
}
+ 6
Mistakes :
1. use public void setAge(int age){ } instead of public void setAge(int a){}
not' int a 'it should ' int age '
+ 2
Thanks HrCoder !
+ 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;
}
}
+ 2
@Srinivas Vedullapalli
Your answer worked out. Thank you so much
+ 1
Maybe try age > 6 as the condition?
0
First show your attempt
0
CarrieForle tried and it is still the same
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;
}
}
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");
}
}
}
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;
}
}
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;
}
}
- 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.