JavaSE核心技术——面向对象编程基础练习题

1、定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(intx0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动,创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。[必作题]

Point 类代码:
public class Point {

    int x;

    int y;

    //创建构造器
    public Point(){

    }
    public Point(int x0,int y0){
        this.x = x0;
        this.y = y0;
    }

    //创建方法
    public void movePoint(int dx,int dy){
        this.x += dx;
        this.y += dy;
    }


}
测试类代码:
    public static void main(String[] args) {
        Point p1 = new Point(2,2);
        p1.movePoint(6, 7);
        System.out.println("p1当前的X坐标为:"+p1.x+",p1当前的Y坐标为:"+p1.y);
        Point p2 = new Point();
        p2.movePoint(6, 7);
        System.out.println("p2当前的X坐标为:"+p2.x+",p2当前的Y坐标为:"+p2.y);
    }

这里写图片描述
• 2、定义一个矩形类Rectangle:(知识点:对象的创建和使用)[必做题]
• 2.1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
• 2.2 有2个属性:长length、宽width
• 2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值
• 2.4 创建一个Rectangle对象,并输出相关信息

Rectangle 类 代码:
public class Rectangle {
    int length;
    int width;

    public int getArea(){
        int area;
        area = length * width;
        return area;
    }
    public int getPer(){
            int per;
            per = 2*(length + width);
            return per;
        }
     void showAll(){
        System.out.println("长为"+length+",宽为"+width+",面积为"+getArea()+",周长为"+getPer());
    }
    public  Rectangle(int width,int length){
        this.length = length;
        this.width = width;
    }

}
测试类 代码:
    public static void main(String[] args) {
        Rectangle rc = new Rectangle(2,3);
        rc.showAll();

    }

这里写图片描述
• 3、定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。 [必做题]
• 3.1 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
• 3.2 输出笔记本信息的方法
• 3.3 然后编写一个测试类,测试笔记本类的各个方法。

Computer 类 代码:
public class Computer {
    //  属性
    char color;
    int cpu;
    //  构造方法
    public void getDate(){

    }
    public void getDate(char color,int cpu){
        this.color = color;
        this.cpu = cpu;

    }
    void showAll(){
        System.out.println("颜色为"+color+",cpu型号为"+cpu);
    }

}
测试类 代码:
    public static void main(String[] args) {
        Computer cp = new Computer();
        cp.getDate('红',500);
        cp.showAll();
    }

这里写图片描述
• 4、设计一个类Student,该类包括姓名、学号和成绩。设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息。[选做题]

Student 类 代码:
public class Student {

    String name;
    int id;
    int score;



    public Student(String name, int id, int score) {
        this.name = name;
        this.id = id;
        this.score = score;
    }



    public void getSort(Student[] s1){
        for(int j = 1;j<s1.length;j++){
            for(int i = 0;i<s1.length-j;i++){
                if(s1[i].score < s1[i+1].score){
                    Student temp = s1[i];
                    s1[i] = s1[i+1];
                    s1[i+1] = temp;
                }
            }
        }
    }
}
测试类 代码:
public static void main(String[] args) {
        Student st1 = new Student("吴奇隆",101,90);
        Student st2 = new Student("陈志朋",102,80);
        Student st3 = new Student("苏有朋",103,100);
        Student[] s = {st1,st2,st3};
        st2.getSort(s);
        for(int i = 0;i<s.length;i++){
            System.out.println(s[i].name+"同学的学号为"+s[i].id+",其成绩为"+s[i].score+",名次为"+(i+1));
        }
    }

这里写图片描述
• 5、定义两个类,描述如下: [必做题]
• 5.1定义一个人类Person:
• 5.1.1定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
• 5.1.2有三个属性:名字、身高、体重
• 5.2定义一个PersonCreate类:
• 5.2.1创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74
• 5.2.2分别调用对象的sayHello()方法。

• 6、定义两个类,描述如下: [必做题]
• 6.1定义一个人类Person:
• 6.1.1定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
• 6.1.2有三个属性:名字、身高、体重
• 6.1.3通过构造方法,分别给三个属性赋值
• 6.2定义一个Constructor类:
• 6.2.1创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74
• 6.2.2分别调用对象的sayHello()方法。

Person 类 代码:
public class Person {
    String name;
    int age;
    double height;
    public void sayHello(){
        System.out.println("hello,my name is "+this.name);
    }
    public void getValue(String name,int age,double height){
        this.name = name;
        this.age = age;
        this.height = height;
    }
}
测试类  Constructor 代码:
public class Constructor {
    public static void main(String[] args) {
        Person p1 = new Person();
        p1.getValue("zhangsan",33,1.74);
        p1.sayHello();
        Person p2 = new Person();
        p2.getValue("lishi",44,1.74);
        p2.sayHello();
    }
}

这里写图片描述
• 7、定义一个汽车类Vehicle,要求如下: [选做题]
• 7.1属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型),并且所有属性为私有。
• 7.2至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
• 7.3为私有属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
• 7.4定义一个一般方法run(),用打印语句描述汽车奔跑的功能
• 7.5定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。

Vehicle 类 代码:
public class Vehicle {
    private String brand ;
    private String color;
    private double speed;

    public String getBrand(){
        return this.brand;
    }
    public void setColor(String color){
        this.color = color;
    }
    public String getColor(){
        return this.color;
    }
    public void setSpeed(double speed){
        this.speed = speed;
    }
    public double getSpeed(){
        return this.speed;
    }

    public  Vehicle(String brand,String color){
        this.brand = brand;
        this.color = color;
        this.speed = 0;
    }

    public void run(){
        System.out.println(getColor()+"的"+getBrand()+"的车速为"+getSpeed());
    }
}
测试类 VehicleTest 类 代码:
public class VehicleTest {
    public static void main(String[] args) {
        Vehicle vehicle = new Vehicle("benz", "black");
        vehicle.setSpeed(70);
        vehicle.run();
    }
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_37067955/article/details/81782509