# 学习中的题目。麻烦高手指教

richard0114 2004-08-28 07:03:46

Exercise 1 (Trip logs)
Electronic submission

To assist the University transport office, design, implement and test a complete console application that reads information about journeys made by vehicles from standard input and writes a summary of the journeys made to standard output.

The input consists of a sequence of zero or more lines on standard input, terminating at end of file. Each line of standard input contains information about a single journey made on a particular day. Specifically, each line contains the registration number, the driver's name, the starting point, the destination, the initial kilometres (on the odometer) and the final kilometres. (The registration number, the driver's name and the destination may each be assumed to be a single word.) A possible input file might be:

222FFF Rodney Nathan City 10500 10530
123EFG Andrew Nathan Southport 12100 12300
123EFG Rodney Nathan Logan 12300 12350
567GGH Colin Logan GoldCoast 21345 21495
222FFF David Nathan City 10530 10570
Lines that are obviously incorrect (e.g., wrong number of tokens, non-integer kilometres, final kilometres less than the initial kilometres) starting point same as destination should be quietly ignored.

The application must display the total distance travelled by all vehicles on that day and the total distance travelled by each driver on that day, arranged alphabetically by the driver's name. For example, the output corresponding to the above input should be:

Andrew 200 km
David 40 km
Colin 150 km
Rodney 80 km

Total 470 km
Names should be left-justified, distances should be right-justified. You may assume that all names are at most 12 characters long, and that each driver travels less than 10,000 km in total.

Your solution must be well-designed and thoroughly documented using Javadoc. It should be thoroughly tested on your own, comprehensive, test data files.

Hints

Store the total distance travelled by each driver in an object together with the driver's name. Implement a mapping from driver names to corresponding objects. This can be done using the collections framework and the Map interface, or by an array of objects and an explicit search method.

Solve the problem one step at a time. Initially ignore output sorting. Initially ignore number formatting.

(5 marks)

Exercise 2 (Block sliding puzzle)
Electronic submission
This is the second of a series of exercises to design and implement a graphical implementation of a popular block sliding puzzle.

As before, each problem consists of several blocks of size 1x1, 2x1, 1x2 or 2x2 placed in a 5x4 grid. Naturally, blocks may never overlap! Each problem has exactly one 2x2 block. Blocks may move left, right, up or down provided there is nothing stopping them. Each problem has a single marked exit from the grid. The goal is to move blocks around until the single 2x2 block reaches the exit.

The purpose of this second programming exercise is to design, implement and test a terminal application that allows a user to try and solve a given problem. The application should read a problem from a file, draw the initial board, repeatedly read moves from standard input, and print an appropriate message if and when the problem is solved. The name of the file containing the problem should be provided as a command-line argument. After reading each move, the program should make the move and draw the resulting board. If a move is illegal, it should be quietly ignored (and the board should not be redrawn), i.e., you must validate that user input is correct.

Each move is entered on a separate line. The form of each move is as follows:

bxyz...

Here, b is a block identifier, a single character, in either upper or lower case. Each character x, y, z, represents one of the directions u, d, l or r, in either upper or lower case. Directions may be repeated in a single move. The move terminates normally when all directions have been followed or when the next direction is not possible. For example, a possible move is:

Alurd

If this move succeeds completely, it moves block A left, up, right and down, i.e., back to its starting point.

Problem descriptions have the same form as in Project 1. Example problems are provided in directory problems. You may assume that problem descriptions are correct, i.e., you do not have to perform any input validation on problem descriptions.

Here is a possible execution of the application. User input is shown in italics.

> java Puzzle problems/problem0
----
|.AA.|
|.AA.|
|....|
|....|
|....|
-++-
Allll
----
|AA..|
|AA..|
|....|
|....|
|....|
-++-
----
|....|
|....|
|.AA.|
|.AA.|
|....|
-++-
----
|....|
|....|
|....|
|.AA.|
|.AA.|
-++-
Solved in 3 moves (min is 1).
>
The exact form of the output is not important, but it should be neat and tidy. There is no need for unnecessary prompts and messages. There is no need to go to a lot of trouble over the output, because it will soon be replaced by graphical output.

Your solution must be well-designed and thoroughly documented using Javadoc. It should be thoroughly tested.

Hints

Solve the problem one step at a time; do not attempt to solve it all at once. For example, initially assume that each command has the form bx, where x is a single atomic move. Initially assume all characters in commands are upper case. Initially ignore the need to recognise when the problem is solved.

As in Project 1, it is desirable to use at least three classes: Block (as provided previously), Board and Puzzle. Class Puzzle is the top-level class responsible for reading the problem description, reading user input, and printing the board. Other classes may also be useful.

