Java刷题类-3

该博客展示了Java编程中的对象实例化,包括学生、车辆、点、矩形和BMI指数类的创建与操作。通过Scanner接收用户输入,实现动态创建对象并计算相关属性,如学生的成绩、车辆的速度、两点之间的距离、矩形的周长和面积以及BMI指数。
摘要由CSDN通过智能技术生成

1学生-成绩关联实体

难点 当一个类调用另外一个类 里面有两个参数的时候 输入直接用new Score

循环创建内容的时候 可以定义一个Student[] 的数组类 在传值的时候直接用循环

package homework.part2;

import java.util.Scanner;

public class Test {


    public static void main(String[] args) {
        Student student = new Student();
        student.setName("李明");
        student.setScore(new Score(88, 99));
        System.out.println(student.toString());

        Scanner scanner = new Scanner(System.in);
        System.out.println("输入学生的姓名 及两门课的成绩");
        Student[] students = new Student[2];

        for (int i = 0; i < students.length; i++) {
            students[i] = new Student(scanner.next(), new Score(scanner.nextFloat(), scanner.nextFloat()));
        }
        System.out.println("学生姓名成绩是:");
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].toString());
        }


    }
}
package homework.part2;

public class Student {
    private String name;
    private Score score;

    public String getName() {
        return name;
    }

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

    public Score getScore() {
        return score;
    }

    public void setScore(Score score) {
        this.score = score;
    }


    public Student() {
    }

    public Student(String name, Score score) {
        this.name = name;
        this.score = score;
    }


    @Override
    public String toString() {
        return "学生姓名 " + name + '\n' + score;
    }
}
package homework.part2;

public class Score {
    private float experiment;
    private float project;

    public Score() {
    }

    public Score(float experiment, float project) {
        this.experiment = experiment;
        this.project = project;
    }

    public float getExperiment() {
        return experiment;
    }

    public float getProject() {
        return project;
    }

    public void setExperiment(float experiment) {
        this.experiment = experiment;
    }

    public void setProject(float project) {
        this.project = project;
    }

    @Override
    public String toString() {
        return
                "实验成绩 " + experiment +'\n' +"项目成绩 " + project ;
    }
}

2vehicle模式

package homework.part2;

public class Vehicle {
    private float speed;

    public Vehicle() {
    }

    public Vehicle(float speed) {
        this.speed = speed;
    }

    public float getSpeed() {
        return speed;
    }

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

    public float speedUp(float up) {
        speed += up;
        if (speed > 240)
            return speed = 240;
        else
            return speed;
    }

    public float speedDown(float down) {
        if (speed < 0) {
            System.out.println("不能减速");
            return speed;
        } else {
            speed -= down;
            return speed;
        }
    }

}
package homework.part2;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Vehicle vehicle = new Vehicle(scanner.nextFloat());
        System.out.println("初始的速度是" + vehicle.getSpeed());
        float up = vehicle.speedUp(scanner.nextFloat());
        System.out.println("加速"+up+"后的速度是" + vehicle.getSpeed());
        float down = vehicle.speedDown(scanner.nextFloat());
        System.out.println("加速" + down + "后的速度是" + vehicle.getSpeed());
    }
}

3MyPoint类求两点之间距离 要求要只传一个参数 

package homework.part2;
public class Point {
    private double x;
    private double y;

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

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

    public void setY(double y) {
        this.y = y;
    }

    public double distance(Point p) {
        return Math.sqrt((p.x - this.x) * (p.x - this.x) + (p.y - this.y) * (p.y - this.y));
    }

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}
package homework.part2;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Point point1 = new Point(scanner.nextDouble(), scanner.nextDouble());
        Point point2 = new Point(scanner.nextDouble(), scanner.nextDouble());
        double dis = point1.distance(point2);
        System.out.println("两点间的距离是");
        System.out.println(String.format("%.2f", dis));

    }
}

4设计一个名为Rectangle的类表示矩形

