クラスParents,Father,Motherを定義し,ここでParentsは親,Father,Motherは子であり,その特性をテストする.

13759 ワード

【問題の説明】クラスParents,Father,Motherを定義し、ここでParentsは親であり、Father,Motherは子であり、その特性をテストする.
【入力形式】お父さんの名前を入力してください:Jackお母さんの名前を入力してください:Roseお父さんの年齢を入力してください:30お母さんの年齢を入力してください:32【出力形式】Please input your father’s name:Please input your mother’s name:Please input your father’s age:Please input your father’s age:Please input your mother’s age:
【サンプル入力】JackRose 3032
【サンプル出力】
I am the father. my name is: Jackmy age is: 30my sex is: MI am the mother.my name is: Rosemy age is: 32my sex is: F
import  java.util.Scanner;

class  Parents  {

        private  String  name;

        private  int  age;

        private  String  sex;

        

        public  String  getName()  {

                return  name;

        }

        

        public  void  setName(String  name)  {

                this.name  =  name;

        }

        

        public  int  getAge()  {

                return  age;

        }

        

        public  void  setAge(int  age)  {

                this.age  =  age;

        }

        

        public  String  getSex()  {

                return  sex;

        }

        

        public  void  setSex(String  sex)  {

                this.sex  =  sex;

        }

        

        public  void  getInfo()  {

                System.out.println("my  name  is:  "  +  getName());

                System.out.println("my  age  is:  "  +  getAge());

                System.out.println("my  sex  is:  "  +  getSex());

        }  

}

class  Father  extends  Parents  {

        public void message() {
		System.out.println("I am the father.");
	}


}

class  Mother  extends  Parents  {

        public void message() {
		System.out.println("I am the mother.");
	}

}

        

public  class  P4T1  {

        public  static  void  main(String[]  args)  {

                

                Father  f  =  new  Father();

                Mother  m  =  new  Mother();

                

                String  name;

                int  age;

                

                Scanner  s  =  new  Scanner(System.in);

                

                System.out.println("Please  input  your  father's  name:");

                f.setName(s.nextLine());

                System.out.println("Please  input  your  mother's  name:");

                m.setName(s.nextLine());

                

                System.out.println("Please  input  your  father's  age:");

                f.setAge(s.nextInt());

                System.out.println("Please  input  your  mother's  age:");

                m.setAge(s.nextInt());

                

                f.setSex("M");

                f.message();

                f.getInfo();

        

                

                m.setSex("F");

                m.message();

                m.getInfo();

        }

}