Java without parameters with parameters

Question 1: Analyze the following requirements and implement them with code
Mobile Phone
Attributes:
brand
price
Behavior:
call call()
Send SMS sendMessage()
play game playGame()

Require:
1. Define the class according to the above requirements, the properties should be private, generate empty parameters, parameterized construction, setter and getter methods
2. Define the test class, create an object of this class in the main method and use the set method to assign values ​​to the properties (price: 998, brand: Xiaomi)
3. Call three member methods, the print format is as follows:
Using a mobile phone priced at 998 yuan to make a call....
Texting using a Xiaomi branded mobile phone....
Playing games with a Xiaomi-branded mobile phone priced at 998 yuan. …

mobile phone

package person;
public class Person {
    private String brand;
    private int price;
    void Person(){
        System.out.println("Brand is"+brand+"The price is"+price);
    }
    void call(int price){
        System.out.println("In use price is"+price+"cell phone call...");
    }
    void sendMessage(String brand){
        System.out.println("is using"+brand+"Branded cell phone texting...");
    }
    void playGame(String brand,int price){
        System.out.println("In use price is"+price+"meta"+brand+"branded mobile games...");
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

main method class

public class Demo01 {
    public static void main(String[] args) {
        Person p1=new Person();
        p1.setBrand("Millet");
        p1.setPrice(998);
        p1.Person();
        p1.call(998);
        p1.sendMessage("Millet");
        p1.playGame("Millet",998);
    }
}

Question 2: Analyze the following requirements and implement them with code
1. Cat Cat
Attributes:
hair color
Breed breed
Behavior:
eat eat()
catch mouse catchMouse()
2. Dog Dog
Attributes:
hair color
Breed breed
Behavior:
Have a meal()
housekeeping lookHome()
Require:
1. Define the Cat class and the Dog class according to the above requirements, the properties should be private, generate empty parameters, parameterized construction, setter and getter methods
2. Define the test class, create an object of each class in the main method and assign a value to the property (demonstrating two methods: setter method and constructor)
3. Call the member method of each object, the print format is as follows:
Colored Persian cat is eating fish.....
The Persian cat is chasing mice....
Black Tibetan Mastiff is chewing on the bone.....
Black Tibetan Mastiff is watching the house.....

Cat and Dog Integration:

package person;
public class Person02 {
     static String color;
     static String breed;

    public String getColor() {
        return color;
    }
    void  eat(){
        System.out.println(color+"of"+breed+"eating fish...");
    }
    void catchMouse(){
        System.out.println(color+"of"+breed+"catching mice...");
    }
    void lookHome(){
        System.out.println(color+"of"+breed+"housekeeping.....");
    }
    void lunch(){
        System.out.println(color+"of"+breed+"gnawing on bones.....");
    }
    public void setColor(String color) {
        this.color = color;
    }

    public String getBreed() {
        return breed;
    }

    public void setBreed(String breed) {
        this.breed = breed;
    }
}

Main method:

public class Demo02 {
    public static void main(String[] args) {
        Person02 p1=new Person02();
        p1.setColor("suit");
        p1.setBreed("persian cat");
        p1.eat();
        p1.catchMouse();
        Person02 p2=new Person02();
        p2.setColor("black");
        p2.setBreed("Tibetan Mastiff");
        p2.lunch();
        p2.lookHome();
    }
}

Question 3: Analyze the following requirements and implement them with code
1. Teacher class
Attributes:
name
age
Lecture content
Behavior:
Have a meal
lecture
2. Student class
Attributes:
name
age
learning content
Behavior:
eat eat()
learnstudy()
Require:
1. Define the Teacher class and the Student class according to the above requirements, the properties should be private, generate empty parameters, parameterized construction, setter and getter methods
2. Define the test class, create an object of each class in the main method and assign a value to the property (demonstrating two methods: setter method and constructor)
3. Call the member method of each object, the print format is as follows:
Teacher Zhou Zhipeng, aged 30, is eating....
Zhou Zhipeng, a 30-year-old teacher, is talking about the object-oriented knowledge in the basics of Java...("Object-oriented in the basics of Java" represents the content of the teacher's lecture)
Han Guang, 18, is having a meal....
Han Guang, who is 18 years old, is listening intently to object-oriented knowledge.... ("object-oriented" represents what students learn)

Teacher class:

package person;

public  class Teacher{
    static String name;
    static int age;
    static String content;

    public static String getName() {
        return name;
    }

    public static void setName(String name) {
        Teacher.name = name;
    }

    public static int getAge() {
        return age;
    }

    public static void setAge(int age) {
        Teacher.age = age;
    }

    public static String getContent() {
        return content;
    }

    public static void setContent(String content) {
        Teacher.content = content;
    }

    //Teacher Zhou Zhipeng, aged 30, is eating....
    void eat(){
        System.out.println("Age is"+age+"of"+name+"teacher is eating...");
    }
    //The 30-year-old teacher Zhou Zhipeng is excitedly talking about the object-oriented knowledge in the basics of Java........
    void  stu(){
        System.out.println("Age is"+age+"of"+name+"The teacher is talking excitedly"+content+"knowledge...");
    }
    //Han Guang, 18, is having a meal....
    void lunch(){
        System.out.println("Age is"+ getAge()+"of"+getName()+"classmates eating...");
    }
    //Han Guang, who is 18 years old, is listening intently to object-oriented knowledge.... ("object-oriented" represents what students learn)
    void study(){
        System.out.println("Age is"+getAge()+"of"+getName()+"Classmates are listening intently"+getContent()+"knowledge...");
    }

}

Student class:

public  class  Student extends  Teacher{
    Teacher t1=new Teacher();
}    

Main method:

public class Demo03 {
    public static void main(String[] args) {
        Teacher t1=new Teacher();
        t1.setName("Zhou Zhipeng");
        t1.setAge(30);
        t1.setContent("Java object-oriented");
        t1.stu();
        t1.eat();
        Student s1=new Student();
        s1.setName("Han Guang");
        s1.setAge(18);
        s1.setContent("object oriented");
        s1.lunch();
        s1.study();
    }
}

Question 4: Analyze the following requirements and implement them with code
Define a human Person with the following members:
Member properties:
name name (String type)
age age(double type)

1. Define Person according to the above requirements, the properties should be private, generate empty parameters, parameterized construction, setter and getter methods
2. Define the test class: Create multiple objects according to the following requirements (created using the full parameter structure, that is, the parameterized structure).
Pharaoh-35 Xiaofang-23
3. Through two objects, compare who is older and print it out.
Example: Lao Wang is older

People class (human)

package person;
public class People {
    private String name;
    private double age;
    void People(){

    }
    void People(String name,double age){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getAge() {
        return age;
    }

    public void setAge(double age) {
        this.age = age;
    }
}

Main method:

public class Demo04 {
    public static void main(String[] args) {
        People p1=new People();
        p1.setName("Lao Wang");
        p1.setAge(35);
        People p2=new People();
        p2.setName("Xiaofang");
        p2.setAge(23);
        if (p1.getAge()>p2.getAge()){
            System.out.println(p1.getName()+"older");
        }else {
            System.out.println(p2.getName()+"older");
        }
    }
}

Posted by DigitalNinja on Wed, 11 May 2022 06:39:13 +0300