栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java学习笔记11

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java学习笔记11

文章目录
  • java学习笔记11
  • 知识点
  • 任务内容
    • 课程1 练习创建对象
      • 任务1 Dog 类的 getter 和 setter
      • 任务2 三个“火枪手”
      • 任务3 猫类大屠杀
      • 任务4 人
      • 任务5 算术平均值
      • 任务6 Person 类的 getter 和 setter
    • 课程2 练习初始化对象
      • 任务1 创建 Friend 类
      • 任务2 初始化猫
      • 任务3 创建 Dog 类
      • 任务4 创建 Circle 类
      • 任务5 我们来组合一个矩形
      • 任务6 程序员创建人
      • 任务7 初始化对象
    • 课程3 练习使用构造方法
      • 任务1 你不能购买朋友
      • 任务2 创建猫
      • 任务3 犬只注册
      • 任务4 创建包含三个构造方法的 Circle 类:
      • 任务5 创建 Rectangle 类
      • 任务6 从构造方法调用构造方法
      • 任务7 最大数量的构造方法
      • 任务8 构造方法
      • 任务9 轮子的基础

java学习笔记11 知识点 任务内容 课程1 练习创建对象 任务1 Dog 类的 getter 和 setter

创建 Dog 类。Dog 类应该包含 String 变量 name 和 int 变量 age。

为 Dog 类的所有斜体样式变量创建 getter 和 setter。

package zh.codegym.task.task05.task0503;




public class Dog {
    String name;
    int age;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

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

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

    public static void main(String[] args) {

    }
}
任务2 三个“火枪手”

在 main 方法中,创建三个 Cat 对象并填充数据。

package zh.codegym.task.task05.task0504;




public class Solution {
    public static void main(String[] args) {
        Cat cat1 = new Cat("xuxumiao", 10, 10, 8);
        Cat cat2 = new Cat("xuxumi", 4, 7, 9);
        Cat cat3 = new Cat("xxm", 1, 4, 5);
    }

    public static class Cat {
        private String name;
        private int age;
        private int weight;
        private int strength;

        public Cat(String name, int age, int weight, int strength) {
            this.name = name;
            this.age = age;
            this.weight = weight;
            this.strength = strength;
        }
    }
}
任务3 猫类大屠杀

使用 Cat 类创建三只猫。

在猫之间举行三组两两对战。

你无需创建 Cat 类。对于战斗,请使用 boolean fight (Cat anotherCat) 方法。

在屏幕上显示每场战斗的结果,每行显示一个结果。

package zh.codegym.task.task05.task0505;



public class Solution {
    public static void main(String[] args) {
        Cat cat1 = new Cat("xuxumiao", 10, 10, 8);
        Cat cat2 = new Cat("xuxumi", 4, 7, 9);
        Cat cat3 = new Cat("xxm", 1, 4, 5);
        if (cat1.fight(cat2)) {
            System.out.println(cat1.name + "战胜了" + cat2.name);
        } else {
            System.out.println(cat1.name + "没打过" + cat2.name);
        }
        if (cat2.fight(cat3)) {
            System.out.println(cat2.name + "战胜了" + cat3.name);
        } else {
            System.out.println(cat2.name + "没打过" + cat3.name);
        }
        if (cat3.fight(cat1)) {
            System.out.println(cat3.name + "战胜了" + cat1.name);
        } else {
            System.out.println(cat3.name + "没打过" + cat1.name);
        }
    }

    public static class Cat {
        protected String name;
        protected int age;
        protected int weight;
        protected int strength;

        public Cat(String name, int age, int weight, int strength) {
            this.name = name;
            this.age = age;
            this.weight = weight;
            this.strength = strength;
        }

        public boolean fight(Cat anotherCat) {
            int ageAdvantage = this.age > anotherCat.age ? 1 : 0;
            int weightAdvantage = this.weight > anotherCat.weight ? 1 : 0;
            int strengthAdvantage = this.strength > anotherCat.strength ? 1 : 0;

            int score = ageAdvantage + weightAdvantage + strengthAdvantage;
            return score > 2; // return score > 2 ? true : false;
        }
    }
}
任务4 人

创建 Person 类。Person 类应包含 String 变量 name、int 变量 age、String 变量 address 和 char 变量 sex。

package zh.codegym.task.task05.task0506;



public class Person {
    String name;
    int age;
    String address;
    char sex;

    public static void main(String[] args) {

    }
}

任务5 算术平均值

使用键盘输入数字,然后计算算术平均值。

如果用户输入 -1,则显示所有输入数字的算术平均值并结束程序。

-1 不应包含在计算中。

package zh.codegym.task.task05.task0507;



import java.util.Scanner;

public class Solution {
    public static void main(String[] args) throws Exception {
        int sum = 0;
        Scanner sc = new Scanner(System.in);
        double i = 1.0;
        double avg = 0;
        while (true) {
            int num = sc.nextInt();
            if (num != -1) {
                sum += num;
                avg = sum / i++;

            } else {
                System.out.println(avg);
                break;
            }
        }

        //在此编写你的代码
    }
}

任务6 Person 类的 getter 和 setter

