Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
DCS 235 Software Engineering 
Laboratory Sheet 1
Programming in the Large: Building a Bank
version 1.1, October 2003
Prerequisites
• You should be able to run BlueJ.  In the ITL, it is available on Linux: type ‘bluej’ at the
shell prompt.  It can be downloaded (from www.bluej.org) for use at home on Windows or
Linux.  Read the user manual, which is also available locally on the SE web page.
• Download the two BlueJ projects from the SE web page.
Aims
The aim of this exercise is develop a program, making use of classes.  The skills you will learn and
practise are:
• Writing Java programs with classes.
• Understanding the different ways classes are related.
• Identifying suitable classes from information about a problem as the basis for a program
design.
The program – modelling a bank account – is developed in steps.  To begin with detailed
instructions are given; for the later steps the instructions are less detailed.  It is recommended that
you use the BlueJ software for Java development.  Two incomplete BlueJ projects are provided with
this exercise – ensure that they are downloaded onto your machine. 
Preparatory Reading
Revise class declarations in Java.  Many textbooks are suitable, including:
• Horstmann’s ‘Computing Concepts with Java Essentials ’
• Jia’s book ‘Object-Oriented Software using Java’, sections 4.1-4.4, 5.1 and 5.2 (Java classes).
1.The Account Class – Project0
Open ‘project0’ using BlueJ.  The project contains a single class ‘Account’.  A complete template of
the class is provided, with comments explaining how each method should behave.  There are 5
methods which you need to complete.
1. View the interface of the class, using BlueJ.  (Note: for a description of the Java documentation
tags see Jia section 6.1.4).  Figure 1 shows what you should see.
2. Implement the methods and test the program.
DCS 235 Lab Sheet 1 (version 1.1_0, 6 Oct 2003) Page 1
Figure 1: Interface of the Account Class
2.Transactions – Project1 
The bank account is simplistic: there is no record of the deposits and withdrawals made by the
account owner.  Here is a UML class diagram1 for a possible design for a more realistic bank
account.  The diagram shows 4 classes: Account, Transaction, Deposit and Withdraw.  Deposit and
Withdraw extend Transaction.
Open ‘project1’ using BlueJ.  The project contains a partial implementation of the ‘Account’ class
and the abstract class ‘Transaction’ in full.  The transactions of the account are held in a linked list;
this has been provided for you.  Read the code and check you understand how it works.
1. Look at the interface of the classes, for example by generating the documentation.  Note that
there are some changes to the interface of this version of the Account:
• the methods ‘getBalancePounds’ and ‘getBalancePence’ have been dropped
• we now specify the day on which a withdrawal or deposit is made
• there is no calculation of interest (we return to this later!).
2. Add the classes ‘Withdrawal’ and ‘Deposit’.  This can be done in BlueJ, which provides a
standard class template.
3. Complete the implementation of the classes ‘Withdraw’ and ‘Deposit’.
4. The ‘Account’ class contains the unimplemented method ‘getCurrentBalance’.  Complete the
implementation of this method.
1 UML class diagrams will be covered in lectures.  To get ahead, read Jia section 2.2.  However, you do not need to
understand beyond the explanation given in the this sheet for this exercise.
DCS 235 Lab Sheet 1 (version 1.1_0, 6 Oct 2003) Page 2
Account Transaction
Deposit Withdraw
*1
5. The Account class contains an unimplemented method ‘showStatement’.  This is intended to
print a statement to the console.  For example:
Opening balance £100.00
Deposited £13.50 on day 1
Withdrew £25.80 on day 2
Current balance £87.70
Complete the implementation of the ‘showStatement’ method.  Note that a method
‘poundsAndPence’ is provided to ensure that the pounds and pence are properly formatted:
private static String poundsAndPence(int amount) {
    int aa = Math.abs(amount) ;
    String leadingZero = aa % 100 < 10 ? "0" : "" ;
    String poundSign = amount < 0 ? "-£" : "£" ; 
    return (poundSign + aa/100 + "." + leadingZero + aa%100)
} ;
3.Reorganising the Code – Project2
Copy the directory ‘project1’ and call it ‘project2’.
1. Did you make use of the ‘poundsAndPence’ method?  You may have thought that this code isn’t
well organised:
• you needed the same routine in other classes – did you copy it or make it public?
• rather than being static, it should operate on the some object, but it isn’t an operation on the
Account class.
2. The ‘balance’ field of the Account class is being used to hold the ‘opening balance’.  This means
that every time the current balance is requested, all the transactions have to be added up.  A
better approach might be to make Account.balance the current balance.  It would need to be
updated for each transaction.  How would the opening balance be held?  Could it be considered
to be another kind of transaction?
Reorganise your code to take account of these points.
4.Monthly Statements – Project3
Copy the directory ‘project2’ and call it ‘project3’.
Real banks do not send you a statement listing all the transactions on an account – each statement
would include the previous one.  Lets enhance the bank to have monthly statements.  The bank’s
requirements are:
• the transactions occur in a month (give each month a number starting at 1, ignore years) 
• the day of a transaction is relative to the start of the month 
• a new method ‘endMonth’ is needed, which completes the current month and starts the
next one
• a statement covers all the transactions in a month, starting with the opening balance and
finishing with the balance at the end of the month
• the statement for any month can be printed.
Enhance the software to meet these requirements:
DCS 235 Lab Sheet 1 (version 1.1_0, 6 Oct 2003) Page 3
1. Decide which new class (or classes) is (are) needed.  Identify the attributes and methods of the
new class(es).  Write this down before you begin to code.
2. Use an array to hold the months, allowing up to twelve months.
5.Challenge Problem – Interest Calculation
We can now implement a more realistic calculation of interest.  The bank uses the following rules to
calculate the interest:
• Interest payment/charges are added to the account at the end of each month (assume all
months have 30 days).
• A payment/charge is calculated for each day, using the annual appropriate rate (assume 365
days in every year).
• If the account is overdrawn on any day in the month, no interest is paid for the days when the
account is in credit.
DCS 235 Lab Sheet 1 (version 1.1_0, 6 Oct 2003) Page 4