Lesson 6-2: Count-Controlled Loops

______

Name ______Date ______

Section______

Use application HiScore for Exercises 1 and 2.

______

// Application HiScore reads and prints three test scores.

// The largest value of the three is printed with an

// appropriate message

// Assumption: The scores are unique

import java.util.Scanner;

public class HiScore

{

static int largest(int one, int two, int three)

{

int highest = 0;

if (one > two)

highest = one;

else

highest = two;

if (three > highest)

highest = three;

return highest;

}

public static void main(String[] args)

{

Scanner inData = new Scanner(System.in);

int score1;

int score2;

int score3;

// Prompt for and read in scores

System.out.println("Enter three test scores.");

score1 = inData.nextInt();

score2 = inData.nextInt();

score3 = inData.nextInt();

System.out.println("The test scores are " + score1

+ ", " + score2 + ", " + score3);

System.out.println("The highest is "

+ largest(score1, score2, score3));

}

}

______

Exercise 1:In order to test application HiScore in Laboratory 5, you had to run it six times with different data in order to thoroughly test it. Rewrite the body of function main so that the application runs six times. Run your program with the data from Laboratory 5.

public static void main(String[] args) {

Scanner inData = new Scanner(System.in);

int score1;

int score2;

int score3;

for (int i = 0; i < 6; i++) {

// Prompt for and read in scores

System.out.println("Enter three test scores.");

score1 = inData.nextInt();

score2 = inData.nextInt();

score3 = inData.nextInt();

System.out.println("The test scores are " + score1 + ", "

+ score2 + ", " + score3);

System.out.println("The highest is " + largest(score1, score2,

score3));

}

}

Ouput:

Enter three test scores.

12 23 43

The test scores are 12, 23, 43

The highest is 43

Enter three test scores.

22 44 66

The test scores are 22, 44, 66

The highest is 66

Enter three test scores.

99 79 100

The test scores are 99, 79, 100

The highest is 100

Enter three test scores.

23 32 12

The test scores are 23, 32, 12

The highest is 32

Enter three test scores.

56

34 78

The test scores are 56, 34, 78

The highest is 78

Enter three test scores.

34 33 67

The test scores are 34, 33, 67

The highest is 67

Exercise 2:Change application HiScore so that the number of data values is a variable rather than a literal. The number of data values is prompted for and read from System.in. Your output should be the same for this exercise and Exercise 1.

public static void main(String[] args) {

Scanner inData = new Scanner(System.in);

//int score1;

// int score2;

// int score3;

int numberOfDataValues = 3;

int score[] = new int[numberOfDataValues];

// Prompt for and read in scores

System.out.println("Enter three test scores.");

score[0] = inData.nextInt();

score[1] = inData.nextInt();

score[2] = inData.nextInt();

System.out.println("The test scores are " + score[0] + ", "

+ score[1] + ", " + score[2]);

System.out.println("The highest is " + largest(score[0], score[1],

score[2]));

}

Exercise 3:In Lesson 4–3, you were asked to enhance class Distance with methods add and toString and to normalize the arguments in the constructor. Your test plan must have been very long or required you to run your program many times. Rewrite your test plan, using one or more count-controlled loops. (In case you never finished that assignment, a copy of the enhanced version of class Distance is provided.)

Answer: Test will be done using a counter controlled loop which will run 100 times

Exercise 4:Implement your test plan.

Include this main method in distance class:

public static void main(String[] args) {

for(int i=0;i<100;i++){

System.out.println(new Distance(i,i*200,i+40));

}

}

Ouput:

0 feet, 0 yards, and 40 miles

1 feet, 200 yards, and 41 miles

2 feet, 400 yards, and 42 miles

0 feet, 601 yards, and 43 miles

1 feet, 801 yards, and 44 miles

2 feet, 1001 yards, and 45 miles

0 feet, 1202 yards, and 46 miles

1 feet, 1402 yards, and 47 miles

