• 【Java刷题系列】03构造类


    一、交通工具类

    要求:

    • 定义一个Vehicle类,其中有如下属性:

      • 速度
      • 体积
      • 方法移动
      • 设置速度
      • 加速
      • 减速
    • 在测试类中实例化一个交通工具对象,并通过方法给它初始化speed和size的值并打印,另外调用加速减速的方法对速度进行修改。

    public class Vehicle {
        private double speed;
        private double size;
        public void move(){
            System.out.println("开始运行!");
        }
        public void speedUp(double a){
                this.speed=this.speed+a;
        }
    
        public Vehicle(double speed, double size) {
            this.speed = speed;
            this.size = size;
        }
    
        public void speedDown(double a){
            this.speed=speed-a;
        }
    
        public double getSpeed() {
            return speed;
        }
    
        public void setSpeed(double speed) {
            this.speed = speed;
        }
    
        public Vehicle() {
        }
    
        public double getSize() {
            return size;
        }
    
        public void setSize(double size) {
            this.size = size;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: Lenovo
     * Date: 2022-06-30
     * Time: 10:21
     */
    public class Test02 {
        public static void main(String[] args) {
            //通过无参数构造方法创建对象
            Vehicle v=new Vehicle(120,5);
            v.setSize(5);
            v.setSpeed(120);
            System.out.println(v.getSpeed());
            System.out.println(v.getSize());
            //通过有参数的构造方法创建对象
            Vehicle v1=new Vehicle(130,4);
            //调用加速方法
            v.speedUp(20);
            System.out.println("加速后的speed:"+v.getSpeed());
            //调用减速方法
            v.speedDown(10);
            System.out.println("减速后的speed:"+v.getSpeed());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    二、实现简单的计算器

    编写一个程序,模拟简单的计算器

    • 定义名为Number的类,其中有两个整型数据成员n1和n2应该声明为私有。
    • 编写构造方法赋予n1和n2初值
    • 再为该类定义:加,减,乘,除运算
    • 在main方法当中创建Number类的对象调用各个方法并显示计算结果
    public class Number {
        private double n1;
        private double n2;
    
        public Number(double n1, double n2) {
            this.n1 = n1;
            this.n2 = n2;
        }
        public Number() {
        }
    
        public double getN1() {
            return n1;
        }
    
        public void setN1(double n1) {
            this.n1 = n1;
        }
    
        public double getN2() {
            return n2;
        }
    
        public void setN2(double n2) {
            this.n2 = n2;
        }
    
        public void addition(){
            double result=n1+n2;
            System.out.println(n1+" + "+n2+" = "+result);
        }
        public void subtraction(){
            double result=n1-n2;
            System.out.println(n1+" - "+n2+" = "+result);
        }
        public void multiplication(){
            double result=n1*n2;
            System.out.println(n1+" * "+n2+" = "+result);
        }
        public void division(){
            double result=n1/n2;
            System.out.println(n1+" / "+n2+" = "+result);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    public class Test01 {
        public static void main(String[] args) {
            Number n=new Number();
            n.setN2(20);
            n.setN1(110);
            n.addition();
            n.division();
            n.multiplication();
            n.subtraction();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    三、时间类

    • 定义名字为MyTime的类,其中应该有三个整型成员:时,分,秒
    • 为了保证数据的安全性,这三个成员应该定义为私有的
    • 为MyTime类定义构造方法以方便创建对象时初始化成员变量
    • 再定义display方法用于将时间信息打印出来

    这里只对秒的加法做了取模和取余,因为是数学的问题:

    public class MyTime {
        private int hour;
        private int minute;
        private int second;
    
        public int getHour() {
            return hour;
        }
    
        public void setHour(int hour) {
            this.hour = hour;
        }
    
        public int getMinute() {
            return minute;
        }
    
        public void setMinute(int minute) {
            this.minute = minute;
        }
    
        public int getSecond() {
            return second;
        }
    
        public void setSecond(int second) {
            this.second = second;
        }
    
        public MyTime() {
        }
    
        public MyTime(int hour, int minute, int second) {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }
    
        public void addSecond(int sec){
            int oldSec=this.getSecond();
            int newSec=oldSec+sec;
            if(newSec<60){
                this.setSecond(newSec);
            }else if(newSec==60){
                this.setSecond(0);
                this.addMinute(1);
            }else{
                this.setMinute(minute+newSec/60);
                this.setSecond(newSec%60);
            }
        }
        public void addMinute(int min){
            minute=minute+min;
        }
        public void addHour(int hou){
            hour=hour+hou;
        }
        public void subSecond(int sec){
            second=second-sec;
        }
        public void subMinute(int min){
            minute=minute-min;
        }
        public void subHour(int hou){
            hour=hour-hou;
        }
        public void display(){
            System.out.println("现在的时间是"+this.getHour()+"时"+this.getMinute()+"分"+this.getSecond()+"分");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    public class Test02 {
        public static void main(String[] args) {
            MyTime m=new MyTime();
            m.setHour(10);
            m.setMinute(20);
            m.setSecond(30);
            m.display();
            m.addHour(1);
            m.addMinute(2);
            m.addSecond(113);
            m.display();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

  • 相关阅读:
    excel打开只显示菜单栏内容却不显示改如何处理
    Git(9)——Git多人协同开发之创建初始项目
    ​探秘 Web 水印技术
    经济数据预测 | Python实现ARIMA时间序列金融市场预测
    Daniel Gross:硅谷的创投天才,能否成为下一个 Sam Altman?
    YoloV7实战:手把手教你使用Yolov7进行物体检测(附数据集)
    计算机网络【CN】介质访问控制
    数据结构与算法_AVL平衡二叉树_四种旋转,插入和删除
    服务间歇性停顿问题优化|得物技术
    python 蓝桥杯之并查集
  • 原文地址:https://blog.csdn.net/wxfighting/article/details/125535775