第十次作业
题目1:
编写一个应用程序,模拟中介和购房者完成房屋购买过程。
interface Business{ double RATIO = 0.022; double TAX = 0.03; void buying(double price); } class Buyer implements Business{ String name; public void buying(double price) { System.out.println(name+"购买一套标价为"+price+"元的住宅"); } } class Intermediary implements Business{ Buyer buyer; Intermediary(Buyer buyer){ this.buyer=buyer; } public void buying(double price) { charing(price); } public void charing(double price) { System.out.println("房屋中介费:"+price*RATIO+"交纳的契税"+price*TAX); } } public class zuoyeb { public static void main(String[] args) { // TODO Auto-generated method stub Scanner reader=new Scanner(System.in); Buyer b=new Buyer(); b.name="Lisa"; double price=reader.nextDouble(); b.buying(price); Intermediary i=new Intermediary(b); i.buying(price); } }
题目2:
输入5个数,代表学生成绩,计算其平均成绩。当输入值为负数或大于100时,通过自定义异常处理进行提示。
package zuoyea;
import java.util.Scanner;
class yichang extends Exception{
yichang(String x){
super(x);
}
}
public class zy {
import java.util.Scanner;
class yichang extends Exception{
yichang(String x){
super(x);
}
}
public class zy {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader=new Scanner(System.in);
double sum=0;
try
{
for(int i=1;i<=5;i++)
{
System.out.println("请输入第"+i+"学生的成绩");
double x=reader.nextDouble();
if(x<0||x>100)
throw new yichang("输入非法值");
sum+=x;
System.out.println("累加和"+sum);
}
System.out.println("平均值:"+sum/5);
}
catch(yichang y)
{
System.out.println(y.toString());
}
}
// TODO Auto-generated method stub
Scanner reader=new Scanner(System.in);
double sum=0;
try
{
for(int i=1;i<=5;i++)
{
System.out.println("请输入第"+i+"学生的成绩");
double x=reader.nextDouble();
if(x<0||x>100)
throw new yichang("输入非法值");
sum+=x;
System.out.println("累加和"+sum);
}
System.out.println("平均值:"+sum/5);
}
catch(yichang y)
{
System.out.println(y.toString());
}
}
}