Directions: Use your own paper for answers. Do not spend too much time on any one question.
// first seven prime numbers
public int[] nums = {2, 3, 5, 7, 11, 13, 17};
Write a simple code segment (with a loop) that will add up the
square of each array entry.
When you are done, print your answer. (Your program
should not directly use the number 7,
the number of elements in the array, but should work unchanged
if one added 19 to the end of the above array.)
// months of the year
public String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Write a simple code segment (with a loop) that will find the smallest value
(first in alphabetic order) in this array. Your loop should use
the length field of the array, and not the constant
12. (Remember that you cannot use <
to compare two strings.)
Write a code segment (not a complete program) that will simulate playing Roulette over and over again, each time betting on ODD (so that you win if one of the 18 numbers 1, 3, 5, ..., 35 comes up). Each time you will bet one dollar. You start with an initial stake of 100 dollars. If you win, your stake increases by $1, and if you lose it decreases by $1. Simulate play until either you have doubled your stake (to $200), or until you have lost all your money. Count the number of times you have to play till you finish in this way.
[You may assume that you have a class Wheel with a static method roll() that will return the result of rolling Roulette: an integer from 0 to 36 inclusive. Also, an int n is odd in case n%2 == 1.]
// 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; // total number of grade points
private double GPA; // grade point average = totalGradePoints / totalHours
// uses name, total hours taken, and total gradepoints as parameters
public Student(String n, int initHours, int initGradePoints) {
// Fill in for Part a. here
}
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 using GPA.
// Fill in for Part b. here
}
}
// Students.java: test the Student class
public class Students {
public static void main (String[] args) {
// four students, make up 4 variable names
// fill in for part c. here
// invoke getGPA method for John West
// fill in for part d. here
// create an array names students to hold 4 student classes
// fill in for part e. here
// insert the 4 student classes into the array
// fill in for part f. here
// find student with highest GPA
// fill in for part g. here
}
}
// 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. here
}
// 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. here
persons[1] = // Fill in for Part g. here
persons[2] = // Fill in for Part g. here
// loop to print each element of persons array
// Fill in for Part h. here
}
}