CS 171 Lab 9



Handed out:  Tue, Apr 11, 2000

Date Due:  Mon, Apr 24, 2000.



 
1. Write a Java class named lab9a that will read up to 100 integer numbers from the keyboard. Do not print out a prompt like "Enter a number" -- your program should just assume that numbers are going to be entered, one per line. As you read the numbers in, store them in an array.  After you have read in all the numbers, then print the numbers back out in reverse order.

To know when the input stops, "stdin.readLine()" returns a value called "null" if there is no more input. When you are entering numbers from the keyboard, to end the input you must type Control-D (i.e., hold down the "Ctrl" key and press the "D" key (no shift)). You can assign a string variable the value that is returned from "stdin.readLine()" and test it at the same time, by doing the following:

class readnums
{
  public static void main(String Args[])
  throws IOException
  {
     int val;
     String line;
     BufferedReader stdin =
        new BufferedReader(
        new InputStreamReader(System.in));
     while ((line = stdin.readLine()) != null)
     {
        val = Integer.parseInt(line);
        System.out.println("val is " + val);
     }
  }
}

This program will read lines of input and print out the integer value that was typed in. You should use it as a template for this problem. To test your program, it would be easier to have a file of numbers that you don't have to keep typing in. Unix allows you to send a data file to a program as if the contents of the file were typed in from the keyboard. This is called I/O redirection. On the command line, you write " < filename " after the name of your program, and the lines in the file act as if you had typed them in. A sample file of numbers is "~gupta/pub/unsorted".

Remember, there may not be 100 numbers. Your program should work for any amount of numbers, up to 100.


2. Write a Java class named lab9b that will input an array of up to 100 numbers from a file whose name is a command line argument. This program will read directly from the file, unlike lab9a, which assumed keyboard input. To do this, you can use the following lines in your program in place of your BufferedReader line for keyboard input:

With this line, you can use the expression "inputfile.readLine()" just like you had used "stdin.readLine()". But the lines will come from the file instead of keyboard input.

After you have read in all the numbers, then perform the following actions:

  1. Input a number from the keyboard.
  2. Use the linear search method presented in class to search for the number in the array.
  3. Display a message to tell the user whether or not the number was found. If it was found, print out the index of where it is in the array.

3.   Write a Java class named lab9c that will determine all the prime numbers less than 1000, and somehow store that knowledge.  A prime number is a number that is only divisible by itself and 1. HINT: look at the operator '%' (Section 3.4). Once the prime numbers have been determined, allow the user to input a number at the keyboard.  If the number is prime, print the number and "is prime".  If the number is not prime, print the number and "is not prime".  It should decide this by looking it up from where you stored the knowledge, not by brute force again. Repeat inputting numbers and printing messages until the user enters a negative number. (hint: you will probably use an array somehow, for example you could define an array of size 1000 of type boolean called primes. If i is a prime then, primes[i] will hold the value "true", otherwise it will hold the value "false").


4. Write a Java class named lab9d that will take a file name as a single command-line argument, and input an array of up to 1000 numbers (integers) from the file. It should then use bubblesort to sort these numbers. The sorted array should be printed on the screen.


5. Extend the class lab9e as follows: after the sorted array is obtained in the array, input another number from the terminal. Use binary search discussed in class to check if this number is present in the sorted array. Print an appropriate message (number found or not found) on the terminal.