- A rational value is one with an integer numerator and positive integer denominator. Create a class Rational with
- a constructor that requires a legal numerator and
denominator (use 0 as the numerator, 1 as denominator if
the values given are illegal).
- an accessor method that gets the numerator.
- an accessor method that gets the denominator.
- a method public void add(Rational r) that adds
the Rational r onto this rational.
- Short answer: use the following class definitions in this
question, as necessary:
public class Bug {
private int numLegs;
private static final int SPIDER_LEGS = 8;
private static final int INSECT_LEGS = 6;
public Bug(int n){
numLegs=n;
}
public boolean isInsect(){
boolean result=false;
if (numLegs==INSECT_LEGS){
result=true;
}
return result;
}
}
public class Hive {
private Bug queen;
private Bug [] workers;
public Hive(int n) {
workers=new Bug[n]; // line 1
queen = new Bug(6); // line 2
...
}
private void replaceQueen(Bug q){
...
}
}
- T/F: queen.numLegs = 5; would be a legal statement to add to the
constructor of the Hive class. (Explain)
- T/F: queen is an instance variable. (Explain)
- T/F: The variable result could be referenced from a method
inside of class Hive. (Explain)
- Explain the difference between the line marked "line 1" and the one marked "line 2" in terms of what is being created in each.
- Explain why someone might use a local variable rather
than an instance variable.
- Explain why a method would use a parameter.
- Explain the difference between a white-box test and a
black-box test.
- Explain the difference between a HashMap and a HashSet.
- Find as many errors as you can in the code below:
public class Student {
String name;
String id;
public Student(String name, String id){
name=name;
id = id;
}
public String getName() {
return name;
}
public String getID (){
return id;
}
}
import java.util.ArrayList;
import java.util.Iterator;
public class Registrar {
private ArrayList studentList;
public Registrar(){
studentList = new ArrayList ();
}
public void enrollStudent (Student newStudent){
Student student=newStudent;
studentList.add(student);
}
public Student findStudent(String targetID) {
Iterator studentIT = studentList.iterator();
Student current = null;
boolean found = false;
while ((studentIT.hasNext()) && (!found)){
studentIT.next();
current = (Student)studentIT.next();
if (targeted == current.getID()){
found=true;
}
}
return current;
}
public Student removeStudent(String targetID){
boolean found=false;
Student current = null;
for (int i=0; i < studentList.size(); i++){
current = (Student) studentList.get(i);
if (current.equals(targetID)){
found=true;
studentList.remove(i);
}
}
return current;
}
}
-
- What is the output from the following code if we pass "this is a test" to the constructor in creating a Test1 object and then call doSomethingMore() on that object?
import java.util.ArrayList;
public class Test1
{
private int [] mystery1;
private String string;
private ArrayList mystery2;
public Test1(String str)
{
mystery1=new int[str.length()];
for (int i=0; i < mystery1.length; i++){
mystery1[i]=0;
}
string=str;
mystery2 = new ArrayList();
}
public void doSomething(){
for (int i=0; i < string.length(); i++){
String mystery3=string.substring(i, i+1);
boolean x=false;
for (int j=0; j < mystery2.size(); j++){
if(mystery2.get(j).equals(mystery3)){
x=true;
mystery1[j]++;
}
}
if(!x){
mystery1[mystery2.size()]++;
mystery2.add(mystery3);
}
}
}
public void doSomethingMore(){
doSomething();
for (int i=0; i < mystery2.size(); i++){
System.out.println(mystery2.get(i)+": "+mystery1[i]);
}
}
}
- What happens if we call doSomethingMore() again on the same Test1 object?
What is the output?
- Consider the CalendarDate class and the method
boolean isLaterThan(CalendarDate comparisonDate)
- When specifying a test case, what information would we need to give?
- List a set of test cases that you think would convince a user that the method is correct.