Directions: Use your own paper for answers. When you are done you may keep this exam sheet and should pick up an answer sheet.
public int[] nums = {34, 27, 56, 12, 89, 67, 23};
Write a simple code segment that will find the largest value
in this array.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, ...After you start with 0 and 1, each later number is just the sum of the two previous numbers.
Write a program segment (no need for an enclosing class or main method) that will calculate and print the first 40 Fibonacci numbers in the following way:
// Student.java: student grade record
public class Student implements Comparable{
private String name; // In the form "LastName, FirstName"
private int totalHours; // total number of hours completed
private int totalGradePoints; // GPA = totalGradePoints / totalHours
private double GPA; // grade point average
// uses name, total hours taken, and total gradepoints as parameters
public Student(String n, int initHours, int initGradePoints) {
// Fill in for Part a.
}
// same as above, but replaces total gradepoints by grade point average
public Student(String n, int initHours, double initGPA) {
// Fill in for Part b.
}
public double getGPA() { // return the GPA
return GPA;
}
// assume the students takes a single course and gets a grade
public void updateRecord(int hours, int grade) {
totalHours += hours; totalGradePoints += hours * grade;
GPA = (double)totalGradePoints / totalHours;
}
public String toString() {
return "Name: " + name + ", GPA: " + GPA +
", Total Hours: " + totalHours;
}
public int compareTo(Object t) { // compare names in alphabetic order.
// Fill in for Part d.
}
}
// Person.java: abstract base class
public abstract class Person {
public abstract double getWages(); // abstract, so omit body
}
// Employee.java: the class Employee
public class Employee extends Person {
protected String name; // in the form "Last, First"
protected String address;
// constructor
public Employee(String n, String a) { // first name, then address
// Fill in for Part a.
}
// convert the point into a String representation
public String toString() {
return "[" + name + ", " + address + "]";
}
public double getWages() { // must be either faculty or staff for wages
return 0.0;
}
}
// Faculty.java: the class Faculty
public class Faculty extends Employee { // inherits from Employee
protected double salary;
// Constructor
public Faculty(double s, String n, String a) { // salary, name, address
// Fill in for Part b.
}
// convert the Faculty class to a String
public String toString() {
// Fill in for Part c.
}
public double getWages() {
return salary;
}
}
// Staff.java: the class Staff
public class Staff
// Fill in for Parts d., e., f.
}
// Persons.java: test the Person-Employee-Faculty-Staff hierarchy
public class Persons {
public static void main (String[] args) {
Person[] persons = new Person[10];
persons[0] = // Fill in for Part g.
persons[1] = // Fill in for Part g.
persons[2] = // Fill in for Part g.
printPersons(persons);
System.out.println();
}
public static void printPersons(Person[] s) {
for (int i = 0; i < s.length; i++)
System.out.println(s[i]);
}
}
public class Bottles {
private String[] ones =
{"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"};
private String[] teens =
{"Zero", "One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private String[] tens =
{"", "Ten", "Twenty", "Thirty", "Forty", "Fifty",
"Sixty", "Seventy", "Eighty", "Ninety"};
private String shortB = " bottles of beer.";
private String longB = " bottles of beer on the wall.";
private String wall = "What would happen if one should fall?";
public void printSong() {
// answer to part b. here
}
private String toEnglish(int n) {
// answer to part a. here
}
}
The song you are to print starts like this:
Ninety-nine bottles of beer on the wall. Ninety-nine bottles of beer. What would happen if one should fall? Ninety-eight bottles of beer on the wall. Ninety-eight bottles of beer on the wall. Ninety-eight bottles of beer. What would happen if one should fall? Ninety-seven bottles of beer on the wall. Ninety-seven bottles of beer on the wall. ... (and so on until) ... zero bottles of beer on the wall.