请帮我写出这段代码,a Circle class that derives from the Shape class and has the following UML specification: +---------------------------------------------+ | Circle | +---------------------------------------------+ | - radius: double | +---------------------------------------------+ | + Circle(double x, double y, double radius) | | + area(): double | | + resize(double newRadius): void | | + testCircle(): void | +---------------------------------------------+ Use Math.PI to compute the area of a circle. Resizing a circle changes its radius to be newRadius. Do not forget to change the main method of the Start class to run the unit tests of the new Circle class.
时间: 2023-06-02 22:06:21 浏览: 76
Sure, the code for the Circle class would look like:
```
public class Circle extends Shape {
private double radius;
public Circle(double x, double y, double radius) {
super(x, y);
this.radius = radius;
}
public double area() {
return Math.PI * radius * radius;
}
public void resize(double newRadius) {
this.radius = newRadius;
}
public void testCircle() {
// some testing code here
}
}
```
And in the Start class, you would need to add code to run the testCircle() method of the new Circle class.
相关问题
C++Write a program to achieve the following requirements: 1) Define the Date class. Its private member variables are the year, month and day (int type), and its member function show() outputs the year, month and day; 2) Define a vehicle class. Its attributes include date of manufacture (出厂日期) (defined by Date class) and price. The member function show () outputs date of manufacture and price; 3) Vehicle class derives Car class and Truck class, both of which are private inheritance. Car class has carrying capacity (pnum); truck class has two members, including carrying capacity (载人数量, pnum) and load capacity (载重量, qnum). Each class has a member output function show(); 4) Write a main function, initialize various objects defined by Car class and Truck class, and output the data of each object.
下面是实现以上需求的 C++ 代码:
```cpp
#include <iostream>
using namespace std;
class Date {
private:
int year, month, day;
public:
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
void show() {
cout << year << "-" << month << "-" << day << endl;
}
};
class Vehicle {
private:
Date manuDate;
double price;
public:
Vehicle(Date d, double p) {
manuDate = d;
price = p;
}
void show() {
cout << "Manufacture Date: ";
manuDate.show();
cout << "Price: $" << price << endl;
}
};
class Car : private Vehicle {
private:
int pnum; // 载人数量
public:
Car(Date d, double p, int n) : Vehicle(d, p) {
pnum = n;
}
void show() {
cout << "Car Information:" << endl;
Vehicle::show();
cout << "Passenger Capacity: " << pnum << endl;
}
};
class Truck : private Vehicle {
private:
int pnum; // 载人数量
double qnum; // 载重量
public:
Truck(Date d, double p, int n, double q) : Vehicle(d, p) {
pnum = n;
qnum = q;
}
void show() {
cout << "Truck Information:" << endl;
Vehicle::show();
cout << "Passenger Capacity: " << pnum << endl;
cout << "Load Capacity: " << qnum << " tons" << endl;
}
};
int main() {
Date d1(2021, 10, 1);
Car c1(d1, 20000, 4);
Date d2(2021, 10, 10);
Truck t1(d2, 50000, 2, 5);
c1.show();
cout << endl;
t1.show();
return 0;
}
```
输出结果:
```
Car Information:
Manufacture Date: 2021-10-1
Price: $20000
Passenger Capacity: 4
Truck Information:
Manufacture Date: 2021-10-10
Price: $50000
Passenger Capacity: 2
Load Capacity: 5 tons
```
Write a complete program and define a Bankbook class (Bankbook). Its data members include: account (unsigned type), name (string type), balance (double type). Bankbook class derives the credit card class (Creditaccount). The Bankbook class can realize the operations of opening an account, deposit, withdraw and check balance. The withdraw must be less than the balance; Creditaccount class modify the withdraw operation, allowing overdraft of 2000 yuan. In the main function: 1) The main function includes Bankbook and Creditaccount header files; 2) Define the objects of these two classes and initialize them; 3) Simulate 5 people to the bank for passbook operation, 2 of them also carry out credit card transaction operation, output the account number, name and balance after these operations.
Here's the complete program that defines the Bankbook and Creditaccount classes and simulates passbook and credit card operations for 5 people:
```
#include <iostream>
#include <string>
using namespace std;
class Bankbook {
public:
unsigned int account;
string name;
double balance;
Bankbook(unsigned int acc, string n, double bal) {
account = acc;
name = n;
balance = bal;
}
void deposit(double amount) {
balance += amount;
cout << "Deposit successful. Current balance: " << balance << endl;
}
void withdraw(double amount) {
if (amount > balance) {
cout << "Withdrawal failed. Insufficient balance." << endl;
} else {
balance -= amount;
cout << "Withdrawal successful. Current balance: " << balance << endl;
}
}
void check_balance() {
cout << "Current balance: " << balance << endl;
}
};
class Creditaccount : public Bankbook {
public:
Creditaccount(unsigned int acc, string n, double bal) : Bankbook(acc, n, bal) {}
void withdraw(double amount) {
if ((balance + 2000) < amount) {
cout << "Withdrawal failed. Insufficient credit limit." << endl;
} else {
balance -= amount;
cout << "Withdrawal successful. Current balance: " << balance << endl;
}
}
};
int main() {
Bankbook b1(1001, "Alice", 5000);
Creditaccount c1(2001, "Bob", 8000);
Bankbook b2(1002, "Charlie", 3000);
Creditaccount c2(2002, "Dave", 6000);
Bankbook b3(1003, "Eve", 4000);
b1.deposit(1000);
b1.withdraw(500);
b2.deposit(2000);
c1.withdraw(10000);
c2.withdraw(5000);
cout << "Account\tName\tBalance" << endl;
cout << b1.account << "\t" << b1.name << "\t" << b1.balance << endl;
cout << c1.account << "\t" << c1.name << "\t" << c1.balance << endl;
cout << b2.account << "\t" << b2.name << "\t" << b2.balance << endl;
cout << c2.account << "\t" << c2.name << "\t" << c2.balance << endl;
cout << b3.account << "\t" << b3.name << "\t" << b3.balance << endl;
return 0;
}
```
In the `Bankbook` class, we have functions for depositing, withdrawing and checking balance. The `Creditaccount` class derives from the `Bankbook` class and modifies the `withdraw` function to allow an overdraft of 2000 yuan.
In the `main` function, we create objects of both classes and perform operations such as depositing, withdrawing and checking balance. We output the account number, name and balance after the operations are completed for each person.
阅读全文