难点:获取创建矩形对象的个数  用一个静态变量的计数 每调用一次构造函数就加1

package homework.part2;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Rectangle rectangle1 = new Rectangle(scanner.nextDouble(), scanner.nextDouble());
        System.out.println(rectangle1.toString());
        Rectangle rectangle2 = new Rectangle(scanner.nextDouble(), scanner.nextDouble());
        System.out.println(rectangle2.toString());
        Rectangle rectangle3 = new Rectangle();
        rectangle3.setWidth(scanner.nextDouble());
        rectangle3.setHeight(scanner.nextDouble());
        System.out.println(rectangle2.toString());
    }
}
package homework.part2;
public class Rectangle {
    private static int numberOfRectangle ;
    private double width = 1;
    private double height = 1;

    public Rectangle() {
        numberOfRectangle++;
    }

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
        numberOfRectangle++;
    }


    public double getWidth() {
        return width;
    }

    public double getHeight() {
        return height;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getArea() {
        return width * height;
    }

    public double getPerimeter() {
        return 2 * (width + height);
    }

    public static int getNumberOfRectangle() {
        return numberOfRectangle;
    }


    @Override
    public String toString() {
        return "宽= " + width + '\n' +
                "高= " + height + '\n' +
                "周长= " + getPerimeter() + '\n' +
                "面积= " + getArea() + '\n' +
                "矩形个数= " + numberOfRectangle;
    }
}

5BMI指数

package homework;
public class BMI {
    private String name;
    private int age;
    private double weight;
    private double height;

    public BMI() {
    }

    public BMI(String name, int age, double weight, double height) {
        this.name = name;
        this.age = age;
        this.weight = weight;
        this.height = height;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getWeight() {
        return weight;
    }

    public double getHeight() {
        return height;
    }

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

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

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getBMI() {
        return weight / Math.pow(height, 2);
    }

    public String getStatus() {
        double status = getBMI();
        if (status < 18.5) {
            return "低重";
        } else if (status < 25 && status >= 18.5) {
            return "正常";
        } else if (status < 30 && status >= 25) {
            return "超重";
        } else {
            return "肥胖";
        }
    }

    @Override
    public String toString() {
        return "BMI{" +
                "name='" + name + '\n' +
                ", age=" + age +'\n' +
                ", weight=" + weight +'\n' +
                ", height=" + height +'\n' +
                "体重指数="+getBMI()+'\n' +getStatus()+
                '}';
    }
}
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BMI bmi = new BMI(scanner.next(), scanner.nextInt(), scanner.nextDouble(), scanner.nextDouble());
        System.out.println(bmi.toString());
    }

6三角形面积

package homework.part2;
public class Triangle {
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = 1.0;
    private boolean filled;
    private String color;

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    public Triangle(double side1, double side2, double side3, boolean filled, String color) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
        this.filled = filled;
        this.color = color;
    }

    public double getSide1() {
        return side1;
    }

    public double getSide2() {
        return side2;
    }

    public double getSide3() {
        return side3;
    }

    public boolean isFilled() {
        return filled;
    }

    public String getColor() {
        return color;
    }

    public void setSide1(double side1) {
        this.side1 = side1;
    }

    public void setSide2(double side2) {
        this.side2 = side2;
    }

    public void setSide3(double side3) {
        this.side3 = side3;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

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

    public double getArea() {
        double p = (side1 + side2 + side3) / 2;
        return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
    }
    public double getPerimeter(){
        return side1 + side2 + side3;
    }

    @Override
    public String toString() {
        return "Triangle " +
                "side1=" + side1 +'\n'+
                " side2=" + side2 +'\n'+
                " side3=" + side3 +'\n'+
                " filled=" + filled +'\n'+
                " color='" + color + '\n'+
                " 周长='" + getPerimeter() + '\n'+
                " 面积='" + getPerimeter()
                ;
    }
}
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Triangle triangle =
                new Triangle(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextBoolean(), scanner.next());
        System.out.println(triangle.toString());
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值