Shared Flashcard Set

Details

Ap Computer Science Set 1
Set 1
18
Computer Science
Not Applicable
01/26/2013

Additional Computer Science Flashcards

 


 

Cards

Term

public class Player implements Comparable {

    private String name;

 

    public Player(String n) { name = n };

 

    public String getName() { return name };

    public int compareTo(Object o) { /* not shown */ }

}

 

What will happen when the following code runs?

 

Comparable c = new Player("E.L. Doctorow");

System.out.println( c.getName() );

Definition

A compile time error:  the method getName() is unidentified for the type Comparable.

 

The solution:  cast it to be of type Player.

 

System.out.println( ((Player)c).getName() );

Term

What, if anything, is the problem with the following compareTo method?


public class Range implements Comparable {

    private int low, high;

 

    public Range(int a, int b) {

        low = Math.min(a, b);

        high = Math.max(a, b);

    }

 

    public int compareTo(Object o) {

        int mySize = high - low;

        int otherSize = o.high - o.low;

        if (mySize > otherSize) return 1;

        if (mySize < otherSize) return -1;

        return 0;

    }

}

Definition

    public int compareTo(Object o) {

        int mySize = high - low;

        int otherSize = o.high - o.low;

        if (mySize > otherSize) return 1;

        if (mySize < otherSize) return -1;

        return 0;

    }


The variable o is of type Object, so it has no high or low fields to access.  The solution is to cast it:


((Range)o).high - ((Range)o).low


You might think that this will also cause a problem because those fields are private.  This is actually ok because the executing object and the argument are both of the type Range.

Term

What, if anything, is the problem with the following compareTo method?

 

public class Range implements Comparable {

    private int low, high;

    public Range(int a, int b) {

        low = Math.min(a, b);

        high = Math.max(a, b);

    }

 

    public int compareTo(Object o) {

        Range r = (Range)o;

        int otherSize = r.high - r.low;

        if (high-low > otherSize) return 1;

        if (high-low < otherSize) return -1;

        return 0;

    }

}

Definition
Nothing is wrong.  Someone might think that r.high and r.low are the problem because low and high are private fields.  This is actually ok because both r and the executing object are the same type, so it can access the private fields.
Term

Given the following interface:

 

public interface Named {

    public String getName();

}

 

(a)  True or false:  Can I make the following declaration....

 

Named my_variable;

 

(b)  What kinds of things could go in the code that's not shown?

 

my_variable = /* not shown */;

Definition

(a)  True.  Interfaces define valid data types.

 

(b)

CANNOT DO THIS:  my_variable = new Named();

 

You can instantiate any class which implements the Named interface and assign it to my_variable.

Term

Given the following interface and class:

 

public interface GridObj {

    public double getX();  public double getY();

}

 

public class Point implements GridObj {

    private double x, y;

    public Point(double a, double b) { x = a; y = b }

    public boolean inQuad1() { return (x > 0 && y > 0);

    /* getX() and getY() not shown */

}

 

What will happen when you try and run the following code?

 

GridObj x = new Point(3, 4);

if ( x.inQuad1() ) { ... }

 

Definition

x is of type GridObj which doesn't have an inQuad1 method.  It doesn't matter that the object referred to by x does.  To use this method you would have to cast it first:

 

GridObj x = new Point(3, 4);

if ( ((Point)x).inQuad1() ) { ... }

Term

public class Person {

    private String name;

    public Person(String n) { name = n; }

}

 

public class Student extends Person {

    private int gradeLevel;

    public Student(String n, int g) { super(n); gradeLevel = g; }

}

 

Which of the following is ok?  Why?  How do you decide this kind of question in general?

 

Person p1 = new Student("Molly", 9);

Student s1 = new Person("Molely");

Definition

Since Student extends Person, Student is-a Person.  This means that you can assign a Student object to a Person variable.  Another way to think about it is that a Student object has all the methods a Person datatype does, so it's safe to assign it.

 

You cannot assign a Person object to a Student variable.  A Person object does not necessarily have all the methods a Student data type does, so it's not safe to assign it.

Term

public class Person {

    private String name;

    public Person(String n) { name = n; }

    public String getName() { return name; }

}

 

public class Student extends Person {

