Exam 2 Spring 1999 Java
- A subclass is generally smaller than its superclass (True or False and why) (10)
False, a subclass, by definition, contains all the instance variables and methods from its parent plus any that it adds
2. We have discussed several access modifiers for class instance variables and methods (public, etc) list them and briefly explain each (10)
public – any instantiated class anywhere has access
private – only methods of this class have access
package – only methods of this package and this class have access
protected – only methods in this class, child classes, and this package have access
3. Write statements that will perform the following single scripted array operations (5 each):
- create an array of 10 ints called points and initialize each element to 1
- set all 10 elements in the array points above to 99
- add 5 to the third element in the points array
- int[] points = {1,1,1,1,1,1,1,1,1,1};
- for (int i=0; i<10;i++) points[I] = 99;
- points[2] += 5;
5. Write the method bubblesort. The method should take an int array as its only argument and should sort the array using the MOST EFFICIENT BUBBLE SORT YOU KNOW (15).
public void bubblesort(int[] a) {
boolean bSwapped = false;
check:
for(int i=0; i<a.length-1;i++) {
bSwapped = false;
for(int j=i+1;j<a.length;j++) {
if (a[i] > a[j]) {
swap(a, i, j)
bSwapped = true;
}
}
if (!bSwapped) break check;
}
}
// or something similar
6. A design hint in a very competent Java textbook gives the following advice regarding inheritance:
Whenever you find code of the form (10)
if (x is of type 1)
action1(x);
else if (x is of type 2)
action2(x);
think polymorphism.
Please explain this statement.
The point is that I can do the same thing with more flexibility using a parent class with an abstract method. Then inherit the class and create the method in the child class. That way when I call the method using the child object the correct method is done without any if statements.
- Write the above classes in Java (40)
public abstract class Lifeform {
private int weight;
private int height;
public Lifeform() {
this(0,0);
}
public Lifeform(int a, int b) {weight = a; height = b;}
public String Description() {return "Lifeform";}
public abstract long NumberChildren(long years);
}
public class Bugs extends Lifeform {
private int number_legs;
public Bugs() {
super();
number_legs = 0;
}
public Bugs(int a, int b, int c) {
super(a, b);
number_legs = c;
}
public String Description() {return "Bug";}
public long NumberChildren(long years) {return years*12*9999;}
}
public abstract class Animals extends Lifeform {
private int number_legs;
private String home;
private String Sex;
public Animals() {
super();
number_legs = 0;
home = "";
Sex = "";
}
public Animals(int a, int b, int c, String h, String S) {
super(a, b);
number_legs = c;
home = h;
Sex = (S.toLowerCase().equals("male")) ? "male" : "female";
}
public String Description() {return "Animal";}
}
public class Humans extends Animals {
private String LastName;
private String FirstName;
public Humans() {
LastName = "";
FirstName = "";
}
public Humans(int a, int b, int c, String h, String S, String L, String F) {
super(a, b, c, h, S);
LastName = L;
FirstName = F;
}
public String Description() {return "Human";}
public long NumberChildren(long years) {return years*12/10;}
}
public class Rabbit extends Animals {
private String Type;
private String Color;
public Rabbit() {
Type = "";
Color = "";
}
public Rabbit(int a, int b, int c, String h, String S, String T, String C) {
super(a, b, c, h, S);
Type = T;
Color = C;
}
public String Description() {return "Rabbit";}
public long NumberChildren(long years) {return years*52/9*10;}
}