// problem 4
public static int gcd (int one, int two) {
// if either one or two is 0
return 0
if (one == 0 || two == 0) return 0;
// if either one or two is
negative multiply them
// by negative 1
if (one < 0) one *= -1;
if (two < 0) two *= -1;
// as long as neither divide
evenly into each other
while ((one % two != 0) && (two % one != 0)) {
//
take the larger of the two and replace it
//
it with the remainder of dividing the two
if (one > two) one = one % two;
else two = two % one;
}
// return the smaller of the
two
if (one > two) return two;
else return one;
}
// problem 5.9
public class
Person extends Object
{
private static int theNumPersons =
0; // initialize num
// **** problem 9 change
private static int sumyears = 0;
/////////////////////////////////
private String itsFirstName;
private String itsLastName;
private int itsBirthYear;
// **** problem 9 method!
public static int avgyear() {
return sumyears / theNumPersons;
}
/** Construct a new Person with given names and birth year. */
public Person (String first, String last, int year)
{ super();
theNumPersons++;
// update num
// **** problem 9 change
sumyears += year;
itsFirstName = first;
itsLastName =
last;
// initialize last name
itsBirthYear = year;
} //=======================
/** Tell how many different Persons exist. */
public static int
getNumPersons()
// access num
{ return theNumPersons;
} //======================
/** Return the birth year. */
public int getBirthYear()
{ return itsBirthYear;
} //=======================
/** Return the first name. */
public String getFirstName()
{ return itsFirstName;
} //=======================
/** Return the last name. */
public String
getLastName()
// access last name
{ return itsLastName;
} //=======================
/** Replace the last name by the specified value. */
public void setLastName (String
name) // update last name
{ itsLastName = name;
} //=======================
}
// problem 5.48
public class
supervic extends Vic
{
public boolean canFillAllSlots() {
boolean retval = false;
// if we have no slots, then clearly
// we can fill them no matter what
if (!seesSlot()) retval = true;
else {
// if we have slots, but no cds in the
// stack then clearly we cannot fill them
if (!stackHasCD()) retval = false;
// if we have slots, and cds in the stack..
else {
// and we have a cd in the current slot
if (seesCD()) {
// move to the next one
moveOn();
// recurse
retval = canFillAllSlots();
// undo the moving on
backUp();
}
// if we have a slot, and cds in the stack
// and we have an open slot
else {
// take a cd off of the stack to fill
// the slot
putCD();
// move to the next slot
moveOn();
// recurse
retval = canFillAllSlots();
// undo the moveon
backUp();
// undo the putCD
takeCD();
}
}
}
// return the value
return retval;
}
}