JAVA 两道编程小题(赶)

1、编程实现矩形类,其中应包括计算矩形周长和面积的方法。(1)使用矩形类,编程统计若干块土地的相关信息。由用户输入每块土地的长与宽(自己确定),程序将计算其面积并显示出来... 1、编程实现矩形类,其中应包括计算矩形周长和面积的方法。
(1)使用矩形类,编程统计若干块土地的相关信息。由用户输入每块土地的长与宽(自己确定),程序将计算其面积并显示出来。
(2)从矩形类派生一个子类:长方体类。对长方体类的操作是求表面积和体积。编程检查、运行所编写的长方体类。
2、编程实现复数类,实现复数的四则运算。并编写程序,测试所写的复数类。

两题很简单的,要绝对原创的。不然百度知道吧里我自己也搜索的到答案,尤其是第二题我知道网上很多。。我最近没时间学JAVA所以暂时做不了,大家帮忙下。做完能运行后发我邮箱里:llldyr@yahoo.com.cn
好的话追加50分~~~~~~~绝对。今晚之前。。
发到我邮箱,老乡哦
展开
 我来答
kysdvnm
2009-10-15 · TA获得超过381个赞
知道小有建树答主
回答量:460
采纳率:0%
帮助的人:0
展开全部
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));
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式