java程序设计课程--实验报告-实验06
- 格式:doc
- 大小:51.00 KB
- 文档页数:6
《Java开发技术》实验报告
实验序号:实验06 实验项目名称:编写类(一)
学 号 姓 名 专业、班
实验地点 实1-316 指导教师 实验时间 2023- 10-24
一、 实验目的及规定
掌握编写类的方法
掌握方法声明的方法
了解构造方法与其他方法的区别
理解可见性和作用域的概念
二、 实验设备(环境)及规定
PC机,windows xp,软件环境(jdk1.6,tomcat web服务器,Eclipse)
硬件规定:CPU PII 以上,64M 内存,100M 硬盘空间。
软件规定:Windows98/Me/XP/NT/2023,IE 5 以上。
开发环境:JDK1.6.0_10, NotePad或者EditPlus。
三、 实验内容与环节
1)A Bank Account Class
1. 文献Account.java是一个银行账户类。将该文献保存至本地磁盘目录,并仔细查看该类包含的方法,然后填写该类不完整的语句。注意:不必在本题中测试该程序。
a. 为方法toString补充代码,该方法返回包含姓名,账户号码以及账户余额信息的一段字符串。
b. 为方法chargeFee补充代码,从账户中扣去服务费。
c. 修改chargeFee方法,使其返回一个新的账户余额。
d. 为方法changeName补充代码,该方法有一个参数,为字符串类型,可以变更账户的姓名。
2. 文献ManageAccounts.java是一个外壳程序(shell program),该程序使用上面的Account类。将该程序保存至本地磁盘目录,并按照注释补充代码。
Account.java源代码如下(红色部分为补充代码)
import java.text.NumberFormat;
public class Account {
private double balance;
private String name;
private long acctNum; //---------------------------------------
//Construtor -- initializes balance, owner, and account number
//---------------------------------------
public Account(double initBal, String owner, long number){
balance = initBal;
name = owner;
acctNum = number;
}
//---------------------------------------
//Checks to see if balance is sufficient for withdrawal.
//If so, decrenents balance by anount; if not, prints message.
//---------------------------------------
public void withdraw(double amount){
if(balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
//---------------------------------------
//Adds deposit amount to balance.
//---------------------------------------
public void deposit(double amount){
balance += amount;
}
//---------------------------------------
//Returns balance.
//---------------------------------------
public double getBalance(){
return balance;
}
//---------------------------------------
//Returns a string containing the name, account number, and balance.
//---------------------------------------
public String toString(){
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return name + "\t" +acctNum + "\t" + fmt.format(balance);
}
//---------------------------------------
//Deducts $10 service fee
//---------------------------------------
public void chargeFee(){ balance = balance - 10;
}
//---------------------------------------
//changes the name on the account
//---------------------------------------
public void changeName(String newName){
name = newName;
}
}
ManageAccounts.java源代码如下(红色部分为补充代码)
public class ManageAccounts {
public static void main(String[] args){
Account acct1,acct2;
//create accont1 for Sally with $1000
acct1 = new Account(1000,"Sally",1111);
//create accont1 for Joe with $500
acct2 = new Account(500,"Joe",2222);
//deposit $100 to Joe's account
acct2.deposit(100);
//print Joe's new balance (use getBalance())
System.out.println("Joe new balance: "+acct2);
//withdraw $50 from Sally's account
acct2.withdraw(50);
//print Sally's new balance (use getBalance())
System.out.println("Sally's new balance: "+acct1);
//charge fees to both accounts
acct1.chargeFee();
acct2.chargeFee();
//change the name on Joe's account to joseph
acct2.changeName("Joseph");
//print summary for both accounts
System.out.println(acct1);
System.out.println(acct2);
}
}
2)Representing Names
Name.java源代码如下:
public class Name{
private String firstName;
private String middleName; private String lastName;
public Name(String first, String middle, String last){
firstName = first;
middleName = middle;
lastName = last;
}
public String getFirst(){
return firstName;
}
public String getMiddle(){
return middleName;
}
public String getLast(){
return lastName;
}
public String firstMiddleLast(){
return firstName+"-"+middleName+"-"+lastName;
}
public String lastFirstMiddle(){
return lastName+"-"+firstName+"-"+middleName;
}
public boolean equals (Name otherName){
if(this.firstName.equals(otherName.firstName)&&this.middleName.equals(otherName.middleName)&&stName.equals(stName))
{return true;}
else
{return false;}
}
public String initials(){
String a = firstName.substring(0,1) + middleName.substring(0,1)
+lastName.substring(0,1);
return a.toUpperCase();
}
public int length(){
int length = firstName.length() + middleName.length() +
lastName.length();