### Introduction to Computers and Programming Midterm 1

1. (25 points) Write a program to determine whether a series of purchases comes in over or under a budget. The program starts by reading a budget and a sales tax rate. It then prompts the user for the price (before tax) of each item purchased. After reading all the input, it prints the total cost, the budget and the relationship between the budget and the cost.

Here are two sample runs of the program (user input in italics):

Run 1 (input: JOptionpane):
What is the budget? 1000.00
What is the tax rate? .10
What is the price of the item (0 to end)? 100.00
What is the price of the item (0 to end)? 200.00
What is the price of the item (0 to end)? 300.00
What is the price of the item (0 to end)? 0

(output: System.out)
You came in under budget by \$ 340.00

Run 2:
(input: JOptionPane)
What is the budget? 100.00
What is the tax rate? .08
What is the price of the item (0 to end)? 100
What is the price of the item (0 to end)? 0

(output: System.out)
You came in over budget by \$ 8.00

Notes and hints:
• All the values have decimal places.
• Do not worry about formatting the output.
• If you come out exactly on budget, it does not matter what your last line of output states.
• 0.0 is one float/double value which is represented exactly.

2. (25 points) Write a program that that will print an arbitrary number of lines of arbitrary length. First you need to prompt the user for the desired number of lines. The program will then prompt the user for the length of each line. Each line should be printed to the screen using asterisks on a separate line of output. For example (user input in italics):

(input: JOptionPane)
How many lines would you like to print? 4
How long is the next line? 5
How long is the next line? 12
How long is the next line? 3
How long is the next line? 5

(output: System.out)
*****
************
***
*****

3. (20 points) What does the following code produce?
```public class NestedFor
{
public static void main( String args [])
{
int outer = 0, inner = 0;

for(outer = 3; outer <= 7; outer ++)
{
for(inner = 0; inner <= outer; inner ++)
{
if ((inner / outer) >= 1)
System.out.print(inner);
}
System.out.println ();
}
}
}
```
4. (15 points) The following code contains at least 5 errors. Identify them. Comments, formatting or lack thereof are not any of the errors.
import javax.swing.JOptionPane;

```public class SampleMidterm1_2_Q4 {

public static void main( String args[] )
{
int outstanding = 0, satisfactory = 0, unsatisfactory = 0;

"Enter First Grade or -1 to Quit:" );

while ( currentGrade != -1 ) {

outstanding++;
satisfactory++;
else
unsatisfactory++;

"Enter Next Grade or -1 to Quit:" );

}

System.out.println ("Outstanding: " + outstanding);
System.out.println ("Satisfactory: " + satisfactory);
System.out.println ("Unsatisfactory: " + unsatisfactory);
}
System.exit( 0 );
}
```
5. (3 points each)
a. Assuming the following declarations:

```	int a = 40;
int b = 5;
```

What would be the output of the following code segment?

```	if (a / b <= b)
System.out.print (a);
else
System.out.print (b);
```

b. What is the value of the following expression?

```	(66 / 6) + 15 * 3 % 2
```

c. How many times does the following loop execute?

```	int counter = 1;
while (counter < 100)
{
System.out.println("counter is now: " +  counter);
counter *= 2;
}
```
d. Would the following expression evaluate to true or false?

```	(54 == 64) || (77 > 55) || (99 < 1)
```

e. Rewrite the following if statement as a switch statement:

```	if (a == 1)
System.out.println ("one");
else if (a == 2)
System.out.println ("two");
else
System.out.println ("larger than 2");
```