将课堂练习题提交(类图+代码),以附件形式上传即可。 1、一个人(Person)在过河的时候需要使用(useTool())一条船(Boat)。请用类图表示人与船的关系。 package 面向对象基

将课堂练习题提交(类图+代码),以附件形式上传即可。
1、一个人(Person)在过河的时候需要使用(useTool())一条船(Boat)。请用类图表示人与船的关系。

package 面向对象基本概念综合练习;
//一个人(Person)在过河的时候需要使用(useTool())一条船(Boat)。
public class 课堂练习题1 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	class Person {
	    
	        // code to use the boat to cross the river
	    }
	}

	class Boat {
	    // code for the boat class
	}

}

+---------+ +---------+
| Person | | Boat |
+---------+ +---------+
| | | |
+---------+ +---------+
| |
| |
useTool() addPerson()
|
removePerson()

注释:

  • Person类有一个useTool()方法。
  • Boat类有两个方法addPerson()和removePerson()用于添加和移除人。
  • Person类与Boat类之间是一对多的关系。

2、有一个收音机(Radio),需要安装电池(Battery)才能够打开(openRadio())。电池原有100格(electricityAmount=100)电。请模拟出收音机在安装上“南孚”电池,打开收音机后,就消耗了10格电的场景。

package 面向对象基本概念综合练习;

public class 课堂练习题2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	class Radio {
	    private int electricityAmount;

	    public Radio() {
	        this.electricityAmount = 0;
	    }

	   

	    public void openRadio() {
	        if (this.electricityAmount > 0) {
	            System.out.println("Radio is open.");
	        } else {
	            System.out.println("Battery has no electricity.");
	        }
	    }
	}

	class Battery {
	    private int electricityAmount;

	    public Battery() {
	        this.electricityAmount = 100;
	    }

	    public int getElectricityAmount() {
	        return this.electricityAmount;
	    }
	}

	
}

}

+----------+ +---------+
| Radio | | Battery |
+----------+ +---------+
| | | |
| | | |
| openRadio() <------+ | |
+----------+ | | |
| | | |
electricityAmount=100 | addBattery()
| | |
electricityAmount=100
|
consumeElectricity()
|
electricityAmount=90

注释:

  • Radio类有一个openRadio()方法用于打开收音机,以及一个consumeElectricity()方法用于消耗电池电量。
  • Battery类有一个属性electricityAmount表示电池电量,以及一个addBattery()方法用于安装电池。
  • Radio类与Battery类之间是一对一的关系。

3、软工19级辅导员( Counselor )负责管理软工班级(ClassGrade)的日常工作维持4年时间。请用类图表示他们之间的关系;并用代码进行实现。

package 面向对象基本概念综合练习;

class Counselor {
private String name;
private ClassGrade classGrade;
private int yearsOfService;

public Counselor(String name, ClassGrade classGrade, int yearsOfService) {
    this.name = name;
    this.classGrade = classGrade;
    this.yearsOfService = yearsOfService;
}

public void manageClass() {
    // code to manage the class
}

public void setYearsOfService(int yearsOfService) {
    this.yearsOfService = yearsOfService;
}

public int getYearsOfService() {
    return yearsOfService;
}

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

public String getName() {
    return name;
}

public void setClassGrade(ClassGrade classGrade) {
    this.classGrade = classGrade;
}

public ClassGrade getClassGrade() {
    return classGrade;
}

}

public class ClassGrade {
// code for class grade
}


+--------+ +--------------+
| ClassGrade | | Counselor |
+--------+ +--------------+
| |1 * | |
+--------+ +--------------+
| * |
| |
+--------------+
|manageDailyTask()|
+--------------+

注释:

  • ClassGrade类与Counselor类之间是一对多的关系,一个辅导员可以管理多个班级。
  • Counselor类有一个manageDailyTask()方法用于管理班级的日常工作。
  • 一个班级只对应一个辅导员,是一对一关系。

4、班级(ClassGrade)是由学生(Student)组成的。请用类图表示他们之间的关系;并用代码进行实现。



package 面向对象基本概念综合练习;

import java.util.List;

public class 课堂练习题4 {
	
	
	public class ClassGrade {
	    private List<Student> studentList;

	    public ClassGrade(List<Student> studentList) {
	        this.studentList = studentList;
	    }

	    public List<Student> getStudentList() {
	        return studentList;
	    }
	}

	public class Student {
	    private String name;
	    private int age;
	    private String gender;
	    private String studentId;

