Java Notes 020-Object-Oriented Programming (Advanced Part) Chapter Practice

Object-Oriented Programming (Advanced Part) Chapter Exercises

1. Try to write the execution result of the following code

package com10.exercise;

/**
 * @author Jia Qi
 * @version 1.0
 * 2023/1/3-15:16
 * com10.exercise
 */
public class Exercise01 {
    public static void main(String[] args) {
        Car car = new Car();
        Car car1 = new Car(100);
        System.out.println(car);//9.0 red
        System.out.println(car1);//100.0 red
    }
}

class Car {
    static String color = "white";
    double price = 10;

    public Car() {
        this.price = 9;
        this.color = "red";
    }

    public Car(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return price + "\t" + color;
    }
}

operation result

2. Programming questions

1. Declare a private static attribute currentNum[int type] in the Frock class, with an initial value of 100000, which is used as the initial value of the serial number of the garment when it leaves the factory.

2. Declare the public static method getNextNum as a method to generate the unique serial number of the jacket. Each call increases currentNum by 100 as the return value.

3. In the main method of the TestFrock class, call the getNextNum method twice to obtain the serial number and print it out.

4. Declare the serialNumber (serial number) attribute in the Frock class and provide the corresponding get method;

5. In the constructor of the Frock class, obtain a unique serial number for the Frock object by calling the getNextNum method, and assign it to the serialNumber property.

6. In the main method of the TestFrock class, create three Frock objects respectively, and print the serial numbers of the three objects to verify whether they are incremented by 100.

package com10.exercise;

/**
 * @author Jia Qi
 * @version 1.0
 * 2023/1/3-15:29
 * com10.exercise
 */
public class Exercise02 {
}

class Frock {
    private static int currentNum = 100000;
    private int serialNumber;

    public Frock() {
        serialNumber = getNextNum();
    }

    public static int getNextNum() {
        currentNum += 100;//Increment currentNum by 100
        return currentNum;
    }

    public int getSerialNumber() {
        return serialNumber;
    }
}

class TestFrock {
    public static void main(String[] args) {
        System.out.println(Frock.getNextNum());//100100
        System.out.println(Frock.getNextNum());//100200
        Frock frock1 = new Frock();//The serial number is 100300
        Frock frock2 = new Frock();//The serial number is 100400
        Frock frock3 = new Frock();//The serial number is 100500
        System.out.println(frock1.getSerialNumber());//100300
        System.out.println(frock2.getSerialNumber());//100400
        System.out.println(frock3.getSerialNumber());//100500
    }
}

operation result

3. Programming questions

Implement the following questions as required:

1. The animal class Animal contains the abstract method shout();

2. The Cat class inherits from Animal, and implements the shout method, which prints "Cats will scream"

3. The Dog class inherits from Animal, and implements the method shout, which prints "Dogs can bark"

4. Instantiate the object Animal cat =new Cat() in the test class, and call the shout method of cat

5. Instantiate the object Animal dog=new Dog() in the test class, and call the shout method of the dog

package com10.exercise;

/**
 * @author Jia Qi
 * @version 1.0
 * 2023/1/3-15:41
 * com10.exercise
 */
public class Exercise03 {
    public static void main(String[] args) {
        Animal cat = new Cat();
        cat.shout();
        Animal dog = new Dog();
        dog.shout();
    }
}

abstract class Animal {
    public abstract void shout();
}

class Cat extends Animal {
    @Override
    public void shout() {
        System.out.println("kitten, meow~~~");
    }
}

class Dog extends Animal {
    @Override
    public void shout() {
        System.out.println("puppy barking~~~");
    }
}

operation result

4. Programming questions

1. The calculator interface has a work method whose function is calculation. There is a mobile phone class Cellphone, which defines the method testWork to test the calculation function, and calls the work method of the calculation interface.

2. It is required to call the testWork method of the CellPhone object, using the anonymous inner class

package com10.exercise;

interface ICalculate {
    public double work(double num1, double num2);
}

/**
 * @author Jia Qi
 * @version 1.0
 * 2023/1/3-15:51
 * com10.exercise
 */
public class Exercise04 {
    public static void main(String[] args) {
        Cellphone cellphone = new Cellphone();
        cellphone.testWork(new ICalculate() {
            @Override
            public double work(double num1, double num2) {
                return num1 + num2;
            }
        }, 100, 33);
    }
}

class Cellphone {
    public void testWork(ICalculate iCalculate, double num1, double num2) {
        double result = iCalculate.work(num1, num2);
        System.out.println("calculated result=" + result);
    }
}

operation result

5. Programming questions

inner class

1. Write a class A, define a local internal class B in the class, B has a private constant name, and a method show() to open

Print the constant name. carry out testing

2. Advanced: A also defines a private variable name, and prints the test in the show method

package com10.exercise;

/**
 * @author Jiaqi
 * @version 1.0
 * 2023/1/3-16:02
 * com10.exercise
 */
public class Exercise05 {
    public static void main(String[] args) {
        new A().f1();
    }
}

class A {
    private String name = "iron Man";

    public void f1() {
        class B {//local inner class
            private final String name = "Jiaqi";

            public void show() {
                System.out.println("name=" + name + " external class name=" + A.this.name);
            }
        }
        B b = new B();
        b.show();
    }
}

operation result

6. Programming questions

1. There is a vehicle interface class Vehicles with a work interface

2. There are Horse class and Boat class to implement Vehicles respectively

3. Create a vehicle factory class, there are two methods to obtain the vehicle Horse and Boat respectively

4. There is a Person class with name and Vehicles properties, assign values ​​to the two properties in the constructor

