Java实验三
C++ 程序设计实验报告册 学 学 院________________ 班 班 级________________ 学 学 号________________ 姓 姓 名________________
《a Java 程序设计》实 验 报 告(三)学号:
姓名:
班级:
信本 成绩:
实验名称 :
面向对象编程基础 实验地点:6a-2 所使用的开发工具及环境:JDK+eclipse 一、实验目的 1.验证面向对象三大特性 2.学习封装的实现 3.学习继承的实现 4.编写多态实例 5.学习抽象类的使用 6.学习接口的使用 二、实验要求 1.掌握封装的实现方法 2.掌握继承的编程方式和思想 3.理解多态现象 4.掌握抽象类和接口的使用 三、实验内容与步骤 1.封装的实现(1)编写程序模拟个人银行账号类。考虑个人银行的特点,建立类模型(注意属性和方法的访问权限修饰符)[参考代码] public class BankAccount { private String accountID;private String password;private int balance;public BankAccount(String accountID,String password,String operator){ this.accountID=accountID;this.password=password;this.balance=0;System.out.println("Create a BankAccount");System.out.println("AccountID:"+this.accountID);System.out.println("Current Balance:"+this.balance);System.out.println("Operator:"+operator);System.out.println("Save Account info to Database");} public void queryBalance(String password){ if(password==this.password){ System.out.println("Password OK");
System.out.println("Current Account Balance:"+this.balance);}else { System.out.println("Password Erro");} } public void changePassword(String oldPassword,String newPassword){ if(oldPassword==this.password){ System.out.println("Password OK");this.password=newPassword;System.out.println("Change Passord OK");}else { System.out.println("Password Erro");} } public void deposit(int money,String operator){ this.balance+=money;System.out.println("add balance OK.Operator:"+operator);System.out.println("Save Account change to database");} public void withdraw(String password,int money,String operator){ if(password==this.password){ System.out.println("Password OK");if(this.balance>money){ this.balance-=money;System.out.println("withdraw:"+money+" ok, Operator:"+operator);System.out.println("Current Account Balance:"+this.balance);System.out.println("Save Account change to database");}else{ System.out.println("withdraw:"+money+" erro.Because of not enough balance");} }else { System.out.println("Password Erro");} }
}(2)编写测试类,完成如下(1)中类方法的测试 实现如下业务:
开户,存款 100,查询余额,取款 50,查询余额,取款 200,查询余额 2.继承的实现(1)按如下类图编写代码 [参考代码] class Person { String id;String name;String age;public void sleep(){ System.out.println("I am Person,I am sleeping");} public void eat(){ System.out.println("I am Person,I am eating");} } class Student extends Person { String sno;public void study(){ System.out.println("I am Student,I am studying");} }
class Teacher extends Person { String tid;public void tech(){ System.out.println("I am Student,I am taching");} }(2)编写测试类并创建 main()方法,完成如下操作 A.分别创建 Person、Student、Teacher 对象,完成属性和每个方法的调用测试 B. 在 Student 和 Teacher 中完成 eat()方法的重写 C. 编写类型转化示例(向上类型转化、向下类型转化)3.多态现象 在完成(2)中 Student、Teacher 类 eat()方法重载后,在测试类中编写如下代码并在 main()方法中调用 static void askAllToEat(Person[] ps){ for(int i=0;i
4.抽象类的使用 编写以上类图所示类,并编写测试代码测试抽象类的使用 [参考代码] abstract class Printer{ private String printerType;Printer(String printerType){ this.printerType=printerType;} abstract void print(String txt);void showMyType(){
System.out.println("My Type is:"+printerType);} } class InkPrinter extends Printer{ InkPrinter(String inkPrinterType){ super(inkPrinterType);} void print(String txt){ System.out.println("I am Ink Printer");System.out.println("Start to Print:"+txt);} } class LasertPrinter extends Printer{ LasertPrinter(String laserPrinterType){ super(laserPrinterType);} void print(String txt){ System.out.println("I am Lasert Printer");System.out.println("Start to Print:"+txt);} } 编写测试类及 main()方法,完成如下操作 A.创建 Printer,InkPrinter,LaserPrinter 类的对象。
B.设计并编写演示多态现象的代码 5.接口的使用 编写以上类图的代码,并编写测试类测试接口的使用 [参考代码] interface IScan{ void scan();
} abstract class Printer{ private String printerType;Printer(String printerType){ this.printerType=printerType;} abstract void print(String txt);void showMyType(){ System.out.println("My Type is:"+printerType);} } abstract class InkPrinter extends Printer{ InkPrinter(String inkPrinterType){ super(inkPrinterType);} void print(String txt){ System.out.println("I am Ink Printer");System.out.println("Start to Print:"+txt);} } class LasertPrinter extends Printer{ LasertPrinter(String laserPrinterType){ super(laserPrinterType);} void print(String txt){ System.out.println("I am Lasert Printer");System.out.println("Start to Print:"+txt);} } 思考与练习:
(1)自行设计并编写代码演示面向对象的三大特性(2)练习抽象类的使用(3)练习接口的使用 实验题目一:代码如下:
import java.util.Scanner;public class Test { public static void main(String[] args){ Account account = new Account(123,"tom","666",0);boolean flag = true;while(flag){ System.out.println("请输入选项:");System.out.println("********************");System.out.println("1、查询余额");System.out.println("2、取款");System.out.println("3、存款");System.out.println("4、修改密码");System.out.println("5、退出");System.out.println("********************");Scanner scanner = new Scanner(System.in);int option = scanner.nextInt();switch(option){ case 1: System.out.println("余额是:" + account.queryBalance());break;case 2: withdraw(account, scanner);break;case 3: System.out.println("请输入存款金额:");double depositAmount = scanner.nextDouble();deposit(account, depositAmount);break;case 5: flag = false;break;} } } private static void withdraw(Account account, Scanner scanner){ System.out.println("请输入取款金额:");double input = scanner.nextDouble();boolean result = account.withdraw(input);if(result){ System.out.println("取款成功");System.out.println("余额是:" + account.getBalance());}else{ System.out.println("取款失败");}
} private static void deposit(Account account, double amount){ boolean result = account.deposit(amount);if(result){ System.out.println("存款成功");}else{ System.out.println("存款失败");} } } 运行结果如下:
题目二:代码如下:
public class Test { public static void main(String arg[]){ Person p = new Person();Student s = new Student();Teacher t = new Teacher();System.out.println("-----Person类测试-----");p.age = "20";p.id = "asd";p.name = "yang";System.out.println(p.age+" "+p.id+" "+p.name);p.eat();p.sleep();System.out.println("-----Student类测试-----");s.sno = "20121080";
System.out.println(s.sno);s.eat();s.study();System.out.println("-----Teacher类测试-----");t.tid = "qwe";System.out.println(t.tid);t.tech();t.eat();} } 运行结果:
题目三:代码已知:
运行结果:
题目四:注:abstract 类不能创建对象,代码如下:
public class Test { public static void main(String arg[]){ // Printer p = new Printer("sd");InkPrinter ip = new InkPrinter("InkPrinter");LasertPrinter lp = new LasertPrinter("Lasert");System.out.println("---InkPrinter类测试----");ip.showMyType();ip.print("A-B");System.out.println("---LaserPrinter测试---");lp.showMyType();lp.print("Laser");}
} 运行结果:
题目五:
代码如下:
public class Test { public static void main(String arg[]){ LasertPrinter lp = new LasertPrinter("laser");System.out.println("----测试结果----");lp.showMyType();lp.print("C-D");lp.print("A-B");lp.scan();} } 运行结果如下:
通过本次实验,学会了 Java 中面向对象编程的一些基本操作,并写入代码编译成功。
指导教师签名:
2014 年 06 月 12 日
