JAVA高手来帮帮忙啦,两道编程题。悬赏30分。在线等!!!
编程题两道,编完回复前请验证下,我也会验证,验证正确分就是你的了。比较急!!!根据下面的要求编程实现复数类Complex。(1)复数类Complex具有如下属性:real...
编程题两道,编完回复前请验证下,我也会验证,验证正确分就是你的了。比较急!!!
根据下面的要求编程实现复数类Complex。
(1)复数类Complex具有如下属性:real代表复数的实数部分;imagin 代表复数的虚数部分
(2)复数类Complex的方法:
Complex():构造函数,将实部、虚部都置为0;
Complex(double r,double i):构造函数,创建复数对象的同时完成复数的实部、虚部的初始化,r 为实部的初值,i 为虚部的初值;
getReal():获得复数对象的实部;getImagin():获得复数对象的虚部;
complexAdd(Complex Number):当前复数对象与形参复数对象相加,所得的结果也是复数值,返回给此方法的调用者;
complexMinus(Complex Number): 当前复数对象与形参复数对象相减,所得的结果也是复数值,返回给此方法的调用者;
complexMulti(Complex Number):当前复数对象与形参复数对象相乘,所得的结果也是复数值,返回给此方法的调用者;
toString():把当前复数对象的实部、虚部组合成a+bi的字符串形式,其中a 和b 分别为实部和虚部的数值。
利用多态性编程,实现求三角形、正方形和圆形的面积。方法:抽象出一个共享父类,定义一函数为求面积的公共接口,再重新定义各形状的求面积函数。在主类中创建不同类的对象,并求得不同形状的面积 展开
根据下面的要求编程实现复数类Complex。
(1)复数类Complex具有如下属性:real代表复数的实数部分;imagin 代表复数的虚数部分
(2)复数类Complex的方法:
Complex():构造函数,将实部、虚部都置为0;
Complex(double r,double i):构造函数,创建复数对象的同时完成复数的实部、虚部的初始化,r 为实部的初值,i 为虚部的初值;
getReal():获得复数对象的实部;getImagin():获得复数对象的虚部;
complexAdd(Complex Number):当前复数对象与形参复数对象相加,所得的结果也是复数值,返回给此方法的调用者;
complexMinus(Complex Number): 当前复数对象与形参复数对象相减,所得的结果也是复数值,返回给此方法的调用者;
complexMulti(Complex Number):当前复数对象与形参复数对象相乘,所得的结果也是复数值,返回给此方法的调用者;
toString():把当前复数对象的实部、虚部组合成a+bi的字符串形式,其中a 和b 分别为实部和虚部的数值。
利用多态性编程,实现求三角形、正方形和圆形的面积。方法:抽象出一个共享父类,定义一函数为求面积的公共接口,再重新定义各形状的求面积函数。在主类中创建不同类的对象,并求得不同形状的面积 展开
展开全部
第一题:
public class Complex {
private double real;// 实部
private double imagin;// 虚部
// 重载默认构造方法
public Complex() {
real = 0;
imagin = 0;
}
// 有参构造方法
public Complex(double real, double imagin) {
this.real = real;
this.imagin = imagin;
}
// 获取实部
public double getReal() {
return real;
}
// 获取虚部
public double getImagin() {
return imagin;
}
// 虚数相加
public Complex complexAdd(Complex number) {
return new Complex(this.getReal() + number.getReal(), this.getImagin()
+ number.getImagin());
}
// 虚数想减
public Complex complexMinus(Complex number) {
return new Complex(this.getReal() - number.getReal(), this.getImagin()
- number.getImagin());
}
// 虚数想乘
public Complex complexMulti(Complex number) {
return new Complex(this.getReal() * number.getReal(), this.getImagin()
* number.getImagin());
}
//重载toString
public String toString() {
return ""+real+"+"+imagin+"i";
}
public static void main(String[] args) {
//测试桩
Complex c1 = new Complex(2,3);
Complex c2 = new Complex(4,6);
//测试加法
System.out.println(c1.complexAdd(c2));
//减法
System.out.println(c1.complexMinus(c2));
//乘法
System.out.println(c1.complexMulti(c2));
//toString验证
Complex c = new Complex(2,3);
System.out.println(c);
}
}
第二题:
1. 抽象父类Shape:
//父类接口
public abstract class Shape {
public double getSquare(){
return 0;
}
}
2. 三角形子类
public class Triangle extends Shape {
private double a;// 三角形底边长度
private double h;// 三角形的高
// 构造方法
public Triangle(double a, double h) {
this.a = a;
this.h = h;
}
// 三角形面积
public double getSquare() {
return a * h / 2;
}
}
3.正方形子类
public class Square extends Shape {
private double a;// 正方形边长
// 构造方法
public Square(double a) {
this.a = a;
}
public double getSquare() {
return a*a;
}
}
3.圆形子类
public class Circle extends Shape {
public static final double PI = 3.14;
private double r;// 圆形半径
// 构造方法
public Circle(double r) {
this.r = r;
}
public double getSquare() {
return PI*r*r;
}
}
4. 测试桩主方法:
//测试桩主方法
public class TestShape {
public static void main(String[] args) {
//三角形实现
Triangle t = new Triangle(2,2);
System.out.println(t.getSquare());
//正方形实现
Square s = new Square(3);
System.out.println(s.getSquare());
//圆形实现
Circle c = new Circle(4);
System.out.println(c.getSquare());
}
}
public class Complex {
private double real;// 实部
private double imagin;// 虚部
// 重载默认构造方法
public Complex() {
real = 0;
imagin = 0;
}
// 有参构造方法
public Complex(double real, double imagin) {
this.real = real;
this.imagin = imagin;
}
// 获取实部
public double getReal() {
return real;
}
// 获取虚部
public double getImagin() {
return imagin;
}
// 虚数相加
public Complex complexAdd(Complex number) {
return new Complex(this.getReal() + number.getReal(), this.getImagin()
+ number.getImagin());
}
// 虚数想减
public Complex complexMinus(Complex number) {
return new Complex(this.getReal() - number.getReal(), this.getImagin()
- number.getImagin());
}
// 虚数想乘
public Complex complexMulti(Complex number) {
return new Complex(this.getReal() * number.getReal(), this.getImagin()
* number.getImagin());
}
//重载toString
public String toString() {
return ""+real+"+"+imagin+"i";
}
public static void main(String[] args) {
//测试桩
Complex c1 = new Complex(2,3);
Complex c2 = new Complex(4,6);
//测试加法
System.out.println(c1.complexAdd(c2));
//减法
System.out.println(c1.complexMinus(c2));
//乘法
System.out.println(c1.complexMulti(c2));
//toString验证
Complex c = new Complex(2,3);
System.out.println(c);
}
}
第二题:
1. 抽象父类Shape:
//父类接口
public abstract class Shape {
public double getSquare(){
return 0;
}
}
2. 三角形子类
public class Triangle extends Shape {
private double a;// 三角形底边长度
private double h;// 三角形的高
// 构造方法
public Triangle(double a, double h) {
this.a = a;
this.h = h;
}
// 三角形面积
public double getSquare() {
return a * h / 2;
}
}
3.正方形子类
public class Square extends Shape {
private double a;// 正方形边长
// 构造方法
public Square(double a) {
this.a = a;
}
public double getSquare() {
return a*a;
}
}
3.圆形子类
public class Circle extends Shape {
public static final double PI = 3.14;
private double r;// 圆形半径
// 构造方法
public Circle(double r) {
this.r = r;
}
public double getSquare() {
return PI*r*r;
}
}
4. 测试桩主方法:
//测试桩主方法
public class TestShape {
public static void main(String[] args) {
//三角形实现
Triangle t = new Triangle(2,2);
System.out.println(t.getSquare());
//正方形实现
Square s = new Square(3);
System.out.println(s.getSquare());
//圆形实现
Circle c = new Circle(4);
System.out.println(c.getSquare());
}
}
展开全部
public class Complex {
private double real;
private double imagin;
public Complex(){
this.real=0;
this.imagin=0;
}
public Complex(double r,double i){
this.real=r;
this.imagin=i;
}
public double getReal(){
return this.real;
}
public double getImagin(){
return this.imagin;
}
public Complex complexAdd(Complex Number){
this.real+=Number.real;
this.imagin+=Number.imagin;
return this;
}
public Complex complexMinus(Complex Number){
this.real-=Number.real;
this.imagin-=Number.imagin;
return this;
}
public Complex complexMulti(Complex Number){
this.real=this.real*Number.real-this.imagin*Number.imagin;
this.imagin=this.real*Number.imagin+this.imagin*Number.real;
return this;
}
public String toString(){
return this.real+"+"+this.imagin+"i";
}
}
******************************************************第二题********************************************************
interface Method{
public double getArea();
}
abstract class Type implements Method{
}
class Triangle extends Type{
private double width;
private double height;
public Triangle(double w,double h){
this.width=w;
this.height=h;
}
public double getArea() {
return 1/2*width*height;
}
}
class Square extends Type{
private double edge;
public Square(double e){
this.edge=e;
}
public double getArea() {
return this.edge*this.edge;
}
}
class Round extends Type{
private double radius;
public Round(double r){
this.radius=r;
}
public double getArea() {
return Math.PI*this.radius*this.radius;
}
}
public class TypeTest{
public static void main(String[] args) {
Type triangle=new Triangle(3.3,5.7);
Type square=new Square(5.3);
Type round=new Round(2.5);
System.out.println(triangle.getArea());
System.out.println(square.getArea());
System.out.println(round.getArea());
}
}
private double real;
private double imagin;
public Complex(){
this.real=0;
this.imagin=0;
}
public Complex(double r,double i){
this.real=r;
this.imagin=i;
}
public double getReal(){
return this.real;
}
public double getImagin(){
return this.imagin;
}
public Complex complexAdd(Complex Number){
this.real+=Number.real;
this.imagin+=Number.imagin;
return this;
}
public Complex complexMinus(Complex Number){
this.real-=Number.real;
this.imagin-=Number.imagin;
return this;
}
public Complex complexMulti(Complex Number){
this.real=this.real*Number.real-this.imagin*Number.imagin;
this.imagin=this.real*Number.imagin+this.imagin*Number.real;
return this;
}
public String toString(){
return this.real+"+"+this.imagin+"i";
}
}
******************************************************第二题********************************************************
interface Method{
public double getArea();
}
abstract class Type implements Method{
}
class Triangle extends Type{
private double width;
private double height;
public Triangle(double w,double h){
this.width=w;
this.height=h;
}
public double getArea() {
return 1/2*width*height;
}
}
class Square extends Type{
private double edge;
public Square(double e){
this.edge=e;
}
public double getArea() {
return this.edge*this.edge;
}
}
class Round extends Type{
private double radius;
public Round(double r){
this.radius=r;
}
public double getArea() {
return Math.PI*this.radius*this.radius;
}
}
public class TypeTest{
public static void main(String[] args) {
Type triangle=new Triangle(3.3,5.7);
Type square=new Square(5.3);
Type round=new Round(2.5);
System.out.println(triangle.getArea());
System.out.println(square.getArea());
System.out.println(round.getArea());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询