Download Software Development ( 2500) Assignment 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Software Development (cs2500) Assignment 2
Scanning Input (Due: October 26th. Marks: 10)
Rules and Regulations for All Assignments
Regulations
For the cs2500 assignments it is assumed that you complete them without help or assistance
from anybody. However, you are allowed to discuss the assignment with other students in
general terms (only). Here general terms excludes showing your code, sharing your code, and
using other people’s code (including code from the web).
Demonstrators
If you have technical problems then you may ask the demonstrators for help. You may also
ask them questions about theory in general. Finally, you may ask them questions about
the assignment in general terms — as opposed to “I don’t know what to do. What should I
type here?”
The demonstrators have been instructed:
• To do the best they can to help you understand the theory.
• To give clues about the assignments.
• Not to give you actual code.
Assignment 2: Scanning Input (10 Marks)
This assignment is about creating arrays, reading input (also known as scanning), filling
the arrays with the input, and printing what’s in the arrays. The assignment consists of two
parts. The first part is about basic scanning. The second part is about robust scanning.
The following section explains how to scanning is done in Java. This is followed by
a section which outlines assumptions about the input and the remainder describes the
assignment.
1
Introduction to Scanner Objects
Many programs take their input from stdin, which is Unix speak for standard input. For the
purpose of this assignment you may assume that standard input means ‘the keyboard,’ but
technically speaking this is not correct.
To implement a program that reads from stdin, you usually use a Scanner object. A
Scanner object is to reading input what a BitSequencer object1 is to getting the bits of
a bit sequence. As a matter of fact, many Java classes have a similar api (Application
Programming Interface): you create an object and then use the object’s instance methods
to get “the” next thing from the object.
The Scanner class provides the following instance and constructor methods.
Scanner( String source ): This creates a Scanner object that reads its input from the
String source. You don’t need this constructor method for this assignment but it
may be useful for testing.
Scanner( InputStream source ): This creates a Scanner objects that reads its input from
the InputStream source. This is the constructor which is needed for reading input
from stdin. You should use it as follows:
Scanner scanner = new Scanner( System.in );
Java
Here the ‘System.in’ corresponds to stdin. Of course you may also use a different
identifier for the variable.
boolean hasNext( ):
This method returns true iff (if and only if ) there is more input.
String next( ): This method removes the next token from the input and returns it as a
String. The method results in a runtime error if there is no more input left, so you
should only use next( ) if hasNext( ) returns true.
boolean hasNextInt( ):
This method returns true iff there is more input and the next
“thing” in the input “is” an int.
int nextInt( ): This method removes the next token from the input, converts
int, and returns the result of the conversion as an int. This method should
used if hasNextInt( ) returns true.
it to an
only be
You may read more about Scanner objects in the online api documentation at http:
//download.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html.
Before you can use the Scanner class you have to import it:
Java
import java.util.Scanner;
1 See
Lectures 9 and 10.
2
Using stdin
This section presents several tips which may be useful when dealing with programs reading
from stdin. These tips should be useful for most of the cs2500 assignments. If you
attended the first two lectures then you should know most of this already.
• In interactive mode, a program that reads from stdin will read any text you input
from the keyboard. However, it will only read the input line by line: when you hit the
hreturni key the operating system passes the next line to the program.
• Typing hCtrl-Di (simultaneously pressing the hCtrli-key and the hDi-key) tells the
operating system that there is no more input left. Basically, this ends the input stream.
However, it does not necessarily mean the program terminates because the program
may still be processing previous unprocessed lines of input.
• You may also run a program in combination with input redirection. Here you prepare
the input in a file, save the file, and use what’s in the file as the input of the program.
In Unix you do this as follows:
$
Unix
hprogrami < hinput filei
Session
In examples like this, the ‘$’ is the prompt. If input is the name of the input file and
if ‘java MyLuvelyClass’ is the name of the program then you use:
Unix
$ java MyLuvelyClass < input
Session
• Remember that output redirection lets you save the output of a program in a file. In
Unix you use output redirection as follows:
$
Unix
hprogrami > houtput filei
Session
This runs hprogrami and saves the standard output of hprogrami in houtput filei.
Of course you can combine input and output redirection.
$
hprogrami < hinput filei > houtput filei
Unix
Session
Unix
Session
The following also works.
$
hprogrami > houtput filei < hinput filei
Assumptions
For sake of this assignment you should only use ints as input of your programs. Furthermore, the first int should be non-negative. Do not use other input.
3
Part 1 (6 Marks)
This part of the assignment is about basic scanning. Your main task is to implement a
program that should work as follows:
• It starts by creating a Scanner object. Your program should create only one Scanner
object. From now on, the program should read its input using the Scanner object.
• The program reads an int maxNumbers. Remember that it is assumed that this int
should be non-negative.
• It continues by creating an int array that holds exactly maxNumbers ints. The following
shows how you create an array that holds exactly maxNumbers.
int[] array = new int[ maxNumbers ];
Java
• The program continues by reading maxNumbers ints and storing them in the array.
Remember that Java arrays are indexed by index positions, which should be nonnegative ints. The first index is 0 and the last is ‘array.length - 1,’ where array
is the array variable. For example, the following shows how you put a 3 in the first
position in the array and get the value from position 2.
Java
array[ 0 ] = 3;
int number = array[ 2 ];
The order in which you put the ints in the array matters. The first int should be
stored at index position 0 in the array, the second at position 1, and so on.
• Once the array is filled, the program should print the stored ints in reverse order.
Implement this part of the assignment as a single Java source file which is named PartOne.java.
Part 2 (4 Marks)
In this part you will make the previous program more robust. Specifically, you will take
into account that scanning may fail. This time the program should work as follows.
• It starts by creating a Scanner object.
• From now on, the program will read its input using the Scanner object.
• The program attempts to read an int integer maxNumbers. If the program fails reading
the integer, it should set maxNumbers to 0.
• It continues by creating an array that can hold exactly maxNumbers ints.
4
• The program continues by reading (up to) maxNumbers ints and storing them in the
array. However, this time, the program should stop reading if the Scanner object has
no more ints.
• Finally, the program prints out all stored ints in reverse order.
Implement this part of the assignment as a single Java source file which is named PartTwo.java.
Submission Details
• Make sure that each program starts with a comment like the following:
/**
* Name:
* Number:
* Assignment:
*
* Purpose:
*
*
* Statement:
**/
Java
Fill in your name.
Fill in your student ID.
Number of the assignment.
(Add Part Number if there is a Part Number.)
The purpose of the program. This should be
your description: do not copy and paste
from the assignment description.
I have read and understood the rules and regulations.
• Use the Computer Science Assignment Submission Website and upload your program
as a single .tgz archive called Lab-2.tgz before 23.59pm, October 26th, 2011. You may
find the website at https://cosmos.ucc.ie/submit/index.php. To create the .tgz
archive, do the following:
– Create a directory Lab-2 in your working directory.
– Copy all required files into the directory.
– Run the command ‘tar cvfz Lab-2.tgz Lab-2’ from your working directory.
The option ‘v’ makes tar very chatty: it should tell you exactly what is going into
the .tgz archive. Make sure you check the tar output before submitting your
archive.
• This week there is no need to comment your programs.
• However, marks are deducted for poor choice of variable names and/or poor layout.
• No marks shall be awarded for programs that do not compile.
5