Lab 7c – Scanner, Arrays, and Computations

Create a file named Lab7c.java in your lab7 directory.  Write a program in Lab7c.java that satisfies the following specifications.


    • Read the contents of random.txt and store the random values in an array.
    • Print to the screen the values in the array.
    • Compute
      • the sum of the values in the array,
      • the mean average of the values in the array,
      • the minimum value in the array, and
      • the maximum value in the array.
    • Print to the screen the sum, mean average, minimum, and maximum in a well formatted manner
    • Print to the screen “Greater than mean” followed by a list of the values in the array that are greater than the mean.
    • Print to the screen “Less than mean” followed by a list of the values in the array that are less than the mean.
    • Print to the screen “Closest to mean” followed by the value in the array that is closest to the mean.

Test your program and when satisfied that it is correct, push to GitHub.

Lab 7b – Populating Array From File

Create a file named Lab7b.java in your lab7 directory.  Write a program in Lab7b.java that satisfies the following specifications.


Inside the class (but after the main method) write a method named printArray that takes an array of integers as an argument.  The method uses a for-loop to print to the screen (on a single line with spaces between them) the elements in the array that is passed to the method.

In the main method, do the following:

    • Print to the screen “Populate Array from File”.
    • Ask the user to enter a file name, read the file name from the keyboard, and store the value in a variable named fileName.
    • Create an instance of the Scanner class that can read from the file whose name is in the variable named fileName.
    • Use the Scanner to read the integer value that is on the first line of the file and store it in a variable named size.
    • Declare an array of integers whose length is equal to the value in the variable named size.
    • Read the values on the second line of the file and store them in the array.
    • Print the contents of the array to the screen using the printArray method.

Compile and test your program using the file name random.txt.  Push your code to GitHub when your code is correct.

Lab 7a – Writing Contents of Array to File

Log into cs.bridgewater.edu and navigate to your csci101/labs directory in your repository.  Create a directory named lab7 and change your working directory to lab7.

Create a file named Lab7a.java and in it, write a program that satisfies the following specifications.


Inside the class (but after the main method) write a method named printArray that takes an array of integers as an argument.  The method uses a for-loop to print to the screen (on a single line with spaces between them) the elements in the array that is passed to the method.

In the main method, do the following:

    • Print to the screen “Writing Contents of Array to File”.
    • As the user to enter an integer, read the integer from the keyboard, and store the value in a variable named size.
    • Declare an array of integers whose length is equal to the value in the variable named size.
    • Populate the entire array with random values between 1 and 100.
    • Print the contents of the array to the screen using the printArray method.
    • Create an instance of the PrintWriter class that can write to a file named random.txt.
    • Write the value in the variable named size onto the first line of random.txt.
    • Write the contents of the array onto the next line of random.txt with spaces between the values.
    • Close the PrintWriter.

Compile and test your program thoroughly.  Push your code to GitHub when your code is correct.

 

Lab 6c – Arrays

Create a file named Lab6c.java inside your lab6 directory.  Inside Lab6c.java write a program that satisfies the following requirements.


Inside the class (but after the main method) write a method named printArray that takes an array of integers as an argument.  The method uses a for-loop to print to the screen (on a single line with spaces between them) the elements in the array that is passed to the method.  (Hint: we did this in class on Wednesday)

Inside the main method of the class, do the following:

    • Declare an array that can hold 15 integers.
    • Print the values in the array to the screen by calling the printArray method.
    • Set all of the elements in the array to random integers between 0 and 20.
    • Print the values in the array to the screen by calling the printArray method.
    • Use a for-loop to compute the sum of the values in the array, then print to the screen “sum: ” followed by the sum.
    • Use a for-loop to determine how many elements in the array are even, then print to the screen “num even: ” followed by the number that are even.
    • Use a for-loop to compute the smallest value in the array, then print to the screen “smallest: ” followed by the smallest value.
    • Use a for-loop to compute the largest value in the array, then print to the screen “largest: ” followed by the largest value.

Test and when satisfied that your solution is correct, give the person next to you a hug, then push to GitHub.