0% found this document useful (0 votes)
6 views

APCS Spring 2024 Unit Nine Exercises

The document contains a series of exercises related to object-oriented programming concepts, specifically focusing on class inheritance, method legality, and variable shadowing in Java. It includes questions about class relationships, method accessibility, and code implementation for various scenarios involving classes like Polygon, Triangle, Red, and Green. Additionally, it addresses legal and illegal code examples regarding object instantiation and method calls.

Uploaded by

Jaiden Briscoe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

APCS Spring 2024 Unit Nine Exercises

The document contains a series of exercises related to object-oriented programming concepts, specifically focusing on class inheritance, method legality, and variable shadowing in Java. It includes questions about class relationships, method accessibility, and code implementation for various scenarios involving classes like Polygon, Triangle, Red, and Green. Additionally, it addresses legal and illegal code examples regarding object instantiation and method calls.

Uploaded by

Jaiden Briscoe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

APCS Spring 2024 Unit Nine Exercises

Name: _____________________________________

For questions 1 – 7, assume we have these two classes and their methods:

Class Name Public methods Private methods


Polygon perimeter numVertices
area
Triangle side1 angle1
side2 angle2
side3 angle3

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;
}

public class Texan


{
public String parseNames(String nm)
{ … }
. . .
public int zx = 22;
}

Use the above code for questions 8 through 12:


8. Using the above two classes, which is the superclass and which is the subclass?

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 class Red extends Green


{
public int blue(double x)
{ . . . }

public String s;
private int i;
}

public class Green


{
public double peabody(double y)
{
return mm;
}

private boolean crunch( )


{ . . . }

private double mm;


public long xx;
}

14. Which of the above two classes is the base class?

15. Which of the above two classes is the subclass?

16. Which of the above two classes is the superclass?

17. Which of the above two classes is the derived class?

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 double peabody(double vv)


{
}

public String s;
private int i;
}

public class Green


{
public Green(long j)
{
xx = j;
}

public double peabody(double y)


{
return mm;
}

private Boolean crunch( )


{ . . . }

private double mm;


public long xx;
}

22. Consider the following constructor in the Red class:


public Red( )
{
// What code would you put here to invoke the constructor in the
// superclass and send it a parameter value of 32000?
}

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.

25. Consider the following constructor in the Red class:


public Red( )
{
String s = “Hello”;
super(49);
}
Is the code above legal? If not, why not?

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;
}

public class Child extends Parent


{
public void busterStein( )
{ . . . }
. . .
public int x = 39;
}

28. Consider the following code in a Tester class:


Child myChild = new Child( );
System.out.println(myChild.x);

What is printed?

29. Consider the following code in a Tester class:


Child myChild = new Child( );

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( );

32. Is the following legal? If not, why not?


Child theObj = new Child( );
Parent newObj = theObj;
newObj.rubyDoo( );

33. Is the following legal? If not, why not?


Parent meatloaf = new Child( );
For problems 34 through 38, consider the following. In each problem either
state what is printed or indicate that it won’t compile:
public class A
{
public A (int x)
{
this.x = x;
}
public int f( )
{
return x;
}
public int g( )
{
return x;
}
public int x;
}

public class B extends A


{
public B (int x, int y)
{
super(x);
this.x = y;
}
public int f( ) { return x + g( ); }
public int zorro( ) { return x + g( ); }
public int x;
}

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.

Cheese c = new Food( ); __________


Velveeta v = new Food( ); __________
Cheese c = new Velveeta( ); __________
Food f = new Velveeta( ); __________
Food f = new Cheese( ); __________

You might also like