Java笔记012-面向对象编程(基础部分)章节练习

目录

面向对象编程(基础部分)章节练习

1、编写类A01,定义方法max,实现求某个double数组的最大值,并返回

2、编写类A02,定义方法find,实现查找某字符串是否在数组中,并返回索引,如果找不到,返回-1

3、编写类Book,定义方法updatePrice,实现更改某本书的价格,具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变

4、编写类A03,实现数组的复制功能copyArr,输入旧数组,返回一个新数组元素和旧数组一样

5、定义一个圆类Circle,定义属性:半径,提供显示圆周长功能的方法,提供显示圆面积的方法

6、编程创建一个Cale计算类,在其中定义2个变量表示两个操作数,定义四个方法实现求和、差、乘、商(要求除数为0的话,要提示)并创建两个对象,分别测试

7、设计一个Dog类,有名字、颜色和年龄属性,定义输出方法show()显示其信息。并创建对象,进行测试、[提示this.属性]

8、给定一个Java程序的代码如下所示,则编译运行后,输出结果是

9、定义Music类,里面有音乐名name、音乐时长times属性,并有播放play功能和返回本身属性信息的功能方法getInfo

10、试写出以下代码的运行结果

11、在测试方法中,调用method方法,代码如下,编译正确,试写出method方法的定义形式,调用语句为:System.out.println(method(method(10.0,20.0),100));

12、创建一个Employee类,属性有(名字,性别,年龄,职位,薪水)

13、将对象作为参数传递给方法

14、拓展题-猜拳游戏


面向对象编程(基础部分)章节练习

1、编写类A01,定义方法max,实现求某个double数组的最大值,并返回

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
        A01 a01 = new A01();
        double[] arr = {1.0, 4.7, 1.8};
//        double[] arr = {};
        Double res = a01.max(arr);
        if (res != null) {
            System.out.println("arr的最大值=" + res);
        } else {
            System.out.println("arr的输入有误");
        }
    }
}

class A01 {
    public Double max(double[] arr) {
        if (arr != null && arr.length > 0) {
            double max = arr[0];//假定第一个元素就是最大值
            for (int i = 0; i < arr.length; i++) {
                if (max < arr[i]) {
                    max = arr[i];
                }
            }
            return max;
        } else {
            return null;
        }
    }
}

运行结果

2、编写类A02,定义方法find,实现查找某字符串是否在数组中,并返回索引,如果找不到,返回-1

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
        String[] strs = {"jack", "tom", "mary", "milan"};
        A02 a02 = new A02();
        int index = a02.find("tom", strs);
        System.out.println("查找的index=" + index);
    }
}

//编写类A02,定义方法find,实现查找某字符串是否在数组中,
// 并返回索引,如果找不到,返回-1
//分析
//1.类名 A02
//2.方法名 find
//3.返回值 int
//4.形参 (String,String[])
class A02 {
    public int find(String findStr, String[] strs) {
        //直接遍历字符串数组,如果找到,则返回索引
        for (int i = 0; i < strs.length; i++) {
            if (findStr.equals(strs[i])) {
                return 1;
            }
        }
        //如果没有,就返回-1
        return -1;
    }
}

运行结果

3、编写类Book,定义方法updatePrice,实现更改某本书的价格,具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
        Book book = new Book("笑傲江湖", 300);
        book.info();
        book.updatePrice();
        book.info();
    }
}

//编写类Book,定义方法updatePrice,实现更改某本书的价格,
// 具体:如果价格>150,则更改为150,
//      如果价格>100,更改为100,否则不变
//分析
//1.类名 Book
//2.属性 price,name
//3.方法名 updatePrice
//4.形参 ()
//5.返回值 void
//6.提供一个构造器
class Book {
    String name;
    double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public void updatePrice() {
        //如果方法中,没有price局部变量,this.price等价price
        if (this.price > 150) {
            this.price = 150;
        } else if (this.price > 100) {
            this.price = 100;
        }
    }

    //显示书籍的情况
    public void info() {
        System.out.println("书名=" + this.name + " 价格=" + this.price);
    }
}

运行结果