2 feet, 1602 yards, and 48 miles

0 feet, 43 yards, and 50 miles

1 feet, 243 yards, and 51 miles

2 feet, 443 yards, and 52 miles

0 feet, 644 yards, and 53 miles

1 feet, 844 yards, and 54 miles

2 feet, 1044 yards, and 55 miles

0 feet, 1245 yards, and 56 miles

1 feet, 1445 yards, and 57 miles

2 feet, 1645 yards, and 58 miles

0 feet, 86 yards, and 60 miles

1 feet, 286 yards, and 61 miles

2 feet, 486 yards, and 62 miles

0 feet, 687 yards, and 63 miles

1 feet, 887 yards, and 64 miles

2 feet, 1087 yards, and 65 miles

0 feet, 1288 yards, and 66 miles

1 feet, 1488 yards, and 67 miles

2 feet, 1688 yards, and 68 miles

0 feet, 129 yards, and 70 miles

1 feet, 329 yards, and 71 miles

2 feet, 529 yards, and 72 miles

0 feet, 730 yards, and 73 miles

1 feet, 930 yards, and 74 miles

2 feet, 1130 yards, and 75 miles

0 feet, 1331 yards, and 76 miles

1 feet, 1531 yards, and 77 miles

2 feet, 1731 yards, and 78 miles

0 feet, 172 yards, and 80 miles

1 feet, 372 yards, and 81 miles

2 feet, 572 yards, and 82 miles

0 feet, 773 yards, and 83 miles

1 feet, 973 yards, and 84 miles

2 feet, 1173 yards, and 85 miles

0 feet, 1374 yards, and 86 miles

1 feet, 1574 yards, and 87 miles

2 feet, 14 yards, and 89 miles

0 feet, 215 yards, and 90 miles

1 feet, 415 yards, and 91 miles

2 feet, 615 yards, and 92 miles

0 feet, 816 yards, and 93 miles

1 feet, 1016 yards, and 94 miles

2 feet, 1216 yards, and 95 miles

0 feet, 1417 yards, and 96 miles

1 feet, 1617 yards, and 97 miles

2 feet, 57 yards, and 99 miles

0 feet, 258 yards, and 100 miles

1 feet, 458 yards, and 101 miles

2 feet, 658 yards, and 102 miles

0 feet, 859 yards, and 103 miles

1 feet, 1059 yards, and 104 miles

2 feet, 1259 yards, and 105 miles

0 feet, 1460 yards, and 106 miles

1 feet, 1660 yards, and 107 miles

2 feet, 100 yards, and 109 miles

0 feet, 301 yards, and 110 miles

1 feet, 501 yards, and 111 miles

2 feet, 701 yards, and 112 miles

0 feet, 902 yards, and 113 miles

1 feet, 1102 yards, and 114 miles

2 feet, 1302 yards, and 115 miles

0 feet, 1503 yards, and 116 miles

1 feet, 1703 yards, and 117 miles

2 feet, 143 yards, and 119 miles

0 feet, 344 yards, and 120 miles

1 feet, 544 yards, and 121 miles

2 feet, 744 yards, and 122 miles

0 feet, 945 yards, and 123 miles

1 feet, 1145 yards, and 124 miles

2 feet, 1345 yards, and 125 miles

0 feet, 1546 yards, and 126 miles

1 feet, 1746 yards, and 127 miles

2 feet, 186 yards, and 129 miles

0 feet, 387 yards, and 130 miles

1 feet, 587 yards, and 131 miles

2 feet, 787 yards, and 132 miles

0 feet, 988 yards, and 133 miles

1 feet, 1188 yards, and 134 miles

2 feet, 1388 yards, and 135 miles

0 feet, 1589 yards, and 136 miles

1 feet, 29 yards, and 138 miles

2 feet, 229 yards, and 139 miles

0 feet, 430 yards, and 140 miles

1 feet, 630 yards, and 141 miles

2 feet, 830 yards, and 142 miles

