(10%) You are given the following code:
public class Person{
/** The name of the person.*/
private String name;
/** 'M' for male and 'F' for female. */
private char sex;
/** Height in inches. */
private double height;
public Person(String name, char sex, double height){
this.name = name;
this.sex = sex;
this.height = height;
}
public String toString(){
return name;
}
public char getSex(){
return sex;
}
public void setSex(char sex){
this.sex = sex;
}
public double getHeight(){
return height;
}
public void setHeight(double height){
this.height = height;
}
}
Implement a Student
class that extends the
Person
class. Each student has a major, which is
represented by a string. The Student
class also
needs to implement a constructor, and a
getSexMajor()
function which returns a String
with the student's sex, a dash '-', and the major. For
example, the following program:
public class Student extends Person{
/** Student's major. */
private String major;
public Student(String name, char sex, double height, String major){
super(name,sex,height);
this.major = major;
}
public String getSexMajor(){
return getSex() + "-" + major;
}
public static void main(String[] args){
Student a = new Student("John", 'M', 60.5, "CSCE");
Student b = new Student("Jill", 'F', 65.5, "CSCE");
System.out.println(a + " " + a.getSexMajor());
System.out.println(b + " " + b.getSexMajor());
}
}
should print out
John M-CSCE
Jill F-CSCE
(10%) Write a function called getBonus()
for the Employee
class below which returns the
amount of bonus pay the employee will receive. You, as the boss,
have determined that the final bonuses will be determined
following these rules:
- Anyone with a grade of 'D' or lower will get no bonus.
- Employees with a level bigger than or equal to 3 will get a bonus of 5% of their salary.
- Employees with a level less than 3 will get a bonus of 6% of their salary.
- All bonuses are capped at $5,000. That is, no one can get more than $5,000.
- Anyone with a grade of 'A' or age > 60 gets an extra $1000.
public class Employee {
/** In dollars */
private double salary;
/** In years. */
private int age;
/** Level is from 0 (lowest) to 6 (highest) */
private int level;
/** Appraisal grade for this year:
It is one of: 'A', 'B', 'C', 'D' or 'F' */
private char grade;
public Employee(double salary, int age, int level, char grade){
this.salary = salary;
this.age = age;
this.level = level;
this.grade = grade;
}
public double getBonus(){
if (grade == 'D' || grade == 'F')
return 0;
double bonus = 0;
if (level >= 3)
bonus = salary * 0.05;
else
bonus = salary * 0.06;
if (grade == 'A')
bonus += 1000;
if (bonus > 5000)
bonus = 5000;
return bonus;
}
public static void main(String[] args){
Employee[] people = new Employee[8];
people[0] = new Employee(120000, 65, 6, 'B');
people[1] = new Employee(110000, 61, 5, 'A');
people[2] = new Employee(84000, 42, 5, 'A');
people[3] = new Employee(50000, 35, 3, 'F');
people[4] = new Employee(44000, 32, 2, 'C');
people[5] = new Employee(40000, 28, 1, 'B');
people[6] = new Employee(1200, 25, 0, 'A');
people[7] = new Employee(1200, 23, 0, 'C');
for (int i=0; i< people.length; i++)
System.out.println(people[i].getBonus());
}
}
(15%) What does the following program print out?
public class A {
protected int[] x;
public A(){
x = new int[3];
x[0] = 0;
x[1] = 1;
x[2] = 2;
};
public String getX(){
String result = "";
for (int i =0; i < x.length; i++)
result += x[i] + "-";
return result;
}
public void setXi(int index, int value){
x[index] = value;
}
public void setX(int[] x){
this.x = x;
}
public String toString(){
return "A";}
}
public class B extends A{
public B(){
super ();};
public void setXi(int index, int value){
x[index] = value + 10;
}
public String toString(){
return "B";}
}
public class C extends B{
public C(){super ();};
public void setXi(int index, int value){
super.setXi(index, value - 5);
}
public String toString(){
return "C";}
public static void main (String[] args){
A[] v = new A[3];
v[0] = new A();
v[1] = new B();
v[2] = new C();
for (int i =0; i < v.length; i++){
System.out.println(v[i]);
if (v[i] instanceof B) {
System.out.println("It is a B");
}
}
System.out.println("---");
A a = new A();
System.out.println(a.getX());
int[] x = new int[3];
x[0] = 5;
x[1] = 6;
x[2] = 7;
a.setX(x);
System.out.println(a.getX());
a.setXi(0,0);
System.out.println(a.getX());
a.setXi(1,1);
System.out.println(a.getX());
x[2] = 2;
System.out.println(a.getX());
System.out.println("---");
B b = new B();
System.out.println(b.getX());
b.setXi(1,1);
System.out.println(b.getX());
A a2 = (A)b;
a2.setXi(2,2);
System.out.println(a2.getX());
System.out.println(b.getX());
System.out.println("---");
C c = new C();
System.out.println(c.getX());
c.setXi(0,5);
System.out.println(c.getX());
}
}
A
B
It is a B
C
It is a B
---
0-1-2-
5-6-7-
0-6-7-
0-1-7-
0-1-2-
---
0-1-2-
0-11-2-
0-11-12-
0-11-12-
---
0-1-2-
10-1-2-
(30%) A 3x3 normal magic square is a 3x3 grid where
the numbers on each row, each column, and both diagonals
all add up to the same number, and the square contains the
numbers 1 to 9 exactly. For example, the following is the Lo
Shu magic square:
Implement a function which, given a two-dimensional 3 by 3 array of
ints returns a boolean that tells us if the given square (represented
by the array) is a normal 3 by 3 magic square or not.
public class Magic {
public static boolean check(int[][] a){
int sum,s;
sum=0;
for (int col = 0; col< 3; col++) sum += a[0][col];
s = 0;
for (int row = 1; row < 3; row++){
s = 0;
for (int col = 0; col< 3; col++)
s += a[row][col];
if (s != sum)
return false;
}
for (int col = 0; col< 3; col++) {
s = 0;
for (int row = 0; row < 3; row++){
s += a[row][col];
}
if (s != sum)
return false;
}
s =0;
for (int row = 0; row < 3; row++){
s += a[row][row];
}
if (s != sum)
return false;
s =0;
for (int row = 0; row < 3; row++){
s += a[row][2 - row];
}
if (s != sum)
return false;
boolean[] appears = new boolean[9];
for (int i=0; i < 9; i++){
appears[i] = false;
}
for (int col = 0; col< 3; col++) {
for (int row = 0; row < 3; row++){
int num = a[row][col];
if (appears[num-1]) return false; appears[num-1] = true;
}
}
return true;
}
public static void main (String[] args){
int[][] loshu = {{4,9,2},{3,5,7},{8,1,6}};
System.out.println(Magic.check(loshu));
int[][] no1 = {{4,9,2},{3,7,5},{8,1,6}};
System.out.println(Magic.check(no1));
int[][] no2 = {{4,9,2},{3,3,5},{8,1,6}};
System.out.println(Magic.check(no2));
}
/** From wikipedia: The Lo Shu square (3×3 magic square)
Chinese literature dating from as early as 2800 BC tells the legend of
Lo Shu or "scroll of the river Lo". In ancient China, there was a huge
flood. The people tried to offer some sacrifice to the river god of
one of the flooding rivers, the Lo river, to calm his anger. Then,
there emerged from the water a turtle with a curious figure/pattern on
its shell; there were circular dots of numbers that were arranged in a
three by three nine-grid pattern such that the sum of the numbers in
each row, column and diagonal was the same: 15. This number is also
equal to the number of days in each of the 24 cycles of the Chinese
solar year. This pattern, in a certain way, was used by the people in
controlling the river. */
}