2021-7-26 JAVA练习题

import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;

/**
 * @author Peter Cheung
 * @user PerCheung
 * @date 2021/7/26 19:53
 * <p>
 * 1、一个类A有两个成员变量v、num,v有一个初值100。
 * 定义一个方法guess,对A类的成员变量v,用num进行猜。
 * 如果大了则提示大了,小了则提示小了。等于则提示猜测成功。
 * 在main方法中测试。
 * <p>
 * 2、创建一个圆Circle类。为该类提供一个变量r表示半径,一个常量PI表示圆周率;
 * 同时为该类提供两个方法:方法一用于求圆的面积,方法二用于求圆的周长;
 * 为该类提供一个无参的构造方法,用于初始化r的值为4。
 * 为该类提供一个有参的构造方法,用于初始化r的值。
 * 在main方法中测试。
 * <p>
 * 3、定义交通工具类型,控制速度
 * 请定义一个交通工具(Vehicle)的类,其中有:
 * 属性:速度(speed),车的类型(type)等等
 * 方法:移动(move()),设置速度(setSpeed(double s)),加速speedUp(double s),减速speedDown(double s)等等.
 * 最后在测试类Vehicle中的main()中实例化一个交通工具对象,
 * 并通过构造方法给它初始化speed,type的值,并且打印出来。另外,调用加速,减速的方法对速度进行改变。
 * <p>
 * <p>
 * 4、定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,
 * 2个构造器Point()和Point(int x0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动,
 * 创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。
 * <p>
 * 5、定义一个矩形类Rectangle:
 * <p>
 * (1) 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
 * <p>
 * (2)有2个属性:长length、宽width
 * <p>
 * (3)通过构造方法Rectangle(int width, int length),分别给两个属性赋值
 * (4) 创建一个Rectangle对象,并输出相关信息
 * <p>
 * <p>
 * 6、为“无名的粉”写一个类:class WuMingFen (注:“无名”是长沙一家很有特色的粉店)
 * 要求:
 * <p>
 * (1).有三个属性:面码:String theMa 粉的分量(两):int quantity
 * 是否带汤:boolean likeSoup
 * <p>
 * (2).写一个普通方法:check(),用于查看粉是否符合要求。即:将对象的三个属性打印在控制台上。
 * <p>
 * (3).写一个构造方法,以便于简化初始化过程,
 * 如,叫一碗3两牛肉带汤的粉:
 * <p>
 * WuMingFen f1 = new WuMingFen(“牛肉”,3,true);
 * <p>
 * (4).重载构造方法,使得初始化过程可以多样化,
 * 比如叫一碗2两鸡丝粉,则默认是带汤的:
 * <p>
 * WuMingFen f2 = new WuMingFen(“鸡丝”,2);
 * <p>
 * (5).如何顾客这样说:老板,一碗粉。则默认是酸辣面码、2两、带汤的?
 * <p>
 * WuMingFen f3 = new WuMingFen();
 * <p>
 * (6).调用check()方法,将上面每碗粉的属性都打印在控制台上。
 * <p>
 * <p>
 * 7、设计一个BankAccount类,实现银行某账号的资金往来账目管理,包括建账号、存入、取出等。
 * BankAccount类包括,账号(BankAccountId)、开户日期Date(日期),Rest(余额)。
 * 另有一个构造函数和三个成员函数Bankin()(处理存入账),Bankout()处理取出账)和和一个负责生成账号的自动增长的函数。
 * <p>
 * <p>
 * 8、设计一个类Student,该类包括姓名、学号和成绩。
 * 设计一个方法,对有5名学生按照成绩从高到低的顺序输出姓名、学号和成绩信息。
 * <p>
 * 9、编写一个程序,输入N个学生数据,包括学号、姓名、成绩,要求只输出成绩在80~89分的学生数据。
 * <p>
 * <p>
 * 10、编写一个程序,输入N个学生数据,包括学号、姓名、成绩,求学生的成绩总和、平均值、最大值、最小值。
 */
public class practice726 {
    public static void main(String[] args) {
        toptic();
    }

    public static void toptic() {
        System.out.print("请输入你想运行题目的序号:");
        Scanner s = new Scanner(System.in);
        int t = s.nextInt();
        switch (t) {
            case 1:
                toptic1();
                break;
            case 2:
                toptic2();
                break;
            case 3:
                toptic3();
                break;
            case 4:
                toptic4();
                break;
            case 5:
                toptic5();
                break;
            case 6:
                toptic6();
                break;
            case 7:
                toptic7();
                break;
            case 8:
                toptic8();
                break;
            case 9:
                toptic9();
                break;
            case 10:
                toptic10();
                break;
        }

    }


    public static void toptic1() {
        A a = new A();
        a.guessing();
    }

    public static void toptic2() {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入半径:");
        double r = s.nextDouble();
        Circle circle = new Circle();
        Circle circle1 = new Circle(r);
        double area1 = circle1.area();
        double area = circle.area();
        double perimeter = circle.perimeter();
        double perimeter1 = circle1.perimeter();
        System.out.println(area1);
        System.out.println(area);
        System.out.println(perimeter1);
        System.out.println(perimeter);
    }

