Watch the Lesson Video HERE Welcome to Lesson 21! Learning Objectives
By the end of today's class, you should know...
Announcements
Lesson 20 Practice Exam Questions
/** * Given an array of Strings, print the array * to the console, with each element in the array * separated by a blank space * the method should be named printArray * @param data the array of String values */
Wrapping Up File I/O
Using Loops to Read Files
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.io.*;
|
Some Test Methods of a Scanner Object
Method | Description |
---|---|
hasNext() |
Returns true if this scanner has another token in its input. |
hasNextLine() |
Returns true if there is another line in the input of this scanner. |
hasNextDouble() |
Returns true if the next token can be interpreted as a double value. |
hasNextInt() |
Returns true if the next token can be interpreted as an int value. |
Activity 21.1: Averages (10pts) - due Thursday, March 19 at 11:20am
- We are going to write a program that takes in a list of numbers of unknown size from a file, and then outputs the average of these numbers.
- This activity will help you practice using a loop to help you read data from a file.
- Open up a new Java project and name it Averages
- Now add the two import statements that are required for file I/O to the top of your program:
import java.io.*;
- Next, lets create a new File variable at the top of main and pass it in the name of the file "nums.txt"
- Then, let's Scanner to open this file.
- Next, lets read in the numbers from the file. Since we are computing the average of the numbers, we need two pieces of information
- Remember: Average = sum / count
- Therefore, as we read in the numbers from the file, we need to computer their sum and count how many there are.
- We will therefore need two variables to keep track of this information. Add the following two variable declarations to the top of main:
- We will also need a variable to temporarily store each number as we read it in from the file. Add an additional variable declaration to the top of main like so:
- Now, let's read in the numbers from the file and process them inside a loop. Add the following loop to your program:
num = input.nextDouble();
}
- Now, let's print the average to the console and output it to a file.
- To print the average to the console, add the following line of code:
- To output the average to a file, we need to open a new output file. Below the print statement, add the following line of code:
- Then, we need to connect this file to an output stream.
- Finally, lets write the average to the file.
- As a last step we need to close our input and output streams. Add the following lines of code to the bottom of main:
- Let's create a file to test our program. Open up a new empty file in Eclipse and name file nums.txt. In the file, add a list of numbers like this:
- Now, run the code and open up your file to make sure everything is working properly. Open up the file averages.txt and verify that you got the correct output.
- When you are finished, upload Averages.java to Canvas.
Activity 21.2: Sonnet Statistics (10 pts) due Thursday, March 19 at 11:20am
- Open up a new Java project named Statistics and add the starter code below to it:
import java.util.Scanner;
public class Statistics {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
int count = 0;
String word, line;
//rest of code goes here
}
}
-
Now, using Eclipse, create a new empty file called sonnet.txt and copy and paste the below sonnet into your file:
- We are going to write some code to count the number of words and the number of lines in this file.
- Declare two new files at the top of your program:
File infile = new File("sonnet.txt");
File outfile = new File("statistics.txt");
- Also declare a new Scanner and connect it to infile.
- Now, let's write a while loop to count up how many words are in the file.
- Finally, declare a new PrintWriter, connect it to outfile and print out the number of words contained in the file
- Now, close your input file stream.
- Now, we need to count the number of lines in the file.
- For this purpose, we are going to need a new input file stream as we already used the previous one to count the number of words in the file (and we cannot reset the input stream to point to the beginning of the file).
- Add the following code to your program below the statement to close fin:
- Next, we are going to write a while loop to count the number of lines in the file.
while (?????) {
}
- Write an print statement to print out the number of lines in the sonnet.
- Finally, close input and output and run your program.
- Note that you should get 114 words and 14 lines.
- Did you get the expected result inside of statistics.txt?
- Submit your code to Canvas when you are finished.
Activity 21.3: Methods + Arrays Worksheet (10 pts) - due Thursday, March 19 at 11:20am
- Open a new Java file called ArrayMethods.java.
- Read the Javadoc comment for each method below and write the method according to the description in the comment
- Once you are getting the correct output for all tests inside of main, upload MethodicalArrays.java to Canvas
/**
* Displays the contents of an array on the console
* with each element separated by a blank space
* Prints a new line character last
* @param nums the array to print to the console
*/
public static void printArray(int[] nums) {
return;
}
/**
* Assuming an array of integers, return true if 1
* is the first element or 1 is the last element
* Note: you may assume that you will be given an array
* of at least length 1
* is1FirstLast([1, 2, 10]) → true
* is1FirstLast([10, 1, 2, 1]) → true
* is1FirstLast([13, 10, 1, 2, 3]) → false
* @param numbers the array of int numbers
* @return whether or not 10 is the first or last element
*/
public static boolean is1FirstLast(int numbers[]) {
return false;
}
/**
* Given an array of int numbers, return true
* if the array is size 1 or more,
* AND the first element and the last element are the same.
* sameFirstLast([1, 2, 3]) → false
* sameFirstLast([1, 2, 3, 1]) → true
* sameFirstLast([1, 2, 1]) → true
* @param numbers the array of integers
* @return whether the first and last element are the same
*/
public static boolean sameFirstLast(int[] numbers){
return false;
}
/**
* Given an array of int numbers, add 10 to each element in the array
* return nothing.
* arrayAdd10([1,2,3]) -> [11, 12, 13]
* arrayAdd10([10, 30, 50, 79, 85]) -> [20, 40, 60, 89, 95]
* arrayAdd10([5]) -> [15]
* @param array the array of integer values
*/
public static void arrayAdd10(int array[]) {
return;
}
/**
* Given an array of ints numbers, multiply each element in the array by itself
* arraySquared([1,2,3]) -> [1, 4, 9]
* arraySquared([9, 10, 11, 12]) -> [81, 100, 121, 144]
* arraySquared([151]) -> [22801]
* @param array the array of integer values
*/
public static void arraySquared(int[] array){
return;
}
public static void main(String[] args)
{
boolean answer;
System.out.println("***Testing is1FirstLast***\n");
int array1[] = {1, 2, 10};
answer = is1FirstLast(array1);
System.out.println("Should print true: " + answer);
int array2[] = {10, 1, 2, 1};
answer = is1FirstLast(array2);
System.out.println("Should print true: " + answer);
int array3[] = {13, 10, 1, 2, 3};
answer = is1FirstLast(array3);
System.out.println("Should print false: " + answer);
System.out.println();
System.out.println("***Testing sameFirstLast***\n");
int array4[] = {1, 2, 3};
answer = sameFirstLast(array4);
System.out.println("Should print false: " + answer);
int array5[] = {10, 20, 50, 60, 80, 90, 10};
answer = sameFirstLast(array5);
System.out.println("Should print true: " + answer);
int array6[] = {1};
answer = sameFirstLast(array6);
System.out.println("Should print true: " + answer);
System.out.println();
System.out.println("***Testing arrayAdd10***\n");
int array7[] = {1, 2, 3};
arrayAdd10(array7);
System.out.println("Should print 11 12 13: ");
printArray(array7);
System.out.println();
int array8[] = {10, 30, 50, 79, 85};
arrayAdd10(array8);
System.out.println("Should print 20 40 60 89 95: ");
printArray(array8);
System.out.println();
int array9[] = {5};
arrayAdd10(array9);
System.out.println("Should print 15: ");
printArray(array9);
System.out.println("\n");
System.out.println("***arraySquared***\n");
int array10[] = {1, 2, 3};
arraySquared(array10);
System.out.println("Should print 1 4 9: ");
printArray(array10);
System.out.println();
int array11[] = {9, 10, 11, 12};
arraySquared(array11);
System.out.println("Should print 81 100 121 144: ");
printArray(array11);
System.out.println();
int array12[] = {151};
arraySquared(array12);
System.out.println("Should print 22801: ");
printArray(array12);
System.out.println("\n");
System.out.println("***End of Tests***");
}
}
- With a partner, answer the questions from today's learning objectives
Upcoming Assignments
- Activities 21.1-21.3 due Thursday at 11:20am
- Assignment 21 due Thursday at 11:20am
- Lab 10 due Friday
- Final Exam on Tuesday, March 24!