Object Instantiation and Code Examples

Classified in Technology

Written on in English with a size of 4.2 KB

Poodle Instantiation Example

B. The Poodle variable myDog is instantiated as a Poodle. The instance variable size is initialized to "toy". An implicit call to the no-argument Dog constructor is made, initializing the instance variable name to "NoName".

Book Instantiation Example

C. Object myBook is created using the one-argument Book constructor, which uses super to set myBook's title attribute to "Adventure Story". Object yourBook is created using super to call the Publication no-argument constructor to set yourBook's title attribute to "Generic".

Tree Instantiation Example

B. Object tree1 is created using the DeciduousTree constructor, which uses super to set tree1's treeVariety attribute to "Oak". Object tree2 is created using the EvergreenTree constructor, which uses super to set tree2's treeVariety attribute to "Fir".

Bird Singing Output

E. I, II, and III

EvilPerson Laughing Output

D. II and III only

Wheels on the Bus Output

B. II only

ClosedRange Output Example

C. ClosedRange r3 = new ClosedRange(1, 10); System.out.println(r3);

BallCap Output Example

A. BallCap myHat = new BallCap("L", "Denver"); System.out.println(myHat);

Grandchild Output "PQRTS" Example

D. Grandchild d = new Grandchild(); d.first();

MultiTool Compilation Error

E. Line 8 causes a compile-time error because the getCompass method is not defined for objects of type MultiTool.

Thing Array Error

C. Line 5 will cause an error because the types of arr[0] and t1 are different.

Appliance Display Error

D. Line 8 causes a compile-time error because the parameter a1 in the call displayFeatures(a1) has the incorrect data type.

First, Second, Third Compilation Error

A. Line 3 causes a compile-time error because the variable sec should be declared as type Second.

Teacher Compilation Error

D. The variable teach should be declared as a Teacher data type because teacherInfo is a method in the Teacher class.

Aclass and Bclass Incorrect Output

B. The variable thing should be instantiated as an Aclass object because methodY is overridden in Bclass.

Vehicle Output Example

D. Vehicle obj = new Motorized(4, 55); System.out.println(obj);

Silly Class Equals Method Example

C. s3.equals(s4)

Time Class Equals Method Example

B. II only

Game and GameListing Class Definitions

public class VideoGame extends Game {
private String system;

public VideoGame(String t, String c, String s) {
super(t, c);
system = s;
}

@Override
public String gameInfo() {
return super.gameInfo() + ", first released on " + system;
}
}

Game game1 = new Game("Monopoly", "Hasbro");
VideoGame game2 = new VideoGame("Sonic the Hedgehog", "Sega", "Sega Genesis");

public class GameListing {
private Game game;
private double price;

public GameListing(Game g, double p) {
game = g;
price = p;
}

public String description() {
return game.gameInfo() + ", $" + String.format("%.2f", price);
}
}

Related entries: