第四章 类和对象 实践与练习(1)

综合练习 1 简易计算器

使用静态方法模拟一个只能进行两个数加减乘除的简易计算器。

java 复制代码
static double a,b;
    public static void main(String[] args) {
        简易计算器01 sum = new 简易计算器01();//创建一个对象
        System.out.println("4.4加上7.11的结果:"+sum.add(4.4,7.11));
        System.out.println("8.9减去2.28的结果:"+sum.red(8.9,2.28));
        System.out.println("5.2乘以13.14的结果:"+sum.mul(5.2,13.14));
        System.out.println("92除以89的结果"+sum.div(92,89));

    }
    public double add(double a, double b) {//加法
        this.a = a;
        this.b = b;
        return a + b;
    }
    public double red(double a, double b) {//减法
        this.a = a;
        this.b = b;
        return a - b;
    }
    public double mul(double a, double b) {//乘法
        this.a = a;
        this.b = b;
        return a * b;
    }
    public double div(double a, double b) {//除法
        this.a = a;
        this.b = b;
        return a / b;
    }

综合练习 2 购买电影票

购买电影票有优惠:满18岁的付40元,未满18岁的享受半价。使用成员变量,成员方法,构造方法和this关键字,控制台输出姓名,年龄,票价。

java 复制代码
String name;
    int money,age;
    public static void main(String[] args) {
        购买电影票02 cot1 = new 购买电影票02("李明",20);//创建对象
        购买电影票02 cot2 = new 购买电影票02("钱丽",16);
        购买电影票02 cot3 = new 购买电影票02("周刚",8);
        购买电影票02 cot4 = new 购买电影票02("吴红",32);
        System.out.println("姓名     年龄     票价(元)");
        System.out.println("------------------------");
        System.out.print(cot1.name+"     "+cot1.age+"      ");
        System.out.println(cot1.moneys());
        System.out.print(cot2.name+"     "+cot2.age+"      ");
        System.out.println(cot2.moneys());
        System.out.print(cot3.name+"      "+cot3.age+"      ");
        System.out.println(cot3.moneys());
        System.out.print(cot4.name+"     "+cot4.age+"      ");
        System.out.println(cot4.moneys());

    }
    public 购买电影票02(String name,int age){//构造方法
        this.name=name;
        this.age=age;
    }
    public int moneys(){//成员方法
        if(age<18){
            return 20;
        }
        else{
            return 40;
        }
    }

综合练习 3 计算平均分

使用成员变量,成员方法,构造方法和this关键字,先记录4名学生的语文,数学,英语3科成绩,在计算每个人的平均分。

java 复制代码
int num;
    String name;
    double C,M,E,sum,ave;
    public static void main(String[] args) {
        计算平均分03 stu1 = new 计算平均分03(1,"张三",91.5,98.0,89.0);//创建对象
        计算平均分03 stu2 = new 计算平均分03(1,"李四",96.0,98.5,93.0);
        计算平均分03 stu3 = new 计算平均分03(1,"王五",97.0,100.0,98.5);
        计算平均分03 stu4 = new 计算平均分03(1,"钱六",77.0,81.0,81.0);
        System.out.println("学号  姓名   语文    数学    英语    总分    平均分");
        System.out.println("---------------------------------------------------------");
        System.out.println(stu1.num+"    "+stu1.name+"   "+stu1.C+"   "+stu1.M+"   "+stu1.E+"   "+stu1.sum+"   "+stu1.ave);
        System.out.println(stu2.num+"    "+stu2.name+"   "+stu2.C+"   "+stu2.M+"   "+stu2.E+"   "+stu2.sum+"   "+stu2.ave);
        System.out.println(stu3.num+"    "+stu3.name+"   "+stu3.C+"  "+stu3.M+"   "+stu3.E+"   "+stu3.sum+"   "+stu3.ave);
        System.out.println(stu4.num+"    "+stu4.name+"   "+stu4.C+"   "+stu4.M+"   "+stu4.E+"   "+stu4.sum+"   "+stu4.ave);

    }
    public 计算平均分03(int num,String name,double C,double M,double E ){//有参构造方法
        this.num = num;
        this.name = name;
        this.C = C;
        this.M = M;
        this.E = E;
        sum = C+M+E;
        ave = sum/3;

    }

综合练习 4 厘米与英寸互换

编写工具类,提供厘米与英寸之间的相互交换的工具方法。

java 复制代码
double a ;
    public static void main(String[] args) {
        changeC(10);
        changeE(10);



    }
    public static void changeE(double a) {//厘米转变英寸的方法
        double b = a/2.54;
        System.out.println(a+"厘米转变为英寸为:"+b);
    }
    public static void changeC(double a) {//英寸转变厘米的方法
        double b = a*2.54;
        System.out.println(a+"英寸转变为厘米为:"+b);
    }
相关推荐
沐知全栈开发2 分钟前
API 类别 - 实用工具
开发语言
Cx330❀8 分钟前
Qt 入门指南:从零搭建开发环境到第一个图形界面程序
xml·大数据·开发语言·网络·c++·人工智能·qt
SilentSamsara9 分钟前
装饰器基础:从闭包到装饰器的自然演变
开发语言·前端·vscode·python·青少年编程·pycharm
搬砖的小码农_Sky16 分钟前
AI Agent:OpenClaw的算法架构
人工智能·算法·ai·架构·人机交互·agi
aXin_ya23 分钟前
微服务 第十天 (Redis多级缓存)
java·redis·微服务
热心网友俣先生37 分钟前
2026年金地杯A题解题思路
算法
逸Y 仙X38 分钟前
文章二十五:ElasticSearch 分页查询
java·大数据·数据库·elasticsearch·搜索引擎·全文检索
科研前沿38 分钟前
SpaceOS™空间计算底座与五大自研引擎,实现多项关键技术突破
大数据·运维·人工智能·算法·重构
今天长肉了吗40 分钟前
风控指标平台实战:大数据量下如何设计分批处理
开发语言·数据库·python
昵称小白44 分钟前
C++ 刷题语法速查
c++·算法