JAVA 两道编程小题(赶)

2024-12-01 03:29:00
推荐回答(1个)
回答1:

import java.util.Scanner;

public class Rectangle{//长方体类
private double length;//长
private double width;//宽

public Rectangle(double length,double width){
this.setLength(length);
this.setWidth(width);
}

public double calculateArea(){//计算面积
return length*width;
}

public double getLength() {
return length;
}

public void setLength(double length) {
this.length = length;
}

public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}

public static void main(String[] args){//测试方法
Scanner sc = new Scanner(System.in);
System.out.print("请输入土地的长度");
double length = sc.nextDouble();
System.out.print("请输入土地的宽度");
double width = sc.nextDouble();
Rectangle rec = new Rectangle(length,width);
System.out.println("您土地的面积为:"+rec.calculateArea());

System.out.print("请输入长方体的长度");
double len = sc.nextDouble();
System.out.print("请输入长方体的宽度");
double wid = sc.nextDouble();
System.out.print("请输入长方体的高度");
double hei = sc.nextDouble();
Cuboid cu = new Cuboid(len,wid,hei);
System.out.println("长方体的表面积为:"+cu.calCuboidArea());
System.out.println("长方体的体积为:"+cu.calculateVolume());
}

}

class Cuboid extends Rectangle{//长方体类
private double height;
public Cuboid(double length, double width,double height) {
super(length, width);
this.setHeight(height);
}

public double calculateVolume(){//计算体积
return super.calculateArea()*height;
}

public double calCuboidArea(){//计算表面积
double length = super.getLength();
double width = super.getWidth();
double area = (length*width+length*height+width+height)*2;
return area;
}

public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}

}
**********************第二题****************************
import java.util.Scanner;

public class ComplexNumber {
protected double realPart;
protected double imaginPart;

public ComplexNumber() {
realPart = 0.0;
imaginPart = 0.0;
}

public ComplexNumber(double r, double i) {
realPart = r;
imaginPart = i;
}

public void setReal(double r) {
realPart = r;
}

public void setImagin(double i) {
imaginPart = i;
}

public double getReal() {
return realPart;
}

public double getImagin() {
return imaginPart;
}

public String toString() { //重写toString方法,方便测试
return String.valueOf(realPart + " + " + imaginPart + "i");
}

public static ComplexNumber complexAdd(ComplexNumber num1,ComplexNumber num2) {//复数相加
ComplexNumber result = new ComplexNumber();
result.setReal(num1.getReal() + num2.getReal());
result.setImagin(num1.getImagin() + num2.getImagin());
return result;
}

public static ComplexNumber complexMinus(ComplexNumber num1,ComplexNumber num2) {//复数相减num1-num2
ComplexNumber result = new ComplexNumber();
result.setReal(num1.getReal() - num2.getReal());
result.setImagin(num1.getImagin() - num2.getImagin());
return result;
}
public static ComplexNumber complexMulti(ComplexNumber num1,ComplexNumber num2) {//复数相乘
ComplexNumber result = new ComplexNumber();
double realTemp = 0.0;
double imaginTemp = 0.0;
realTemp = num1.getReal() * num2.getReal() - num1.getImagin()* num2.getImagin();
imaginTemp = num1.getReal() * num2.getImagin() + num1.getImagin()* num2.getReal();
result.setReal(realTemp);
result.setImagin(imaginTemp);
return result;
}

public static ComplexNumber complexDiv(ComplexNumber num1,ComplexNumber num2) {//复数相除num1/num2
ComplexNumber result = new ComplexNumber();
double realTemp = 0.0;
double imaginTemp = 0.0;
double div = 0.0;
div = Math.pow(num2.getReal(), 2) + Math.pow(num2.getImagin(), 2);
realTemp = num1.getReal() * num2.getReal() + num1.getImagin()* num2.getImagin();
imaginTemp = num2.getReal() * num1.getImagin() - num1.getReal()* num2.getImagin();
result.setReal(realTemp / div);
result.setImagin(imaginTemp / div);
return result;
}

public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("请输入复数num1的实数部分");
double real1 = sc.nextDouble();
System.out.print("请输入复数num1的虚数部分");
double imagin1 = sc.nextDouble();
ComplexNumber num1 = new ComplexNumber(real1,imagin1);
System.out.println("实数num1为:"+num1.toString());

System.out.print("请输入复数num2的实数部分");
double real2 = sc.nextDouble();
System.out.print("请输入复数num2的虚数部分");
double imagin2 = sc.nextDouble();
ComplexNumber num2 = new ComplexNumber(real2,imagin2);
System.out.println("实数num2为:"+num1);

System.out.println("num1 + num2 = "+ComplexNumber.complexAdd(num1, num2));
System.out.println("num1 - num2 = "+ComplexNumber.complexMinus(num1, num2));
System.out.println("num1 * num2 = "+ComplexNumber.complexMulti(num1, num2));
System.out.println("num1 / num2 = "+ComplexNumber.complexDiv(num1, num2));
}
}