CPE 102

Winter 2016

Lab 03

 

 

Objectives

 

 

Resources

 

  1. Your text.
  2. Your peers.
  3. Your instructor.
  4. The Java Standard Library. Book-mark this web page, you will be using the Java Standard Library documentation regularly this quarter. Find String in the alphabetic ordered "All Classes" frame and pay special attention to the discussion of the "+" operator for string concatenation, especially useful for this lab. 

 

Ground Rules

 

  1. You must use the ArrayList class to store the various user responses. Look up ArrayList in the Java Library and become familiar with how it works. ArrayList is convenient to use instead of a Java array when you will be adding an unknown number of items to your list. (see Hints & Suggestions below for assistance).
  2. You must use the Scanner class to read the user input and to determine its type (see Hints & Suggestions below for assistance).

 

Orientation

 

You will be developing a simple Java program that makes use of the ArrayList and Scanner classes from the Java Standard Library to read user input from the command line and separate the responses into different collections based on the type of response.  You can (and should) write this program in one source file containing one class and with only a main method. No instance variables, constructors, or other methods are required.

 

Specification

 

  1. In a file called Lab3.java implement the main for a Java program.  Remember, all methods must be defined within a class and you should know what the class name is based on the given file name.

 

  1. Write the entire program in the main method.  Use only local variables (no instance variables).  The program should prompt for user input and then determine if the response was an integer, double, or some other string of characters.  Depending on the type of the response, it should be placed in one of three different ArrayList objects.  The program should repeatedly prompt and filter the input until the user types quit.  The program then prints out all of the integer values, double values, and other values in a specified format.  Sample runs of the program are provided below (the user responses are bold for clarity only).  Note that your program must work for any data in any order:

 

Sample Run 1

 

Enter an int, double, any random text, or type "quit" to end: Here is some text

Enter an int, double, any random text, or type "quit" to end: 95

Enter an int, double, any random text, or type "quit" to end: more text

Enter an int, double, any random text, or type "quit" to end: 0.765

Enter an int, double, any random text, or type "quit" to end: -6.7

Enter an int, double, any random text, or type "quit" to end: 2

Enter an int, double, any random text, or type "quit" to end: abc

Enter an int, double, any random text, or type "quit" to end: 4.5.6

Enter an int, double, any random text, or type "quit" to end: 789

Enter an int, double, any random text, or type "quit" to end: quit

Integers:

   Integer[0] is 95

   Integer[1] is 2

   Integer[2] is 789

Doubles:

   Double[0] is 0.765

   Double[1] is -6.7

Other:

   Other[0] is Here is some text

   Other[1] is more text

   Other[2] is abc

   Other[3] is 4.5.6

 

Sample Run 2

 

Enter an int, double, any random text, or type "quit" to end: quit

Integers:

Doubles:

Other:

 

Hints & Suggestions

 

  1. The ArrayList class is a generic collection class meaning it can be used to collect any type of object.  You must specify the type of object that will be collected in an ArrayList when you declare and construct it.  You will be working with int, double, and String values.  You cannot store primitive data values (byte, char, short, int, long, float, double, or boolean) in an ArrayList.  To add integer and real values to an ArrayList you will need to use the appropriate wrapper classes.  A wrapper class is a class that "wraps" some other type and provides methods to operate on the wrapped type.  There is a class called Integer that wraps primitive int values and a class called Double that wraps primitive double values in the Java Standard Library. Read the javadocs for help on using these classes.

 

  1. Here is an example of declaring and constructing an ArrayList for Integer objects:

 

ArrayList<Integer> intList = new ArrayList<Integer>();

 

Notice that the type to be collected in an ArrayList, in this case Integer, is specified immediately after the ArrayList class by putting it inside the <>-brackets.  To create an ArrayList to collect different types simply substitute the appropriate class name for Integer in the example above.

 

  1. Here is an example of constructing an Integer to wrap a primative int and add it to the ArrayList from #2 above:

 

intList.add(new Integer(7));

 

Notice that the example above shows adding the literal int 7 to the ArrayList. You will be constructing Integer objects from ints scanned in using the Scanner class.

 

  1. Recall that the Scanner class can be used to read from the command line. To do so you need to construct a Scanner object as follows:

 

Scanner scanner = new Scanner(System.in);

 

You would then call the appropriate methods to read in different types of data.  The Scanner class has several methods to determine the type of the next data to read without actually reading the data. Read the javadocs for the class to see what methods are available and some nice examples on how to parse text for various types of data using this class.

 

  1. When using the various "next" methods of the Scanner class to read user input you need to know that none but nextLine() will read the newline character.  So, to avoid reading the newline that follows int or double values input by the user as the next user input, you will need to call nextLine() after reading int or double values.  You should use nextLine() to read any other input, (i.e., not int or double), so it won't be an issue in that case.

Lab courtesy of Julie Workman.