Encapsulation
This commit is contained in:
@ -1,7 +1,12 @@
|
|||||||
package com.oop;
|
package com.oop;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
29
more_on_classes/src/main/java/com/oop/Pupil.java
Normal file
29
more_on_classes/src/main/java/com/oop/Pupil.java
Normal 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.
BIN
more_on_classes/target/classes/com/oop/Pupil.class
Normal file
BIN
more_on_classes/target/classes/com/oop/Pupil.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user