Java从入门到精通章节练习题——第六章

Exercise 1 简易计算器

package chapter6;

public class Exercise1 {
    public static double res = 0;

    //完成两个数加法运算的静态方法
    static public void add(double num1, double num2) {
        res = num1 + num2;
        System.out.println(num1 + "加上" + num2 + "的结果为" + res);
    }

    //完成两个数减法运算的静态方法
    static public void sub(double num1, double num2) {
        res = num1 - num2;
        System.out.println(num1 + "减去" + num2 + "的结果为" + res);
    }

    //完成两个数乘法运算的静态方法
    static public void mul(double num1, double num2) {
        res = num1 * num2;
        System.out.println(num1 + "c乘以" + num2 + "的结果为" + res);
    }

    //完成两个数除法运算的静态方法
    static public void div(double num1, double num2) {
        res = num1 / num2;
        System.out.println(num1 + "除以" + num2 + "的结果为" + res);
    }

    public static void main(String[] args) {
        add(4.4, 7.11);
        sub(8.9, 2.28);
        mul(5.2, 13.14);
        div(92, 89);
    }
}

Exercise 2 购买电影票

package chapter6;

/**
 * 购买电影票
 */
public class Exercise2 {

    public static void main(String[] args) {
        Info list1 = new Info("李明", 20);
        Info list2 = new Info("钱丽", 16);
        Info list3 = new Info("周刚", 8);
        Info list4 = new Info("吴红", 32);
        Info[] oneList = new Info[4];
        oneList[0] = list1;
        oneList[1] = list2;
        oneList[2] = list3;
        oneList[3] = list4;

        //输出显示
        System.out.println("姓名\t" + "年龄\t" + "票价(元)");
        System.out.println("——————————————————————————————————");
        for (int i = 0; i < 4; i++) {
            System.out.println(oneList[i]);//重写了toString方法,输出对象时输出信息
        }
    }

}

class Info {

    private int age;//年龄
    private double price;//票价
    private String name;//姓名

    public Info() {
    }

    //构造方法中判断得出并设置价格
    public Info(String name, int age) {
        this.age = age;
        this.name = name;
        if (age >= 18)
            setPrice(40);
        else
            setPrice(20);
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name + "\t" + age + "\t\t" + price;
    }
}

Exercise 3 计算平均分

package chapter6;

/**
 * 计算平均分
 */
public class Exercise3 {
    private int num;
    private String name;
    private double chinese;
    private double math;
    private double english;
    private double avg;

    public Exercise3(int num, String name, double chinese, double math, double english) {
        this.num = num;
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    public int getNum() {
        return num;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getChinese() {
        return chinese;
    }

    public void setChinese(double chinese) {
        this.chinese = chinese;
    }

    public double getMath() {
        return math;
    }

    public void setMath(double math) {
        this.math = math;
    }

    public double getEnglish() {
        return english;
    }

    public void setEnglish(double english) {
        this.english = english;
    }

    public double getAvg() {
        return avg;
    }

    public void setAvg(double avg) {
        this.avg = avg;
    }

    @Override
    public String toString() {
        return num + "\t\t" + name + '\t' + chinese +
                "\t" + math +
                "\t" + english +
                "\t" + avg
                ;
    }
}

class ShowInfo {
    public static void main(String[] args) {

        //保存学生成绩
        Exercise3 stu1 = new Exercise3(1, "张三", 91.5, 98, 89);
        stu1.setAvg((stu1.getChinese() + stu1.getEnglish() + stu1.getMath())/3);
        Exercise3 stu2 = new Exercise3(2, "李四", 96, 98.5, 93);
        stu2.setAvg((stu2.getChinese() + stu2.getEnglish() + stu2.getMath())/3);
        Exercise3 stu3 = new Exercise3(3, "王五", 97, 100, 98.5);
        stu3.setAvg((stu3.getChinese() + stu3.getEnglish() + stu3.getMath())/3);
        Exercise3 stu4 = new Exercise3(4, "钱六", 77, 83, 81);
        stu4.setAvg((stu4.getChinese() + stu4.getEnglish() + stu4.getMath())/3);
        System.out.println("学号" + "\t" + "姓名" + "\t" + "语文" + "\t" + "数学" + "\t" + "英语" + "\t" + "平均分");
        System.out.println("—————————————————————————————————————————————————————————————————");
        System.out.println(stu1);
        System.out.println(stu2);
        System.out.println(stu3);
        System.out.println(stu4);
    }

}

Exercise 4 厘米与英寸互转

package chapter6;

import java.util.Scanner;

/**
 * 厘米与英寸互转
 */
public class Exercise4 {
    private double cm;
    private double in;
    //1 英寸 = 2.54 厘米

    public static void main(String[] args) {
        boolean loop = true;
        double cm;//厘米
        double in;//英寸
        do {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入选项(1.厘米->英寸 2.英寸->厘米 0.退出)");
            int key = scanner.nextInt();
            //选择转换方向
            switch (key) {
                case 1:
                    System.out.print("请输入厘米数:");
                    cm = scanner.nextDouble();
                    in = 2.54 * cm;
                    System.out.println(in);
                    break;
                case 2:
                    System.out.println("请输入英寸数:");
                    in = scanner.nextDouble();
                    cm = in / 2.54;
                    System.out.println(cm);
                    break;
                case 0:
                    loop = false;
            }
        } while (loop);
    }
}

Exercise 5 多种权限的工具

package chapter6;

/**
 * 多种权限的工具
 */
public class Exercise5 {
    public void getRandomNumber() {}//对外公开
    protected void setNumber() {}//对子类和同一个包中的类公开
    private void sort() {}//只有类本身可以访问,不对外公开
}

Exercise 6 计算矩形的面积

package chapter6;

/**
 * 计算矩形的面积
 */
public class Exercise6 {
    private double length;//长
    private double width;//宽

    //构造方法,初始化长和宽
    public Exercise6(double length, double width) {
        this.length = length;
        this.width = width;
    }
    
    //计算面积方法
    public double getArea() {
        return length * width;
    }

    public static void main(String[] args) {
        double area = new Exercise6(10, 5).getArea();
        System.out.println(area);
    }
}

Exercise 7 判断是否存在运行时参数

package chapter6;

/**
 * 判断是否存在运行时参数
 */
public class Exercise7 {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("无运行参数");
        } else {
            for (int i = 0; i < args.length; i++) {

                System.out.println(args[i]);
            }
        }
        
    }
}

Exercise 8 单例模式

package chapter6;

/**
 * 单例模式
 */
public class Exercise8 {
    public static void main(String[] args) {
        GirlFriend instance = GirlFriend.getInstance();
        System.out.println(instance);
    }
}

class GirlFriend {

    private String name;
    //创建静态对象,保证只创建一次对象
    private static GirlFriend gf = new GirlFriend("小红");

    //将构造器私有化
    public GirlFriend(String name) {
        System.out.println("有参构造方法被调用");
        this.name = name;
    }

    //得到创建的对象
    public static GirlFriend getInstance() {
        return gf;
    }

    @Override
    public String toString() {
        return "GirlFriend{" +
                "name='" + name + '\'' +
                '}';
    }
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

earlytrain9653

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值