求 类与对象编程 的答案
答案:2 悬赏:70 手机版
解决时间 2021-06-01 06:45
- 提问者网友:焚苦与心
- 2021-05-31 22:41
1、定义一个三角形类Ctriangle,求三角形的面积和周长
2、定义一个点类Point,并定义成员函数double Distance(const& Piont)
3、设计一个学生类,包含学生学号、姓名、课程、成绩等基本信息,计算学生的平均成绩
要能在编译器上运行的啊
急!!!!
最佳答案
- 五星知识达人网友:风格不统一
- 2021-05-31 23:53
是java语言吗?
public double Ctriangle(doubble a,double,b,double c,){
double c=a*b/2 //面积
double c=a+b+c; //周长
}
全部回答
- 1楼网友:行雁书
- 2021-06-01 01:10
是负数类还是复数类?
public class Test {
public static void main(String[] args) {
Patient april=new Patient();
april.setName("ZhangLi");
april.setSex('f');
april.setAge(33);
april.setWeight(154.72f);
april.setAllergies(true);
System.out.println("Name: "+april.getName());
System.out.println("Sex: "+april.getSex());
System.out.println("Age: "+april.getAge());
System.out.println("Weight: "+april.getWeight());
System.out.println("Allergies: "+april.getAllergies());
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
System.out.println(c1.add(c2));
System.out.println(c1.subtract(c2));
System.out.println(c1.multiply(c2));
System.out.println(c1.divide(c2));
}
}
class Patient {
private String name;
private char sex;
private int age;
private float weight;
private boolean allergies;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex == 'M' ? sex : 'F';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = Math.max(1, age);
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = Math.max(0.0f, weight);
}
public boolean getAllergies() {
return allergies;
}
public void setAllergies(boolean allergies) {
this.allergies = allergies;
}
}
class Complex {
private double realPart;
private double imaginaryPart;
public Complex(double real, double imaginary) {
this.realPart = real;
this.imaginaryPart = imaginary;
}
public Complex add(Complex other) {
return new Complex(this.realPart + other.realPart,
this.imaginaryPart + other.imaginaryPart);
}
public Complex subtract(Complex other) {
return new Complex(this.realPart - other.realPart,
this.imaginaryPart - other.imaginaryPart);
}
public Complex multiply(Complex other) {
return new Complex(this.realPart * other.realPart -
this.imaginaryPart * other.imaginaryPart,
this.realPart * other.imaginaryPart +
this.imaginaryPart * other.realPart);
}
public Complex divide(Complex other) {
if (other.realPart == 0 && other.imaginaryPart == 0) {
throw new ArithmeticException("Divided By 0");
}
if (this.realPart == 0 && this.imaginaryPart == 0) {
return this;
}
Complex numerator = this.multiply(new Complex(other.realPart,
-other.imaginaryPart));
double denominator = other.realPart * other.realPart +
other.imaginaryPart * other.imaginaryPart;
return new Complex(numerator.realPart / denominator,
numerator.imaginaryPart / denominator);
}
public String toString() {
StringBuilder sb = new StringBuilder();
if (this.realPart != 0) {
sb.append(this.realPart);
}
if (this.imaginaryPart > 0) {
sb.append(" + ").
append(this.imaginaryPart).
append("i");
} else if (this.imaginaryPart < 0) {
sb.append(" - ").
append(Math.abs(this.imaginaryPart)).
append("i");
}
return sb.toString();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