题目汇总二

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());
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/767764.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

AI人才争夺战:巨头眼中的产品经理必备技能

前言 在人工智能的浪潮下&#xff0c;BAT等一线互联网企业纷纷加码布局&#xff0c;对AI领域的人才需求空前高涨。然而&#xff0c;要在众多求职者中脱颖而出&#xff0c;成为企业眼中的人才&#xff0c;不仅需要深厚的产品功底&#xff0c;更要具备对AI的深刻理解和应用能力。…

JAVASE进阶day03(lamda表达式 ,内部类)

内部类 1.内部类的基本使用 package com.lu.day03;public class Student {private int b 12;public class A{private int b 11;public void show(){int b 10;System.out.println("我是A");System.out.println(b);System.out.println(this.b);System.out.println(…

开源即正义,3D软件Blender设计指南

在当今数字化时代&#xff0c;开源软件的崛起不仅代表着技术的发展&#xff0c;更象征着一种信息自由和技术民主的理念。其本质是集众人之智&#xff0c;共同去完善一个软件&#xff0c;最终使双方互惠共赢。具体来说&#xff0c;开源的价值&#xff0c;在于打破资源垄断&#…

PIN对PIN替代T J A 1 0 2 8,LIN芯片

内部集成高压 LDO&#xff08;耐压 40V&#xff09; 稳压源&#xff0c;可为外部 ECU微控制器或相关外设提供稳定的 5V/3.3V 电源&#xff0c;输出电流可达70mA&#xff1b; 可在 5.5V ~ 28V 电压范围内工作&#xff0c;支持 12V 应用。在休眠模式下可实现极低电流消耗&#x…

Java集合整理笔记

目录 1.集合基础概念 1.1 集合 1.2 单例集合 1.2.1 List系列 1、ArrayList 2、LinkedList 3、Voctor​编辑 1.2.2 Set系列 1、HashSet 集合 2、LinkedHashSet 集合 3、TreeSet集合 1.3 双例集合 1.3.1 HashMap 1.3.2 LinkedHashMap 1.3.3 TreeMap 1.4 快速失败…

python pdfplumber优化表格提取

样例pdf 直接使用文本提取效果&#xff1a; 使用表格提取 根据提取的文本信息是没办法获取到表格数据的&#xff0c;太乱了。尤其是 3 4列。 解决&#xff1a; 自行画线&#xff0c;根据画线进行提取。 效果&#xff1a; 思路&#xff1a; 1.根据表头进行画竖线 2.根据行坐…

thinksboard 网络请求方式

html模块 deleteImage点击事件 <div class"tb-images tb-absolute-fill" [ngClass]"{tb-dialog-mode: dialogMode, mat-padding: !dialogMode}"><div fxFlex fxLayout"column" class"tb-images-content" [ngClass]"{t…

elementPlus自定义el-select下拉样式

如何在f12元素选择器上找到下拉div呢&#xff1f; 给el-select添加 :popper-append-to-body"false" 即可&#xff0c;这样就可以将下拉框添加到body元素中去&#xff0c;否则当我们失去焦点&#xff0c;下拉就消失了&#xff0c;在元素中找不到el-select。剩下就可以…

Amos结构方程模型---探索性分析

初级 第5讲 探索性分析_哔哩哔哩_bilibili amos中基本操作&#xff1a; 椭圆潜变量&#xff0c;不可预测 数据导入 改变形状 判定系数 方差估计和假设检验&#xff1a; 探索性分析&#xff1a; ses&#xff08;潜变量&#xff09;社会经济指数 从考虑最大的MI开始&#xff0c;卡…

模拟 ADC 的前端

ADC 的 SPICE 模拟 反复试验的方法将信号发送到 ADC 非常耗时&#xff0c;而且可能有效也可能无效。如果转换器捕获电压信息的关键时刻模拟输入引脚不稳定&#xff0c;则无法获得正确的输出数据。SPICE 模型允许您执行的步是验证所有模拟输入是否稳定&#xff0c;以便没有错误…

全网最详细金融APP测试功能点-测试用例,详细整理(全)

2024软件测试面试刷题&#xff0c;这个小程序&#xff08;永久刷题&#xff09;&#xff0c;靠它快速找到工作了&#xff01;&#xff08;刷题APP的天花板&#xff09;-CSDN博客跳槽涨薪的朋友们有福了&#xff0c;今天给大家推荐一个软件测试面试的刷题小程序。https://blog.c…

mov文件怎么转换成mp4格式?这四种转换方法超级好用!

mov文件怎么转换成mp4格式&#xff1f;在数字娱乐的世界中&#xff0c;你是否曾遇到过MOV格式的视频&#xff1f;也许&#xff0c;对于许多人来说&#xff0c;这并不是一个常见的格式&#xff0c;但这并非偶然&#xff0c;首先&#xff0c;我们来谈谈MOV的兼容性问题&#xff0…

「漏洞复现」时空智友ERP系统updater.uploadStudioFile 任意文件上传漏洞

0x01 免责声明 请勿利用文章内的相关技术从事非法测试&#xff0c;由于传播、利用此文所提供的信息而造成的任何直接或者间接的后果及损失&#xff0c;均由使用者本人负责&#xff0c;作者不为此承担任何责任。工具来自网络&#xff0c;安全性自测&#xff0c;如有侵权请联系删…

Python处理异常用操作介绍

Python中的异常处理主要用于捕获和处理程序运行过程中出现的错误。 在编写Python程序时&#xff0c;我们经常会遇到各种错误&#xff0c;如语法错误、运行时错误等。为了确保程序的稳定性和健壮性&#xff0c;我们需要对可能出现的错误进行捕获和处理。本文将介绍Python中常用的…

Python入门 2024/7/3

目录 for循环的基础语法 遍历字符串 练习&#xff1a;数一数有几个a range语句 三个语法 语法1 语法2 语法3 练习&#xff1a;有几个偶数 变量作用域 for循环的嵌套使用 打印九九乘法表 发工资案例 continue和break语句 函数的基础定义语法 函数声明 函数调用 …

MLLM QLoRA微调实战:基于最新的袖珍Mini-InternVL模型

引言 大型语言模型&#xff08;LLM&#xff09;的世界正在不断发展&#xff0c;新的进步正在迅速出现。一个令人兴奋的领域是多模态LLM&#xff08;MLLMs&#xff09;的发展&#xff0c;这种模型既能够理解文本又能够理解图像&#xff0c;并与之进行交互。因此&#xff0c;这种…

ICCV2023鲁棒性相关论文速览

Paper1 Towards Better Robustness against Common Corruptions for Unsupervised Domain Adaptation 摘要原文: Recent studies have investigated how to achieve robustness for unsupervised domain adaptation (UDA). While most efforts focus on adversarial robustnes…

udp发送数据如果超过1个mtu时,抓包所遇到的问题记录说明

最近在测试Syslog udp发送相关功能&#xff0c;测试环境是centos udp头部的数据长度是2个字节&#xff0c;最大传输长度理论上是65535&#xff0c;除去头部这些字节&#xff0c;可以大概的说是64k。 写了一个超过64k的数据(随便用了一个7w字节的buffer)发送demo&#xff0c;打…

Geotools系列说明之LineString仿高德航路截取说明

需求分析 我们在做webgl的时候经常会遇到这样的需求&#xff0c;计算给定航路的拥堵情况&#xff0c;不同的拥堵显示不同的颜色&#xff0c;航路截取计算等等。基于这类问题统一都可以使用LineString进行处理 实现思路 如上图所示&#xff0c;航路是几个关键的点然后练成线&a…

MySql Innodb 索引有哪些与详解

概述 对于MYSQL的INNODB存储引擎的索引&#xff0c;大家是不陌生的&#xff0c;都能想到是 B树结构&#xff0c;可以加速SQL查询。但对于B树索引&#xff0c;它到底“长”得什么样子&#xff0c;它具体如何由一个个字节构成的&#xff0c;这些的基础知识鲜有人深究。本篇文章从…