Encapsulation

This commit is contained in:
2024-09-17 08:44:32 +02:00
parent f7483e5e68
commit 74e421aa7e
4 changed files with 35 additions and 1 deletions

View File

@ -1,7 +1,12 @@
package com.oop;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner read = new Scanner(System.in);
int a = read.nextInt();
Pupil pupil = new Pupil();
pupil.setAge(a);
}
}

View File

@ -0,0 +1,29 @@
package com.oop;
// 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
public class Pupil {
private int age;
//complete setter method
public void setAge(int a){
if (a > 6) {
age = a;
System.out.println("Welcome");
}else{
System.out.println("Sorry");
}
}
public int getAge(){
return age;
}
}

Binary file not shown.