    private int gradeLevel;

    public Student(String n, int g) { super(n); gradeLevel = g; }

    public String getName() { return "Student: " + name; }

    public void mysteryMethod() { System.out.println( "Mystery!" ); }

}


What will happen when the following code executes?


Person p = new Student("Molly", 9);

System.out.println( p.getName() );

p.mysteryMethod();

Definition

The first print statement will display the Student version of getName(), even though the variable is of type Person.  This is called Polymorphism.

 

The call to mysteryMethod() will be an error, because the datatype is Person and Person objects don't have mysteryMethod().

 

So, the rule is:  If the superclass has the method that's been overloaded, the child version runs.  If the superclass doesn't have the child method, it's an error.

Term

Write the correct class declaration line for a class called Point which implements two interfaces named Comparable an Draggable.

 

Definition
public class Point implements Comparable, Draggable { ... }
Term
Write the correct class declaration line for a class called PokerHand which extends the class Hand and implements the interface Comparable.
Definition
public class PokerHand extends Hand implements Comparable { ... }
Term
If a class implements comparable, what is the exact method signature for the method that needs to be implemented?
Definition

public int compareTo(Object o) { .... }

 

Should return a positive number if o is larger than executing object.

Should return 0 if they're equal.

Should return a -1 if o is larger than the executing object.

Term

public interface Racer {

    public int getNumber();

    public String getName();

}

 

public class Horse implements Racer {

    private String name;

    // constructor and methods not shown    

}

 

public class CompetitionHorse extends Horse implements Comparable {

    private int ranking;

    // constructor and methods not shown

}

 

Consider this code:

 

1  Comparable c1 = new CompetitionHorse( /* correct parameters */ );

2  Comparable c2 = new CompetitionHorse( /* correct parameters */ );

3  if (c1.compareTo(c2) < 0)

4      int x = c1.getRank();


Why are lines 1 and 2 ok?  Why might someone think they're not ok?

Why is line 4 not ok?  Why might someone think it is ok?

How can you fix line 4 so it works as intended?

Definition

Comparable c1 = new CompetitionHorse( /* correct parameters */ );


It's ok to assign a CompetitionHorse object to Comparable because it implements Comparable, so it can be treated as that data type.


int x = c1.getRank();


This line causes an error because variable c1 is of type Comparable, which doesn't have a getRank() method.


To make this work you have to cast it as a CompetitionHorse:


4    int x = ((CompetitionHorse)c1).getRank();

Term

public interface Racer {

    public int getNumber();

    public String getName();

}

 

public class Horse implements Racer {

    private String name;

    // constructor and methods not shown    

}

 

public class CompetitionHorse extends Horse implements Comparable {

    private int ranking;

    // constructor and methods not shown

}

 

Consider this code:

 

1  Horse h1 = new Horse( /* correct parameters */ );

2  Horse h2 = new Horse( /* correct parameters */ );

3  if (h1.compareTo(h2) < 0)

4      int x = h1.getRank();


 

Why is line 3 not ok?  Why might someone think it is ok?

Is there any way to make this code work as intended?

Definition

3  if (h1.compareTo(h2) < 0)


h1 is a Horse object.  Horse does not implement Comparable.  The subclass of Horse does, but that doesn't matter, because Horse doesn't.


There is no particularly good way to make this code work as intended, because it's trying to compare objects that just aren't comparable.  Probably the best thing to do is to implement the Comparable interface in Horse.

Term

Class B and C both extend Class A.  Class D extends class B.

 

Is this allowed?  Explain.

 

A x = new B();

A y = new C();

A z = new D();

Definition
Yes.  B, C, and D are all sub-classes of A, so you can assign them to a variable of type A.
Term

Class B and C both extend Class A.  Class D extends class B.

 

Is this allowed?  Explain.

 

B p = new A();

Definition
No.  B is-a A because B extends A.  But A is not a B, so you can't assign an A object to the B datatype.
Term

Class B and C both extend Class A. Class D extends class B.

 

Is this allowed? Explain.

 

B p = new C();

Definition
No.  Just because B and C both extend A doesn't mean you can treat them like each other.  C is not a type of B, so you can't assign it.
Supporting users have an ad free experience!