Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. Consider the following code snippet: public class inventory implements Measur

ID: 3815010 • Letter: 5

Question

5. Consider the following code snippet: public class inventory implements Measurable double get Measure return onHandCount What is wrong with this code? The getMeasure 0 method must be declared as public. 6. It is often the case that two or more classes share a common set of methods. For programming purposes we might wish to treat the objects of those classes in a similar way by invoking some of their common routines For the Dog and cat classes listed below agree on the void method Because Dog and Cat objects have the ability to "speak, it is natural to think putting types of objects in an and both invoking speak on every object in the list. this possible? Certainly we create an ArrayList of Dog would hold all the Dog objects, but can we could of Dog? then a cat to an ArrayList Try running the main program below as it is written. Run it a second time after uncommenting the line that instantiates a Cat object and tries to add it to the ArrayList. import java.util. lic class Animal Runne public static void main (string args) Array List Dog> dogcatList new ArrayList

Explanation / Answer

public class Inventory implements Measurable

{

public double getMeasure() //here we provided Semicolon(;) We should'nt provide semicolon.

{

return onHandCount;

}

}

____________________

6)

We cant add Cat class objects to the ArrayList.Because we can only add Dog class objects to the ArrayList as we specified the Generics.

Speakable.java

public interface Speakable {
void speak();
}

________________

Dog.java

public class Dog implements Speakable{
private String name;

public Dog(String name) {
   super();
   this.name = name;
}
public void speak()
{
System.out.println("Woof! Woof!");  
}
@Override
public String toString() {
   return "Dog :" + name ;
}

}

___________________

Cat.java

public class Cat implements Speakable {
   private String name;


   public Cat(String name) {
       super();
       this.name = name;
   }
   public void speak()
   {
   System.out.println("Meow! Meow!");  
   }
   @Override
   public String toString() {
       return "Cat :" + name ;
   }
}

_____________

AnimalRunner.java

public class AnimalRunner {

   public static void main(String[] args) {
       ArrayList<Speakable> dogcatList=new ArrayList<Speakable>();

       dogcatList.add(new Dog("Fred"));
       dogcatList.add(new Cat("Wanda"));
       for(Speakable obj:dogcatList)
       {
           obj.speak();
       }

   }

}

________________

Output:

Woof! Woof!
Meow! Meow!

_______________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote