Java object oriented exercises

Keywords: Java Mobile

1. Number guessing game:

A Class A has two member variables v, num, and v has an initial value of 100.

Define a method guess, guess the member variable v of class A with num.

If it is large, the prompt is large; if it is small, the prompt is small. Equal to indicates a successful guess.

Testing in the main method

package hello.guess;

public class Guess {
    // Create properties
    private int a = 100;

    public static void main(String[] args) {
        int num;
        Guess g = new Guess();

        num = 101;
        g.game(num);

        num = 99;
        g.game(num);

        num = 100;
        g.game(num);


    }

    //  Construction method
    public void guess() {
        System.out.println("Create object..");
    }

    //Writing method
    public void game(int num) {
        if (num < a) {
            System.out.println("Guess a little small, small how many do not know!!!");
            System.out.println(num);
        } else if (num > a) {
            System.out.println("Guess a little big, big how much do not know!!!");
            System.out.println(num);
        } else {
            System.out.println("Guess right.");
            System.out.println(num);
        }
    }

}

Operation result:

 

 

2. Calculate the area and perimeter of the circle:

Create a Circle class. For this class, a variable r is provided to represent radius, and a constant Pi is provided to represent PI;

At the same time, two methods are provided for this class: method one is used to find the area of a circle, and method two is used to find the circumference of a circle;

A construction method without parameters is provided for this class. The value used to initialize r is 4.

Test in the main method.

 

package hello.circle;

/**
 * 2.Create a Circle class.
 * For this class, a variable r is provided to represent radius, and a constant Pi is provided to represent PI;
 * At the same time, two methods are provided for this class: method one is used to find the area of a circle, and method two is used to find the circumference of a circle;
 * A construction method without parameters is provided for this class. The value used to initialize r is 4.
 * Test in the main method.
 */
//Create a circle Circle class
public class Circle {

    //Provide a variable for this class r Represents radius, a constant PI Represents the PI
    public double r;
    public final double PI = 3.14;

    //Provide a parameter free constructor for this class to initialize r The value of is 4.
    public Circle() {
        System.out.println("Parameterless constructor: for R Assignment is 4 ---");
        r = 4;
    }

    //Method 1 for the area of a circle
    public void area() {
        System.out.println(r);
        System.out.println("The area of the circle is:" + PI * r * r);
    }

    //Method 2 for finding the circumference of a circle
    public void girth() {
        System.out.println(r);
        System.out.println("The circumference of the circle is:" + 2 * PI * r);
    }

    // main Method
    public static void main(String[] args) {

        System.out.println("-----");
        Circle c = new Circle();
        System.out.println("-----");

        c.area();

        c.girth();
    }
}

Run results:

 

3. Define vehicle type and control speed

Please define a vehicle class, including:

Attributes: speed, type of car, etc

Methods: move (), set speed (double s), speed up (double s), speed down (double s), etc

Finally, a Vehicle object is instantiated in main() of the test class Vehicle,

And initialize the value of speed and type through the construction method, and print it out. In addition, the acceleration and deceleration methods are called to change the speed.

 

package hello.vehicle;

/**
 * Please define a vehicle class, including:
 * Attributes: speed, type of car, etc
 * Methods: move (), set speed (double s), speed up (double s), speed down (double s), etc
 * Finally, a Vehicle object is instantiated in main() of the test class Vehicle,
 * And initialize the value of speed and type through the construction method, and print it out. In addition, the acceleration and deceleration methods are called to change the speed.
 */
public class Vehicle {
    public double speed;
    public String type;

    // Mobile method
    public void move() {
        System.out.println(type + " speed " + speed + " Mai----");
    }

    // Set speed method
    public void setSpeed(double s) {
        speed = s;
    }

    // accelerate
    public void speedUp(double num) {
        speed += num;
    }

    // Slow down
    public void speedDown(double num) {
        if (speed >= num) {
            speed -= num;
        } else {
            speed = 0;
        }
    }

    public Vehicle() {
        speed = 70;
        type = "Cadillac";
        System.out.println(type + " speed " + speed + " Mai----");
    }

    public static void main(String[] args) {
        Vehicle v = new Vehicle();//create object

        v.move();

        v.setSpeed(80);// set The speed is 80.
        v.move();
        v.speedUp(50);//Speed plus 50
        v.move();
    }
}

 

Operation result:

Posted by willeh_ on Wed, 06 Nov 2019 09:09:40 -0800