5. Instantiate the Person object "Tang Seng". It is required to use the Horse as the means of transportation under normal circumstances, and use the Boat as the means of transportation when encountering a big river.

package com10.exercise;

interface Vehicles {
    public void work();
}

/**
 * @author Jiaqi
 * @version 1.0
 * 2023/1/3-16:09
 * com10.exercise
 */
public class Exercise06 {
    public static void main(String[] args) {
        Person person = new Person("Tang monk", new Horse());
        person.common();
        person.passRiver();
        person.passFireHill();

    }
}

class Horse implements Vehicles {
    @Override
    public void work() {
        System.out.println("Under normal circumstances, riding a white dragon horse forward....");
    }
}

class Boat implements Vehicles {
    @Override
    public void work() {
        System.out.println("When crossing the river, use a speedboat....");
    }
}

class Plane implements Vehicles {
    @Override
    public void work() {
        System.out.println("After passing the Flame Mountain, Tang Seng flew the plane....");
    }
}

class VehiclesFactory {
    private static Horse horse = new Horse();

    public static Horse getHorse() {
//        return new Horse();
        return horse;
    }

    public static Boat getBoat() {
        return new Boat();
    }

    public static Plane getPlane() {
        return new Plane();
    }
}

class Person {
    private String name;
    private Vehicles vehicles;

    public Person(String name, Vehicles vehicles) {
        this.name = name;
        this.vehicles = vehicles;
    }

    public void passRiver() {
//        Boat boat = VehiclesFactory.getBoat();
//        boat.work();
        if (!(vehicles instanceof Boat)) {
            vehicles = VehiclesFactory.getBoat();
        }
        vehicles.work();
    }

    public void common() {
        if (!(vehicles instanceof Horse)) {
            vehicles = VehiclesFactory.getHorse();
        }
        vehicles.work();
    }

    public void passFireHill() {
        if (!(vehicles instanceof Plane)) {
            vehicles = VehiclesFactory.getPlane();
        }
        vehicles.work();
    }
}

operation result

7. Programming questions

inner class exercise

There is a Car class with an attribute temperature (temperature), and an Air (air conditioner) class in the car, which has the function of blowing flow. Air will monitor the temperature in the car and blow air-conditioning if the temperature exceeds 40 degrees. Turn on the heat if the temperature is below 0 degrees, and turn off the air conditioner if it is in between. Instantiate Car objects with different temperatures, call the flow method of the air conditioner, and test whether the air blown by the air conditioner is correct.

package com10.exercise;

/**
 * @author Jiaqi
 * @version 1.0
 * 2023/1/3-16:45
 * com10.exercise
 */
public class Exercise07 {
    public static void main(String[] args) {
        Mclaren mclaren1 = new Mclaren(99);
        mclaren1.getAir().flow();
        Mclaren mclaren2 = new Mclaren(-30);
        mclaren2.getAir().flow();
        Mclaren mclaren3 = new Mclaren(26);
        mclaren3.getAir().flow();
    }
}

class Mclaren {
    private double temperature;

    public Mclaren(double temperature) {
        this.temperature = temperature;
    }

    public Air getAir() {
        return new Air();
    }

    class Air {
        public void flow() {
            if (temperature > 40) {
                System.out.println("The temperature is greater than 40, the air conditioner blows cold air...");
            } else if (temperature < 0) {
                System.out.println("When the temperature is less than 0, the air conditioner is heating...");
            } else {
                System.out.println("The temperature is normal Turn off the air conditioner...");
            }
        }
    }
}

operation result

8. Programming questions

enum class

1. Create a Color enumeration class

2. There are five enumerated values/objects of RED, BLUE, BLACK, YELLOW, and GREEN;

3. Color has three attributes redValue, greenValue,blueValue,

4. Create a construction method, the parameters include these three attributes,

5. Each enumeration value must be assigned to these three attributes, and the corresponding values ​​of the three attributes are

6,red: 255,0,0 blue:0,0,255 black: 0,0,0 yellow: 255,255,0 green: 0,255,0

7. Define the interface, which contains the method show, which requires Color to implement the interface

8. The value of the three attributes is displayed in the show method

9. Match the enumeration object in the switch statement

package com10.exercise;

enum Color implements IMyInterface {
    RED(255, 0, 0),
    BLUE(0, 0, 255),
    BLACK(0, 0, 0),
    YELLOW(255, 255, 0),
    GREEN(0, 255, 0);
    private int redValue;
    private int greenValue;
    private int blueValue;

    Color(int redValue, int greenValue, int blueValue) {
        this.redValue = redValue;
        this.greenValue = greenValue;
        this.blueValue = blueValue;
    }

    @Override
    public void show() {
        System.out.println("The attribute value is" + redValue + "," + greenValue + "," + blueValue);
    }
}

interface IMyInterface {
    public void show();
}

/**
 * @author Jiaqi
 * @version 1.0
 * 2023/1/3-16:55
 * com10.exercise
 */
public class Exercise08 {
    public static void main(String[] args) {
        Color green = Color.GREEN;
        green.show();

        switch (green) {
            case RED:
                System.out.println("match to red");
                break;
            case YELLOW:
                System.out.println("match to yellow");
                break;
            case BLUE:
                System.out.println("match to blue");
                break;
            case BLACK:
                System.out.println("match to black");
                break;
            case GREEN:
                System.out.println("match to green");
                break;
            default:
                System.out.println("no match...hot hot");
        }
    }
}

operation result

Tags: Java intellij-idea

Posted by ahundiak on Wed, 04 Jan 2023 10:28:40 +1030