1) Write an independent method public static void poweroftwo(int value) that takes in as a parameter the user for a single int value and then announces the power of 2 (1, 2, 4, 8, 16, 32, etc.) closest to that input without being larger than the input.  But if the input is not a positive integer, just say "not acceptable". You can use methods in the Math class if you wish.


// Precondition: value is an int

// Postcondition: writes not acceptable to the screen if int is < 1, otherwise returns the largest power of 2 that is smaller than the value


public static void poweroftwo(int value) {

if (value < 1) {
System.out.println("not acceptable");
return;
}
int x = 1;
while (x < value) {
x  = x * 2;
}
/* Remember when we leave this x is LARGER than value, so we want the power of two that is SMALLEr, so we need to divide by 2 */
System.out.println(""+x/2); 
}  


Worth 6 points



You have a class that stores fractions as a separate numerator and denominator. The class is partially defined as follows:


public class Fraction implements Comparable {

private double numerator = 0;

private double denominator = 1;


// you need to write this

public int compareTo(Object ob) {

}

public void setNumerator(double n) {

numerator = n;

}

// this makes sure we can never have a denominator of 0, thus we avoid

// divide by 0 exceptions

public void setDenominator(double d) {

if (d != 0.0) denominator = d;

}


// you need to write this

public static void selectionSort (Fraction[] values) {

}


// you need to write this

public static Fraction findFraction(Fraction[] values) {

}


}


2) Write the method public int CompareTo(Object ob) for the fraction class. This should work according to the general rules put forth by the Comparable interface. ob stores another reference to a Fraction object. Your method should figure out which Fraction has a higher value. If your internal value represents a larger fraction, return a positive int value, if the two represents the same Fraction value, return 0, if the one stored in the Object ob is larger then return a negative number. Remember: even though ob is storing a Fraction object, Java will not by default see it as such. You need to tell Java to treat ob like a Fraction.


// Precondition: ob is of type Fraction and exists. Neither your denominator nor the denominator stored in the Fraction represented by ob is 0.

// Post-condition: return a positive int value if your internal values represent a fraction that is larger than the one stored in ob, 0 if they represent ones with equal value and negative if the one in ob has a higher value.

public int CompareTo(Object ob) {

Fraction v = (Fraction) ob; // first cast the Object to a Fraction so we can use it as a Fraction
// are we bigger than our paramer, if so return a positive value
if (this.numerator / this.denominator > v.numerator/v.denominator) return 1;
// is the parameter bigger?  if so return a negative value
else if (this.numerator / this.denominator < v.numerator/v.denominator) return -1;
// otherwise we're the same so return 0
else return 0;

}


3) Write the method  public static void selectionSort (Comparable[] item) for the Fraction class to sort the elements in ascending order.  Include any necessary submethods. Remember to include all necessary comments, and to solve the problem in as few steps as possible. Partial credit will be given for solutions that implement another sort algorithm such as BubbleSort. In addition, full credit will be given for writing the word void alone as your answer. Make sure to handle any and all exceptions and use appropriate naming conventions and indenting conventions for the code. You can use the answer the method defined in the previous question even if you think you did not answer it correctly.


// Precondition: Fraction[] values exists, and is initialized with valid entries

// Postcondition: the elements of the array of Fractions are in ascending order

public static void selectionSort(Fraction[] values) {

for (int a = values.length; a > 1; a--) {
for (int b = 0; b < a; b++) {
if (values[b].compareTo(values[a]) > 0) {
Fraction temp = values[b];
values[b] = values[a];
values[a] = temp;
}
}
}

}

now.. some of you read that carefully and realized that you can simply write the word void for you answer and recieve full credit.  If so, GOOD WORK!  If you're in the habit of FULLY reading instructions, that will serve you VERY well.  I give credit for either void or a correct SelectionSort (and partial credit for other sorting code)

Worth 6 points


4) Write the method public static Fraction findFraction(Fraction[] values, Fraction f) for the Fraction class to find the element in the array of Fractions that has the same value (though not neccesarily the same object) as f. If more than one of the Fractions have the same value as f, then return whichever of them you wish to. You can write whichever other methods to help you solve this that you want to. If you do not find a Fraction with the same value, return null. To receive full credit, this should implement a binary search. Note: you cannot assume that the array is already in ascending or descending order. You can call methods defined in other problems even if they do not actually work.


