Java object oriented exercises

Posted by virken on Mon, 31 Jan 2022 17:00:01 +0100

Knowledge points

object-oriented

Topic 1 (training)

Define the mobile phone class. The mobile phone has three attributes: brand, price and color. It has two functions: call() and sendMessage().

Please define the mobile phone class. There should be empty parameters, parameter construction methods and set/get methods in the class.

Define the test class, create the object with null parameter construction in the main method, and assign value with set method.

Call the two functions of the object, and the printing effect is as follows:

I'm using a black Xiaomi mobile phone with a price of 3998 yuan to make a call
 I'm using the black Xiaomi mobile phone with a price of 3998 yuan to send text messages

Training tips

  1. The attribute in the class is the member variable, and the behavior function in the class is the member method.

  2. Member variables should be decorated with private.

Problem solving scheme

Operation steps

  1. Define the mobile phone class. The mobile phone class defines the brand of String type, the price of int type and the color of String type. The three member variables are decorated with price.

  2. Provide null parameter construction method and parametric construction method.

  3. Provide set/get methods.

  4. Write a member method for calling, in which the member variable is used.

  5. Write the member method of sending text messages, in which the member variables are used.

  6. Create a mobile phone object in the test class, assign values with the set method, and call each method respectively.

Reference answer

Mobile phones:
public class Phone {
    //Define brand
    private String brand;
    //Define price
    private int price;
    //define color
    private String color;
​
    //Provide null parameter structure
    public Phone() {
    }
    //Provide parametric structure
    public Phone(String brand, int price, String color) {
        this.brand = brand;
        this.price = price;
        this.color = color;
    }
    //Provide set/get method
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    //Define call method
    public void call(){
        System.out.println("Price in use is"+price+"element"+color+"of"+brand+"Phone call....");
    }
    //Define texting method
    public void sendMessage(){
        System.out.println("Price in use is"+price+"element"+color+"of"+brand+"Send SMS via mobile phone....");
    }
}
​
Test class:
public class Demo1 {
    public static void main(String[] args) {
        //Create mobile object
        Phone p = new Phone();
        //Call set method assignment
        p.setBrand("millet");
        p.setPrice(3998);
        p.setColor("black");
        //Call call function
        p.call();
        //Call SMS function
        p.sendMessage();
    }
}

Topic 2 (training)

Define a girlfriend class. The attributes of girlfriend include: name, height and weight. Behaviors include: washing clothes (), cooking (). In addition, define a show() method for displaying the values of three properties. Please create objects and assign values through the parametric construction method in the test class, and then call the display method, laundry method and cooking method respectively. The printing effect is as follows:

My girlfriend's name is Sister Feng. She is 155.0 cm tall and weighs 130.0 kg
 My girlfriend helps me wash clothes
 My girlfriend cooks for me

Training tips

  1. The attribute in the class is the member variable, and the behavior function in the class is the member method.

  2. Member variables should be decorated with private.

  3. The display method is used to print the values of three member variables: name, height and weight.

Problem solving scheme

Operation steps

  1. Define girlfriend class, define three member variables: String type name, double type height and double type weight, and all three member variables are decorated with price.

  2. Provide null parameter construction method and parametric construction method.

  3. Provide set/get methods.

  4. Write the display method show(), which prints the values of three member variables.

  5. Write the wash() method to output the wash statement.

  6. Write cook() method to output cooking statement.

  7. Create a girlfriend object in the test class using a parametric construct, and call each method separately.

Reference answer

Girlfriends:
public class Girlfriend {
    //full name
    private String name;
    //height
    private double height;
    //weight
    private double weight;
    //Empty parameter structure
    public Girlfriend() {
    }
    //Parametric structure
    public Girlfriend(String name, double height, double weight) {
        this.name = name;
        this.height = height;
        this.weight = weight;
    }
    //set/get method
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    //Display method
    public void show(){
        System.out.println("My girlfriend's name is" +name+",height" +height+"centimeter,weight"+weight+"Jin");
    }
    //Laundry method
    public void wash(){
        System.out.println("My girlfriend helps me wash clothes");
    }
    //Cooking method
    public void cook(){
        System.out.println("My girlfriend cooks for me");
    }
}
Test class:
public class Demo2 {
    public static void main(String[] args) {
        //Create girlfriend object
        Girlfriend gf = new Girlfriend("Miss Luo Yu feng",155,130);
        //Call presentation method
        gf.show();
        //Call the laundry method
        gf.wash();
        //Call cooking method
        gf.cook();
    }
}

Topic 3 (training)

Define the project Manager class Manager. Attributes: name, job id, salary, bonus. Behavior: work() defines the programmer class Coder. Attributes: name, job id, salary. Behavior: work()

requirement:

1. Define the Manager class and Coder class according to the above requirements. The properties should be private, generate null parameter and parametric structures, and set and get methods # 2 Define the test class, create the object of this class in the main method and assign a value to the attribute (set method or parametric construction method) 3 Call the member method, and the print format is as follows:

The project manager with job number 123, basic salary 15000 and bonus 6000 is trying to do management work, assign tasks and check the code submitted by employees
 A programmer with a job number of 135 and a basic salary of 10000 is trying to write code

Training tips

  1. The attribute in the class is the member variable, and the behavior function in the class is the member method.

  2. Member variables should be decorated with private.

  3. In the work work() method, member variables are called to output the values of member variables.

Problem solving scheme

Operation steps

  1. Define the project manager class, define member variables, construction methods, set and get methods, and work methods. In the methods, the values of ID, salary, and bonus are output according to the print format.

  2. Define program classes, define member variables, construction methods, set and get methods, and work methods, in which the values of id and salary are output according to the print format.

  3. In the test class, the project manager object is created and assigned by using the parametric structure, and the working method is called to print the results.

  4. In the test class, use the parametric structure to create the programmer object and assign values, and call the working method to print the results.

