Title: CSC 260 Review
1CSC 260 Review
- This set of slides contains a few items that your
CSC 260 teacher may not have covered but you
should know for this course or your teacher may
have covered but you are not very familiar with
this - This material will not be tested on an exam
directly, but it is expected that all 262
students know this material for programming
assignments and exams
2Input From Keyboard
- add import java.io. to your imported
packages/classes - add throws IOException to any method that will
have input statements - create a variable of type BufferedReader as
follows - BufferedReader keyboard new BufferedReader(new
InputStreamReader(System.in)) - to obtain input, use keyboard.readLine( )
- you may use any variable name, keyboard is a
descriptive one though - precede any input statement with a prompting
message - Notice that the prompting message should be a
print instead of a println, why?
- The 260 textbook offered the Keyboard class (from
the cs1 package) as a way to get keyboard-based
input - Instead, most faculty used the JOptionPane class
- In this course, students will be expected to get
input from the keyboard directly by using the
BufferedReader class - This is very easy to do, just follow the steps to
the right - An example is given on the next slide
3Input From Keyboard Example
import java.io. public class Student
// data instances and constructor here public
void getInput( ) throws IOException
BufferedReader keyb new BufferedReader (new
InputStreamReader(System.in))
System.out.print(Enter the students name
) name keyb.readLine( )
System.out.print(Enter the number of hours this
student has earned ) hours
Integer.parseInt(keyb.readLine( ))
System.out.print(Enter the students GPA )
gpa Double.parseDouble(keyb.readLine(
)) // etc
4I/O From/to Disk
- Inputting from disk or Outputting to disk is very
similar to inputting from keyboard - The class name for input is again BufferedReader
but rather than a new InputStreamReader, we use a
FileReader - Output goes to a PrintWriter which uses a
FileWriter as in - PrintWriter outfile new PrintWriter(new
FileWriter(filename)) - where filename is a String that is the name of
the file as it will appear to the Operating
System, including any subdirectories - filename can be input from the keyboard, or be a
literal value but in the case of a literal, /
must be // instead - Like inputting from keyboard, dont forget import
the java.io. package and add throws IOException
to the method header - An additional instruction is to close the file
when done as in outfile.close( ) - An example follows on the next slide which asks
the user for a file name to be input, reads this
input file copying every line to an output file
called c/CSC262/CopiedFiles/firstfile.txt
5Input/Output to Disk Example
import java.io. // class definition goes here
public void copyfile( ) throws IOException
BufferedReader keyb new
BufferedReader(new InputStreamReader(System.in))
System.out.print(What file do you want to
copy? ) String filename
keyb.readLine( ) BufferedReader infile
new BufferedReader(new FileReader(filename))
PrintWriter outfile new PrintWriter (new
FileWriter(c//CSC262//CopiedFiles//firstfile.txt
)) String nextLine infile.readLine( )
while(nextLine ! null)
outfile.println(nextLine) nextLine
infile.readLine( ) infile.close( )
outfile.close( )
Notice the use of // here but if you type in
the filename via the keyboard, type it in as
c/CSC262/CopiedFiles/firstfile.txt
6Final Comments on I/O
- Each object that is a BufferedReader, PrintWriter
or other represents an input or output stream - You can have as many streams as you want
- In the previous example, we had 3, one input
stream from keyboard, one from disk, one output
stream to disk - However, a stream can only be an input or an
output stream, not both - Also notice the use of the while loop to read
each line from one file and write to another, we
looped while the input was not null, this is
common
- There are other I/O objects in Java, but these
will suffice for our purposes in 262 - Notice that we used readLine( ) and println( )
statements, there are also read( ) and print( )
statements, but unless you know exactly what you
are doing, you should avoid using these and use
only the readLine( ) and println( ) versions - One last comment, if a method calls a method that
might throw IOException, the calling method must
also have throws IOException in its header
7Menu-Driven Code
- One way to promote user-friendliness is to write
programs where the user selects what should
happen through a menu of options - Menus can be done either in a graphical
environment or in a text-based environment - We will focus on the latter of those here and
look at graphical solutions later in the semester
- Basic strategy
- Loop
- Display the menu
- Ask the user for his/her choice
- Select the appropriate set of code or call the
appropriate method based on the users choice - Until the user selects the exit/quit option
- We implement this by using a do-while loop for
the loop and a switch statement to select the
appropriate set of code - An example follows on the next slide
8Menu Example
public void menuExample( ) throws IOException
int choice BufferedReader keyb
new BufferedReader(newInputStreamReader(System.in)
) do displayMenu( )
do System.out.println(Ent
er your choice (1 5) ) choice
Integer.parseInt(keyb.readLine( ))
while (choice choice 5) switch (choice)
case 1 / do whatever choice 1
is / break case 2 / do
whatever choice 2 is / break
case 3 / do whatever choice 3 is / break
case 4 / do whatever choice 4
is / break while
(choice ! 5)
notice the user of another method to display
the menu Assume choice 5 is quit and 1-4
do other things
9Data Verification
- The previous example included data verifying the
users choice - This is a useful habit to ensure that the users
input value does not exceed a range based on the
programs expectations - To data verify, wrap the input statement in a
do-while loop that continues to loop until the
users input is within legal bounds - Here is an example where we expect the user to
input a test score between 0 and 100
do System.out.print(Enter a score
between 0 and 100 ) score
Integer.parseInt(keyboard.readLine( ))
while (score 100)
10Modifiers
- You have by now learned several modifiers for
variables and methods, make sure you use them
appropriately - public
- Data instances should never be public unless
there is a very good reason, methods should be
public if they make up the interface for the
class that is, you intend these methods to be
invoked from other classes - private
- All data instances should be private, methods
should only be private if they are intended only
to be called from inside the same class - static
- For now, only make items your main program
static, but all items (data instances declared
there, and all methods) in the main program must
be static, we will see other uses of static later
in the semester - final
- This is used to create a constant, you should use
constants as needed, but never declare a variable
to be final unless you know for sure that it will
never change values