题目汇总二

Question One

面向对象基础,声明 Box 类并实现对应操作

public class Box{
    public int length;
    public int width;
    public int height;

    public void setBox(int length, int width, int height){
        this.length = length;
        this.width = width;
        this.height = height;
    }

    public int volume(){
        return length * width * height;
    }

    public static void main(String[] args) {
        Box b = new Box();
        b.setBox(10, 10, 10);
        System.out.println(b.volume());
    }
}

Question Two

定义一个银行帐户类BankAccount实现银行帐户的概念,在BankAccount类中定义两个变量:“帐号” (account_number) 和"存款余额" (leftmoney),再定义四个方法:“存款” (savemoney)、“取款” (getmoney) 、 “查询余额” (getleftmoney)、构造方法(BankAccount)。

最后,在main()方法中创建一个BankAccount类的对象ba,假设ba的账号为:123456,初始的存款余额为500元。首先向该账户存入1000元,再取出2000元。

no code

Question Three

java 里面类似 toString() 的方法

public class Main {
    public static void main(String[] args) {
        System.out.println(String.valueOf(12));
        System.out.println(String.valueOf(13.1231321d));
        System.out.println(String.valueOf(123333333333333312L));
    }
}
return type : String
parameter : int or long or double
role : tarns num into string

Question Four

自定义抛出异常

class myCustomException extends Exception {
    public myCustomException(String message) {
        super(message);
    }
}

public class Main{
    public static void sort(){

    }
    public static void main(String[] args) {
        int[] nums = new int[100];
        for(int i = 0; i < 100; i ++){
            nums[i] = (int)(10000 * Math.random());
            try{
                if(nums[i] >= 5000){
                    System.out.print(nums[i] + " ");
                    throw new myCustomException("greater than or equal to 5000");
                }
            }
            catch (myCustomException e){
                System.out.println(e.toString());
            }
        }
    }
}

Question Five

GUI 实现二分猜数字游戏

import javax.swing.*;

public class Main{
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "Give you an integer between 1 and 100, try to guess it !");
        int targetNum = (int)(Math.random() * 100) + 1;

        int guessNum = 0;
        while(true) {
            guessNum = Integer.parseInt(JOptionPane.showInputDialog("please guess the number:"));
            if (guessNum == targetNum) {
                JOptionPane.showMessageDialog(null, "You guessed the right number " + guessNum + " !");
                break;
            }
            else if(guessNum < targetNum){
                JOptionPane.showMessageDialog(null, "You guessed the lesser number " + guessNum + " !");
            }
            else{
                JOptionPane.showMessageDialog(null, "You guessed the greater number " + guessNum + " !");
            }
        }
    }
}

Question Six

接口示例

 interface MyComputer{
    int compute(int n, int m);
 }

class Add implements MyComputer{
    public int compute(int n, int m){
        return n + m;
    }
}

class Sub implements MyComputer{ // 定义了一个类Sub,实现了MyComputer接口
    public int compute(int n, int m){ // 实现了MyComputer接口中的compute方法,用于进行减法计算
        return n - m; // 返回n和m的差
    }
}

class Mul implements MyComputer{ // 定义了一个类Mul,实现了MyComputer接口
    public int compute(int n, int m){ // 实现了MyComputer接口中的compute方法,用于进行乘法计算
        return n * m; // 返回n和m的积
    }
}

class Div implements MyComputer{ // 定义了一个类Div,实现了MyComputer接口
    public int compute(int n, int m){ // 实现了MyComputer接口中的compute方法,用于进行除法计算
        try{ // 尝试进行除法计算
            return n / m; // 返回n除以m的结果
        }
        catch (Exception e){ // 捕获除零异常
            System.out.println(e.getMessage()); // 输出异常信息
            return -1; // 返回-1表示出现异常
        }
    }
}

class UseComputer{ // 定义了一个类UseComputer
    public static void useCom(MyComputer com, int num1, int num2){ // 定义了一个静态方法useCom,接受一个MyComputer类型的对象和两个整数作为参数
        int result = com.compute(num1, num2); // 调用传入的MyComputer对象的compute方法进行计算
        System.out.println(result); // 输出计算结果
    }
}

public class Main{ // 定义了一个公共类KY7_1
    public static void main(String[] args){ // 定义了一个公共的静态方法main,程序的入口点
        UseComputer.useCom(new Add(), 12, 6); // 使用Add类进行加法计算
        UseComputer.useCom(new Sub(), 12, 6); // 使用Sub类进行减法计算
        UseComputer.useCom(new Mul(), 12, 6); // 使用Mul类进行乘法计算
        UseComputer.useCom(new Div(), 12, 6); // 使用Div类进行除法计算
        UseComputer.useCom(new Div(), 12, 0); // 使用Div类进行除法计算,除数为0
    }
}

Question Seven

类继承多接口

interface AddInterface {
    double add(double a, double b);
}

interface SubInterface {
    double sub(double a, double b);    
}

interface MulInterface{
    double mul(double a, double b);
}

interface DivInterface{
    double div(double a, double b);
}

class Calculator implements AddInterface, SubInterface, MulInterface, DivInterface{
    public double add(double a, double b) {
        return a + b;
    }
    public double sub(double a, double b) {
        return a - b;
    }
    public double mul(double a, double b) {
        return a * b;
    }
    public double div(double a, double b) {
        try {
            return a / b;
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            return -1;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();

        // 测试加法
        double sum = calculator.add(5.0, 3.0);
        System.out.println("加法结果: " + sum);

        // 测试减法
        double difference = calculator.sub(5.0, 0.0);
        System.out.println("减法结果: " + difference);

        double mul = calculator.mul(5.0, 3.0);
        System.out.println(mul);

        double div = calculator.div(5.0, 3.0);
        System.out.println(div);
        System.out.println(calculator.div(5.0, 0.0));

        System.out.println(-5/0.0);
    }
}

Question Eight

多个类实现一个接口

interface areaOrVolume {
    double size(); // 抽象方法,用于计算图形的面积或体积
}

class Rectangle implements areaOrVolume {
    private double width;
    private double height;

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

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

class Circle implements areaOrVolume {
    private double r; // 圆的半径

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

    public double size() {
        return Math.PI * r * r; // 计算圆的面积
    }
}

class Cylinder implements areaOrVolume {
    private double radius; // 圆柱体的底面半径
    private double height; // 圆柱体的高度

    public Cylinder(double radius, double height) {
        this.radius = radius;
        this.height = height;
    }

    public double size() {
        return Math.PI * radius * radius * height; // 计算圆柱体的体积
    }
}

public class Main {
    public static void main(String[] args) {
        // 创建Rectangle类的对象o1
        Rectangle o1 = new Rectangle(5, 3);
        // 创建Circle类的对象o2
        Circle o2 = new Circle(4);
        // 创建Cylinder类的对象o3
        Cylinder o3 = new Cylinder(3, 5);

        // 分别调用对象o1、o2和o3的size()方法,计算面积或体积
        System.out.println("矩形的面积: " + o1.size());
        System.out.println("圆的面积: " + o2.size());
        System.out.println("圆柱体的体积: " + o3.size());
    }
}

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值