Classic comprehensive topics of Java object-oriented, collection and common classes

Question 2 [score 30 points]

  1. There are human beings, including name, age, gender, and work methods
  2. There are men. Men's job is to "make money to support their family" and their unique method of drink
  3. There are women. Women's work is "as beautiful as flowers", and the unique method of breed breeding
  4. The following requirements now exist:
    1. The pregnant function of women needs the participation of men.
    2. The pregnant age of a woman must be greater than or equal to 20 years old, and that of a man must be greater than or equal to {22 years old
    3. After conception, the gender of the child is generated by random numbers.
      1. If the random number is odd, it {is male
      2. The random data are even, but women.
    4. Now it is required that the child's name must be consistent with the father's name.
      1. For example, my father's last name is Zhang San
      2. Then the child's last name is Zhang
      3. The child's name is given by the current week. For example, today Friday is called Zhang 5
      4. If today is Saturday, it is called Zhang 6.

Demonstrate the specific presentation process: (compliance with requirements)

Step 1:

Please enter the man's name and age in the format of Zhang San, 23, male

Guo Jing, 26, male

Please enter the woman's name and age in the format of Li Si, 24, female

Huang Rong, 24, female

Step 2:

The display results are as follows:

Call the man's method and show "make money to support the family" and "drink a lot"

Call a woman's method and show "beautiful as a flower" and "their child is a girl, name is Guo 5"

Demonstrate the specific presentation process: (in case of error)

Step 1:

Please enter the man's name and age in the format of Zhang San, 23, male

Lao Wang, 56, male

Please enter the woman's name and age in the format of Li Si, 24, female

Jobillo, 18, female

Step 2:

The display results are as follows:

Call the man's method and show "make money to support the family" and "drink a lot"

Call a woman's method to show "beautiful as a flower" and "not in line with the legal marriage age, great Xia, please wait 20 years"

human beings

package Demo02;

public abstract class human {
    private String name;
    private int age;
    private String gender;
    public human(){};
    public human(String name,int age,String gender){
        this.age=age;
        this.gender=gender;
        this.name=name;
    }

    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 getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public abstract void work();
}

Men

package Demo02;

public class man extends human{
    @Override
    public void work() {
        System.out.println("Make money to support the family");
    }
    public void  drink(){
        System.out.println("Gulp");
    }
}

Women

package Demo02;

import java.util.Calendar;
import java.util.Random;

public class wuman extends human{
    Random random=new Random();
    Calendar calendar=Calendar.getInstance();
    @Override
    public void work() {
        System.out.println("as beautiful as flowers");
    }
    public void breed(int ageB,int ageG,String name){
        if (ageB>=22&&ageG>=20){
            char xing=name.charAt(0);
            int ming=calendar.DAY_OF_WEEK-2;
            char sex;
            if (random.nextInt(1)==1){
                sex='male';
            }else{
                sex='female';
            }
            System.out.println("Their children are"+sex+"My name is:"+xing+ming);
        }else{
            System.out.println("You do not meet the legal age for marriage. Please wait 20 years, great Xia");
        }
    }
}

Test class

package Demo02;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        man man=new man();
        man.setName("Zhang San");
        man.setAge(25);
        man.setGender("male");
        wuman woman=new wuman();
        woman.setAge(23);
        woman.setName("Li Si");
        woman.setGender("female");
        man.drink();
        man.work();
        woman.work();
        woman.breed(man.getAge(),woman.getAge(),man.getName());
    }
}

Operation results

Keywords: Java

Added by ethan6 on Tue, 04 Jan 2022 13:11:44 +0200