	    public Student(String name, int age, String gender, String studentId) {
	        this.name = name;
	        this.age = age;
	        this.gender = gender;
	        this.studentId = studentId;
	    }

	    public String getName() {
	        return name;
	    }

	    public int getAge() {
	        return age;
	    }

	    public String getGender() {
	        return gender;
	    }

	    public String getStudentId() {
	        return studentId;
	    }
	
}
}

+---------------------+ +------------------------+
| ClassGrade | | Student |
+---------------------+ +------------------------+
| -className: string | | -studentName: string |
| -studentList: list | | -studentID: int |
| | | -classGrade: ClassGrade |
| +addStudent(): void | | |
| +removeStudent(): void| | |
| | | |
+---------------------+ +------------------------+

代码实现:

// Person类
public class Person {
public void useTool() {
// 使用工具
}
}
// Boat类
public class Boat {
private List personList = new ArrayList<>();
public void addPerson(Person person) {
personList.add(person);
}
public void removePerson(Person person) {
personList.remove(person);
}
}
// Radio类
public class Radio {
public void openRadio() {
// 打开收音机
}
public void consumeElectricity(Battery battery) {
battery.setElectricityAmount(battery.getElectricityAmount() - 10);
}
}
// Battery类
public class Battery {
private int electricityAmount = 100;
public void addBattery() {
electricityAmount = 100;
}
public void setElectricityAmount(int amount) {
electricityAmount = amount;
}
public int getElectricityAmount() {
return electricityAmount;
}
}
// ClassGrade类
public class ClassGrade {
private Counselor counselor;
// 其他属性和方法
public void setCounselor(Counselor counselor) {
this.counselor = counselor;
}
}
// Counselor类
public class Counselor {
public void manageDailyTask(ClassGrade classGrade) {
// 管理班级日常工作
}
}

5、一个公司(Company)拥有多个部门(Department)。请用类图表示它们之间的关系;并用代码进行实现。

package 面向对象基本概念综合练习;

import java.util.ArrayList;

public class 课堂练习题5 {

class Department {
    private String name; // 部门名称

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

    public String getName() {
        return name;
    }
}

//定义公司类
class Company {
    private String name; // 公司名称
    private ArrayList<Department> departments; // 公司部门列表

    public Company(String name) {
        this.name = name;
        departments = new ArrayList<>();
    }

    // 添加部门
    public void addDepartment(Department department) {
        departments.add(department);
    }

    // 获取指定名称的部门
    public Department getDepartment(String name) {
        for (Department department : departments) {
            if (department.getName().equals(name)) {
                return department;
            }
        }
        return null;
    }

    public String getName() {
        return name;
    }
}

//测试代码
public class Test {
    public void main(String[] args) {
        // 创建公司对象
        Company company = new Company("ABC公司");

        // 添加部门
        company.addDepartment(new Department("财务部"));
        company.addDepartment(new Department("销售部"));
        company.addDepartment(new Department("人力资源部"));

        // 获取部门
        Department department = company.getDepartment("销售部");
        if (department != null) {
            System.out.println("获取部门:" + department.getName());
        } else {
            System.out.println("没有找到部门");
        }
    }
}

}

类图如下:

+----------+ +----------+
| Company |<>-------o| Department|
+----------+ +----------+

代码实现如下:

