.

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

(lab02) What will be the output produced by the following code? int[] a = new int[10]; for (int i = 0; i < a.length; i++) a[i] = 2 * i; for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");

0 2 4 6 8 10 12 14 16 18

(lab05) What is the output of this loop? int limit = 6; for (int count = 1; count<=limit; count++) System.out.println(count + " squared is " + (count * count));

1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25 6 squared is 36

(lab04) What specific kind of exception does the FileOutputStream constructor throw? How many overloaded println() methods does the PrintStream class define? How many overloaded print() methods does the PrintStream class define? Why are there so many overloaded print() and println() methods?

FileNotFoundException 10 9 Each type of data needs its own print() methods. There are many types of data to print.

(lab03) 1. Scanner infile = new Scanner( new File("diskfile.txt") ); 2. String s = infile.next(); 3. double d = infile.nextDouble(); 4. int n = infile.nextInt(); 5. System.out.print("You read: " + s + ", " + d + ", " + n); Trace the code above. If diskfile.txt contains 18 234.5 Shane what happens?

It reads "18" as a string, 234.5 as a double, and gives an error "Type mismatch" on Shane.

(lab03) 1. Scanner infile = new Scanner( new File("diskfile.txt") ); 2. String s = infile.next(); 3. double d = infile.nextDouble(); 4. int n = infile.nextInt(); 5. System.out.print("You read: " + s + ", " + d + ", " + n); Trace the code above. If diskfile.txt contains Shane 234.5 what happens?

It reads Shane, then 234.5, then tries to read a blank and gives an error.

(lab03) What class is the method showInputDialog() defined in?

JOptionPane

(lab03) What class is the method nextDouble() defined in?

Scanner

(lab04) It turns out that, to calculate the sum, average, and max, you don't need to store the data in an array. In this case you can process the data from the text file on the fly, without knowing in advance how much data you have. The Scanner class has two methods hasNext() and nextDouble() to help. Fill in the blanks: Scanner infile = new Scanner( new File("data.txt") ); double sum = ____; double max = Double.MIN_VALUE; int count = ____; while( infile.hasNext() ) { double x = infile.nextDouble(); sum = sum + _____; max = Math.max(____________, ____); count++; } System.out.println("Sum: " + _______); System.out.println("Average: " + _______________________________); System.out.println("Max: " + _______);

Scanner infile = new Scanner( new File("data.txt") ); double sum = 0.0; double max = Double.MIN_VALUE; int count = 0; while( infile.hasNext() ) { double x = infile.nextDouble(); sum = sum + x; max = Math.max( max, x ); count++; } System.out.println("Sum: " + sum ); System.out.println("Average: " + sum / count ); System.out.println("Max: " + max );

(lab03) Suppose you had to read from a text file a person's Social Security Number, two semester averages (which could be decimals), and a letter final grade. Write the code to do that.

Scanner infile = new Scanner( new File("diskfile.txt") ); String ssn = infile.next(); double avg1 = infile.nextDouble(); double avg2 = infile.nextDouble(); String letter = infile.next(); infile.close();

(lab04) Up till now, your professor has kept exam scores on paper. She assigns you to write a program so that someone can enter the exam scores into an array, from the keyboard. The number of scores has to be entered from the keyboard as well. She wants you to use good, friendly prompts.

Scanner key = new Scanner( System.in ); System.out.println("How many scores?"); int n = key.nextInt(); double[] examScores = new double[n]; for(int i = 0; i < examScores.length; i++) { System.out.print("Enter a score: "); examScores[i] = key.nextDouble(); }

(lab02) Write the code to fill an array with 20 values of the type double, read in from the keyboard.

Scanner keyboard = new Scanner( System.in ); double[] array = new double[20]; System.out.println("Enter 20 numbers: "); for( int i= 0; i< array.length; i++ ) array[i] = keyboard.nextDouble();

(lab03) What is the return type of the method next()?

String

(lab03) What is the return type of the method showInputDialog()?

String

(lab04) Your professor teaches several courses. Each set of exam scores is saved to disk under different filenames. Each set of exam scores has an integer in the first line, which tells the number of exam scores in the file. Write the code fragment that prompts the user for the file name, reads the first line, and creates an array of the correct size.

String fileName = JOptionPane.showInputDialog("Enter name of file: "); Scanner scoresFile = new Scanner( new File( filename ) ); int numberOfScores = scoresFile.nextInt(); double[] examScores = new double[numberOfScores];

