Question

Casting class objects 1.2 Compile and execute the code listed below as it is written. Run...

Casting class objects

1.2 Compile and execute the code listed below as it is written. Run it a second time after uncommenting the line obj.speak();.

public class AnimalRunner

{

   public static void main(String[] args)

   {

      Dog d1 = new Dog("Fred");

      d1.speak();

      Object obj = new Dog("Connie");

      // obj.speak();

   }

}

The uncommented line causes a compile error because obj is an Object reference variable and class Object doesn’t contain a speak() method. This is the case, in spite of the fact that obj refers to a Dog object and that class Dog has a speak() method. For safety, the Java compiler limits the methods that can be called using a reference variable to only those methods that belong to the class that was used to create the reference variable. So obj can only invoke Object methods.

Use the following method to make Connie speak: Create a second Dog reference variable d2. Cast obj to a Dog and assign the new reference to d2. Now invoke speak on d2. What happens if you create a Cat object and try to cast it to a Dog?

Your answer :

 

What methods do you need to add to BankAccount to implement Comparable?

2.1. The Comparable interface is a commonly used interface in Java. Look up the Comparable interface in the API documentation.

If you wanted to modify the BankAccount class so that it implements the Comparable interface, what method(s) do you need to implement?

Give the method signature(s), that is, the return type(s), the method name(s), and the method parameter(s) needed.

Your answer :

 

Homework Answers

Answer #1

1.2

SOLUTION:

public class AnimalRunner

{

public static void main(String [ ] args)

{

Dog d1=new Dog ("Fred");

d1.speak();

object obj = new Dog("Connie");

Dog d2 =(Dog) obj;

d2.speak();

}

}

If we cast a cat to a dog, what will happens is that the compiler signals an "inconvertible types" error since the object being cast is not a member of the class which is the target of the cast.

2.1

SOLUTION:

The method i will implement to modify the BankAccount class so that it implements the Comparable interface is

int compareTo(T o)

5

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has...
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has a firstName, lastName, customerNumber, emailAddress. You can add some more fields if you want to, but those 4 are a minimum. Override the equals method, and the toString method from the Object class. Implement the Comparable interface. In the main method, create some instances of the Customer class, and demonstrate the use of the accessor and mutator methods, as well as the compareTo method.
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance:...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance: ⦁   A base class called Pet ⦁   A mix-in class called Jumper ⦁   A Dog class and a Cat class that each inherit from Pet and jumper ⦁   Two classes that inherit from Dog: BigDog and SmallDog ⦁   One classes that inherit from Cat: HouseCat The general skeleton of the Pet, Dog, and BigDog classes will be given to you (see below, "Skeleton", but...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
Complete the implementation of the Card class. The two methods that you need to complete are...
Complete the implementation of the Card class. The two methods that you need to complete are compareTo and equals. CopareTo API: Compares this card to another card for order. This method imposes the following order on cards: Cards are first compared by rank. If the rank of this card is less than the rank of the other card then -1 is returned. If the rank of this card is greater than the rank of the other card then 1 is...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume that the Employee class has not implemented its own toString() method. What value will the variable str contain when this code is executed? Group of answer choices the variable will become a null reference the code will not compile because Employee has not implemented toString() the default from Object, which is the class name of the object and its hash code the string representations...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If you can't implement all of the features of Project described in this document, implement what you can. Start by implementing the StockItem class, and its derived classes. Then add in the InventoryManager class later. In each case, the test files must be in separate classes. UML Diagram: Use Violet or other drawing software to draw the UML diagrams of each class that you use...