创建 Person 类。Person 类应包含 String 变量 name、int 变量 age 和 char 变量 sex。

为 Person 类的所有变量创建 getter 和 setter。

package zh.codegym.task.task05.task0508;



public class Person {
    int age;
    String name;
    char sex;

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

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

    public char getSex() {
        return sex;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public static void main(String[] args) {

    }
}

课程2 练习初始化对象 任务1 创建 Friend 类

创建包含三个初始化器(三个 initialize 方法)的 Friend 类:

  • name

  • name, age

  • name, age, sex

package zh.codegym.task.task05.task0509;



public class Friend {
    String name;
    int age;
    char sex;

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

    public void initialize(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void initialize(String name, int age, char sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;

    }

    public static void main(String[] args) {

    }
}

任务2 初始化猫

创建包含五个初始化器的 Cat 类。

  • name,

  • name, weight, age

  • name、age(标准 weight)

  • weight, color(未知的 name、address 和 age,即流浪猫)

  • weight, color, address(别人的宠物)

初始化器的工作是使对象有效。

例如,如果 weight 未知,则需要指定某个平均 weight。

一只猫不可能没有 weight。

age 和 color 也是如此。

但是,name 则可能有,也可能没有(即,name 可能为 null)。

address 也是如此(它可能为 null)。

package zh.codegym.task.task05.task0510;



public class Cat {
    String name = null;
    int weight = 5;
    int age = 2;
    String color = "Black";
    String address = null;

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

    public void initialize(String name, int weight, int age) {
        this.name = name;
        this.weight = weight;
        this.age = age;
    }

    public void initialize(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void initialize(int weight, String color) {
        this.weight = weight;
        this.color = color;
    }

    public void initialize(int weight, String color, String address) {
        this.weight = weight;
        this.color = color;
        this.address = address;
    }


    public static void main(String[] args) {

    }
}

任务3 创建 Dog 类

创建包含三个初始化器的 Dog 类:

  • name

  • name, height

  • name, height, color

package zh.codegym.task.task05.task0511;



public class Dog {
    String name;
    int height;
    String color;

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

    public void initialize(String name, int height) {
        this.name = name;
        this.height = height;
    }

    public void initialize(String name, int height, String color) {
        this.name = name;
        this.height = height;
        this.color = color;
    }

    public static void main(String[] args) {

    }
}

任务4 创建 Circle 类

创建包含三个初始化器的 Circle 类:

  • centerX, centerY, radius

  • centerX, centerY, radius, width

  • centerX, centerY, radius, width, color

package zh.codegym.task.task05.task0512;



public class Circle {
    int centerX;
    int centerY;
    int radius;
    int width;
    int color;

    public void initialize(int centerX, int centerY, int radius) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
    }

    public void initialize(int centerX, int centerY, int radius, int width) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
        this.width = width;
    }

    public void initialize(int centerX, int centerY, int radius, int width, int color) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
        this.width = width;
        this.color = color;
    }

    public static void main(String[] args) {

    }
}

任务5 我们来组合一个矩形

创建 Rectangle 类。此类的数据为 top、left、width、height。

创建尽可能多的 initialize(…) 方法

package zh.codegym.task.task05.task0513;



public class Rectangle {
    int top;
    int left;
    int width;
    int height;

    public void initialize(int top, int left, int width, int height) {
        this.top = top;
        this.left = left;
        this.width = width;
        this.height = height;
    }

    public void initialize(int top, int left) {
        this.top = top;
        this.left = left;
        width = height = 0;
    }

    public void initialize(int top, int left, int width) {
        this.top = top;
        this.left = left;
        this.width = width;
        height = width;
    }

    public void initialize(int top) {
        this.top = top;
        this.left = 0;
        this.width = 0;
        height = 0;
    }


    public static void main(String[] args) {

    }
}

任务6 程序员创建人

创建 Person 类。Person 类应包含 String 变量 name 和 int 变量 age。

添加 initialize(String name, int age) 方法,你将在该方法中初始化变量 name 和 age。

在 main 方法中,创建一个 Person 对象,并在变量 person 中存储对该对象的引用。

使用任意值调用 initialize 方法。

package zh.codegym.task.task05.task0514;



public class Solution {
    public static void main(String[] args) {
        Person person = new Person();
        person.initialize("海马濑人",16);
    }

    static class Person {
        String name;
        int age;

        public void initialize(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
}
任务7 初始化对象

仔细研究 Person 类。

更改该类,以便只有一个 initialize 方法初始化 Person 类的所有实例变量。

package zh.codegym.task.task05.task0515;



public class Person {
    String name;
    char sex;
    int money;
    int weight;
    double size;

    public void initialize(String name, int money, char sex, int weight, double size) {
        this.name = name;
        this.money = money;
        this.sex = sex;
        this.weight = weight;
        this.size = size;
    }

    public static void main(String[] args) {

    }
}

课程3 练习使用构造方法 任务1 你不能购买朋友

创建包含三个构造方法的 Friend 类:

  • name

  • name, age

  • name, age, sex

package zh.codegym.task.task05.task0516;



public class Friend {
    String name;
    int age;
    char sex;