(lab03) For every line, explain what's happening. 1. Scanner infile = new Scanner( new File("diskfile.txt") ); 2. String s = infile.next(); 3. double d = infile.nextDouble(); 4. int n = infile.nextInt(); 5. System.out.print("You read: " + s + ", " + d + ", " + n); Trace the code above. If diskfile.txt contains Shane 234.5 18 what is the output?

You read : Shane, 234.5, 18

(lab03) What is the return type of nextDouble()?

a double

(lab01) public static final int NUMITEMS = 100; double[] myAr = new double[NUMITEMS]; Explain exactly what the two declarations above accomplish.

creates a public class constant named NUMITEMS that stores ints, sets that constant equal to 100; instantiates an array of 100 doubles, all 0.0 right now.

public static final int NUMITEMS = 100; double[] myAr = new double[NUMITEMS]; (lab01) Use the result from #5 to calculate the average of the numbers in the array.

double average = sum / myAr.length;

public static final int NUMITEMS = 100; double[] myAr = new double[NUMITEMS]; (lab01) Write the code to traverse the array, and find the smallest number in the array above.

double smallest = myAr[0]; for (int i=1; i< myAr.length; i++) if(myAr[i] < smallest) smallest = myAr[i]; double smallest = myAr[0]; for (int i=1; i< myAr.length; i++) smallest = Math.min(smallest, myAr[i]);

public static final int NUMITEMS = 100; double[] myAr = new double[NUMITEMS]; (lab01) Write the code to calculate the sum of all the numbers in the array above.

double sum = 0.0; for (int i = 0; i < myAr.length; i++) sum += myAr[i];

(lab02) Given the following declaration, double [] banana = {12.2, -7.3, 14.2, 11.3}; write the code to find the average of the array. Then output the numbers in the array as well as how much each number differs from the average.

double sum = 0.0; for (int i= 0; i< banana.length; i++) sum += banana[i]; double avg = sum/banana.length; for (int i= 0; i< banana.length; i++) System.out.println(banana[i] + " " + Math.abs(avg - banana[i]) );

public static final int NUMITEMS = 100; double[] myAr = new double[NUMITEMS]; (lab01) Traverse the array above, calculating the sum of the negative numbers and the sum of the positive numbers.

double sumNeg = 0; double sumPos = 0.0; for (int i = 0; i < myAr.length; i++) { if(myAr[i] > 0) sumPos += myAr[i]; else sumNeg += myAr[i]; }

(lab04) Your professor assigns you to write part of a program that processes a set of exam scores. She says there are exactly 181 exams. The scores are in a text file "exam01.txt", one score per line. Write the code fragment to read the exam scores into an array.

double[] examScores = new double[181]; Scanner scoresFile = new Scanner( new File("exam01.txt") ); for(int i = 0; i < examScores.length; i++) examScores[i] = scoresFile.nextDouble();

(lab02) Given the following declaration, double [] apple = {12.2, -7.3, 14.2, 11.3}; write the code to print each number, its half, and its double.

for (int i = 0; i < apple.length; i++) System.out.println(apple[i]+ "\t" + apple[i]/2 + "\t" + apple[i]*2);

public static final int NUMITEMS = 100; double[] myAr = new double[NUMITEMS]; (lab01) Write the code to visit each cell in the array above, and fill it with random doubles between -50 and 50.

for (int i = 0; i < myAr.length; i++) myAr[i] = Math.random() * 101 + -50;

(lab05) There are three kinds of loops. What are they?

for while do-while

(lab00) Given an array of size NUMITEMS containing doubles, write the code to multiply each element by 10.0.

for(int i = 0; i < NUMITEMS; i++) someArray[i] = someArray [i] * 10.0;

(lab00) char[] answers = new char[4]; Write the code to fill each cell in the array above with an 'A'.

for(int index = 0; index < 4; index++) answers[index] = 'A';

(lab00) int[] scores = new int[5]; Write the code to fill the array above with 0.

for(int index = 0; index < 5; index++) scores[index] = 0;

(lab00) Given an integer array of size N which is empty, write the code to insert N number of random integers, between 5000 and 10000, inclusive.

