实验三 接口、内部类、异常类、实用类
一、实验目的
1.掌握用类怎样实现接口;
2.掌握内部类用法;
3.掌握使用try-catch语句;
4.掌握String类的常用方法;
5.掌握使用Scanner类的对象从字符串中解析程序所需要的数据;
6.掌握Date类以及Calendar类常用方法;
二、实验内容
1.编写一个Java应用程序,计算全班同学的平均成绩。
2.编写一个Java应用程序,模拟内部购物券。
3.编写模拟设备发现危险品。
4.编写一个Java应用程序,判断简历中的姓名、出生日期、个人网站、身高体重是否正确。
5.编写一个Java应用程序,输出菜单中的价格数据,并计算出菜单的总价格。
6.编写一个Java应用程序,用户输入两个日期,程序将判断两个日期的大小关系以及两个日期的间隔天数。
三、实验步骤与实验结果
1.平均成绩
源代码:
package chapert1;
interface CompurerAverage {
public double average(double x[]);
}
class Gymnastics implements CompurerAverage {
public double average(double x[]) {
int count=x.length;
double aver=0,temp=0;
for(int i=0;i<count;i++) {
for(int j=i;j<count;j++) {
if(x[j]<x[i]) {
temp=x[j];
x[j]=x[i];
x[i]=temp;
}
}
}
for(int i=1;i<count-1;i++) {
aver=aver+x[i];
}
if(count>2)
aver=aver/(count-2);
else
aver=0;
return aver;
}
}
class School implements CompurerAverage {//重写public double average(double x[]);返回数组x[]的元素的算术平均
public double average(double x[]){
int count=x.length;
double aver=0;
for(int i=0;i<count;i++) {
aver=aver+x[i];
}
aver=aver/count;
return aver;
}
}
public class Estimator{
public static void main(String args[]) {
double a[] = {9.89,9.88,9.99,9.12,9.69,9.76,8.97};
double b[] ={89,56,78,90,100,77,56,45,36,79,98};
CompurerAverage computer;
computer=new Gymnastics();
double result= computer.average(a); //computer调用average(double x[])方法,将数组a传递给参数x
System.out.printf("%n");
System.out.printf("体操选手最后得分:%5.3f\n",result);
computer=new School();
result=computer.average(b); //computer调用average(double x[])方法,将数组b传递给参数x
System.out.printf("班级考试平均分数:%-5.2f",result);
}
}
2.内部购物券
源代码:
package chapert1;
class MobileShop {
InnerPurchaseMoney purchaseMoney1;
InnerPurchaseMoney purchaseMoney2;
private int mobileAmount; //手机的数量
MobileShop(){
purchaseMoney1 = new InnerPurchaseMoney(20000);
purchaseMoney2 = new InnerPurchaseMoney(10000);
}
void setMobileAmount(int m) {
mobileAmount = m;
}
int getMobileAmount() {
return mobileAmount;
}
class InnerPurchaseMoney {
int moneyValue;
InnerPurchaseMoney(int m) {
moneyValue = m;
}
void buyMobile() {
if(moneyValue>=20000) {
mobileAmount = mobileAmount-6;
System.out.println("用价值"+moneyValue+"的内部购物卷买了6部手机");
}
else if(moneyValue<20000&&moneyValue>=10000) {
mobileAmount = mobileAmount-3;
System.out.println("用价值"+moneyValue+"的内部购物卷买了3部手机");
}
}
}
}
public class NewYear
{
public static void main(String args[]) {
MobileShop shop = new MobileShop();
shop.setMobileAmount(30);
System.out.println("手机专卖店目前有"+shop.getMobileAmount()+"部手机");
shop.purchaseMoney1.buyMobile();
shop.purchaseMoney2.buyMobile();
System.out.println("手机专卖店目前有"+shop.getMobileAmount()+"部手机");
}
}
3.检查危险品
源代码:
package chapert2;
public class Check {
public static void main(String args[]) {
Machine machine = new Machine();
String name[] ={"苹果","炸药","西服","硫酸","手表","硫磺"};
Goods [] goods = new Goods[name.length]; //检查6件货物
for(int i= 0;i<name.length;i++) {
goods[i] = new Goods();
if(i%2==0) {
goods[i].setIsDanger(false);
goods[i].setName(name[i]);
}
else {
goods[i].setIsDanger(true);
goods[i].setName(name[i]);
}
}
for(int i= 0;i<goods.length;i++) {
try {
machine.checkBag(goods[i]);
System.out.println(goods[i].getName()+"检查通过");
}
catch(DangerException e) {
e.toShow();
System.out.println(goods[i].getName()+"被禁止!");
}
}
}
}
package chapert2;
public class DangerException extends Exception {
String message;
public DangerException() {
message = "危险品!";
}
public void toShow() {
System.out.print(message+" ");
}
}
package chapert2;
public class Goods {
boolean isDanger;
String name;
public void setIsDanger(boolean boo) {
isDanger = boo;
}
public boolean isDanger() {
return isDanger;
}
public void setName(String s) {
name = s;
}
public String getName() {
return name;
}
}
package chapert2;
public class Machine {
public void checkBag(Goods goods) throws DangerException {
if(goods.isDanger()) {
DangerException danger=new DangerException();
throw danger;
}
else {
System.out.print(goods.getName()+"不是危险品! ");
}
}
}
4.检索简历
源代码:
package chapert1;
public class FindMess {
public static void main(String args[]) {
String mess="姓名:张三 出生日期:1989.10.16。个人网站:http://www.zhang.com。"+"身高:185cm 体重:72kg";
int index=mess.indexOf(":");
String name=mess.substring(index+1);
if(name.startsWith("张")) {
System.out.println("简历中的姓名姓\"张\"");
}
index=mess.indexOf(":",index+1);
String date=mess.substring(index+1,index+11);
System.out.printf(date);
index=mess.indexOf(":",index+1);
int heightPosition=mess.indexOf("身高");
String personNet=mess.substring(index+1,heightPosition-1);
System.out.println(personNet);
index=mess.indexOf(":",heightPosition);
int cmPosition=mess.indexOf("cm");
String height=mess.substring(index+1,cmPosition);
height=height.trim();
int h=Integer.parseInt(height);
if(h>=180) {
System.out.println("简历中的身高"+height+"大于或等于180cm");
}
else {
System.out.println("简历中的身高"+height+"小于180cm");
}
index=mess.lastIndexOf(":");
int kgPosition=mess.indexOf("kg");
String weight=mess.substring(index+1,kgPosition);
weight=weight.trim();
int w=Integer.parseInt(weight);
if(w>=75) {
System.out.println("简历中的体重"+weight+"大于或等于75kg");
}
else {
System.out.println("简历中的体重"+weight+"小于75kg");
}
}
}
5.菜单的价格
源代码:
package chapert2;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ComputePrice {
public static void main(String args[]) {
String menu = "北京烤鸭:189元 西芹炒肉:12.9元 酸菜鱼:69元 铁板牛柳:32元";
Scanner scanner = new Scanner(menu);
String regex = "[^0123456789.]+";
scanner.useDelimiter(regex);
double sum=0;
while(scanner.hasNext()){
try{
double price = scanner.nextDouble();
sum = sum+price;
System.out.println(price);
}
catch(InputMismatchException exp){
String t = scanner.next();
}
}
System.out.println("菜单总价格:"+sum+"元");
}
}
6.比较日期
源代码:
package chapert3;
import java.util.*;
public class CompareDate {
public static void main(String args[ ]) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入第一个年,月,日数据");
System.out.print("输入年份");
short yearOne = scanner.nextShort();
System.out.print("输入月份");
byte monthOne = scanner.nextByte();
System.out.print("输入日期");
byte dayOne = scanner.nextByte();
System.out.println("输入第二个年,月,日数据");
System.out.print("输入年份");
short yearTwo = scanner.nextShort();
System.out.print("输入月份");
byte monthTwo= scanner.nextByte();
System.out.print("输入日期");
byte dayTwo = scanner.nextByte();
Calendar calendar = Calendar.getInstance();
calendar.set(yearOne, monthOne-1, dayOne);
long timeOne =calendar.getTimeInMillis(); //calendar表示的时间转换成毫秒
calendar.set(yearTwo,monthTwo-1,dayTwo);
long timeTwo=calendar.getTimeInMillis();
Date date1 = new Date(timeOne); // 用timeOne做参数构造date1
Date date2 =new Date(timeTwo);
if(date2.equals(date1))
System.out.println("两个日期的年、月、日完全相同");
else if(date2.after(date1))
System.out.println("您输入的第二个日期大于第一个日期");
else if(date2.before(date1))
System.out.println("您输入的第二个日期小于第一个日期");
long days= Math.abs(timeTwo-timeOne)/(1000*60*60*24);//使用timeTwo,timeOne计算两个日期相隔天数
System.out.println(yearOne+"年"+monthOne+"月"+dayOne+"日和"
+yearTwo+"年"+monthTwo+"月"+dayTwo+"相隔"+days+"天");
}
}
四、实验总结
在本次实验中我学会了用类实现接口、掌握了内部类的用法,学会了使用try-catch语句、string类的常用方法、date类以及calendar类的常用方法。