Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Tutorials CI228
© Mike Smith October 6, 2012 Page 1
Tutorials
Input of an integer number
The static method BIO.getInt() will read from the keyboard an integer number typed
by the user. This value must be assigned to a variable of type int.
int iDays = BIO.getInt();  //Whole number
Will input into the variable iDays an integer number typed by the user.
A program in Java to input a number of days and calculate the number of weeks and
days this represents.
class Main
{
  public static void main( String args[] )
  {
    System.out.print( "#Input number of days  " );
    int iDays = BIO.getInt();
    System.out.print( iDays );
    System.out.print( " days is equal to " );
    int weeks = iDays / 7;              //Number of weeks
    System.out.print( weeks );
    System.out.print( " week(s) " );
    int days = iDays % 7;               //Remaining days
    System.out.print( days );
    System.out.println( " day(s) " );
  }
}
#Input number of days  10
10 days is equal to 1 week(s) 3 day(s)
The Charon system will ignore any line in your output that contains a #.
Hence you can output any line (containing a # ) and it will be ignored
when Charon compares your answer with the expected answer.
The class BIO is automatically included when you use BlueJ or submit a
program to Charon. If you want to use this at home then you need to
include the code for the class BIO (see programs on web site) in your
program.
Tutorials CI228
© Mike Smith October 6, 2012 Page 2
#1 LAB - Programming: Complete exercises 1.1, 1.2, 1.3
You will need to look at the coursework for CI228 (Java) for more details
about the exercise(s).
Using the skeleton below
class Main
{
  public static void main( String args[] )
  {
    // Java code
  }
}
Complete the introduction exercises 1.1, 1.2 & 1.3
Use eclipse, if this occurs after the first lecture.
Tutorials CI228
© Mike Smith October 6, 2012 Page 3
#2 LAB - Complete exercise 2.1
Hints
Start the work before the tutorial so that you will know what
questions to ask at the tutorial.
Try using eclipse, a very simple introduction to eclipse is at:
http://www.stanford.edu/class/cs108/JavaTools/eclipse-guide/
make sure you load the correct version of eclipse its the Java version.
See if you can run exercise 1.1 etc using eclipse. If you use the class BIO
then you will need to creat a class for BIO in your project alongside the
class Main.
If this occures after the lecture, or if you have read ahead then try
implementing exercise 2.1
This involves writing the class AccountBetter1 which inherits from the
class Account and implements the interface Transfer.
You will need to look at the coursework for CI228 (Java) for more details
about the exercise.
Tutorials CI228
© Mike Smith October 6, 2012 Page 4
#3 LAB - Complete exercise 2.1 & 2.2
Hints
Start the work before the tutorial so that you will know what
questions to ask at the tutorial.
This involves writing the class AccountBetter2 which inherits from the
class Account and implements the interface Interest
You will need to look at the coursework for CI228 (Java) for more details
about the exercise.
Tutorials CI228
© Mike Smith October 6, 2012 Page 5
#4 LAB - Complete exercise 2.3
Hints
Start the work before the tutorial so that you will know what
questions to ask at the tutorial.
This involves writing the class AccountStudent which inherits from the
class Account.
You will need to look at the coursework for CI228 (Java) for more details
about the exercise.
Tutorials CI228
© Mike Smith October 6, 2012 Page 6
#5 Terminal Room The Catalogue shop
In groups of 3 (Only for this tutorial)
Take it in turns to play the following 3 roles
When you play the role of an object you will be sent messages by a client
(asked by one of your colleagues) and will return your answer on paper
to the client (colleague)
Role 1 An instance of  StockR
The class StockR implements the following methods
• getDetails
Returns the stock level, price, and description for a given
product number
• exists
Returns true if the product number is valid
The initial state of the stock list is as follows:
Product no Description price Stock level
0005 Toaster £19.99 10
0006 25 * DVD £8.00 7
0008 MP3 Player £49.99 2
Role 2 An instance of Basket
The class Basket implements the following methods
• add
Adds a product to the product list. There is no response
• details
Returns a string description of all the products in the stock
list plus their total value.
• size
Returns the number of entries in the product list
When playing this role you will need to record on paper the
products that you have been given to record.
Role 3 The code that implements the functionality of the customer
client. See the next page for a (interaction diagram) description of
what the code does. In addition you will write on paper the data
that you would wish displayed to the customer (GUI)
Tutorials CI228
© Mike Smith October 6, 2012 Page 7
Role 3
CustomerGUI Transaction StockReader
theStock
exists
if (stock level adiquate )
else
else
Clear display
Customer
theCB
GUIBasket
add
Display "not available"
Display details of product
getDetails
if ( exists )
Display "unknown product"
clear
details
theShoppingList
Button 
pressed
if ( Check Button pressed )
if ( Clear Button pressed )
Tutorials CI228
© Mike Smith October 6, 2012 Page 8
As a group of 3
• What should happen when a request is made to the stock list for details of a
product that does not exist.
• Produce a description of the complete signatures for the methods in the
class  Order. What should the methods do. Write this description down on
paper, How would you then implement the class Order.
Tutorials CI228
© Mike Smith October 6, 2012 Page 9
#6 & #7Lab -  The Catalogue shop
The tutorial this week and next (8 & 9 ) is about running the catalogue shop
application and making some simple modifications to it.
Before the tutorial you should review the tutorial and plan on paper the
modifications that you are to make.
Extract the file jd3.exe (See the student central page on CI228 for location)
Execute the file jd3.exe, choose a directory where the files will be extracted on
your h: drive.
Setting up an odbc connection to the database (You will need to do this only once):
● Start -> Settings -> Control Panel -> Administrative Tools -> Data
Sources(ODBC)
Alternatively from Start-> run
type odbcad32
This will load the ODBC Data Source Administrator
● Press button Add
● Select Microsoft Access Driver (*.mdb)
● Press Finish
● DataSource name: type in cshop
● Press Select and select the file cshop.mdb
The file cshop.mdb is included in the archive.
● Press OK
● Press OK
The file jd3_run.bat will run the system.  Remember however that you need to
set up the ODBC connection to the Access data base first (See above).
The file jd3.bat will compile the application. If you take the files from the self
extracting archive then compiled versions of all files are
included.
Tutorials CI228
© Mike Smith October 6, 2012 Page 10
The process of running the catalogue shop system in detail
The first time the system is run the DataBase representing the stock of the catalogue
shop needs to be bound to the URL jdbc:odbc:cshop. Then the following
command line command line command is issued:
• From the Windows 7 start menu
In the  search box from programs (Just above the windows 7 start button,
type in cmd and press return.
To use the command line Java tools you may need to set up the correct path
to the directory that contains the Java compiler and interpreter. This can be
done by setting the shell variable that contains the directories that will be
searched when a command is typed. (Check for the exact location of the bin
directory for the JDK. If unsure ask at the tutorial for the exact location of
the Java JDK)
: Example location of bin directory for Java JDK, may change
PATH="c:\Program Files\Java\jdk1.6\bin";%PATH%
Change to the directory where you unpacked the files of the Catalogue
system, then run the batch file jd3.bat, to compile the system. You can
then run the system with the  batch file jd3_run.bat
 jd3.bat
 jd3_run.bat
This application will start and display several windows, as the windows
are on top of each other you may wish to move the windows to other parts
of the screen so all windows are visible.
Camera £19.99 (7)
0005
Check
Clear
0004 Watch  (1) £99.99
0005 Camera (1) £19.99
----------------
               £119.98
0005
Check
Buy
Order number: 123
0004 Watch  (1) £99.99
0005 Camera (1) £19.99
----------------
               £119.98
Cancel
Next
Camera £19.99 (7)
You may now type in requests to interrogate the catalogue shop and place
orders.
Tutorials CI228
© Mike Smith October 6, 2012 Page 11
Modification of the display of information about products bought / selected
The class Basket is responsible for maintenance of a list of products. In particular
the method details returns a string representation of this list of products.
● Create a new class Basket2 that is inherited from the class Basket with an
overloaded method details that now returns the string:
1)  Watch  (1) £99.99
2)  Camera (1) £19.99
------------------------
              £119.98