Reference answer

Manager:
public class Manager {
    private String name;
    private int id;
    private int salary;
    private int bonus;
    public Manager() {
    }
    public Manager(String name, int id, int salary, int bonus) {
        this.name = name;
        this.id = id;
        this.salary = salary;
        this.bonus = bonus;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public int getBonus() {
        return bonus;
    }
    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
    public void work(){
        System.out.println("Job No"+id+"The basic salary is"+salary+"Bonus is"+bonus+"The project manager is trying to do the management work,Assign tasks,Check the code submitted by the employee.....");
    }
}
Programmer class:
package com.day07;
​
public class Coder {
    private String name;
    private int id;
    private int salary;
    public Coder() {
    }
    public Coder(String name, int id, int salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public void work(){
        System.out.println("Job No"+id+"The basic salary is"+salary+"Our programmers are trying to write code......");
    }
}
Test class:
public class Demo3 {
    public static void main(String[] args) {
        //Create manager object
        Manager m = new Manager("Lao Wang",123,15000,6000);
        //Call working method
        m.work();
        //Create programmer object
        Coder c = new Coder("Xiao Wang",135,10000);
        //Call working method
        c.work();
    }
}

Topic 4 (training)

Define cat. Attributes: color of hair, variety, breed. Behavior: eat(), catch mice, catchMouse(), define Dog. Attributes: color of hair, variety, breed. Behavior: eat(), look after the house () requirements: 1 Cat class and Dog class are defined according to the above requirements. The attributes should be private, and null parameter and parametric structures are generated. Set and get methods # 2 Define the test class, create the object of this class in the main method and assign a value to the attribute (set method or parametric construction method) 3 Call the member method, and the print format is as follows:

The colorful Persian cat is eating fish
 The colorful Persian cat is catching mice
 The black Tibetan mastiff is gnawing at the bone
 The black Tibetan mastiff is watching the house

Training tips

  1. The attribute in the class is the member variable, and the behavior function in the class is the member method.

  2. Member variables should be decorated with private.

Problem solving scheme

Operation steps

  1. Define cat class, define member variable, construction method, set and get method, eat method (), catch mouse method (), and output the value of member variable according to the format given by the title.

  2. Define Dog class, define member variable, construction method, set and get method, eat method (), housekeeping method lookHome(), which outputs the value of member variable according to the format given by the title.

  3. Create cat class objects in the test class by using parametric construction, and call eat() method and catchMouse() method.

  4. Create a dog object in the test class with a parameter construct, and call the eat() method and lookHome() method.

Reference answer

Cats:
public class Cat {
    private String color;
    private String breed;
    public Cat(){}
    public Cat(String color, String breed) {
        this.color = color;
        this.breed = breed;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getBreed() {
        return breed;
    }
    public void setBreed(String breed) {
        this.breed = breed;
    }
    public void eat(){
        System.out.println(color+"of"+breed+"Eating fish.....");
    }
    public void catchMouse(){
        System.out.println(color + "of"+ breed +"Catching mice....");
    }
}
Dogs:
public class Dog {
    private String color;
    private String breed;
    public Dog(){}
    public Dog(String color, String breed) {
        this.color = color;
        this.breed = breed;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getBreed() {
        return breed;
    }
    public void setBreed(String breed) {
        this.breed = breed;
    }
    public void eat(){
        System.out.println(color + "of"+ breed +"Gnawing at a bone.....");
    }
    public void lookhome(){
        System.out.println(color + "of"+ breed +"Looking after the house.....");
    }
}
Test class:
public class Demo4 {
    public static void main(String[] args) {
        //Create cat object
        Cat c = new Cat("Decor","Persian cat");
        //Call cat's method
        c.eat();
        c.catchMouse();
        //Create dog object
        Dog d = new Dog("black","Tibetan Mastiff");
        d.eat();
        d.lookhome();
    }
}

1. Which of the column options is the default constructor of class public class Test {}_ D__? (knowledge point: constructor)

A.Test();

B.Test(void);

C.public Test();

D.public void Test();

2. In Java, a class can define many methods with the same name at the same time. The number, type or order of formal parameters of these methods are different,

The returned values can also be different. This feature of object-oriented programs is called (single choice). C

A. Hide B. overwrite

C. Overloaded D, Java does not support this feature

3. In Java, the following statement about method overloading is wrong (C).

A. Method overloading requires that the method names must be the same. B. the parameter list of overloaded methods must be inconsistent

C. The return types of overloaded methods must be consistent. D. a method can only be overloaded once in its class

4. In Java, the following description of the constructor is correct (D).

A. Class must explicitly define constructor B. the return type of constructor is void C. constructor and class have the same name and cannot take any parameters

D. A class can define multiple constructors

5. The running result of the following Java code is (C).

class Penguin {
String name=null; //  name
 int health=0; //  Health value
 String sex=null; //  Gender
 public void Penguin() {/ / is a general method, not a construction method
health = 10;
sex = "male";
System.out.println("execute construction method");
}
public void print() {
System.out.println("Penguin's name is" + name + ", health value is" + health + ", and gender is" + sex + "); 
}
​
    
public static void main(String[] args) {
Penguin pgn = new Penguin();
pgn.print();
}
}
​
​

A. Penguin's name is null, its health value is 10, and its gender is male.

B. Execute the construction method. Penguin's name is null, health value is 0, and gender is null.

C. Penguin's name is null, health value is 0, and gender is null.

D. Execute the construction method. Penguin's name is null, its health value is 10, and its gender is male.

Topics: Java Back-end OOP