Part I : True or False
Part II: Multiple Choice
if(i == 10 || i == 15) d = 2.2;
else if(i == 25) d = 7.7;
else d = 1.1;
int n = 5, factorial = 1;
do {
factorial = factorial * n;
n = n + 1;
} while (n > 0);
Part III: Written Answers
--
***
----
*****
------
class Bus {
private int maxCapacity; //maximum number of people on bus (pos. int)
private int currentNum; //number of people currently on bus (pos. int)
private int totalPassengers; //total number of people ever on bus (pos. int)
private double ticketPrice; //amount people pay to get on bus (pos. double)
/**
* Constructor: sets currentNum and totalPassengers to 0.
* @param m sets the maxCapacity
* @param price sets the ticketPrice
* Bugs: Does no error checking on parameters
**/
public Bus(int m, double price){
totalPassengers = 0;
currentNum = 0;
ticketPrice = price;
maxCapacity = m;
}
/**
* calcIncome returns the income generated by all people who ever got
* on this bus
* @param none
* @return totalPassengers * ticketPrice
**/
public double calcIncome(){
return totalPassengers * ticketPrice;
}
/**
* addPassengers returns error code if parameter is negative, otherwise
* adds upto the capacity number of people wanting on this bus. If this
* would have exceeded the capacity, then return how many extra, else
* return 0
* @param people number of people wanting on this bus
* @return -1 is error, 0 is all fit, pos int is overflow
**/
public int addPassengers(int people){
if(people < 0)
return -1;
//If we add people to bus, how much does that exceed capacity?
int overflow = currentNum + people - maxCapacity;
if(overflow > 0) { //people is too much!
totalPassengers += people - overflow; //add only those who fit
currentNum = maxCapacity; //set to max
return overflow;
} else { //just add people
currentNum += people;
totalPassengers += people;
return 0;
}
}
/**
* removePassengers returns -1 if parameter is negative, otherwise
* removes at most all the current passengers. If people is more than
* the current number, then remove all and return 1. Otherwise, remove
* requested number and return 0.
* @param people number of people who wish to get off bus
* @return -1 is error, 1 is underflow, 0 is OK
**/
public int removePassengers(int people){
if(people < 0)
return -1;
if(people > currentNum) { //people is too much!
currentNum = 0;
return 1; //underflow value
} else { //just remove people
currentNum -= people;
return 0;
}
}
}
Bus busOne = new Bus(40, .5);
Bus busTwo = new Bus(15, .75);
busOne.addPassengers(22);
busTwo.addPassengers(22);
//Assume we know that busOne has more passengers
out.printLine("The income is $" + busOne.calcIncome());