1.用面向对象的思想编写一个程序,求两个复数的和。
package gaomengna2;
import java.util.Scanner;
class Complex {
private double real;
private double imag;
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public double getReal() {
return real;
}
public double getImag() {
return imag;
}
public Complex add(Complex other) {
double realSum = this.real + other.real;
double imagSum = this.imag + other.imag;
return new Complex(realSum, imagSum);
}
@Override
public String toString() {
return real + " + " + imag + "i";
}
}
public class _2home1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个复数的实部:");
double real1 = scanner.nextDouble();
System.out.print("请输入第一个复数的虚部:");
double imag1 = scanner.nextDouble();
System.out.print("请输入第二个复数的实部:");
double real2 = scanner.nextDouble();
System.out.print("请输入第二个复数的虚部:");
double imag2 = scanner.nextDouble();
Complex complex1 = new Complex(real1, imag1);
Complex complex2 = new Complex(real2, imag2);
Complex sum = complex1.add(complex2);
System.out.println("两个复数的和为:" + sum);
}
}
2.用面向对象的思想编写一个应用程序,该程序有两个类:Tank类和Fight类。
要求:1.模拟坦克加速、减速
2.给定炮弹的数量,模拟开炮行为。
3.在主类Fight的main方法中创建坦克,并让坦克调用方法设置炮弹的数量,显示加速、减速和开炮行为。
package gaomengna2;
class Tank {
private int speed;
private int bulletCount;
public Tank(int speed, int bulletCount) {
this.speed = speed;
this.bulletCount = bulletCount;
}
public void accelerate() {
speed += 5;
System.out.println("坦克加速,当前速度:" + speed);
}
public void decelerate() {
speed -= 5;
System.out.println("坦克减速,当前速度:" + speed);
}
public void setBulletCount(int bulletCount) {
this.bulletCount = bulletCount;
System.out.println("设置炮弹数量为:" + bulletCount);
}
public void fire() {
if (bulletCount > 0) {
System.out.println("发射炮弹!剩余炮弹数量:" + --bulletCount);
} else {
System.out.println("炮弹已经用尽,无法开炮!");
}
}
}
public class Fight {
public static void main(String[] args) {
Tank tank = new Tank(20, 10);
tank.accelerate();
tank.decelerate();
tank.setBulletCount(15);
tank.fire();
tank.fire();
tank.fire();
}
}
3.编写一个Java应用程序,模拟在计算机中放入光盘,即计算机将CD类型的对象作为自己的一个成员变量。具体要求如下。
(1)有三个源文件:Computer.java、CD.java和User.java,其中CD.java中的CD类负责创建光盘对象。Computer.java中的Computer类负责创建计算机对象。User.java中的User类是主类。
(2)在主类的main方法中首先使用CD类创建一个对象dataCD,然后使用Computer类再创建一个对象computerIMB,computerIMB对象将dataCD的引用传递给computerIMB对象的成员变量includeCD。
CD.java
package gaomengna2;
public class CD {
private String content;
public CD(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
Computer.java
package gaomengna2;
public class Computer {
private CD includeCD;
public void insertCD(CD cd) {
includeCD = cd;
System.out.println("光盘已插入计算机。");
}
public void readCD() {
if (includeCD != null) {
System.out.println("计算机正在读取光盘内容: " + includeCD.getContent());
} else {
System.out.println("没有插入光盘。");
}
}
}
User.java
package gaomengna2;
public class User {
public static void main(String[] args) {
CD dataCD = new CD("这是光盘的内容");
Computer computerIMB = new Computer();
computerIMB.insertCD(dataCD);
computerIMB.readCD();
}
}