类与对象练习

对象数组题目[复习的时候需要自己实现一遍]

定义类Student,包含三个属性:学号number(int),年级state(int),成绩
score(int)。 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息



public class TwoTwo {
    public static void main(String[] args) {
        // 创建
        Student[] students = new Student[20];
        for (int i = 0; i < 20; i++){
            // 注意,这是必须的[我的错误点]
            students[i] = new Student();
            students[i].setNumber(i + 1);
            students[i].setState((int)(Math.random() * 6 + 1)); ; //[0, 6) ---> [1, 7) ,  int 是直接截段, 因此 [1, 6]
            students[i].setScore((int)(Math.random() * 101)); // [0, 101) ----> int 是直接截断, 因此[0, 100]
        }

        // 打印出所有state为3的信息
        for (Student student: students) {
            if (student.getState() == 3){
                System.out.println(student.toString());
            }
        }

        System.out.println("-------------------------");

        // 因为students是对象,传递的是地址的拷贝,在函数里面修改数组改的是传参指向的地址
        bubbleSort(students);

        // 因此会改变原来的数据
        for (Student student: students) {
            System.out.println(student.toString());
        }

    }

    // 冒泡排序法:比较相邻两个元素的大小,如果大就交换, --->
    /*  数组当中比较小的数值向下沉,数值比较大的向上浮!
     * 1 6 3 2 5 4
     *   1 < 6  ---- 1 6 3 2 5 4
     *  6 > 3  ---- 1 3 6 2 5 4
     *  6 > 2  ---- 1 3 2 6 5 4
     *  6 > 5  ---- 1 3 2 5 6 4
     *  6 > 4  ---- 1 3 2 5 4 6  最大值登顶
     *   每次都要从头开始比较
     *
     * */
    public static void bubbleSort(Student[] students){
        //外层循环:控制循环次数
        for (int i = 0; i <  students.length; i++) {
            // 控制相邻连个元素比较
            // 当i = 0时, 最后一个比较的时  students.length  - 2 和  students.length - 0  - 1
            // 当i = 1时, 最后一个比较的时  students.length  - 3 和  students.length - 1  - 1
            for (int j = 0; j <  students.length - i - 1; j++){
                if (students[j].getScore() > students[j + 1].getScore()){ // 最大值登顶: 左边大于右边就要交换
                        // 不是交换两个学生的分数,而是交换两个学生
                    Student temp = students[j]; // students[j]存的是j学生的地址
                    students[j] = students[j+1];
                    students[j+1] = temp;
                }
            }
        }
    }

}

利用面向对象的编程方法,设计类Circle计算圆的面积。

public class Circle {
    private double radio;
    private double Pi = 3.14;


    public double getRadio() {
        return radio;
    }

    public void setRadio(float radio) {
        this.radio = radio;
    }

    public double calArea(){
        return this.radio * this.radio * Pi;
    }
}



public class CircleTest{
    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.setRadio(5);
        System.out.println(circle.calArea());
    }

}

打印一个10 * 8 的*型矩形[太简单]

  • 编写程序,声明一个method方法,在方法中打印一个10 * 8 的*型矩形, 在main方法中调用该方法
public class TwoTwo {
    public static void main(String[] args) {
        printRect();
    }

    public static void printRect(){
        for (int i = 0;  i <10; i++){
            for (int j = 0; j < 8; j++){
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
  • 修改上一个程序,在method方法中,除打印一个108的型矩形外,再
    计算该矩形的面积,并将其作为方法返回值。在main方法中调用该方法,
    接收返回的面积值并打印。
public class TwoTwo {
    public static void main(String[] args) {
        int i = printRect();
        System.out.println(i);
    }

    public static int printRect(){
        for (int i = 0;  i <10; i++){
            for (int j = 0; j < 8; j++){
                System.out.print("* ");
            }
            System.out.println();
        }

        return  10 * 8;
    }
}

  • 修改上一个程序,在method方法提供m和n两个参数,方法中打印一个
    m* n的*型矩形,并计算该矩形的面积, 将其作为方法返回值。在main方法
    中调用该方法,接收返回的面积值并打印
public class TwoTwo {
    public static void main(String[] args) {
        int i = printRect(10, 8);
        System.out.println(i);
    }

    public static int printRect(int m, int n){
        if (m < 0 || n < 0 ){
            return  -1;
        }
        for (int i = 0;  i < m; i++){
            for (int j = 0; j < n; j++){
                System.out.print("* ");
            }
            System.out.println();
        }

        return  m * n;
    }
}

创建一个Person类,其定义如下:[太简单]

要求:(1)创建Person类的对象,设置该对象的name、age和sex属性,调用study方法,输出字符串“studying”,调用showAge()方法显示age值,调用addAge()方法给对象的age属性值增加2岁。
(2)创建第二个对象,执行上述操作,体会同一个类的不同对象之间的关系

在这里插入图片描述

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public int getSex() {
        return sex;
    }

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


    public void study(){
        System.out.println("studying");
    }

    public void showAge(){
        System.out.println("age" + this.age);
    }

    public int addAge(int i){
        this.age = this.age + i;
        return this.age;
    }
}

public class PersonTest{
    public static void main(String[] args) {
        Person p1 = new Person();
        p1.setName("tom");
        p1.setAge(18);
        p1.setSex(1);

        p1.study();
        p1.showAge();
        p1.addAge(2);
        p1.showAge();

        Person p2 = new Person();
        p2.setName("tom");
        p2.setAge(18);
        p2.setSex(1);

        p2.study();
        p2.showAge();
        p2.addAge(2);
        p2.showAge();
    }

}

编写教师类和学生类,并通过测试类创建对象进行测试[太简单了,可以不看]

在这里插入图片描述


public class Student {
    private String name;
    private int age;
    private String major;
    private String interests;

    public Student() {
    }

    public Student(String name, int age, String major, String interests) {
        this.name = name;
        this.age = age;
        this.major = major;
        this.interests = interests;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getInterests() {
        return interests;
    }

    public void setInterests(String interests) {
        this.interests = interests;
    }


    public String say() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", major='" + major + '\'' +
                ", interests='" + interests + '\'' +
                '}';
    }
}

public class Teacher {
    private String name;
    private int age;
    private int teachAge ;
    private String course;

    public Teacher() {
    }

    public Teacher(String name, int age, int teachAge, String course) {
        this.name = name;
        this.age = age;
        this.teachAge = teachAge;
        this.course = course;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public int getTeachAge() {
        return teachAge;
    }

    public void setTeachAge(int teachAge) {
        this.teachAge = teachAge;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }


    public String say() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", teachAge=" + teachAge +
                ", course='" + course + '\'' +
                '}';
    }
}



public class TwoTwo {
    public static void main(String[] args) {
        Student student = new Student();
        student.setAge(18);
        student.setInterests("编程");
        student.setName("Tom");
        student.setMajor("计算机");

        Teacher teacher = new Teacher();
        teacher.setAge(33);
        teacher.setName("Lily");
        teacher.setTeachAge(22);
        teacher.setCourse("编程");

        System.out.println(teacher.say());;
        System.out.println(student.say());;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值