    public static void toptic3() {
        Scanner s = new Scanner(System.in);
        System.out.print("设置汽车品牌:");
        String type = s.nextLine();
        System.out.print("设置默认车速:");
        double speed = s.nextDouble();
        Vehicle car = new Vehicle(speed, type);
        System.out.println("汽车默认速度为" + speed + ",汽车品牌为" + type);
        double setspeed = car.setSpeed(speed);
        System.out.println("设置车速" + setspeed);
        double speedup = car.speedUp(setspeed);
        System.out.println("加速至" + speedup);
        double speeddown = car.speedDown(speedup);
        System.out.println("减速至" + speeddown);
    }

    public static void toptic4() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入坐标1的初始坐标");
        int x1 = scanner.nextInt();
        int y1 = scanner.nextInt();
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(6, 3);
        int[] newp1 = p1.movePoint(34, 32);
        int[] newp2 = p2.movePoint(343, 32);
        System.out.println(Arrays.toString(newp1));
        System.out.println(Arrays.toString(newp2));
    }

    public static void toptic5() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入长度:");
        int length = scanner.nextInt();
        System.out.print("请输入宽度:");
        int width = scanner.nextInt();
        Rectangle rectangle = new Rectangle(length, width);
        rectangle.showAll();
    }

    public static void toptic6() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请问你要什么面什么粉?");
        String theMa = scanner.nextLine();
        System.out.println("要汤不?");
        Boolean likeSoup = scanner.nextBoolean();
        System.out.println("要几两?");
        int quantity = scanner.nextInt();
        WuMingFen f1 = new WuMingFen(theMa, quantity, likeSoup);
        WuMingFen f2 = new WuMingFen(theMa, quantity);
        WuMingFen f3 = new WuMingFen();
        f1.check();
        f2.check();
        f3.check();
    }

    public static void toptic7() {
        Scanner scanner = new Scanner(System.in);
        BankAccount b1 = new BankAccount(new Date(), 0);
        System.out.println(b1);
        System.out.println("你要存多少钱?");
        double cun = scanner.nextDouble();
        System.out.println("存款成功,余额:" + b1.Bankin(cun));
        System.out.println("你要取多少钱?");
        double qv = scanner.nextDouble();
        System.out.println("取款成功,余额:" + b1.Bankout(qv));

        BankAccount b2 = new BankAccount(new Date(), 3434);
        System.out.println(b2);
        BankAccount b3 = new BankAccount(new Date(), 234);
        System.out.println(b3);

    }

    public static void toptic8() {
        Student[] sc = {
                new Student("大毛", 1, 0),
                new Student("petr", 2, 23),
                new Student("per", 3, 33),
                new Student("r", 4, 63),
                new Student("peter", 5, 93),
        };

        for (int i = 0; i < sc.length - 1; i++) {
            for (int j = 0; j < sc.length - 1 - i; j++) {
                if (sc[j].getSc() < sc[j + 1].getSc()) {
                    Student temp = sc[j];
                    sc[j] = sc[j + 1];
                    sc[j + 1] = temp;
                }
            }

        }
        for (Student student : sc) {
            System.out.println(student);
        }


    }

    public static void toptic9() {
        Stud[] studs = new Stud[5];
        studs[0] = new Stud(2016001, "lily", 89);
        studs[1] = new Stud(2016002, "linlin", 80);
        studs[2] = new Stud(2016003, "sun", 78);
        studs[3] = new Stud(2016004, "jams", 87);
        studs[4] = new Stud(2016005, "dandan", 95);

    }

    public static void toptic10() {
        Studented s1 = new Studented(2016024301, "李一", 8239);
        Studented s2 = new Studented(2016003451, "李2", 2339);
        Studented s3 = new Studented(2016012301, "李3", 8439);
        Studented s4 = new Studented(2016342001, "李4", 4569);
        Studented s5 = new Studented(2016023401, "李5", 8934);
        Studented.num();
        Studented[] s = {s1,s2,s3,s4,s5};
        double max=Double.MIN_VALUE;
        double min=Double.MAX_VALUE;
        for(int i=0;i<s.length;i++){
            if(max<s[i].deg){
                max=s[i].deg;
            }
        }
        for(int i=0;i<s.length;i++){
            if(min>s[i].deg){
                min=s[i].deg;
            }
        }
        System.out.println("最大值:"+max);
        System.out.println("最小值:"+min);
    }

}


class A {
    // 成员变量
    private int v = 100;
    private int num;

    public int getV() {
        return v;
    }

    public void setV(int v) {
        this.v = v;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    // 构造器
    public A() {

    }

    // 方法
    public String guess() {
        if (this.getNum() > v) {
            return "大了";
        } else if (this.getNum() < v) {
            return "小了";
        } else if (this.getNum() == v) {
            return "赢了";
        }
        return null;
    }

    public void guessing() {
        A a = new A();
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("开始猜:");
            int guess = sc.nextInt();
            a.setNum(guess);
            String str = a.guess();
            System.out.println(str);
            if (str.equals("赢了")) {
                return;
            }
        }
    }
}

