
正文
java分支案例源代码 分析java代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用JAVA 编程的源代码
直接定义两个接口学生接口里面定义一个学费的变量,老师接口里面定义一个工资变量,
eclipse里面可以自动帮你添加相关变量的getterhe
setter方法的。
相关问答
Q1: 求大师写一下Java代码源代码
package Project;
import java.util.Scanner;
public class Complex {
int x;
int y;
Complex(){
x=0;
y=0;
}
Complex(int i,int j){
x=i;
y=j;
}
//显示
public void showComp(){
if(y0){
System.out.print(x+"+"+y+"i"+'\t');
}
if(y0){
System.out.print(x+""+y+"i"+'\t');
}
if(y==0){
System.out.print(x+""+'\t');
}
}
//求和
public static Complex addComp(Complex C1,Complex C2){
Complex newComplex=new Complex();
newComplex.x=C1.x+C2.x;
newComplex.y=C1.y+C2.y;
System.out.println();
System.out.print("和为:");
return newComplex;
}
//求差
public static Complex subComp(Complex C1,Complex C2){
Complex newComplex=new Complex();
newComplex.x=C1.x-C2.x;
newComplex.y=C1.y-C2.y;
System.out.println();
System.out.print("差为:");
return newComplex;
}
//求积
public static Complex multiComp(Complex C1,Complex C2){
Complex newComplex=new Complex();
newComplex.x=C1.x*C2.x-C1.y*C2.y;
newComplex.y=C1.x*C2.y+C1.y*C2.x;
System.out.println();
System.out.print("积为:");
return newComplex;
}
//是否相等
public static boolean equalComp(Complex C1,Complex C2){
System.out.println();
System.out.print("是否相等:");
return ((C1.x==C2.x)(C1.y==C2.y));
}
public static void main(String[] args){
Scanner data=new Scanner(System.in);
int x=data.nextInt();
int y=data.nextInt();
int m=data.nextInt();
int n=data.nextInt();
Complex Comp1=new Complex(x,y);
Complex Comp2=new Complex(m,n);
data.close();
//显示
Comp1.showComp();
Comp2.showComp();
Complex.addComp(Comp1,Comp2).showComp(); //显示 和
Complex.subComp(Comp1, Comp2).showComp(); //显示 差
Complex.multiComp(Comp1, Comp2).showComp(); //显示 积
System.out.println( Complex.equalComp(Comp1, Comp2));
}
}
Q2: Java100行以上源代码,至少五个class以及一个interface,可以简单点?
下面是一个可能的Java源代码,它包含了一个接口(Shape)和五个类(Circle, Rectangle, Triangle, Square 和 Main)。它的功能是计算不同形状的面积和周长。
//定义一个接口Shape,有两个抽象方法:getArea()和getPerimeter()interface Shape { double getArea(); double getPerimeter();
}//定义一个类Circle,实现Shape接口class Circle implements Shape { //定义一个私有属性radius,表示圆的半径
private double radius; //定义一个公有构造方法,用于初始化radius
public Circle(double radius) { this.radius = radius;
} //实现getArea()方法,返回圆的面积
public double getArea() { return Math.PI * radius * radius;
} //实现getPerimeter()方法,返回圆的周长
public double getPerimeter() { return Math.PI * radius * 2;
}
}//定义一个类Rectangle,实现Shape接口class Rectangle implements Shape { //定义两个私有属性width和height,表示矩形的宽度和高度
private double width; private double height; //定义一个公有构造方法,用于初始化width和height
public Rectangle(double width, double height) { this.width = width; this.height = height;
} //实现getArea()方法,返回矩形的面积
public double getArea() { return width * height;
} //实现getPerimeter()方法,返回矩形的周长
public double getPerimeter() { return (width + height) *2;
}
}//定义一个类Triangle,实现Shape接口class Triangle implements Shape { //定义三个私有属性a,b,c表示三角形的三条边长
private double a; private double b; private double c; //定义一个公有构造方法,用于初始化a,b,c,并检查是否满足三角形条件(任意两边之和大于第三边)
public Triangle(double a, double b, double c) throws Exception{ if (a + b c a + c b b + c a) {
this.a = a; this.b = b;
this.c = c;
} else {
throw new Exception("Invalid triangle");
}
} //实现getArea()方法,返回三角形的面积(使用海伦公式)
public double getArea() { //计算半周长p
double p = (a + b + c) /2; //计算并返回面积s(使用Math.sqrt()函数求平方根)
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
} //实现getPerimeter()方法,返回三角形的周长
public double getPerimeter(){ return a + b + c;
}
}//定义一个类Square,继承Rectangle类,并重写构造方法和toString()方法class Square extends Rectangle { //重写构造方法,在调用父类构造方法时传入相同的参数side作为width和height
public Square(double side){ super(side, side);
} //重写toString()方法,在原来基础上加上"Square:"前缀,并只显示side属性而不显示width和height属性(使用String.format()函数格式化字符串)
@Override
public String toString(){ return String.format("Square: side=%.2f", super.width); /* 或者直接使用super.getPerimeter()/4作为side */
/* return String.format("Square: side=%.2f", super.getPerimeter()/4); */
/* 注意:不能直接访问super.side属性,
java分支案例源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于分析java代码、java分支案例源代码的信息别忘了在本站进行查找喔。






