1 2 Coza 4 Loza Coza Woza 8 Coza Loza 11 Coza 13 Woza Coza Loza 16 17 Coza 19 Loza Coza Woza 22 23 Coza Loza 26 Coza Woza 29 Coza Loza 31 32 Coza ...Note that when a number is divisible by both 3 and 5, such as 15, both Coza and Loza are printed. The same applies when a number is divisible by any of the other combinations.
Write a test class for your program and call the appropriate method(s) to get it to print the desired output.
See solution (CozaLozaWoza.java).
1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81
Write a test class for your program and call the appropriate method(s) to get it to print the desired output.
See solution (TimeTable.java).
Note: This program reads words from a file using the Scanner class. You won't be tested on this part in this course. Recall that the Scanner constructor takes the location of where it should read input from and lets us process it. Previously, we used System.in to represent the keyboard input from users. Now, we give it a File object, so the text stored in that file is the location of where we will process the text from.
If you have the following text stored inside "textfile.txt":
Understanding the Patterns there is a lot of Common patterns in English to considerWhat output will be printed? Trace through your loops and convince yourself the output makes sense.
import java.awt.*; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import javax.swing.JPanel; /** * class to reads in a text file and counts the number of vowels in it */ public class TextProcessing { String filename; Scanner fileScan; int numA, numE, numI, numO, numU; /** * constructor * when files are used, we have to handle specific exceptions (see next course) * @throws FileNotFoundException */ public TextProcessing() throws FileNotFoundException { filename = "textfile.txt"; File myFile = new File( filename ); fileScan = new Scanner( myFile ); numA = 0; numE = 0; numI = 0; numO = 0; numU = 0; System.out.println( "reading from: " + filename ); } /** * counts the number of vowels in all the words in a file * @param void * @return void */ public void procFile() { // while there is still another token in the file while( fileScan.hasNext() ) { String oneWord = fileScan.next(); // count how many vowels there are in oneWord, increment accordingly } } } /** * print out the counted vowels * @param void * @return void */ public void prNumVowels() { System.out.println( "A\tE\tI\tO\tU" ); System.out.println( numA + "\t" + numE + "\t" + numI + "\t" + numO + "\t" + numU ); } }
See solution (procFile()).