midterm solutions

advertisement
CMPT 101
MIDTERM
February 27. 2004
PART 1: SHORT ANSWER QUESTIONS (50 points out of 100 total points)
1. (12 points)Define each of the following terms. Please give concise definitions
not exceeding three sentences.
a) Sentinel value and Sentinel variable
The Sentinel variable has a particular sentinel value which is used to
indicate that the loop should be terminated. This value should not be
attainable in the normal execution of the program, except as an indicator
that the loop should be exited.
b) Repetition Statement (part of a repetition structure)
A structure within Java that allows for repetition of a series of
statements a particular number of times or until a particular
condition has been satisfied.
c) Data Type and Primitive data type
A Data Type is a set of a particular type of data along with a set of
operations.. A primitive data type (in Java) is one of the fundamental
integral, floating point or boolean types.
d) Tokenizer
A procedure which breaks a stream of characters into chunks or
tokens. Characters taken from the stream and placed into the token
until a delimeter character is read (often a white space character). The
next non delimeter character will be the start of the next token and the
process will continue until the stream of characters is exhausted.
2. (10 points )State the type and value of each valid expression below. If the
expression is not valid briefly explain why (no more than two sentences per
expression). No explanation is needed if the expression is valid. The variables
used in the expression are as follows:
int i1=7, i2=5, i3 =-8;
double d1=4.5, d2, d3=5.2;
boolean b1=true, b2=false, b3;
String str1=”This is a test”, str2;
a) str1 + “ of concatenation”
This is a test of concatenation
String
b) d1 * i2
22.5
double
c) str1.subString(5,7)
is
String
d) d1<d3 && i3>(i1%i2) || !b3
true && true || undefined b3 has not been initialized so the
statement is invalid
e) !(i2<i1) && b2
i. false
Boolean
3. (10 points) Write one two way decision statement to print the appropriate
messages as follows. When integer variable Obs is even print the message,
Integer is even. When integer variable Obs is odd print the message, Integer is
odd. It will be assumed that all variables you use are of type integer. You need
not declare your variables.
if (Obs%2 == 0)
System.out.println(“Integer is even”);
else
System.out.println(“Integer is odd”);
4. (8 points) What are the steps of the software development process. The software
development process is the series of steps you were asked to perform when
solving the programming problems in your assignments.
Statement of the Problem
Analysis of the problem
Inputs and Outputs
Summary Algorithm
Preparation of test plan
Implementation
Testing
Maintenance
5. (10 points) Declare and instantiate an object of type DecimalFormat to print
floating point numbers with three digits after the decimal point. Use this
instance of DecimalFormat to print the value of double variable with identifier
answer. You need not declare your variable or give answer a value.
DecimalFormat threeDecimal = new DecimalFormat(“0.000”);
System.out.println( threeDecimal.format(answer));
PART 2: PROGRAMMING PROBLEMS ( 50 points from a total of 100
points)
6. (25 points)Write a Java class SumBetween to print and to calculate the sum of
all integers divisible by 4 between and including startInt and endInt. ( If startInt
= 6 and endInt =24, the sum would be 8+12+16+20+24 ). Your program
should do the following things:
a) Use an input dialog box to input startInt from the keyboard
b) Use an input dialog box to input endInt from the keyboard
c) Use a while loop to
i. Print out all integers num divisible by 4 and with
startInt<=num<=endInt. These integers should be printed on one
line and be separated by single spaces.
ii. Calculate the sum of these integers .
d) Print the sum of integers divisible by 4 along with an explanatory
message. The output should be printed to the consol screen
import java.io.*;
import javax.swing.JOptionPane;
public class SumBetween
{
public static void main (String[] args)
{
String startIntStr, endIntStr;
int startInt;
int endInt;
int sum, counter;
startIntStr = JOptionPane.showInputDialog("Enter the smallest
integer: ");
startInt = Integer.parseInt( startIntStr );
endIntStr = JOptionPane.showInputDialog("Enter the largest
integer: ");
endInt = Integer.parseInt( endIntStr );
sum = 0;
counter = startInt;
while(counter <= endInt)
{
if( counter%4 == 0)
{
sum = sum + counter;
System.out.print(counter + " ");
}
counter++;
}
System.out.println();
System.out.println("Line 10: The sum of all numbers divisible by 4
\n"
+ " between and including " + startInt + " and " + endInt
+ " is " + sum);
System.exit(0);
}
}
7. (25 points)You are tabulating the data resulting from a field count of a
particular type of bird. Each observer has provided the number of birds
counted, and the area in acres of the observation region in which those birds
were counted. You must determine the total number of birds counted, and the
number of birds per acre. To accomplish this goal write a Java main program
based on the following assumptions:
a) You are provided with a file, surveydata.txt. The number of observers,
numberObs, is an integer on the first line of the file. Following the first
line in the file there is one line for each observer. On each of these lines is
an integer, nBirds, indicating the number of birds followed by a double,
areaAcres, indicating the area of the observation region in acres. More
information about the survey is included in the file following the lines of
interest to you. (This means you cannot test for end of file to find the end
of your data)
b) All data in the file is correct and complete (YOU ARE NOT REQUIRED
TO CHECK THAT THE DATA VALUES ARE REASONABLE)
c) There is data for more than one observer. (numberObs>1)
d) YOU ARE NOT REQUIRED TO COMMENT THE CODE
Your Java program should do each of the following:
a. Read an integer numberObs from the first line of file surveydata.txt
b. Use a for loop to
i. Read the next line of data from file input.txt. The number of birds
is nBirds, and the area in acres is areaAcres.
ii. Add the number of birds for this observer to the total number of
birds, TNumBirds.
iii. Add the area for this observer to the total area, TArea.
c. Determines the number of birds per acre, birdsPerAcre.
d. Prints the total number of birds and the number of birds per acre on
separate lines. Each line should include an explanation of what the printed
number represents. These results should be printed to the console screen
public static void main (String[] args) throws IOException,
FileNotFoundException
{
int nBirds;
int TNumBirds;
double TArea;
double areaAcres;
int numberObs, counter;
int birdsPerAcre;
StringTokenizer tokenizer;
BufferedReader inFile =
new BufferedReader(new FileReader("surveydata.txt"));
tokenizer =
new StringTokenizer(inFile.readLine());
numberObs = Integer.parseInt(tokenizer.nextToken());
TNumBirds = 0;
TArea = 0;
tokenizer =
new StringTokenizer(inFile.readLine());
for(counter = 1; counter <= numberObs; counter++)
{
nBirds =
Integer.parseInt(tokenizer.nextToken());
areaAcres =
Integer.parseInt(tokenizer.nextToken());
TNumBirds += nBirds;
TArea += areaAcres;
tokenizer =
new StringTokenizer(inFile.readLine());
}
birdsPerAcre = (int)(TNumBirds / TArea);
System.out.println("The total number of birds counted was " +
TNumBirds + " in " + TArea + " Acres ");;
System.out.println("The number of birds per Acre was "
+ birdsPerAcre);
}
Download