4、编写类A03,实现数组的复制功能copyArr,输入旧数组,返回一个新数组元素和旧数组一样

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
        int[] oldArr = {10, 20, 30};
        A03 a03 = new A03();
        int[] newArr = a03.copyArr(oldArr);
        //遍历newArr,验证
        System.out.println("===返回的newArr元素情况===");
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(newArr[i] + "\t");
        }
    }
}

//编写类A03,实现数组的复制功能copyArr,
//输入旧数组,返回一个新数组元素和旧数组一样

class A03 {
    public int[] copyArr(int[] oldArr) {
        //在堆中,创建一个长度为oldArr.length数组
        int[] newArr = new int[oldArr.length];
        //遍历oldArr,将元素拷贝到newArr
        for (int i = 0; i < oldArr.length; i++) {
            newArr[i] = oldArr[i];
        }
        return newArr;
    }
}

运行结果

5、定义一个圆类Circle,定义属性:半径,提供显示圆周长功能的方法,提供显示圆面积的方法

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
        Circle circle;
        circle = new Circle(3);
        System.out.println("面积=" + circle.area());
        System.out.println("周长=" + circle.len());

    }
}

//定义一个圆类Circle,定义属性:半径,
//提供显示圆周长功能的方法
//提供显示圆面积的方法

class Circle {
    double radius;//radius半径

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

    public double area() {//面积
        return Math.PI * this.radius * this.radius;
    }

    public double len() {//周长
        return 2 * Math.PI * this.radius;
    }

}

运行结果

6、编程创建一个Cale计算类,在其中定义2个变量表示两个操作数,定义四个方法实现求和、差、乘、商(要求除数为0的话,要提示)并创建两个对象,分别测试

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
        Cale cale = new Cale(2, 0);
        System.out.println("和=" + cale.Sum());
        System.out.println("差=" + cale.Minus());
        System.out.println("乘=" + cale.Multiply());
        Double divRes = cale.Division();
        if (divRes != null) {
            System.out.println("除=" + cale.Division());
        }
    }
}

//编程创建一个Cale计算类,在其中定义2个变量表示两个操作数,
//定义四个方法实现求和、差、乘、商(要求除数为0的话,要提示)并创建两个对象,分别测试

class Cale {
    double num1;
    double num2;

    public Cale(double num1, double num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    //和
    public double Sum() {
        return this.num1 + this.num2;
    }

    //差
    public double Minus() {
        return this.num1 - this.num2;
    }

    //乘积
    public double Multiply() {
        return this.num1 * this.num2;
    }

    //除法
    public Double Division() {
        if (this.num2 != 0) {
            return this.num1 / this.num2;
        } else {
            System.out.println("除数不能为0");
            return null;
        }
    }
}

运行结果

7、设计一个Dog类,有名字、颜色和年龄属性,定义输出方法show()显示其信息。并创建对象,进行测试、[提示this.属性]

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
        Dog dog = new Dog("tom", "白色", 9);
        dog.show();
    }
}

class Dog {
    String name;
    String color;
    int age;

    public Dog(String name, String color, int age) {
        this.name = name;
        this.color = color;
        this.age = age;
    }

    public void show() {
        System.out.println(this.name);
        System.out.println(this.color);
        System.out.println(this.age);
    }
}

运行结果

8、给定一个Java程序的代码如下所示,则编译运行后,输出结果是

public class Test {//共有类
    int count = 9;//属性

    //这是Test类的main方法,任何一个类,都有main
    public static void main(String[] args) {
        //解读
        //1.new Test() 是匿名对象,匿名对象使用后,就不能使用
        //2.new Test().count1() 创建好匿名对象后,就调用count1()
        new Test().count1();
        Test t1 = new Test();
        t1.count2();
        t1.count2();
    }

    public void count1() {//Test类的成员方法
        count = 10;//这个count就是属性 改成10
        System.out.println("count1=" + count);
    }

    public void count2() {//Test类的成员方法
        System.out.println("count1=" + count++);
    }
}

运行结果

