面向对象的编程练习

练习1

其中方法前的“+”表示权限修饰符为public
在这里插入图片描述

public class PersonTest{
    
    
    public static void main(String[] args){
    
    
        Person p1 = new Person();
        p1.name = "Tom";
        p1.age = 10;
        p1.sex = 1;
        p1.study();
        p1.showAge();
        int newAge = p1.addAge(2);
        System.out.println(p1.name + "的新年龄为" + newAge);
        System.out.println(p1.age);//20
    }
}
 class Person{
    
    
    String name;
    int age;
    int sex;
    public void study(){
    
    
        System.out.println("studying");
    }
    public void showAge(){
    
    
        System.out.println("age为" + age);
    }
    public int addAge(int i){
    
    
        age += i;
        return age;
    }
}

练习2

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

import java.util.Scanner;

//测试类
public class CircleTest{
    
    
    public static void main(String[] args){
    
    
        Circle c1 = new Circle();
        System.out.println("请输入圆的半径:");
        Scanner scan = new Scanner(System.in);
        c1.radius = scan.nextDouble();
        double area = c1.area();
        System.out.println(area);
    }
}
 class Circle{
    
    
    double radius;
    public double area(){
    
    
        return Math.PI * radius * radius;
    }
} 

练习3

在这里插入图片描述

3.1
public Test3{
    
    
	public static void main(String[] args){
    
    
		Test3 t1 = new Test3();
		t1.method();

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

3.2
public class Test3 {
    
    
    public static void main(String[] args) {
    
    
        Test3 t1 = new Test3();
        System.out.println(t1.method());
    }
        public double method() {
    
    
            for (int i = 0; i < 10; i++) {
    
    
                for (int j = 0; j < 8; j++) {
    
    
                    System.out.print("*");
                }
                System.out.println();
            }
            return 10 * 8 ;
        }

}

3.3
public class Test3{
    
    
    public static void main(String[] args) {
    
    
        Test3 t1 = new Test3();
        System.out.println("面积为:" + t1.method(3,5));
    }
    public double method(int m,int n){
    
    
        for (int i = 0; i < m; i++) {
    
    
            for (int j = 0; j < n; j++) {
    
    
                System.out.print("*");
            }
            System.out.println();
        }
        return m * n;
    }

}

练习4

在这里插入图片描述

public class StudentTest{
    
    
	public static void main(String[] args){
    
    
	
		//声明一个Student类型的数组
		Student[] stud = new Student[20];//对象数组
		for(int i = 0; i < stud.length;i++){
    
    
			//给数组元素赋值,每个数组元素都是一个对象
			stud[i] = new Student();
			//给Student对象的属性赋值
			stud[i].number = i + 1;
			stud[i].state = (int)(Math.random()*6 +1);
			stud[i].score = (int)(Math.random()*101);
		}
		//遍历学生数组
		for(int i = 0; i < stud.length;i++){
    
    
			System.out.println(stud[i].info());
		}
		System.out.println("*******************");
		//问题一:打印出3年级的学生信息
		for(int i = 0; i < stud.length;i++){
    
    
			if(stud[i].state == 3){
    
    
			System.out.println(stud[i].info());
			}
		}
		System.out.println("*******************");
		//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
		for(int i = 0; i < stud.length - 1;i++){
    
    
			for(int j = 0; j < stu.length - 1 - i;j++)
				if(stud[j].score < stud[j + 1].score){
    
    
				//如果需要换顺序,交换的是数组的元素:Student对象
				Student temp = stud[j];
				stud[j] = stud[j + 1];
				stud[j + 1] = temp;
			}
		}
		for(int i = 0; i < stud.length;i++){
    
    
			System.out.println(stud[i].info());
		}
	}
}
class Student{
    
    
	int number;//学号
	int state;//年级
	int score;//成绩

	//显示学生信息的方法
	public String info(){
    
    
		return "学号" + number + ",年级" + state + ",成绩" + score;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_46138661/article/details/112600683
今日推荐