编写3个类的Java应用程序,要求3个类分别在3个源文件中。 *一个源文件包含有名字为Teacher的类,该类有两个方法,
double add(double a,double b){
return a+b;}
double sub (double a,double b){
return a-b;}
一个源文件包含有名字为Student的类,有一个方法
void speak() {
System.out.println(“老师好”);}
一个源文件包含名字为MainClass的主类。要求在主类的main方法中分别调用Teacher和Student类创建对象,使得程序输出12和236的和以及234和120的差,并输出“老师好”。
Teacher类:
public class Teacher{
double a,b;
double add(){
return a+b;
}
double sub(){
return a-b;
}
}
Student类
public class Student {
void speak() {
System.out.println("老师好");
}
}
Main类
public class MainClass {
public static void main (String args[]) {
Teacher a,b;
a = new Teacher();
a.a = 12;
a.b = 236;
b = new Teacher();
b.a = 234;
b.a = 120;
Student c;
c = new Student();
double d,e;
d = a.add();
System.out.println("12+236="+d);
e = b.sub();
System.out.println("234-120"+e);
c.speak();
}
}