编程辅导 C C++ Java Python MIPS Processing 网络家教 在线辅导

编程家教 远程写代码 Debug 讲解答疑 课后答疑 不是中介,本人直接答疑

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航

Task 1 – Codes.java (after you read the whole assignment  )Write a Java program called Codes.java that, firstly, prompts (asks) the user to enter aninput file name. This is the name of a text f

Task 1 – Codes.java (after you read the whole assignment )
Write a Java program called Codes.java that, firstly, prompts (asks) the user to enter an
input file name. This is the name of a text file that can contain any number of records. A
record in this file is a single line of text in the following format:
Station code#Station name#date built
where:
Station code is a 3 character code that identifies the station. This code is
unique and is followed by a '#' character, there are no spaces between the '#'
and the Station name and the code is always in uppercase characters.
Station name is the unique name of a railway station. This is a String (text) and
may contain more than one word. This name is followed by a '#' character, there
are no spaces after the '#'.
date built is the date that the station was built. The date is always in the
format
[d]d month yyyy
where:
[d]d is the day, if the day is less than 10 then there will only be one digit
if the date is greater than or equal to 10 then there will be 2 digits
The [d] indicates that this digit may or may not be present
The d indicates that this digit must be present
month is the (text) name of the month, spelt in full
yyyy is the year, always 4 digits
An example of some of the lines of this file might be:
BOX#Box Hill#29 April 1983
RES#Reservoir#8 October 1889
WIN#windsor#19 December 1859
The input file may have 0 to any number of records. The format of the input file is
guaranteed to be correct. Your program does not have to check the format.
Also, your program must work with any file name of the correct format.
(Do not hard code the file name.)
Once this file has been opened, the user is then prompted for a station code.
The program then reads through the file. If a matching station code is found in the file,
all the information for that station is displayed to the screen. The order of display is shown in
the example runs below. Note that the information from the file must be enclosed in " ",
except the date.
Station codes are unique in the file, so there will be at most only one match.
OOF Assignment Part A - due: 10:00 am Mon 22nd Aug this is the first and final hand in date p. 5 of 13
User entered Station codes must be case insensitive, that is, any combination of
uppercase and lowercase letters must give the same result.
If the entire contents of the file has been read and no match is found, then an
appropriate message is displayed to the screen.
Some sample runs of the program are included below (user input is in bold):
(Note that the sample runs do not necessarily show all the functionality required)
> java Codes
Enter file name >> stationsMaster.txt
Enter station code >> PRE
Station name: "Preston" has code "PRE" date built: 8 October 1889
> java Codes
Enter file name >> stationsMaster.txt
Enter station code >> ABC
No station with the code "ABC" was found
> java Codes
Enter file name >> e
file name "e" is an empty file
An example input file for Task 1 and 2 may be copied from the csilib area
cp /home/student/csilib/cse1oof/assignA/s.dat .
Task 2 – Stations.java
Write a Java program called Stations.java that, firstly, prompts (asks) the user to
enter an input file name. This is the name of a text file that can contain any number of
records (lines).
Each record has the same format as Task 1
The input file may have 0 to any number of records. The format of the input file is
guaranteed to be correct. Your program does not have to check the format.
Also, your program must work with any file name of the correct format.
(Do not hard code the file name.)
Once this file has been opened, the program checks if this file is empty (you may assume
that the user always enters a valid file name). If the file is empty, the program displays
an appropriate message to the screen and closes, without using System.exit( ).
If the file is not empty, then the user is prompted (asked) to enter a start year and an end
year.
Consider how you want to read in the start year and end year, as ints or as Strings.
Either data type will work. Since the whole line needs to be read in as a String, maybe having
the user inputs as Strings might prove easier in the long run.
don't forget the dot
OOF Assignment Part A - due: 10:00 am Mon 22nd Aug this is the first and final hand in date p. 6 of 13
The program must first check that the end year is after or equal to the start year. If the end
year is before the start year, then the program displays to the screen an appropriate
message and ends.
If the end year is after, or equal to, the start year, then the program displays to the screen all
the details of Stations (the complete record) that were built within the start year and the end
year (including the start year and the end year).
For example, if the start year was 1906 and the end year was 1909, then all stations that
were built in 1906, 1907, 1908 and 1909 would be displayed to the screen.
To do this, your program will need to read the entire contents of the file, line by line.
Unlike Task 1, there may be more than one Station built within the years between the
start year and the end year.
If there are no Stations within the start year and the end year entered by the user, then an
appropriate message is displayed to the screen.
Your program must work with any input file in the correct format.
This assignment is to consolidate the String class and basic selection (if, if - else, switch)
Note the output format is the same as in Task 1
Some sample runs of the program are included below (user input is in bold):
(Note that the sample runs do not necessarily show all the functionality required) > java Stations Enter file name >> stationsMaster.txt Enter start year >> 1888 Enter end year >> 1901 Here is the list of stations built between 1888 and 1901
Station name "Bayswater" has code "BAY" date built: 4 December 1889
Station name "Bell" has code "BEL" date built: 8 October 1889
Station name "Clifton Hill" has code "CHL" date built: 8 May 1888
Station name "East Camberwell" has code "ECM" date built: 14 May 1900
Station name "Ferntree Gully" has code "FTG" date built: 4 December 1889
Station name "Northcote" has code "NCE" date built: 8 October 1889
Station name "Preston" has code "PRE" date built: 8 October 1889
Station name "Regent" has code "REG" date built: 8 October 1889
Station name "Reservoir" has code "RES" date built: 8 October 1889
Station name "Thornbury" has code "TBY" date built: 8 October 1889
Station name "Williamstown Beach" has code "WBH" date built: 7 August 1889 > java Stations Enter file name >> stationsMaster.txt Enter start year >> 2016 Enter end year >> 1900 The start year must be equal to or before the end year
OOF Assignment Part A - due: 10:00 am Mon 22nd Aug this is the first and final hand in date p. 7 of 13 > java Stations Enter file name >> stationsMaster.txt Enter start year >> 2015 Enter end year >> 2016 No stations were built between the specified dates > java Stations Enter file name >> e file "e" is an empty file
As a final note on this question, consider how you could get your program to only print the
heading (Stations built between start year and end year) if there is at least one Station built
within that time period.
In both Tasks 1 and 2, the idea is that you have to read in a line from the input text file
and break the line up into its 3 parts, station code, station name and date built. The
parts are separated by the '#' character. You can do this using the techniques we
learnt in Lecture/Workshop 2, indexOf, charAt and substring from the String
class.
When you have finished you have 3 Strings, each containing just part of the original
line from the file. Once you get to this point, you can then use more from Lecture /
Workshop 2 such as, equals and equalsIgnoreCase to compare the appropriate
part of the text file line with the user input.
In Task 1 this is searching for a station code, the first part of the text file line.
In Task 2 you should consider that you are looking at the very last entry on the line,
the year. Only if the year at the end of the line is within the start year and the end year
do you need to break up the line so that you can display it to the screen with the
format as shown in the examples.
Just displaying the line directly from the text file, without breaking up the line
will result in this Task being awarded 0, regardless of the correctness of the
other parts of the Task.
OOF Assignment Part A - due: 10:00 am Mon 22nd Aug this is the first and final hand in date p. 8 of 13
Task 3 – Ticket.java
Write a Java program called Ticket.java that prompts (asks) the user to enter a ticket
number. If the format of the ticket is valid (correct), then the cost of the ticket is displayed to
the screen. If the ticket is not in the correct format, then an appropriate error message should
be displayed to the screen and no cost is displayed.
This format is
CDDD[ [+[M|E|ME|EM]] | [-[C|V|CV|VC]] ]
where C is a character and D is a digit. Then there is an optional '+' followed by M, E,
ME or EM. The '+' must be followed by at least one character. This means that a ticket
length of 5 is automatically invalid for example. If both the '+' and '-' options are in the
ticket, the '+' option must come before the '-' option for the ticket to be valid. As
shown in the examples below, it is possible to just have the '-' option without the '+'
option and the '+' option without the '-' option.
Some examples of valid tickets would be:
e100
B204+m-Cv
F045-V
E605+em-vc
b001+Me-c
f085+E
If the ticket conforms to the given format, then the cost of the ticket is calculated and
displayed.
If the ticket does not conform to the given format, then the user must be advised with the
most specific reason why as possible and no cost is displayed.
The ticket must start with the characters 'B', 'F' or 'E'
Note that the input must be case insensitive, that is, for example, 'B' and 'b' are both valid
and produce exactly the same cost.
If the first character is valid, then it will be followed by 3 digits. It is NOT necessary to check
if these are digits!
Provided that the first character is valid, then it is guaranteed that there will be three digits
next.
The three digits have to be converted into an integer number.
The use of class Integer (or class Double) is not allowed
The idea here is to practise using low level methods from the String class
(lecture/workshop 2)
Take each character and convert it to an int by subtracting '0' Then assemble the
integer as we do that
OOF Assignment Part A - due: 10:00 am Mon 22nd Aug this is the first and final hand in date p. 9 of 13
First character (2nd from the left) is the 100's
Second character (3rd from the left) is the 10's
Third character (4th from the left) is the units
This is the basic cost of the ticket.
The first character modifier is then applied to the cost of the ticket
If the first character is M (or m) then the cost is twice the integer worked out above.
If the first character is F (or f) then the cost is one and a half times the integer worked out
above.
If the first character is E (or e) then the cost is just the integer worked out above.
If the ticket is valid and there are no optional characters, then this cost is displayed to the
screen and the program ends.
If there are optional characters, then the cost worked out above will change depending on
the optional characters. The basic cost calculated above is not displayed to the screen if
there are optional characters until the optional characters have been checked to make
sure they are valid and the cost is modified.
Only one final cost is displayed and only if everything is valid.
The first optional character can be either '+' or '-'
If it is '+', then this has to be followed by 'M', 'E', "EM" or "ME" to be valid.
If the first optional character is not '+' or '-' then the whole ticket is invalid.
M adds 25.50 to the cost of the ticket (after the first character modifier has been applied to
the basic cost)
E adds 37.75 to the cost of the ticket (again, after the first character modifier has been
applied to the basic cost)
As noted above, if both M and E are present, the order does NOT matter. If ME (or EM) are
present, then 25.50 + 37.75 is added to the cost of the ticket, as always, after the first
character modifier has been applied to the basic cost)
If '-' is present then it must be followed by 'C', 'V', "CV" or "VC" to be valid.
C reduces the cost of the ticket by 10% - this is the last thing, after the first character modifier
and any extras, if a valid + is present.
V reduces the cost of the ticket by 25% - this is the last thing, after the first character modifier
and any extras, if a valid + is present.
CV or VC (order doesn't matter) reduces the cost of the ticket by 50% this is the last thing,
after the first character modifier and any extra, if a valid + is present.
Note in this task, only the Scanner and String classes are allowed.
OOF Assignment Part A - due: 10:00 am Mon 22nd Aug this is the first and final hand in date p. 10 of 13
Use of any other class(es) will result in the task being awarded 0, regardless of the
correctness of execution.
The user may enter a ticket String of any length. The valid length of a ticket is between 4 and
10 depending on the conditions above (if there are any options entered). Any ticket that is not
in this length range is automatically invalid. A ticket that is invalid for any reason will result in
a message being displayed to the screen saying that the ticket is invalid, and no processing
will be done with that ticket. This includes not showing any cost.
There are many reasons why a user Ticket may be invalid, even if it is within the correct
length range.
Every effort should be made to give the exact reason why the ticket is invalid. For example,
wrong length, starts with an invalid character, has a first optional character that is not '+' or '-'.
These are not the only reasons why a ticket is invalid, part of your task is to list all the
conditions that make a ticket invalid and write code to cover those cases.
User input of a valid ticket should produce the same result regardless of whether the
characters are uppercase or lowercase or a combination of the two, in other words, case
insensitive.
At first, this Task can seem quite complex. Here is a suggested strategy to break the problem
down into smaller parts, to "eat the dragon in little bits".
Start by checking the length of the user input. If the length is not between 4 and 10 inclusive,
then the Ticket is invalid, nothing further needs to be done except to display the appropriate
error message.
If the length is correct, solve the problem assuming that the user enters a correct first
character and three digits, with no optional characters.
Once you have this working correctly, solve the problem for the case where the user enters
an invalid starting character.
Then extend the program to solve the problem where the user enters a valid character first,
then the three digits, then the '+' option with either 'm' or 'e'. Then extend this for the user
entering the '+' option with "ME" or "em". (Recall that the user input has to be case
insensitive).
Continue to work through the valid options.
Once these are working, then start working on testing for user input errors and build up your
program bit by bit to detect these errors and display the appropriate error messages.
The final cases involving tickets of length 9 or 10 can be quite complicated. Recall that you
only get marks for code that actually compiles and runs. You are better off to submit a
program that correctly executes a small number of cases then a program that it much longer
but does not even compile.
OOF Assignment Part A - due: 10:00 am Mon 22nd Aug this is the first and final hand in date p. 11 of 13
Some sample runs of the program are shown below (user input is in bold):
NOTE: the samples DO NOT cover all of reasons a ticket can be invalid, that is, they do
not cover all test cases. Identifying and covering all of the cases is part of your
Task.
The samples do not always display appropriate invalid messages in most cases, your
assignment submission will display the appropriate messages. > java Ticket Enter ticket >> b100 Cost $200.0 > java Ticket Enter ticket >> A609 Invalid ticket, ticket must start with B, F or E > java Ticket Enter ticket >> F008+ Invalid ticket, + at end of ticket with no 'E', 'M' "ME" or "EM" > java Ticket Enter ticket >> 1234567890987 Invalid ticket, wrong length, length must be between 4 and 10 inclusive > java Ticket Enter ticket >> E100+m Cost $125.5 > java Ticket Enter ticket >> F050+mk Invalid character, can only be 'E' after +M > java Ticket Enter ticket >> e100-v Cost $75.0 > java Ticket Enter ticket >> e100-cv+m Invalid character(s) after the '-' modifier. The '-' modifier must be last thing in a valid ticket > java Ticket Enter ticket >> e100+Em-Vc Cost $81.625
Note that we haven't yet learnt how to control the number of decimal places that are
displayed so this will not be an issue.
Note: System.exit() is not be used anywhere in any of the Tasks.
(Marks will be deducted if it is used anywhere in this assignment.)
OOF Assignment Part A - due: 10:00 am Mon 22nd Aug this is the first and final hand in date p. 12 of 13
See Lecture/Workshop 2 for revision on the String class and Lectures 5 and 6 and
Lecture/Workshop 3 for if, if-else statements.
Electronic Submission of the Source Code
Submit all the Java files that you have developed in the tasks above.
The code has to run under Unix on the latcs8 machine.
You submit your files from your latcs8 account. Make sure you are in the same directory
as the files you are submitting. Submit each file separately using the submit command.
submit OOF Ticket.java
submit OOF Codes.java
submit OOF Stations.java
After submitting the files, you can run the following command that lists the files submitted
from your account:
verify
You can submit the same filename as many times as you like before the assignment
deadline; the previously submitted copy will be replaced by the latest one.
Please make sure that you have read page 2 about the submission close off date and
time and the compulsory requirement to attend the execution test in Week 5
Failure to do both of these things will result in your assignment be awarded a mark of
0, regardless of the correctness of the program.
Execution test marks are provisional and subject to final plagiarism checks and
checks on the compliance of your code to this assignment document.
As such final assignment marks may be lower or withdrawn completely.
Final Notes: - transferring files between Windows and Unix
Be very careful transferring files from Windows to Unix. If you do transfer a file from Windows
to Unix open the file, in Unix, using vi.
For example, if you transferred a file named b.txt from Windows to Unix
open the file in Unix with the command
vi –b b.txt
you will (might) see a lot of ^M's at the end of each line.
These MUST be removed using the command shown below or else your input file will have too
many newline characters and will not translate properly. That is, your code will not correctly
read the input file for Tasks 1 and 2.
Your code will work on Windows but NOT on Unix.
OOF Assignment Part A - due: 10:00 am Mon 22nd Aug this is the first and final hand in date p. 13 of 13
Still in vi, in command mode (press the Esc key first) do the following
:%s/ctrl-v ctrl-m//g
ctrl-v ctrl-m means hold down the control key and with the control key down press v then
press m.
Final, final notes
Don't let anyone look at your code and definitely don't give
anyone a copy of your code. The plagiarism checker will pick
up that the assignments are the same and you will both get 0
(it doesn't matter if you can explain all your code).
There will be consultation sessions for the assignment, the times will be posted on LMS, if
you have problems come to consultation.
And a final, final, final note, "eat the dragon in little bits". Do a little bit every night; before you
know it you will be finished. The assignment is marked with running code, so you are better
to have 1 or 2 Tasks completed that actually compile and run, rather than a whole lot of code
that doesn't compile.
The execution test is done on latcs8 so please make sure that your
code runs on latcs8 before you submit.

相关推荐