【Java】学习日记 Day20

【Java】学习日记 Day20_第1张图片

作者|Rickyの水果摊

时间|2022年8月3日


今日知识点总结

Java 面向对象(基础篇)编程练习

1. 求数组最大值

题目描述

编写类 Sort,定义方法 getMax,实现求 double 数组 arr 的最大值(数组元素个数 > 0)并且输出。

源代码

public class Homework01 {
    public static void main(String[] args) {
        double[] arr = { 1.0, 2.34, 9.89, 3.23, 4.56, 7.89 };
        Sort s = new Sort();
        s.getMax(arr);
    }
}

class Sort {
    public void getMax(double[] arr) {
        double max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
        }
        System.out.println("max = " + max);
    }
}

2. 查找字符串

题目描述

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

源代码

public class Homework02 {
    public static void main(String[] args) {
        String s = "Ricky";
        String[] arr = {"Sam","Jack","Peter","Ricky","Victor"};
        Search search = new Search();
      
        int res = search.find(s, arr);  //调用 find 方法
        if(res != -1){
            System.out.println(res);
        }else{
            System.out.println("404 Not Found");
        }

    }
}

class Search {
    public int find(String s, String[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(s) == true)
                return i;
        }
        return -1;  //执行到该行,说明循环未查找到符合要求元素,返回 -1
    }
}

3. 构造器的复用

题目描述

编写类 Employee,属性有(名字,性别,年龄,职位,薪水),提供 3 个构造方法,可以初始化

(名字,性别,年龄,职位,薪水)、(名字,性别,年龄)、(职位,薪水),要求充分复用构造器

源代码

public class Homework04 {
    public static void main(String[] args) {
        Employee employee1 = new Employee("Sam", "male", 20, "engineer", 2000.0);
        employee1.info();
    }
}

class Employee {
    public String name;
    public String gender;
    public int age;
    public String job;
    public double sal;

    // 题干中要求使用“构造器的复用”,因此先写属性少的构造器
    public Employee(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public Employee(String job, double sal) {
        this.job = job;
        this.sal = sal;
    }
  
    public Employee(String name, String gender, int age, String job, double sal) {
        this(name, gender, age); // 利用this实现构造器复用
        this.job = job;
        this.sal = sal;
    }
  
    public void info() {
        System.out.println(this.name + "\t" + this.gender + "\t" + this.age + "\t" + this.job + "\t" + this.sal);
    }
}

4. 将对象作为参数传递给方法

题目描述

(1)编写类 Circle,包含 double 类型的 radius 属性代表圆的半径,findArea 方法返回圆的面积。
(2)编写类 PassObject,包含方法 printAreas,定义如下: public void printAreas(Circle c, int times)。功能是输出半径为 1 到 times 的圆的面积。
(3)在 main 方法中调用 printAreas 方法。

源代码

public class Homework05 {
    public static void main(String[] args) {
        Circle c = new Circle(); //创建Circle对象
        int times = 5;           //定义半径范围
        PassObject passObject = new PassObject(); 
        passObject.printAreas(c, times); //调用打印方法,传入参数
    }
}

class Circle {   //定义Circle类
    double radius;

    public double findArea() {
        return Math.PI * this.radius * this.radius;
    }
  
		//设置一个setter,修改半径值
    public void setRadius(double radius) {
        this.radius = radius;
    }
}


class PassObject {  //定义PassObject类
    //定义打印方法
    public void printAreas(Circle c, int times) {
        System.out.println("radius\t" + "areas");
        for (int i = 1; i <= times; i++) {
            c.setRadius(i);
            System.out.println(c.radius + "\t" + c.findArea());
        }
    }
}

5. 石头剪刀布游戏

题目描述

编写类 Computer ,实现人与电脑的石头剪刀布游戏。

源代码

import java.util.Scanner;
public class Homework06 {
    public static void main(String[] args) {
        Computer Alpha007 = new Computer();  //创建人工智能对手Alpha007
        Alpha007.greeting(); //显示提示信息
        Alpha007.play();     //开始游戏
        Alpha007.info();     //打印游戏结果单
    }
}

class Computer {
    int comChoice; //电脑自动生成的选择
    int count = 0;
    int times = 0;
    int[] array = new int[100];