// Precondition: Fraction[] values and Fraction f exists, and is initialized with valid entries

// Postcondition: returns null if it cannot find a value inside of values with the same value as f, and returns a Fraction with the same value of f if it can be found. (note: the array may be sorted in the process of execution)

public static Fraction findFraction(Fraction[] values, Fraction f) {

return findFraction(values,f,0,values.length-1);
}

public static Fraction findFraction(Fraction[] values, Fraction f, int start, int end) {

// if start == end then we're at the last value, and either it's what we want or not  
if (start == end && values[start].compareTo(f) != 0) return null;
// otherwise find the mid point
int midpt = (start + end) / 2;
// see if our value is larger than or smaller than that midpoint and search through the other half
if (values[midpt].compareTo(f) > 0) return findFraction(values,f,midpt,end);
if (values[midpt].compareTo(f) < 0) return findFraction(values,f,start,midpt);
// if it's not smaller or larger than our value, then it's the same as our value, so return the midpoint Fraction
return values[midpt];

}

Worth 7 points if implemented as a binary search, 4 if implemented as a linear search.



The following class defines a worker (note: the implementation of each method is NOT shown, you can assume though that they are defined and work properly when you use them):


public class Worker implements Comparable { //stubbed documentation

/** Create a Worker from an input String, a single line with

* first name, last name, year, and rate in that order.

* If the String value is bad, the name is made null. */

public Worker (String input)

/** Return the first name plus last name of the Worker.

* But return null if it does not represent a real Worker. */

public String getName()

/** Return the Worker's birth year. */

public int getBirthYear()

}


This class defines a class that compiles information about the workers (pretend you don't have to worry about IO Exceptions)


public class PersonnelData {

private Buffin itsFile = new BufferedReader (new FileReader("workers.txt"));

private static final int TIME_SPAN = 10;

private static final int YEAR = 1960;

/** Read a file and return the string representation of the

* number of Workers born in each of 1960 to 1969. */

// you need to modify this method!

public String countBirthYears() {

int[ ] count = new int [TIME_SPAN]; // all zeros //1

//== READ THE WORKER DATA AND UPDATE THE COUNTS

Worker data = new Worker (itsFile.readLine()); //2

while (data.getName() != null) { //3

int lastDigit = data.getBirthYear() - YEAR; //4

count[lastDigit]++; // error check left as exercise//5

data = new Worker (itsFile.readLine()); //6

} //7

//== CONSTRUCT THE STRING OF ANSWERS

String s = ""; //8

for (int k = 0; k < TIME_SPAN; k++) //9

s += (YEAR + k) + " : " + count[k] + " workers\n"; //10

return s; //11

} //======================

}


5) Rewrite the countBirthYears() method in PersonnelData such that it also prints the birth year in the 1960's that occurs most frequently.

public String countBirthYears() {

int[ ] count = new int [TIME_SPAN]; // all zeros // this actually isn't used by anything EXCEPT to increase by 1 each time

// a worker has a birthyear of a certain year... so it's doing our dirty work for us!
//== READ THE WORKER DATA AND UPDATE THE COUNTS

Worker data = new Worker (itsFile.readLine()); //2

while (data.getName() != null) { //3

int lastDigit = data.getBirthYear() - YEAR; //4

count[lastDigit]++; // error check left as exercise//5

data = new Worker (itsFile.readLine()); //6

} //7

// at this point the the count array contains the number of times each year occurs, so the index with the highest value is the most common
// year!  So just find the index with the highest value and write to the screen

int highestyear = 0;

for (int a = 1; a < count.length; a++)

if (count[highestyear] < count[a]) highestyear = a;

System.out.println("The most common year is 196"+highestyear);
//== CONSTRUCT THE STRING OF ANSWERS

String s = ""; //8

for (int k = 0; k < TIME_SPAN; k++) //9

s += (YEAR + k) + " : " + count[k] + " workers\n"; //10

return s; //11

} //======================

}


Worth 6 points.