for(int index = 0; index < N; index++) arrayOfIntegers[index] = (int (Math.random() * 5001 + 5000);

(lab00) char[] answers = new char[4]; Write the code to print each element of the answers array in the console I/O window.

for(int pos = 0; pos < 4; pos++) System.out.print( " " + answers[pos] );

(lab04) 4 import java.io.*; 5 public class MakeDataFile 6 { 7 public static void main(String[] args) throws Exception 8 { 9 PrintStream outfile = new PrintStream( new FileOutputStream("data.txt")); 10 11 int numitems = (int)(Math.random() * 5000 + 5000); 12 outfile.println(numitems); 13 14 for(int x = 0; x < numitems; x++) 15 outfile.println(Math.random() * 1000); 16 outfile.close(); 18 } 19 } What does Line 11 do?

generates and stores a random number between 5000 and 9,999 inclusive. [5000, 10000)

(lab03) What package must you import to use Scanner()?

import java.util.*;

public static final int NUMITEMS = 100; double[] myAr = new double[NUMITEMS]; (lab01) Write the code that searches the array and counts the number of negative numbers.

int count = 0; for (int i = 0; i < myAr.length; i++) if(myAr[i] < 0) count++;

(lab05) Rewrite the loop in 2a as a do-while loop. int limit = 6; for (int count = 1; count<=limit; count++) System.out.println(count + " squared is " + (count * count));

int limit = 6; int count = 1; do{ System.out.println(count + " squared is " + (count * count)); count++; } while( count <= limit );

(lab05) Rewrite the loop as a while-loop. int limit = 6; for (int count = 1; count<=limit; count++) System.out.println(count + " squared is " + (count * count));

int limit = 6; int count = 1; while( count <= limit ) { System.out.println(count + " squared is " + (count * count)); count++; }

(lab04) 4 import java.io.*; 5 public class MakeDataFile 6 { 7 public static void main(String[] args) throws Exception 8 { 9 PrintStream outfile = new PrintStream( new FileOutputStream("data.txt")); 10 11 int numitems = (int)(Math.random() * 5000 + 5000); 12 outfile.println(numitems); 13 14 for(int x = 0; x < numitems; x++) 15 outfile.println(Math.random() * 1000); 16 outfile.close(); 18 } 19 } Why does Line 7 need throws Exception?

it's required by FileOutputStream

(lab04) 4 import java.io.*; 5 public class MakeDataFile 6 { 7 public static void main(String[] args) throws Exception 8 { 9 PrintStream outfile = new PrintStream( new FileOutputStream("data.txt")); 10 11 int numitems = (int)(Math.random() * 5000 + 5000); 12 outfile.println(numitems); 13 14 for(int x = 0; x < numitems; x++) 15 outfile.println(Math.random() * 1000); 16 outfile.close(); 18 } 19 } In the code above, the PrintStream object is named ... In the code above, the object outfile is of class... If a "data.txt" file already exists on the hard drive, does new FileOutputStream("data.txt") overwrite that file or not?

outfile PrintStream overwrites it

(lab04) 4 import java.io.*; 5 public class MakeDataFile 6 { 7 public static void main(String[] args) throws Exception 8 { 9 PrintStream outfile = new PrintStream( new FileOutputStream("data.txt")); 10 11 int numitems = (int)(Math.random() * 5000 + 5000); 12 outfile.println(numitems); 13 14 for(int x = 0; x < numitems; x++) 15 outfile.println(Math.random() * 1000); 16 outfile.close(); 18 } 19 } What do lines 14 and 15 do?

puts lots of random decimal numbers on the textfile

(lab04) 4 import java.io.*; 5 public class MakeDataFile 6 { 7 public static void main(String[] args) throws Exception 8 { 9 PrintStream outfile = new PrintStream( new FileOutputStream("data.txt")); 10 11 int numitems = (int)(Math.random() * 5000 + 5000); 12 outfile.println(numitems); 13 14 for(int x = 0; x < numitems; x++) 15 outfile.println(Math.random() * 1000); 16 outfile.close(); 18 } 19 } What does Line 12 do?

puts numitems into the textfile on the disk.

(lab04) 4 import java.io.*; 5 public class MakeDataFile 6 { 7 public static void main(String[] args) throws Exception 8 { 9 PrintStream outfile = new PrintStream( new FileOutputStream("data.txt")); 10 11 int numitems = (int)(Math.random() * 5000 + 5000); 12 outfile.println(numitems); 13 14 for(int x = 0; x < numitems; x++) 15 outfile.println(Math.random() * 1000); 16 outfile.close(); 18 } 19 } What does Line 9 do?

the object "outfile" points to the textfile "data.txt"

(lab02) What will be the output produced by the following code? char[] vowels = {'a', 'e', 'i', 'o', 'u'}; for (int index = vowels.length - 1; index > 1; index++) System.out.println(vowels[index]);

u o i e


Ensembles d'études connexes

Legal environment of business Exam 2

View Set

The Presidency of George W. Bush

View Set

Cardiovascular System (Mastering A&P)

View Set

SY0-401:1 TS Quiz Network Security

View Set

Taylor Review Questions - Wounds/Skin Integrity

View Set

Ch. 20 Anxiolytic and Hypnotic Agents

View Set