    public void greeting() {
        System.out.println("\n>>Hello Ricky");
        System.out.println(">>Let's play rock-paper-scissors");
        System.out.println(">>How many times you want play?");

        Scanner myScanner = new Scanner(System.in);
        this.times = myScanner.nextInt();
    }

    public void play() {
        //0 -> rock  1 -> paper  2 -> scissors
        int myChoice = 0;
        Scanner myScanner = new Scanner(System.in);
        for(int i = 0;i<this.times ;i++){
            System.out.println("\n>>Please enter your choice(0 -> rock,1 -> paper,2 -> scissors)");
            myChoice = myScanner.nextInt();
            System.out.println("--------result--------");

            //show my choice
            switch(myChoice){
                case 0:
                    System.out.println(">>In this turn,your choice is rock");
                    break;
                case 1:
                    System.out.println(">>In this turn,your choice is paper");
                    break;
                case 2:
                    System.out.println(">>In this turn,your choice is scissors");
                    break;
            }

            //show computer's choice
            this.comChoice = (int)(Math.random()*3);
            switch(comChoice){
                case 0:
                    System.out.println(">>In this turn,computer's choice is rock");
                    break;
                case 1:
                    System.out.println(">>In this turn,computer's choice is paper");
                    break;
                case 2:
                    System.out.println(">>In this turn,computer's choice is scissors");
                    break;
            }

            //compare these 2 choices
            switch(myChoice){
                case 0:
                    if(this.comChoice == 0){
                        array[this.count] = 0;
                        System.out.println(">>It's a draw play");
                    }else if(this.comChoice == 1) {
                        array[this.count] = -1;
                        System.out.println(">>You lost in this turn");
                    }else{
                        array[this.count] = 1;
                        System.out.println(">>You win in this turn");
                    }
                    break;
                case 1:
                    if(this.comChoice == 1){
                        array[this.count] = 0;
                        System.out.println(">>It's a draw play");
                    }else if(this.comChoice == 2) {
                        array[this.count] = -1;
                        System.out.println(">>You lost in this turn");
                    }else{
                        array[this.count] = 1;
                        System.out.println(">>You win in this turn");
                    }
                    break;
                case 2:
                    if(this.comChoice == 2){
                        array[this.count] = 0;
                        System.out.println(">>It's a draw play");
                    }else if(this.comChoice == 0) {
                        array[this.count] = -1;
                        System.out.println(">>You lost in this turn");
                    }else{
                        array[this.count] = 1;
                        System.out.println(">>You win in this turn");
                    }
                    break;
            }
            System.out.println("--------result--------");
            this.count++;
        }
    }
  
    public void info() {
        System.out.println(" ");
        System.out.println("--------info---------");
        System.out.println("Turn\t\tResult");

        for (int i = 0; i < this.count; i++) {
            if (this.array[i] == 1) {
                System.out.println(i + "\t\twin");
            } else if(this.array[i] == -1) {
                System.out.println(i + "\t\tlose");
            }else{
                System.out.println(i + "\t\tdraw");
            }
        }
        System.out.println("---------------------");
    }
}

✏️ 今日随记

学习 Java 的 第 20 天。⏰

今天主要整理了本章的编程习题。经过今天的编程练习后,对面向对象的基础编程有了更加深刻的了解,对相关知识点变得更加熟悉。

这篇博客本应在很久之前就发的,由于某些原因,一直拖到现在。

博主现在已经回到了学校,开始了数学建模的集训,估计未来这一个月会以数学建模的内容为学习重点,备战国赛。之后 Java 的学习时间也会因此有所压缩,但博主还是会尽可能分配好时间,多更新,多学习。

继续加油✊

今日摘录:

在所谓的人世间摸爬滚打至今,我唯一愿意视为真理的就只有一句话:一切都会过去的太宰治


相关博客

【Java】学习日记 Day19

【Java】学习日记 Day18

【Java】学习日记 Day17

你可能感兴趣的:(Java,java,学习日记)