Create objec-oriented programming that well implement this principle:
"According to plato, a man should marry a woman who is half his age plus 7 years"
Create the following:
1. Two (2) classes (MyMainClass & TheOtherClass)
2. Two (2) mutator methods:
•setName - accepts a man's name then copies that name to the attribute "name"
•setAge - accepts a man's age then copies that age to the attribute "man_age"
3. Two (2) accessor method:
•getName (String) - returns the man name attribute "name"
•getWomanAge(int) - returns the woman's age attribute "woman_age"
Sample output:
Enter the man's name: Hansel
Enter the man's age: 36
Hansel should Mary a girl who's 25 years old.
import java.util.Scanner;
class TheOtherClass{
String name;
int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void getWomanAge() {
System.out.print( getName() + "should Mary a girl who's " + ((age/2)+7) + " years old.");
}
}
public class MyMainClass{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
TheOtherClass oth = new TheOtherClass();
System.out.print("Enter the man's name: ");
oth.setName(in.next());
System.out.print("Enter the man's age:");
oth.setAge(in.nextInt());
oth.getWomanAge();
}
}
Comments
Leave a comment