It is probably necessary to extend your definition of class Board from Project 1 by adding the following methods:

/**
* Returns whether or not a block can move in a direction.
* @param id The identifier of the block to move.
* @param dir The direction to move the block.
*/
public boolean canMove(String id, String dir)

/**
* Moves a block in a direction. Requires canMove(id, dir) to hold.
*/
public void move(String id, String dir)

/**
* Returns whether or not the problem is solved.
*/
public boolean solved()
Some students may find the design and implementation of these three methods challenging. To help them, we present a partial implementation of method move(). This implementation assumes that class Board contains a 2-dimensional array, grid, which contains references to blocks. It assumes that character dirc is the direction to move (character dirc is derived from the single-character string dir), that block is the block to be moved, and that its initial position is (row, col). It uses a switch-statement to choose between the four possible directions.

switch (dirc) {
case 'u': // Up
// Move the block one position up
for (int r = row; r < row+block.height(); r++)
for (int c = col; c < col+block.width(); c++)
grid[r-1][c] = block;
// Clear the array elements below the block
for (int c = col; c < col+block.width(); c++)
grid[row+block.height()-1][c] = null;
// Update the block position
block.setPosition(row-1, col);
// Exit the switch statement
break;
You may need to think explicitly about how to find the block with the identifier given in a move.

It will help you to solve the problem if you prepare pseudocode designs of critical methods such as the ones that read, validate and apply moves from standard input. You must submit one such pseudocode design with your solution.

...全文
50 点赞 收藏 2

2 条回复

edhappy 2004-08-28

richard0114 2004-08-28

(5 marks)

--------------------------------------------------------------------------------

Last updated: 20 August 2004
Author: Rodney Topor
Email: r.topor@griffith.edu.au

CIT1104/CIT1504 Programming II: Project 2: Clarifications and hints

--------------------------------------------------------------------------------

Some minor errors in the project were corrected in the first day or two after it was published.
All programming exercises should be solved using the standard Java input/output library, i.e., not using TerminalIO.
In Exercise 1, some input fields are not used, but may be used in later projects, e.g., to print total distance travelled from each location, or most frequent route.
The easiest way to solve this exercise is to use a Map from driver names to objects. If you use a TreeMap, then iterating through the keys automatically generates them in alphabetical order. The use of maps is described in the lecture on Collections.
To print numbers right justified, you can use complicated features of the Java API, or you can do it yourself (which is more instructive), using question 12 in the FAQ.
In Exercise 3, you may assume that block identifiers are single-character strings and are case-independent. You already know how to get the (problem) file name from the command line, and the lecture on Files explains how to (safely) read from a file. Of course, you must now print the location of the exit on the board.
More to come...

problem0:A 2 2 0 1
Z D 1 2
1

problem1:
A 1 1 0 0
B 1 1 0 1
C 1 1 0 2
D 1 1 0 3
E 1 2 1 0
F 2 2 1 1
G 1 2 1 3
H 2 1 3 0
I 2 1 3 2
J 2 1 4 1
Z U 1 2
10

problem2:
A 2 1 0 0
B 1 1 0 2
C 1 2 0 3
D 1 1 1 0
E 2 2 1 1
F 1 2 2 0
G 1 1 2 3
H 1 1 3 1
I 2 1 3 2
J 2 1 4 1
Z L 1 2
10

problem3:
A 2 2 0 0
B 1 2 0 2
C 1 2 0 3
D 1 1 2 0
E 1 1 2 1
F 1 2 3 0
G 1 2 3 1
H 2 1 3 2
I 2 1 4 2
Z R 0 1
16

problem4:
A 1 1 0 0
B 2 2 0 1
C 1 1 0 3
D 1 1 1 0
E 1 1 1 3
F 2 1 2 1
G 1 2 3 0
H 1 2 3 1
I 1 2 3 2
J 1 2 3 3
Z D 1 2
32

problem5:
A 2 2 0 0
B 2 1 0 2
C 2 1 1 2
D 1 1 2 0
E 1 1 2 1
F 1 2 3 0
G 1 2 3 1
H 2 1 3 2
I 2 1 4 2
Z D 0 1
59

problem6:
A 1 2 0 0
B 2 2 0 1
C 1 2 0 3
D 2 1 2 1
E 1 2 3 0
F 1 1 3 1
G 1 1 3 2
H 1 1 4 1
I 1 1 4 2
J 1 2 3 3
Z D 1 2
85

Web 开发

8.0w+

Java Web 开发

2004-08-28 07:03