0 feet, 1031 yards, and 143 miles

1 feet, 1231 yards, and 144 miles

2 feet, 1431 yards, and 145 miles

0 feet, 1632 yards, and 146 miles

1 feet, 72 yards, and 148 miles

2 feet, 272 yards, and 149 miles

0 feet, 473 yards, and 150 miles

Lesson 6-3: Event-Controlled Loops

______

Name ______Date ______

Section______

Use application OddEven for Exercises 1 through 4.

______

import java.util.Scanner;

public class OddEven

{

public static Scanner inData;

public static PrintWriter outFile;

public static void main(String[] args) throws IOException

{

int value;

int oddCount = 0;

int evenCount = 0;

inData = new Scanner(System.in);

System.out.println("Enter an integer value or 0 to " + "stop");

value = inData.nextInt();

while (/* TO BE FILLED IN: Exercise 1 */)

{

/* TO BE FILLED IN: Exercise 2 */

}

/* TO BE FILLED IN: Exercise 3 */

}

}

______

Exercise 1:Write the while expression that returns true if the first data value is not zero.

while (value!=0)

Exercise 2:Write the loop body that counts the number of odd and the number of even values.

while (value!=0)

{

if(value%2==0)evenCount++;

else oddCount++;

System.out.println("Enter an integer value or 0 to stop");

value = inData.nextInt();

}

Exercise 3:Write the output statement(s) that prints the number of odd and the number of even values.

while (value!=0)

{

if(value%2==0)evenCount++;

else oddCount++;

System.out.println("Enter an integer value or 0 to stop");

value = inData.nextInt();

}

System.out.println("Even: "+evenCount);

System.out.println("Odd: "+oddCount);

Exercise 4:Write and implement a test plan. What data values did you use? What was the output?

Testing done with following set of data:

12 34 33 23 12 45 56 66 67 78 89 99 90 21

Ouput:

Enter an integer value or 0 to stop

12

Enter an integer value or 0 to stop

34

Enter an integer value or 0 to stop

33

Enter an integer value or 0 to stop

23

Enter an integer value or 0 to stop

12

Enter an integer value or 0 to stop

45

Enter an integer value or 0 to stop

56

Enter an integer value or 0 to stop

66

Enter an integer value or 0 to stop

67

Enter an integer value or 0 to stop

78

Enter an integer value or 0 to stop

89

Enter an integer value or 0 to stop

99

Enter an integer value or 0 to stop

90

Enter an integer value or 0 to stop

21

Enter an integer value or 0 to stop

0

Even: 7

Odd: 7

Full code:

import java.util.Scanner;

public class OddEven

{

public static Scanner inData;

// public static PrintWriter outFile;

public static void main(String[] args) //throws IOException

{

int value;

int oddCount = 0;

int evenCount = 0;

inData = new Scanner(System.in);

System.out.println("Enter an integer value or 0 to stop");

value = inData.nextInt();

while (value!=0)

{

if(value%2==0)evenCount++;

else oddCount++;

System.out.println("Enter an integer value or 0 to stop");

value = inData.nextInt();

}

System.out.println("Even: "+evenCount);

System.out.println("Odd: "+oddCount);

}

}

Lesson 6-4: File Input and Output

______

Name ______Date ______

Section______

Exercises 1 through 5 use application shell ReadData.

______

import java.io.*;

import java.util.Scanner;

public class ReadData

{

public static void main(String[] args)

throws IOException

{

/* TO BE FILLED IN: Exercise 1 */

/* TO BE FILLED IN: Exercise 2 */

int count = 0;

int value;

int sum = 0;

while (/* TO BE FILLED IN: Exercise 3 */)

{

value = /* TO BE FILLED IN: Exercise 4 */

sum = sum + value;

count++;

}

/* TO BE FILLED IN: Exercise 5 */

/* TO BE FILLED IN: Exercise 6 */

}

}

______

