实训第三天

1.
public class Car {
    private String brand;
    private String color;
    private double price;

    public Car() {}

    public Car(String brand, String color, double price) {
        this.brand = brand;
        this.color = color;
        this.price = price;
    }

    public void print() {
        System.out.println("品牌:" + brand + " 颜色:" + color + " 价格:" + price);
    }

    public String getBrand() {
        return brand;
    }

    public String getColor() {
        return color;
    }

    public double getPrice() {
        return price;
    }

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

    public void setColor(String color) {
        this.color = color;
    }

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

    public void increasePrice01() {
        this.price += 1000;
    }

    public void increasePrice02(double a) {
        this.price += a;
    }

    public static void main(String[] args) {
        Car c1 = new Car();
        c1.print();

        Car c2 = new Car("奥迪", "黑色", 500000);
        c2.print();

        c2.setBrand("宝马");
        c2.setColor("白色");
        c2.setPrice(600000);
        c2.increasePrice01();
        c2.increasePrice02(5000);
        c2.print();
    }
}

2.

public class Circle {
    double r;
    double PI = 3.14;

    public Circle(double r) {
        this.r = r;
    }

    public double getArea() {
        return PI * r * r;
    }

    public double getLength() {
        return 2 * PI * r;
    }

    public static void main(String[] args) {
        Circle c = new Circle(4);
        System.out.println("圆的面积为:" + c.getArea());
        System.out.println("圆的周长为:" + c.getLength());
    }
}

3.

import java.util.Random;

public class DoubleColorBall {
    public static void main(String[] args) {
        //声明一个int类型 长度为7的一维数组,动态
        int[] arr = new int[7];
        //创建一个随机数对象
        Random random = new Random();

        //开始摇号(),需要先摇6红球 范围1-33
        for (int i = 0; i < arr.length - 1; i++) {
            //红球
            arr[i] = random.nextInt(33) + 1;
            //去重
            for (int j = i - 1; j >= 0; j--) {
                //表示号码重复了
                if (arr[i] == arr[j]) {
                    //若号码出现重复,需重新摇号
                    i--;
                    break;
                }
            }
        }

        //蓝球
        arr[arr.length - 1] = random.nextInt(17) + 1;

        //将数组中的双色球遍历
        System.out.print("本期中奖结果为:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

4.

public class Girl {
    String name;
    int age;
    boolean bf;

    public void show() {
        System.out.println("姓名:" + name + " 年龄:" + age + " 是否有男朋友:" + bf);
    }

    public static void main(String[] args) {
        Girl g = new Girl();
        g.show();
        g.name = "貂蝉";
        g.age = 18;
        g.bf = true;
        g.show();
    }

    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 boolean isBf() {
        return bf;
    }

    public void setBf(boolean bf) {
        this.bf = bf;
    }
}

5.

public class MethodDemo01 {
    public int add(int a,int b) {
        return a + b;
    }

    public static void main(String[] args) {
        MethodDemo01 m = new MethodDemo01();
        int sum = m.add(1,2);
        System.out.println("sum = " + sum);
    }
}

6.

public class Person {
    String name;
    int age ;

    public void show() {
        System.out.println("姓名:" + name + " 年龄:" + age);
    }

    public static void main(String[] args) {
        Person p = new Person();
        p.show();
        p.name = "张飞";
        p.age = 30;
        p.show();
    }
}

7.

public class Phone {
    String brand;
    double price;

    public Phone() {}

    public Phone(String brand, double price) {
        System.out.println("有参构造方法...");
    }

    public void show() {
        System.out.println("品牌:" + brand + " 价格:" + price);
    }

    public static void main(String[] args) {
        Phone p = new Phone();
        p.show();
        p.brand = "OPPO";
        p.price = 1800;
        p.show();
    }

}

8.

public class Point {
    int x;
    int y;

    public Point() {}

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void print() {
        System.out.println("横坐标:" + x + " 纵坐标:" + y);
    }

    public void setX() {
        this.x = x + 1;
    }

    public void setY(int a) {
        this.y += a;
    }

    public static void main(String[] args) {
        //创建一个Point类型的引用point,指向Point类型的对象
        Point p1 = new Point();
        p1.print();
        Point p2 = new Point(1,1);
        p2.print();
        p2.setX();
        p2.setY(2);
        p2.print();

    }
}

9.

public class Rectangle {
    int width;
    int length;

    public Rectangle(int width, int length) {
        this.width = width;
        this.length = length;
    }

    public int getArea() {
        return width * length;
    }

    public int getPer() {
        return 2 * (width + length);
    }

    public void showAll() {
        System.out.println("长:" + length);
        System.out.println("宽:" + width);
        System.out.println("面积:" + getArea());
        System.out.println("周长:" + getPer());
    }

    public static void main(String[] args) {
        Rectangle r = new Rectangle(5, 10);
        r.showAll();
    }
}

10.

public class Vehicle {
    private double speed;
    private String type;

    public Vehicle(double speed, String type) {
        this.speed = speed;
        this.type = type;
    }

    public void move() {
        System.out.println("这辆车以" + speed + "km/h的速度行驶");
    }

    public void setSpeed(double s) {
        this.speed = s;
    }

    public void speedUp(double s) {
        this.speed += s;
    }

    public void speedDown(double s) {
        this.speed -= s;
    }

    public double getSpeed() {
        return speed;
    }

    public String getType() {
        return type;
    }

    public static void main(String[] args) {
        Vehicle v = new Vehicle(80.0, "面包车");
        System.out.println(v.getType() + "的速度是" + v.getSpeed() + "km/h");
        v.speedUp(20.0);
        System.out.println("加速后的速度是" + v.getSpeed() + "km/h");
        v.speedDown(20.0);
        System.out.println("减速后的速度是" + v.getSpeed() + "km/h");
    }
}

  • 18
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值