class Circle {
    public final static double PI = 3.1415926;

    private double r;


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

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

    public double area() {
        return PI * this.r * this.r;
    }

    public double perimeter() {
        return 2 * PI * this.r;
    }

}

class Vehicle {
    private double speed;
    private String type;

    // 移动(move())
    // 设置速度(setSpeed(double s))
    // 加速speedUp(double s)
    // 减速speedDown(double s)

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

    public void move() {
        System.out.println("开始运行");
    }

    public double setSpeed(double s) {
        System.out.println("设置速度");
        this.speed = s;
        return this.speed;
    }

    public double speedUp(double s) {
        System.out.println("加速");
        this.speed = s + 100;
        return this.speed;
    }

    public double speedDown(double s) {
        System.out.println("减速");
        this.speed = s / 2;
        return this.speed;
    }
}

class Point {
    private int x;

    private int y;

    public Point() {
    }

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

    public int[] movePoint(int dx, int dy) {
        this.x = dx;
        this.y = dy;
        int[] point = {x, y};
        return point;
    }
}

class Rectangle {
    private int length;
    private int width;

    public Rectangle() {
    }

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

    // getArea()求面积、getPer()求周长,showAll()
    public int getArea() {
        return this.length * this.width;
    }

    public int getPer() {
        return this.length + this.width + this.length + this.width;
    }

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

class WuMingFen {
    // 面码:String theMa 粉的分量(两):int quantity
    // 是否带汤:boolean likeSoup
    private String theMa;
    private int quantity;
    private boolean likeSoup;

    public WuMingFen() {
        this.theMa = "酸辣粉";
        this.quantity = 2;
        this.likeSoup = true;
    }

    public WuMingFen(String theMa, int quantity) {
        this.theMa = theMa;
        this.quantity = quantity;
        this.likeSoup = true;
    }

    public WuMingFen(String theMa, int quantity, boolean likeSoup) {
        this.theMa = theMa;
        this.quantity = quantity;
        this.likeSoup = likeSoup;

    }

    public void check() {
        System.out.print(this.theMa + " " + this.quantity + "两 ");
        if (this.likeSoup) {
            System.out.println("带汤");
        } else {
            System.out.println("不带汤");
        }
    }
}

class BankAccount {
    // 设计一个BankAccount类,实现银行某账号的资金往来账目管理,包括建账号、存入、取出等。
    //BankAccount类包括,账号(BankAccountId)、开户日期Date(日期),Rest(余额)。
    //另有一个构造函数和三个成员函数Bankin()(处理存入账),Bankout()处理取出账)和和一个负责生成账号的自动增长的函数。
    private static int BankAccountId = 0;
    private Date Date;
    private double Rest;

    public BankAccount() {
        add();
    }

    public BankAccount(Date Date, double Rest) {
        this.Date = Date;
        this.Rest = Rest;
        add();
    }

    public double Bankin(double cash) {
        this.Rest += cash;
        return this.Rest;
    }


    public double Bankout(double withdraw) {
        this.Rest -= withdraw;
        return this.Rest;
    }

    public void add() {
        BankAccountId++;
    }

    @Override
    public String toString() {
        return "BankAccount{" +
                "BankAccountId=" + BankAccountId +
                ", Date=" + this.Date +
                ", Rest=" + this.Rest +
                '}';
    }
}

class Student {
    private String name;
    private int id;

    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 getSc() {
        return sc;
    }

    public void setSc(int sc) {
        this.sc = sc;
    }

    private int sc;

    public Student() {
    }

    public Student(String name, int id, int sc) {
        this.name = name;
        this.id = id;
        this.sc = sc;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", sc=" + sc +
                '}';
    }


}

// 9、编写一个程序,输入N个学生数据,包括学号、姓名、成绩,要求只输出成绩在80~89分的学生数据。
// 10、编写一个程序,输入N个学生数据,包括学号、姓名、成绩,求学生的成绩总和、平均值、最大值、最小值。

class Stud {
    int no;
    String name;
    double deg;


    public Stud(int no, String name, double deg) {
        this.no = no;
        this.name = name;
        this.deg = deg;
        disp();

    }

    public void disp() {
        if (this.deg >= 80 && this.deg <= 89) {
            System.out.println("学号:" + this.no + "  姓名:" + this.name + "  成绩:" + this.deg);
        }
    }
}

class Studented {
    int no;
    String name;
    double deg;
    static double sum = 0;
    static int num = 0;

    public Studented(int no, String name, double deg) {
        this.no = no;
        this.name = name;
        this.deg = deg;
        sum += deg;
        num++;
    }

    public static void num() {
        System.out.println("平均分:" + sum / num);
        System.out.println("总分:" + sum);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PerCheung

觉得有帮助的话就打赏支持一下吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值