Exercise 1:Declare an input file firstIn and an output file firstOut.

Scanner inFile;

PrintWriter outFile;

Exercise 2:Instantiate your files. The input data is on file inData; the output file should go on file outData.

inFile = new Scanner(new FileReader("inData"));

outFile = new PrintWriter(new FileWriter("outData"));

Exercise 3:Fill in the while expression so that the loop continues as long as there is another valueto be read.

while(inFile.hasNext())

Exercise 4:Read an integer value from file firstIn.

value = inFile.nextInt();

Exercise 5:Write a statement that writes the sum of the values on file outData.

outFile.print(sum);

Exercise 6:Close the files.

outFile.close();

inFile.close();

Exercise 7:Run your program and show what is written on file outData.

Output of outData file:

719

Lesson 6-5: Nested Logic

______

Name ______Date ______

Section______

______

// Application IOLoopA counts the number of blanks per line

// and estimates the number of words.

import java.io.*;

import java.util.Scanner;

public class IOLoopA

{

public static Scanner inFile;

public static PrintWriter outFile;

public static int getBlanks(String inputString)

{

// getBlanks returns the number of blanks in inputString

int blankCount = 0;

int index = inputString.indexOf(' ');

while (index != -1)

{

blankCount++;

if (inputString.length() != 1)

{

inputString = inputString.substring(index+1,

inputString.length());

index = inputString.indexOf(' ');

}

else

index = -1;

}

return blankCount;

}

public static void main(String[] args) throws IOException

{

int lineCount = 0;

int blankCount;

String inputString;

inFile = new Scanner(new FileReader("history.dat"));

outFile = new PrintWriter(new FileWriter("data.out"));

while (inFile.hasNextLine())

{

inputString = inFile.nextLine();

lineCount++;

blankCount = getBlanks(inputString);

outFile.println("Line " + lineCount + " contains "

+ blankCount + " blanks.");

}

outFile.close();

inFile.close();

}

}

______

Exercise 1:Run application IOLoopA. What was the last line written on file data.out?

Line 84 contains 2 blanks.

Exercise 2:Application IOLoopA is an alternate version of IOLoop in the Review Section of this laboratory. Look carefully at the structure of the code in IOLoopA and then go back and look at the original.

Describe the differences in the code structure.

IOLoopA is modular as it has method public static int getBlanks(String inputString)

But IOLoop is not modular as all code is written in single method.

Which is easier to understand? Please explain.

IOLoopA is easier to understand and debug as it has separate method for logic.

Exercise 3:The number of blanks plus the number of lines comes close to estimating the number of words in a text file. Alter the code so that the approximation to the number of words in the file is written at the end of the output. You may alter whichever original version is easier for you to understand. Compile and rerun the program.

import java.io.*;

import java.util.Scanner;

public class IOLoopA

{

public static Scanner inFile;

public static PrintWriter outFile;

public static int getBlanks(String inputString)

{

// Method getBlanks returns the number of blanks

// in parameter inputString

int blankCount = 0;

int index = inputString.indexOf(' ');

while (index != -1)

{

blankCount++;

if (inputString.length() != 1)

{

inputString = inputString.substring(index+1,

inputString.length());

index = inputString.indexOf(' ');

}

else

index = -1;

}

return blankCount;

}

public static void main(String[] args) throws IOException

{

int lineCount = 0;

int blankCount;

int allBlankCount=0;

String inputString;

inFile = new Scanner(new FileReader("history.dat"));

outFile = new PrintWriter(new FileWriter("data.out"));

while (inFile.hasNextLine())

{

inputString = inFile.nextLine();

lineCount++;

blankCount = getBlanks(inputString);

allBlankCount += blankCount;

outFile.println("Line " + lineCount + " contains "

+ blankCount + " blanks.");

}

//approximate words = balnks + 1

outFile.println("Words: "+(allBlankCount+1));

outFile.close();

inFile.close();

}

}

Now what is written on file data.out?

Words: 596