public class Company {
private List departments;
public Company() {
departments = new ArrayList<>();
}
public void addDepartment(Department department) {
departments.add(department);
}
public void removeDepartment(Department department) {
departments.remove(department);
}
public List getDepartments() {
return departments;
}
}
public class Department {
private String name;
public Department(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

6、圆锥体(Cone)是由底圆(Circle)和高(height)作为属性特征组成的,可以计算圆锥体的体积(getVolume()),也可以更改底圆的特征(setBottom(Circle c))。底圆(Circle)通过半径(radius)可以计算自身的面积(getArea())。请思考圆锥体和底圆之间的关系,根据描述绘制类图。

package 面向对象基本概念综合练习;

public class 课堂练习题6 {
public class Cone {
private Circle bottom;
private double height;

    public Cone(Circle bottom, double height) {
        this.bottom = bottom;
        this.height = height;
    }

    public void setBottom(Circle c) {
        this.bottom = c;
    }

    public double getVolume() {
        return (1.0 / 3) * bottom.getArea() * height;
    }
}

class Circle {
    private double radius;

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

    public double getArea() {
        return Math.PI * radius * radius;
    }
}

}

圆锥体 (Cone)

  • radius : double
  • height : double
  • bottom : Circle
  • Cone(radius: double, height: double)
  • getVolume() : double
  • setBottom(c: Circle) : void
    圆形 (Circle)
  • radius : double
  • Circle(radius: double)
  • getArea() : double

7、根据代码构建类图
Vehicle c=new Car(“red”,“Audi",123);
Vehicle t=new Truck(“white",“Benz",300);
Driver driver=new Driver();
driver.drive©;
driver.drive(t);

package 面向对象基本概念综合练习;

public class 课堂练习题7 {
// Car class
public class Car extends Vehicle {
public Car(String color, String brand, int maxSpeed) {
super(color, brand, maxSpeed);
}
}

// Truck class
public class Truck extends Vehicle {
    public Truck(String color, String brand, int maxSpeed) {
        super(color, brand, maxSpeed);
    }
}

// Driver class
public class Driver {
    public void drive(Vehicle vehicle) {
        vehicle.run();
    }
}

// Vehicle class (abstract)
public abstract class Vehicle {
    private String color;
    private String brand;
    private int maxSpeed;

    public Vehicle(String color, String brand, int maxSpeed) {
        this.color = color;
        this.brand = brand;
        this.maxSpeed = maxSpeed;
    }

    public void run() {
        System.out.println(color + " " + brand + " is running at " + maxSpeed + " km/h.");
    }
} 

// Main class
public class Main {
    public void main(String[] args) {
        Vehicle c = new Car("red", "Audi", 123);
        Vehicle t = new Truck("white", "Benz", 300);
        Driver driver = new Driver();
        driver.drive(c);
        driver.drive(t);
    }
}

}

±----------------------+
| Vehicle |
±----------------------+
| -color: String |
| -brand: String |
| -horsePower: int |
±----------------------+
| +Vehicle(color: String, brand: String, horsePower: int) |
| +getColor(): String |
| +getBrand(): String |
| +getHorsePower(): int |
| +setColor(color: String): void |
| +setBrand(brand: String): void |
| +setHorsePower(horsePower: int): void |
±----------------------+
/
/
±---------+ ±---------+
| Car | | Truck |
±---------+ ±---------+
| +Car(color: String, brand: String, horsePower: int) |
| +getMaxSpeed(): int |
| +setMaxSpeed(maxSpeed: int): void |
±---------+ |
|
|
|
±-----------------+ |
| Driver | |
±-----------------+ |
| +drive(vehicle: Vehicle): void |
±-----------------+

8、无论是什么样的车(轿车Car、火车Train、卡车Truck等),都属于交通工具(Vehicle)的一种,它们都具有品牌brand、颜色color、价格price等特征,它们都具有能够驾驶drive的特点。但是,不同的交通工具,驾驶的方法却完全不一样。请通过类图将上述的关系表达出来。
package 面向对象基本概念综合练习;

public class 课堂练习题8 {
public abstract class Vehicle {
private String brand;
private String color;
private double price;

	  public Vehicle(String brand, String color, double price) {
	    this.brand = brand;
	    this.color = color;
	    this.price = price;
	  }
	  
	  public String getBrand() {
	    return brand;
	  }
	  
	  public String getColor() {
	    return color;
	  }
	  
	  public double getPrice() {
	    return price;
	  }
	  
	  public abstract void drive();
	}

	public class Car extends Vehicle {
	  public Car(String brand, String color, double price) {
	    super(brand, color, price);
	  }
	  
	  public void drive() {
	    System.out.println("Driving a car");
	  }
	}

	public class Train extends Vehicle {
	  public Train(String brand, String color, double price) {
	    super(brand, color, price);
	  }
	  
	  public void drive() {
	    System.out.println("Driving a train");
	  }
	}

	public class Truck extends Vehicle {
	  public Truck(String brand, String color, double price) {
	    super(brand, color, price);
	  }
	  
	  public void drive() {
	    System.out.println("Driving a truck");
	  }
	}

}


Vehicle
/ \
Car Train Truck
/ \ / \ / \
brand color price brand color price drive

9、Tom(Person)是一名大学生运动员,拥有两个身份(Role),都能做(do)些事情,但做的事情各有不同。他不仅需要作为一名学生(Student)在学校读书(learn),还需要作为一名运动员(Athlete)参加长跑训练(train)。请用类图表示Tom的这种双重身份,请写出student类和athlete类的代码。

package 面向对象基本概念综合练习;

public class 课堂练习题9 {

public class Student {
    //属性
    private String name;
    private int age;
    //构造方法
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //方法
    public void learn() {
        System.out.println(name + " is learning.");
    }
}

public class Athlete {
    //属性
    private String name;
    private int age;
    //构造方法
    public Athlete(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //方法
    public void train() {
        System.out.println(name + " is training.");
    }
}


//注意,这里并没有声明角色(Role)这个类,因为在这个题目中,角色并不需要额外的属性或方法,所以可以直接在学生和运动员类中实现。如果在实际开发中需要,可以再创建一个角色类来继承这两个类,或者使用接口,这样代码会更加规范、易于维护。

}

类图如下:


+------------------+
| Tom |
+------------------+
| -name : String |
+------------------+
| +learn() |
| +train() |
+------------------+
|
|
V
+------------------+ +------------------+
| Student | | Athlete |
+------------------+ +------------------+
| -studentNumber | | -coachName : String|
+------------------+ +------------------+
| +learn() | | +train() |
+------------------+ +------------------+

Student类的代码:

public class Student {
private String name;
private int studentNumber;
public Student(String name, int studentNumber) {
this.name = name;
this.studentNumber = studentNumber;
}
public void learn() {
System.out.println("I am a student and I am learning.");
}
}

Athlete类的代码:

public class Athlete {
private String name;
private String coachName;
public Athlete(String name, String coachName) {
this.name = name;
this.coachName = coachName;
}
public void train() {
System.out.println("I am an athlete and I am training.");
}
}

Tom类的代码:

public class Tom extends Student, Athlete {
public Tom(String name, int studentNumber, String coachName) {
super(name, studentNumber);
Athlete(name, coachName);
}
// Tom类继承了Student类和Athlete类的learn()和train()方法,无需再次定义。
}

10、圆(Circle)、矩形(Rectangle)和多边形(Polygon)都是形状(Shape)的一种。它们都可以移动(move),都可以变换大小(resize)和显示(show)。但是圆具有自己的半径(radius),矩形拥有自己的长(width)和宽(height),多边形拥有自己的顶点列表(points)。

package 面向对象基本概念综合练习;

import java.awt.Point;
import java.util.List;

public class 课堂练习题10 {

public interface Shape {
    void move(double x, double y);
    void resize(double scale);
    void show();
}

public class Circle implements Shape {
    private double x, y;  // 圆心坐标
    private double radius;

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

    @Override
    public void move(double dx, double dy) {
        this.x += dx;
        this.y += dy;
    }

    @Override
    public void resize(double scale) {
        this.radius *= scale;
    }

    @Override
    public void show() {
        System.out.println("Circle(" + x + ", " + y + ", " + radius + ")");
    }
}

public class Rectangle implements Shape {
    private double x, y;  // 左上角坐标
    private double width, height;

    public Rectangle(double x, double y, double width, double height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    @Override
    public void move(double dx, double dy) {
        this.x += dx;
        this.y += dy;
    }

    @Override
    public void resize(double scale) {
        this.width *= scale;
        this.height *= scale;
    }

    @Override
    public void show() {
        System.out.println("Rectangle(" + x + ", " + y + ", " + width + ", " + height + ")");
    }
}

public abstract class Polygon implements Shape {
    private List<Point> points;

    public Polygon(List<Point> points) {
        this.points = points;
    }
}

}

圆(Circle)、矩形(Rectangle)和多边形(Polygon)都是面向对象编程中的类(class),它们都继承自形状(Shape)类。在形状类中,定义了移动(move)、变换大小(resize)和显示(show)等基本方法。而每个子类则增加了自己的特有属性和方法。
圆类中,定义了半径属性(radius),并增加了计算圆面积(get_area)和周长(get_perimeter)的方法。
矩形类中,定义了长属性(width)和宽属性(height),并增加了计算矩形面积(get_area)和周长(get_perimeter)的方法。
多边形类中,定义了顶点列表属性(points),并增加了计算多边形面积(get_area)和周长(get_perimeter)的方法,这些方法的实现则需要用到数学中的向量运算和多边形分割等技术。
通过继承和多态的机制,我们可以在不同的场景中灵活使用这些形状类,实现各种图形的绘制和计算,从而实现更加复杂的应用。

注意:如果课堂上有除此之外的练习,可在附件中一并上传。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值