Program Design I - Mid-Term Exam
Write all of your answers on the answer sheets provided. You may write on
this test paper, but the instructor will be looking for answers on the
answer sheets.
- (2 pts) Name one reason methods are a valuable tool.
- (2 pts) State the “Golden Rule of Loops”.
- (2 pts) What is a try-catch block used for in Java?
- (4 pts) Draw and label the diagram given in class that outlines the
steps of converting Java source code into machine language.
- Consider the following Java program and then answer
the questions. When answering, you need to use proper terminology.
import java.util.Scanner;
public class MyFavoriteExam // line A
{
public static void main(String [] args) // line B
{
Scanner scan= new Scanner(System.in); // line C
int k,val,num=0; // line D
System.out.print("Enter a number: "); // line E
k= scan.nextInt(); // line F
val= 1; // line G
while (val<=k) // line H
{
num= num + val; // line J
val++; // line K
}
System.out.println(num); // line L
}
}
- (2 pts) Which of the “six things a computer can do” discussed
in class is not demonstrated in this program.
- (4 pts) What would be output by this program if, when prompted,
the user enters the value 4?
- (3 pts) What error (if any) would result if line D was modified
to this:
int k,val,num;
- (3 pts) The program currently uses a while loop. Which
type of loop (while, do-while, or for) do you think
would be the best choice for this program? Defend your answer.
- Suppose the input buffer contains the following characters:
9 |
9 |
7 |
0 |
\n |
|
|
3 |
0 |
|
3 |
4 |
|
|
|
\n |
5 |
|
2 |
7 |
\n |
Further assume the following variable declarations and initial values:
Scanner kb= new Scanner(System.in);
double x= 0.0; int i= 5;
String s1="George", s2= "Mary";
- (8 pts) Write the value of the variables
i, x, s1, and s2 after
the following input statements have been processed.
Assume the input pointer starts at the beginning of the buffer.
s1= kb.next();
x= kb.nextDouble();
s2= kb.nextLine();
i= kb.nextInt();
- (2 pts) Where is the input pointer following the final
input statement statement in problem 6a?
- (2 pts each) State the type and the value of each expression
assuming the following declarations and initial values. If the
expression is not a valid Java expression then say so.
int a= 12;
bool isLiked= true;
String size= "large";
double x= 6.0;
size.equals("LARGE") || x >= 6
a + x
a / 5 * 5
size+a
isLiked && !(x-a)
a % 2
(a+x) < 10 && isLiked
- (6 pts) Write a section of code that will use a loop to display
the message “I love this test” 20 times.
- (6 pts) Write a section of code that will allow a user to enter
the radius of a circle and will display the corresponding area
().
The program should keep asking for another radius until the user
enters 0 or a negative number at which point the program should
end without calculating or displaying the area.
- (8 pts) Write a method called getBattingAvg
that will prompt the user to enter a batting average in the range 0.000
to 1.000. The method should validate the input and return the entered
batting average after a value in the proper range has been entered.
Write your method to match the following sample output exactly
(including blank lines, prompts, error messages, etc.):
Enter batting average: -0.001
ERROR: Must be in range 0.000 to 1.000
Enter batting average: 1.001
ERROR: Must be in range 0.000 to 1.000
Enter batting average (0.000-1.000): 0.321
- (16 pts) Write a complete Java program that will allow a user to
calculate a baseball team's average batting average. The code should
work exactly as demonstrated by the following output:
Player 1
Enter batting average: 0.300
Enter another one? (y/n): y
Player 2
Enter batting average: 0.275
Enter another one? (y/n): y
Player 3
Enter batting average: 0.250
Enter another one? (y/n): n
Team batting average is: 0.275
The program should label precede each request for a batting average
with “Player #” where the number is generated sequentially starting
from 1. After getting each batting average the program should ask the
user if they want to enter another one with “y” indicated yes and “n”
indicating no. (Assume the user will cooperate and only enter either
“y” or “n”). The program should continue requested batting
averages until “n” is entered for that question.
The team batting average (an average of the individual batting averages)
should be displayed once after all values have been entered.
IMPORTANT: You should assume that the method described in
problem 10 already exists and has been correctly
written and you should call it to get a a single batting average.