9、定义Music类,里面有音乐名name、音乐时长times属性,并有播放play功能和返回本身属性信息的功能方法getInfo

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
        Music music = new Music("醉赤壁", 300);
        music.play();
        System.out.println(music.getInfo());
    }
}

//定义Music类,里面有音乐名name、音乐时长times属性,
// 并有播放play功能和返回本身属性信息的功能方法getInfo
class Music {
    String name;
    int times;

    public Music(String name, int times) {
        this.name = name;
        this.times = times;
    }

    //播放play功能
    public void play() {
        System.out.println("音乐 " + name + " 正在播放中...时长为" + times + "秒");
    }

    //返回本身属性信息的功能方法getInfo
    public String getInfo() {
        return "音乐 " + name + " 播放时间为" + times + "秒";
    }
}

运行结果

10、试写出以下代码的运行结果

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
        Demo d1 = new Demo();
        Demo d2 = d1;
        d2.m();
        System.out.println(d1.i);
        System.out.println(d2.i);
    }
}

class Demo {
    int i = 100;

    public void m() {
        int j = i++;
        System.out.println("i=" + i);
        System.out.println("j=" + j);
    }
}

运行结果

11、在测试方法中,调用method方法,代码如下,编译正确,试写出method方法的定义形式,调用语句为:System.out.println(method(method(10.0,20.0),100));

    public double method(double num1, double num2) {
        ...
    }

12、创建一个Employee类,属性有(名字,性别,年龄,职位,薪水)

提供3个构造方法,可以初始化

(1)(名字,性别,年龄,职位,薪水)

(2)(名字,性别,年龄)

(3)(职位,薪水),要求充分复用构造器

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
    }
}

//创建一个Employee类,属性有(名字,性别,年龄,职位,薪水)
//提供3个构造方法,可以初始化
//(1)(名字,性别,年龄,职位,薪水)
//(2)(名字,性别,年龄)
//(3)(职位,薪水),要求充分复用构造器
class Employee {
    //名字,性别,年龄,职位,薪水
    String name;
    char gender;
    int age;
    String job;
    double sal;

    //因为要求可以复用构造器,因此先写属性少的构造器
    //职位,薪水
    public Employee(String job, double sal) {
        this.job = job;
        this.sal = sal;
    }

