第一题
package united6.test1;
public class MobilPhone {
public String brand;
public MobilPhone() {
this.brand = "诺基亚";
}
public MobilPhone(String bra) {
this.brand = bra;
}
public String buy() {
return "没发工资,买一个" + brand + "牌子的手机吧!";
}
public String buy(String reason) {
return reason + ",快买一个" + brand + "牌子的手机吧!";
}
}
//以上为MobilPhone类定义代码
下为调用代码
package united6.test1;
public class MobilPhoneTest {
public static void main(String[] args) {
MobilPhone mp = new MobilPhone();
mp.brand = "苹果";
String details=mp.buy("发工资了");
System.out.println(details);
}
}
运行结果
第二题
下为定义类代码
package unit6.test2;
public class Calculation {
static int year;
static double a; //利率
static double b; //每月还贷金额
static double c; //总利息
public double i(double i, int yearchice) {
if (yearchice == 1) {
a = 0.0603;
year=36;
c=i*a;
}
else if(yearchice==2){
a=0.0612;
year=60;
c=i*a;
}
else if(yearchice==3){
a=0.0639;
year=240;
c=i*a;
}
b=(i+c)/year;
System.out.println("月供为:"+b);
return b;
}
}
下为调用类代码
package unit6.test2;
import java.util.Scanner;
public class two {
public static void main(String[] args) {
Calculation moey = new Calculation();
Scanner input = new Scanner(System.in);
System.out.println("请输入贷款金额::");
double i = input.nextDouble();
System.out.println("/请选择贷款年限:1.3年(36个月)\t2.5年(60个月)\t3.20年(240个月);");
int n = input.nextInt();
moey.i(i, n);
}
}
运行结果
第三题
下为定义类代码
package unit6.test3;
public class Triangle {
public boolean isTriangle(int a, int b, int c) {
boolean flag = false;
if ((a + b > c) && (a + c > b) && (b + c > a)) {
flag = true;
}
return flag;
}
public String shape(int a, int b, int c) {
String shape = "";
if (a + b <= c || a + c <= b || c + b <= a) {
System.out.println("不能构成三角形");
} else if (a == b && b == c) {
shape = "等边三角形";
System.out.println("是" + shape);
} else if (a * a == b * b + c * c || b * b == a * a + c * c || c * c == a * a + b * b) {
shape = "直角三角形";
System.out.println("是" + shape);
} else if (a * a > b * b + c * c || b * b > a * a + c * c || c * c > a * a + b * b) {
shape = "钝角三角形";
System.out.println("是" + shape);
} else {
shape = "锐角三角形";
System.out.println("是" + shape);
}
return shape;
}
}
下为调用代码
package unit6.test3;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
boolean i=true;
Triangle t=new Triangle();
String m="";
Scanner input = new Scanner(System.in);
do{
System.out.println("输入第一个边:");
int a = input.nextInt();
System.out.println("输入第二个边:");
int b= input.nextInt();
System.out.println("输入第三个边:");
int c = input.nextInt();
t.isTriangle(a, b, c);
t.shape(a, b, c);
System.out.println("继续吗?(y/n)");
m= input.next();
if (m.equals("n")) {
System.out.println("已输出结果,结束。");
break;
}
}while (m.equals("y"));
}
}
运行结果