|
Sample midterm solutions |
Listed below are questions similar to those I will put on the midterm. Sample solutions will be posted Thursday.
public static double average(double a, double b)
that returns the average of a and b.
Solution:
public static double average(double a, double b) {
double avg = (a + b)/2;
return avg;
}
public static int minimum(int i, int j)
that returns the smaller of i and j.
Solution:
public static int minimum(int i, int j) {
int min;
if (i < j) {
min = i;
}
else {
min = j;
}
return min;
}
Solution: 4*Math.pow(x,3) +
16*Math.pow(x,2) - 9*x + 56.
Solution:
int month;
do {
String s = JOptionPane.showInputDialog(null, "Please enter the number of a month (1 through 12)");
month = Integer.parseInt(s);
} while (month < 1 || month > 12);
public class ExampleProgram {
public static void main(String[] args) {
int index = 0;
int total = 0;
int upper = 10;
while (index <= upper) {
total += index;
index++;
}
System.out.println("The total is " + total);
}
}
Solution: The program prints "The total is 55".
![]()