APCS Spring 2024 Unit Nine Exercises
APCS Spring 2024 Unit Nine Exercises
Name: _____________________________________
For questions 1 – 7, assume we have these two classes and their methods:
1. Show how you would write the header (signature) for the Triangle class so that it
inherits the Polygon class.
In the following problems assume that the Triangle class inherits the Polygon class, that
ply is a Polygon object, and that tri is a Triangle object. Furthermore, assume that all
these objects have been created and are used from within some other class that is
unrelated to the Triangle and Polygon classes. For each problem, state whether the
usage of the method is legal or not.
2. tri.side1( ) __________
3. tri.perimeter( ) __________
4. tri.numVertices( ) __________
5. ply.area( ) __________
6. ply.numVertices( ) __________
7. ply.side3( ) __________
public class Dragon_Student extends Texan
{
public String parseNames(String nm)
{ … }
public void detVee(int zx)
{
<#1>
}
. . .
public int zx = 5;
}
9. What is the meaning of final as applied to a class (example, public final class Texan)?
10. What is the meaning of final as applied to a method (example, public final void met( )
)?
11. Suppose from within some other method of Dragon_Student you wish to call the
parseNames method in Texan. Assuming you wish to pass String “Chris P. Bacon” to that
method, show code that would accomplish this and assign the returned String to String
FirstNm.
12. What code replaces <#1> so that the passed parameter zx is incremented and then
assigned to the Dragon_Student instance variable zx.
Use the following code for questions 14 through 21:
public String s;
private int i;
}
18. Is this code below legal? If not, why not? (Assume this code is in some class other
than the two above).
Red myObj = new Red( );
boolean bb = myObj.crunch( );
19. Is this code below legal? If not, why not? (Assume this code is in some class other
than the two above).
Red myObj = new Red( );
int bb = myObj.blue(105.2);
20. Write code for the blue method that will print out the mm state variable.
21. Write code for the blue method that will print out the xx state variable.
Use the following two classes for questions 22 through 25:
public class Red extends Green
{
//constructor not shown
public int blue(double x)
{ . . . }
public String s;
private int i;
}
23. Is there any method in Red that is overriding a method in Green? If so, what is it?
24. Look at the Peabody method inside Red. Write the code inside that method that will
allow you to access the same method inside its superclass, Green. Let the parameter
have a value of 11.
26. Assume that the following fragments of code are all in a subclass. Match each to an
item from the “sentence bank” to the right.
_____ this.(x,y) a. refers to a constructor in the superclass
_____ this.z b. refers to a constructor in the subclass
_____ super(j) c. refers to an overridden method in the super class
_____ super.calc( ) d. refers to a data member in the subclass
27. What is shadowing (as the term applies to super classes and subclasses)?
The following code applies to problems 28 through 34:
public class Parent
{
public void rubyDoo( )
{ . . . }
. . .
public int x = 0;
}
What is printed?
Is there any way using the myChild object to retrieve the x state field within the Parent
class? Write the code that will do this. You may write a new method for either class if you
need to.
30. What is the name of the class that every class (that does not extend another class)
automatically extends?
31. Is the following legal? If not, why not?
Child theObj = new Child( );
Parent newObj = theObj;
newObj.busterStein( );
34.
A a = new B(5, 10);
System.out.println(a.g( ));
35.
A a = new B(5, 10);
System.out.println( a.f( ) );
36.
A a = new B(5, 10);
System.out.println( a.x );
37.
B a = new B(5, 10);
System.out.println( a.x );
38.
A a = new B(5, 10);
System.out.println( a.zorro( ) );
39. Consider the classes Food, Cheese, and Velveeta where Cheese is a subclass of Food
and Velveeta is a subclass of Cheese. State which of the following lines of code are legal.