    public Friend(String name) {
        this.name = name;
    }

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

    public Friend(String name, int age, char sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }


    public static void main(String[] args) {

    }
}

任务2 创建猫

创建包含五个构造方法的 Cat 类:

  • name,

  • name, weight, age

  • name、age(标准 weight)

  • weight, color(name、address 和 age 未知;是流浪猫)

  • weight, color, address(别人的宠物)

构造方法的工作是使对象有效。

例如,如果 weight 未知,则需要指定某个平均 weight。

一只猫不可能没有 weight。

age 也是如此。但是,name 则可能有,也可能没有(即,name 可能为 null)。

address 也是如此(它可能为 null)。

package zh.codegym.task.task05.task0517;



public class Cat {
    String name = null;
    int weight = 5;
    int age = 2;
    String color = "Black";
    String address = null;

    public  Cat(String name) {
        this.name = name;
    }

    public Cat(String name, int weight, int age) {
        this.name = name;
        this.weight = weight;
        this.age = age;
    }

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

    public Cat(int weight, String color) {
        this.weight = weight;
        this.color = color;
    }

    public Cat(int weight, String color, String address) {
        this.weight = weight;
        this.color = color;
        this.address = address;
    }

    public static void main(String[] args) {

    }
}
任务3 犬只注册

创建包含三个构造方法的 Dog 类:

  • name

  • name, height

  • name, height, color

package zh.codegym.task.task05.task0518;




public class Dog {
    String name;
    int height;
    String color;

    public Dog(String name) {
        this.name = name;
    }

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

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

    public static void main(String[] args) {

    }
}
任务4 创建包含三个构造方法的 Circle 类:
  • centerX, centerY, radius

  • centerX, centerY, radius, width

  • centerX, centerY, radius, width, color

package zh.codegym.task.task05.task0519;




public class Circle {
    int centerX;
    int centerY;
    int radius;
    int width;
    int color;

    public Circle(int centerX, int centerY, int radius) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
    }

    public Circle(int centerX, int centerY, int radius, int width) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
        this.width = width;
    }

    public Circle(int centerX, int centerY, int radius, int width, int color) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
        this.width = width;
        this.color = color;
    }


    public static void main(String[] args) {

    }
}
任务5 创建 Rectangle 类

创建 Rectangle 类。此类的数据为 top、left、width 和 height。

创建尽可能多的构造方法。

package zh.codegym.task.task05.task0520;




public class Rectangle {
    int top;
    int left;
    int width;
    int height;

    public Rectangle(int top, int left, int width, int height) {
        this.top = top;
        this.left = left;
        this.width = width;
        this.height = height;
    }

    public Rectangle(int top, int left) {
        this.top = top;
        this.left = left;
        width = height = 0;
    }

    public Rectangle(int top, int left, int width) {
        this.top = top;
        this.left = left;
        this.width = width;
        height = width;
    }

    public Rectangle(int top) {
        this.top = top;
        this.left = 0;
        this.width = 0;
        height = 0;
    }


    public static void main(String[] args) {

    }
}
任务6 从构造方法调用构造方法

弄明白程序的功能。

更改带两个参数的构造方法,以便它使用 radium 10 调用另一个构造方法。

考虑你需要调用哪个构造方法。

package zh.codegym.task.task05.task0521;



public class Circle {

    public double x;
    public double y;
    public double radius;

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

    public Circle(double x, double y) {
        this(x,y,10);

    }

    public Circle() {
        this(5, 5, 1);
    }

    public static void main(String[] args) {
        Circle circle = new Circle();
        System.out.println(circle.x + " " + circle.y + " " + circle.radius);
        Circle anotherCircle = new Circle(10, 5);
        System.out.println(anotherCircle.x + " " + anotherCircle.y + " " + anotherCircle.radius);
    }
}
任务7 最大数量的构造方法

研究 Circle 类。尽可能多地编写带不同参数的构造方法。

package zh.codegym.task.task05.task0522;



import java.awt.image.CropImageFilter;

public class Circle {
    public double x;
    public double y;
    public double radius;

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

    public Circle(double x, double y) {
        this.x = x;
        this.y = y;
    }

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

    public Circle() {
        x = 0;
        y = 0;
        radius = 1;
    }
    //在此编写你的代码

    public static void main(String[] args) {

    }
}
任务8 构造方法

弄明白程序的功能。

查找并修复 Circle 类中的一个错误。不要更改 main 方法。

提示:

仔细研究默认构造方法。

package zh.codegym.task.task05.task0523;



public class Circle {
    public Color color = new Color();

    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.color.setDescription("红色");
        System.out.println(circle.color.getDescription());
    }
    
    public class Color {
        String description;

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }
}
任务9 轮子的基础

在 Circle 类中,创建一个将初始化所有实例变量的构造方法。

该构造方法必须带有三个参数。

package zh.codegym.task.task05.task0524;



public class Circle {
    public double x;
    public double y;
    public double r;

    public Circle(double x, double y, double r) {
        this.x = x;
        this.y = y;
        this.r = r;
    }

    public static void main(String[] args) {

    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号