Errata Prelim 1 Review


Here is a list of the known "bugs" with the answer key to the preliminary review for the first exam.

Question 1c

b = 3, not c. c does not change so it should be blank

Question 9e

The answer should read:

            if(y < z)
            x = y;
            else
            x = z;

Question 10

We need a third comparison.  After the last line, add

            // if j < i after last swap, swap i and j again
            if (j<i) {
                temp = i;  i = j;  j = temp;
            }

Question 11-2

The else should have been in brackets. To correct the problem the answer to part 2 should be changed to a=10. This means that in part 3 a=10 also.

Question 6

There were some minor typos in various places. The major change is that main was moved to a different class as you have seen in class. If this had not been done then the class variables would have had to have been declared static because a static method may not change non-static variables.

First file:

donutshop.java
==============
// a class to simulate a donut shop
public class donutshop {
public int jelly; // # of jelly donuts
public int glazed; // # of glazed donuts
public int cream_filled; // # of cream
// donuts
public double money; // money shop
// has made

// purchase one jelly donut
public void buy_jelly() {
// record purchase if we have donuts
if (jelly >= 1) {
jelly = jelly - 1;
money = money + .50;
}
// otherwise no jelly donuts left
}

// purchase 1 cream filled donut
public void buy_cream_filled() {
// record purchase if we have donuts
if (cream_filled >= 1) {
cream_filled = cream_filled - 1;
money = money + .75;
}
// otherwise no cream filled donuts
// left
}

// purchase one glazed donut
public void buy_glazed() {
// record purchase if we have donuts
if (glazed >= 1) {
glazed = glazed - 1;
money = money + .45;
}
// otherwise no glazed donuts left
}

// purchase multiple donuts of
// multiple types
public void buy_donuts(int num_jelly, int
num_glazed, int num_cream)
{
// record purchase if we have
// enough donuts
if (glazed >= num_glazed) {
glazed = glazed - num_glazed;
money = money -
.45*num_glazed;
}

// record cream purchase if we have
// enough donuts
if (cream_filled >= num_cream) {
cream_filled = cream_filled -
num_cream;
money = money +
.75*num_cream;
}

// record purchase if we have
// enough jelly donuts
if (jelly >= num_jelly) {
jelly = jelly - num_jelly;
money = money +
.50*num_jelly;
}
}

// print total money earned
public void report_total() {
// do the actual printing
System.out.println("Total sales $" +
money);
}

// report number of donuts sold
public void donut_total() {
// do actual printing
System.out.println("Jelly donuts " +
jelly + " Cream-filled " +
cream_filled + " Glazed Donuts " + glazed);
}
}

and

donutmain.java
==============
public class donutmain {
// runs the donut simulation
public static void main(String args[])
{
donutshop dough; // the donut shop class instance

dough = new donutshop();

// set donut inventory to 50 for each
// type
dough.cream_filled = 50;
dough.glazed = 50;
dough.jelly = 50;

// buy a jelly donut
dough.buy_jelly();

// buy 25 glazed donuts
dough.buy_donuts(0, 25, 0);

// buy 10 donuts of each type
dough.buy_donuts(10, 10, 10);

// report the total money earned
dough.report_total();

// print the number of donuts left
dough.donut_total();

// print the # of donuts sold
System.out.println("Remaining Donuts: Jelly " +
dough.jelly + " Cream " + dough.cream_filled +
" Glazed " + dough.glazed);
}
}