Assignment 20
Due Tuesday, June 19th at 1:20pm


Assignment 20.1: Barnyard Animals (10 pts)
  • In this assignment we write a program that copies five numbers from an input stream to an output stream.
  • Create a new Java class named Animals.java in a project named Animals
  • Then, create a text file to be used in the program..
  • Right-click the Animals project folder and select New->File. Name your file infile.txt.
  • Make sure that you did not accidentally save your file under the src folder. If you did, you can simply drag and drop it into the main project folder.
  • Here are the values to place in your file:
5
3
8
2
4
  • We will read from this file after writing our program.

  • Follow a similar process to create a blank text file named outfile.txt.
  • Add the following import statement to the top of your program
    import java.io.*;
    
  • Alter the signature of main() as required for File I/O (see lesson notes if you are unsure).
  • Then, declare a new file object for your input file, and a new Scanner to read in from this file
  • Next, declare another file object for your output file and a new PrintWrite to write to this file.
  • Declare 5 integer variables at the top of your program, one for each number in the file.
  • Then, use the Scanner to read in each number one-by-one and store it in a variable
  • For example, here is possible code for reading the first number:
    int first;
    first = input.nextInt();
    
  • Add 4 more pairs of statements like the ones above to read in the next 4 numbers.
  • Then, add an output statement to print out a title at the top of your file
    out.println("Animals at our farm");
    
  • Next, write out the contents of your five variables to the file. For example:
out.println(first + " chickens");
  • Add 4 more statements like the one above, so that your file contains the following when you are finished:
Animals at our farm
5 chickens
3 cows
8 ducks
2 horses
4 pigs
  • Close the streams when finished reading and writing:
    input.close();
    out.close();
    
  • Compile and run your program to make sure you made the changes correctly.
  • Notice that you do not see any output on the console screen. The output stream wrote the program output to the output file.
  • Open the output file you created and verify that your file contains the following:

    Animals at our farm
    5 chickens
    3 cows
    8 ducks
    2 horses
    4 pigs
  • Submit your program Animals.java program to Canvas when you are finished


Assignment 20.2: MoreBarnyard Animals (10 pts)

  • In the last assignment we wrote a program to read in numbers from a file and save them in 5 separate integer variables.
  • Now, we will alter the above assignment so that it uses arrays.
  • From the previous version of your program, remove the 5 integer variables that you declared at the top of main, and instead, declare an integer array of length 5.
int numAnimals[] = new int[5];
  • Now, alter your 5 input.nextInt() statements to store values, each at one index of the array
  • Next, at the top of main declare a String array named animals of length 5.
  • Store the following values in the String array, one at each index:
"sheep"
"donkeys"
"goats"
"geese"
"cats"

  • We are now going to write the contents of our two arrays to the output file.
  • Below the statement System.out.println("Animals at our farm");replace the previous 5 out.println statements with a for loop.
  • This for loop to print out the contents of two arrays to the output file.
  • Hint, use one of the array's length variables in the test condition of your for loop.
  • Finally, compile and run your code and verify that the following the same output appears in the file as in the previous version of the program:

    Animals at our farm:
    5 sheep
    3 donkeys
    8 goats
    2 geese
    4 cats
  • Submit your Animals.java program to Canvas when you are finished


Assignment 20.3: Number of Numbers (10 pts)

  • For this assignment you will be working with arrays.
  • Open a new Java project called Nums
  • Prompt a user to enter in the length of the array (the number of numbers).

Enter the number of numbers:
  • Use this information to declare an array of the user-specified length.
  • Next, using a for loop, prompt the user to enter that many numbers, and store each one in your array until your array is at full capacity.
  • Using the same, or a second, for loop, sum the numbers and report their sum to the user.
  • Also, multiply the numbers together and report their product to the user.
  • The sum and the product should be printed to one decimal value only.
  • Your program should work identically to the sample output below:

Enter the number of numbers: 4
Enter 4 numbers:

Number 1: 3.2
Number 2: 6.7
Number 3: 2.9
Number 4: 4.6

The sum of the numbers is: 17.4
And the product is: 286.0


  • Another run of the program might give the following output:

Enter the number of numbers: 8
Enter 8 numbers:

Number 1: 2.1
Number 2: -5.6
Number 3: 9.0
Number 4: 8.7
Number 5: -2.2
Number 6: 8.2
Number 7: 9.5
Number 8: 1.4

The sum of the numbers is: 31.1
And the product is: 220931.3

  • Upload Nums.java to Canvas when you are finished.