    //名字,性别,年龄
    public Employee(String name, char gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    //名字,性别,年龄,职位,薪水
    public Employee(String name, char gender, int age, String job, double sal) {
        this(name, gender, age);//使用到 前面的 构造器
        this.job = job;
        this.sal = sal;
    }
}

13、将对象作为参数传递给方法

题目要求

(1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,findArea()方法返回圆的面积

(2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下:
public void printAreas(Circle c, int times)//方法签名

(3)在printAreas方法中打印输出1到times之间的每个整数半径值,以及对应的面积。
例如,times为5,则输出半径1,2,3,4,5,以及对应的圆面积

(4)在main方法中调用printAreas()方法,调用完毕后输出当前半径值。

public class Exercise {
    //编写一个main方法
    public static void main(String[] args) {
        Circle circle = new Circle();
        PassObject passObject = new PassObject();
        passObject.printAreas(circle, 5);
    }
}

//题目要求
//(1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,findArea()方法返回圆的面积
//(2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下:
//public void printAreas(Circle c, int times)//方法签名
//(3)在printAreas方法中打印输出1到times之间的每个整数半径值,以及对应的面积。
//例如,times为5,则输出半径1,2,3,4,5,以及对应的圆面积
//(4)在main方法中调用printAreas()方法,调用完毕后输出当前半径值。
class Circle {//类
    double radius;//半径

    public Circle() {//无参构造器

    }

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

    public double findArea() {//返回面积
        return Math.PI * radius * radius;
    }

    //添加方法setRadius,修改对象的半径值
    public void SetRadius(double radius) {
        this.radius = radius;
    }
}

class PassObject {
    public void printAreas(Circle c, int times) {
        System.out.println("radius\tarea");
        for (int i = 1; i <= times; i++) {//打印输出1到times之间的每个整数半径值
            c.SetRadius(i);//修改c对象的半径值
            System.out.println((double) i + "  \t" + c.findArea());
        }
    }
}

运行结果

14、拓展题-猜拳游戏

有个人Tom设计他的成员变量。成员方法,可以电脑猜拳
电脑每次都会随机生成0,1,2
0表示石头
1表示剪刀
2表示布
并要可以显示Tom的输赢次数(清单)

import java.util.Random;
import java.util.Scanner;

public class MoraGame {
    //编写一个main方法
    public static void main(String[] args) {
        //创建一个玩家对象
        Tom t = new Tom();
        //用来记录最后输赢的次数
        int isWinCount = 0;
        //创建一个二维数组,用来接收数据,Tom出拳情况以及电脑出拳情况
        int[][] arr1 = new int[3][3];
        int j = 0;
        //创建一个一维数组,用来接收输赢情况
        String[] arr2 = new String[3];

        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {//比赛3次
            //获取玩家出的拳
            System.out.println("请输入你要出的拳(0-拳头 1-剪刀 2-布):");
            int num = scanner.nextInt();
            t.setTomGuessNum(num);
            int tomGuess = t.getTomGuessNum();
            arr1[i][j + 1] = tomGuess;

            //获取电脑出的拳
            int comGuess = t.computerNum();
            arr1[i][j + 2] = comGuess;

            //将玩家猜的拳与电脑的作比较
            String isWin = t.vsComputer();
            arr2[i] = isWin;
            arr1[i][j] = t.count;

            //对每一局的情况进行输出
            System.out.println("============================================");
            System.out.println("局数\t\t玩家出的拳\t电脑出的拳\t输赢情况");
            System.out.println(t.count + "\t\t" + tomGuess + "\t\t\t" + comGuess + "\t\t\t" + isWin);
            System.out.println("============================================");
            System.out.println();
            isWinCount = t.winCount(isWin);
        }

        //对游戏的最终结果进行输出
        System.out.println("局数\t\t玩家出的拳\t电脑出的拳\t\t输赢情况");
        for (int a = 0; a < arr1.length; a++) {
            for (int b = 0; b < arr1[a].length; b++) {
                System.out.print(arr1[a][b] + "\t\t\t");
            }
            System.out.print(arr2[a]);
            System.out.println();
        }
        System.out.println("你赢了" + isWinCount + "次");
    }
}

//有个人Tom设计他的成员变量。成员方法,可以电脑猜拳
//电脑每次都会随机生成0,1,2
//0表示石头
//1表示剪刀
//2表示布
//并要可以显示Tom的输赢次数(清单)
class Tom {//核心代码
    //玩家出拳的类型
    int tomGuessNum;//0,1,2
    //电脑出拳的类型
    int comGuessNum;//0,1,2
    //玩家赢的次数
    int winCountNum;//一共比赛3次
    //比赛次数
    int count = 1;

    //电脑随机生成猜拳的数字的方法
    public int computerNum() {
        Random r = new Random();
        comGuessNum = r.nextInt(3);//方法 返回0-2的随机数
        return comGuessNum;
    }

    public int getTomGuessNum() {
        return tomGuessNum;
    }

    //设置玩家猜拳的数字的方法
    public void setTomGuessNum(int tomGuessNum) {
        if (tomGuessNum > 2 || tomGuessNum < 0) {
            throw new IllegalArgumentException("数字输入错误");
        }
        this.tomGuessNum = tomGuessNum;
    }

    //比较猜拳结果
    //玩家赢返回true,否则返回false
    public String vsComputer() {
        if (tomGuessNum == 0 && comGuessNum == 1) {
            return "你赢了";
        } else if (tomGuessNum == 1 && comGuessNum == 2) {
            return "你赢了";
        } else if (tomGuessNum == 2 && comGuessNum == 0) {
            return "你赢了";
        } else if (tomGuessNum == comGuessNum) {
            return "平手";
        } else {
            return "你输了";
        }
    }

    //记录玩家赢的次数
    public int winCount(String s) {
        count++;//控制玩的次数
        if (s.equals("你赢了")) {
            winCountNum++;
        }
        return winCountNum;
    }
}

运行结果

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

甲柒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值