Note: There is limited space on each line, so for this new format the
product number has been omitted. The first number in each line indicates
that this is the n'th item selected. The second number is the number of
items purchased.
What you need to do
• Create the new class Basket2 in a file called Basket2.java in the
subdirectory Catalogue of the Catalogue shop system.
• Modify the file CustomerGUI.java to change the line
  private Basket    theShoppingList = new Basket()
to
  private Basket    theShoppingList = new Basket2();
This works because an instance of a subclass can be assigned to its
superclass. When a message is sent to the object theShoppingList
the method obeyed will come from the class Basket2 or if not
present then from Basket and if not present there then from its
superclass.
• Recompile the system using the batch file jd3.bat
● Test your changes.
● Modify the application so that more than 1 item can be selected to be
bought. For example, a customer may wish to purchase 2 sets of batteries.
In the current implementation for this to happen the item would have to be
selected twice.
● Test your changes.
Tutorials CI228
© Mike Smith October 6, 2012 Page 12
Hints
Start the work before the tutorial so that you will know what
questions to ask at the tutorial.
Skeleton code for the new class Basket2 is as follows:
package Catalogue;
import java.text.NumberFormat;
import java.util.Locale;
public class Basket2 extends Basket
{
  private NumberFormat theMoney  =
          NumberFormat.getCurrencyInstance( Locale.UK );
  public String details()          //Return list of products as string
  {
    String res = "";
    double total = 0.00;
    ArrayList theProducts = getProducts();
                                        // Required as private in the superclass
  }
}
For the second part, you will need to create a new input text field on the screen.
This will involve making room for the new input area, by re-sizing 1 or more
components. Design your new interface for the customer client before you start
the implementation.
Tutorials CI228
© Mike Smith October 6, 2012 Page 13
#8 Lab -  The Catalogue shop
The tutorial this week is to help your with your development of your extension to
the Catalogue shop.
We can provide help by:
• Suggesting a solution to a syntax / semantic  error
• Suggesting what you can do to find what is wrong with your program
when it does not work as you expected.
You need to know what was expected.
However, you need to help us to help you by bringing along a complete listing of
your program, together with any output.
A neatly indented program will help you and us to understand the program better.
Tutorials CI228
© Mike Smith October 6, 2012 Page 14
#9 Lab -  The Catalogue shop
The tutorial this week is to help your with your development of your extension to
the Catalogue shop.
We can provide help by:
• Suggesting a solution to a syntax / semantic  error
• Suggesting what you can do to find what is wrong with your program
when it does not work as you expected.
You need to know what was expected.
However, you need to help us to help you by bringing along a complete listing of
your program, together with any output.
A neatly indented program will help you and us to understand the program better.
Tutorials CI228
© Mike Smith October 6, 2012 Page 15
#10 Lab -  The Catalogue shop
The tutorial this week is to help your with your development of your extension to
the Catalogue shop.
We can provide help by:
• Suggesting a solution to a syntax / semantic  error
• Suggesting what you can do to find what is wrong with your program
when it does not work as you expected.
You need to know what was expected.
However, you need to help us to help you by bringing along a complete listing of
your program, together with any output.
A neatly indented program will help you and us to understand the program better.
Tutorials CI228
© Mike Smith October 6, 2012 Page 16
#11 Lab -  The Catalogue shop
The tutorial this week is to help your with your development of your extension to
the Catalogue shop.
We can provide help by:
• Suggesting a solution to a syntax / semantic  error
• Suggesting what you can do to find what is wrong with your program
when it does not work as you expected.
You need to know what was expected.
However, you need to help us to help you by bringing along a complete listing of
your program, together with any output.
A neatly indented program will help you and us to understand the program better.
Tutorials CI228
© Mike Smith October 6, 2012 Page 17
#12 Lab -  The Catalogue shop
The tutorial this week is to help your with your development of your extension to
the Catalogue shop.
We can provide help by:
• Suggesting a solution to a syntax / semantic  error
• Suggesting what you can do to find what is wrong with your program
when it does not work as you expected.
You need to know what was expected.
However, you need to help us to help you by bringing along a complete listing of
your program, together with any output.
A neatly indented program will help you and us to understand the program better.
Tutorials CI228
© Mike Smith October 6, 2012 Page 18
#13 Lab -  The Catalogue shop
Demonstration of your solution
Hand in the completed solution on your coursework
day with the following index.
Indicative table of contents of your course work
Listing of program 1.1 - 1.3 Page no
Listing of program 2.1 - 2.3 Page no
Back door client
Listing (Your code highlighted) Page no
Brief documentation on application Page no
Testing of application Page no
Display client
Listing (Your code highlighted) Page no
Brief documentation on application Page no
Testing of application Page no
Code for the class Order
Listing (Your code highlighted) Page no
Brief description of methods used Page no
Testing of the class Order
The JUnit test code Page no
Critique of your Unit test Page no
Critique of your code for the Cat shop Page no