- The Largest class:
// Find largest element in an array.
public class Largest {
public static int[] nums = {34, 27, 56, 12, 89, 67, 23};
public static void main (String[] args) {
int largest = nums[0];
for (int i = 1; i < nums.length; i++)
if (nums[i] > largest) largest = nums[i];
System.out.println("Largest value: " + largest);
}
}
/* Output:
Largest value: 89
*/
- The Triples class:
// Triples.java: keep rolling triples of dice until three 1's appear
public class Triples {
public void runSim() {
int count = 0;
while (true) {
count++;
if (roll() + roll() + roll() == 3) break;
}
System.out.println("Number of times: " + count);
}
int roll() {
return (int)(6.0*Math.random() + 1.0);
}
public static void main(String[] args) {
Triples t = new Triples();
t.runSim();
}
}
/* Output:
Typical results: 68 207 31 41 153 73 363 52 124
*/
- The Fibonacci class:
// Calculate and print successive Fibonacci numbers.
public class Fibonacci {
public static void main (String[] args) {
int[] fib = new int[40]; // Part 1.a.
fib[0] = 0; fib[1] = 1; // Part 1.b.
for (int i = 2; i < fib.length; i++) // Part 1.c.
fib[i] = fib[i-1] + fib[i-2];
for (int i = 0; i < fib.length; i++) { // Part 1.d.
System.out.print(fib[i]);
if (i != fib.length - 1) System.out.print(", ");
else System.out.println();
}
} // end of main
}
/* Output (actually all on one line):
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,
1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393,
196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887,
9227465, 14930352, 24157817, 39088169, 63245986
*/
- The Student and Studentsclasss
(Answers to question in
boldface):
// 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) {
name = n; totalGradePoints = initGradePoints; totalHours = initHours;
GPA = (double)totalGradePoints / totalHours;
}
// same as above, but replaces total gradepoints by grade point average
public Student(String n, int initHours, double initGPA) {
name = n; totalGradePoints = (int)(initGPA * totalHours);
totalHours = initHours;
GPA = initGPA;
}
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) {
return name.compareTo(((Student)t).name);
}
}
// Students.java: test the Student class
public class Students {
public static void main (String[] args) {
Student bWilson = new Student("Wilson, Bob", 20, 3.4);
Student bSanders = new Student("Sanders, Bruce", 35, 92);
Student jSamson = new Student("Samson, John", 25, 3.2);
Student rWeston = new Student("Weston, Robert", 40, 145);
System.out.println(bWilson + "\n" + bSanders +
"\n" + jSamson + "\n" + rWeston);
System.out.println("GPA of Bruce Sanders: " + bSanders.getGPA());
System.out.println(bSanders.compareTo(jSamson));
System.out.println(bSanders.compareTo(rWeston));
}
}
Output:
Name: Wilson, Bob, GPA: 3.4, Total Hours: 20
Name: Sanders, Bruce, GPA: 2.6285714285714286, Total Hours: 35
Name: Samson, John, GPA: 3.2, Total Hours: 25
Name: Weston, Robert, GPA: 3.625, Total Hours: 40
GPA of Bruce Sanders: 2.6285714285714286
1
-4
- The Person hierarchy
(Answers to question in boldface):
// 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
name = n; address = 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
super(n, a); // call the superclass constructor
salary = s;
}
// convert the Faculty class to a String
public String toString() {
return "Name and address:" + super.toString() +
"; Wages = " + getWages();
}
public double getWages() {
return salary;
}
}
// Staff.java: the class Staff
public class Staff extends Employee { // inherits from Employee
protected double hourlyRate;
protected int hoursWorked;
// Constructor
public Staff(double hr, int hw, String n, String a) {
super(n, a); // call the superclass constructor
hourlyRate = hr; hoursWorked = hw;
}
// convert the Faculty class to a String
public String toString() {
return "Name and address:" + super.toString() +
"; Wages = " + getWages();
}
public double getWages() {
return hourlyRate * hoursWorked;
}
}
// 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] = new Employee("Nobody, Nonce",
"120 Worry Lane");
persons[1] = new Faculty(20200, "Blow, Joe",
"222 Center St.");
persons[2] = new Staff( 7.5, 40, "Bonkers, Bruce",
"1 Prime Ave");
persons[3] = new Staff(10.0, 40, "Silly, Sally",
"1213 West 8th");
persons[4] = new Faculty(31500, "Wayne, Bruce",
"34 S. Main");
persons[5] = new Faculty(25000, "Lee, Bruce",
"4242 E. 42nd St.");
persons[6] = new Employee("Bruce, Lenny",
"54 S. 121st St.");
persons[7] = new Faculty(35700, "Boring, Ass",
"54 54th Ave");
persons[8] = new Staff(6.5, 50, "Think, Ofnames",
"4747 Melon St.");
persons[9] = new Staff(7.5, 45, "Crazy, Going",
"27 West Ave");
printPersons(persons);
System.out.println();
}
public static void printPersons(Person[] s) {
for (int i = 0; i < s.length; i++)
System.out.println(s[i]);
}
}
Output:
[Nobody, Nonce, 120 Worry Lane]
Name and address:[Blow, Joe, 222 Center St.]; Wages = 20200.0
Name and address:[Bonkers, Bruce, 1 Prime Ave]; Wages = 300.0
Name and address:[Silly, Sally, 1213 West 8th]; Wages = 400.0
Name and address:[Wayne, Bruce, 34 S. Main]; Wages = 31500.0
Name and address:[Lee, Bruce, 4242 E. 42nd St.]; Wages = 25000.0
[Bruce, Lenny, 54 S. 121st St.]
Name and address:[Boring, Ass, 54 54th Ave]; Wages = 35700.0
Name and address:[Think, Ofnames, 4747 Melon St.]; Wages = 325.0
Name and address:[Crazy, Going, 27 West Ave]; Wages = 337.5
- The Bottles class (Answers to question in
boldface):
// Bottles.java: print song "99 bottles of beer on the wall"
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?";
// just to drop the "s" when only one bottle
private String shortB2 = " bottle of beer.";
private String longB2 = " bottle of beer on the wall.";
public void printSong() {
for (int bot = 99; bot > 0; bot--) {
System.out.print(toEnglish(bot));
// each if below just to drop "s" when one bottle
// this was not required for the answer
if (bot == 1) System.out.println(longB2);
else System.out.println(longB);
System.out.print(toEnglish(bot));
if (bot == 1) System.out.println(shortB2);
else System.out.println(shortB);
System.out.println(wall);
System.out.print(toEnglish(bot-1));
if (bot-1 == 1) System.out.println(longB2);
else System.out.println(longB);
System.out.println();
}
}
private String toEnglish(int n) {
if (n < 20) return teens[n];
if (n%10 == 0) return tens[n/10];
return tens[n/10] + "-" + ones[n%10];
}
// Instead of a separate BottlesMain class with a main function,
// one can put the main inside here.
public static void main (String[] args) {
Bottles bottles = new Bottles();
bottles.printSong();
}
}
Partial Output (Complete song):
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.
... (many lines deleted)
Two bottles of beer on the wall.
Two bottles of beer.
What would happen if one should fall?
One bottle of beer on the wall.
One bottle of beer on the wall.
One bottle of beer.
What would happen if one should fall?
Zero bottles of beer on the wall.