Question

In: Computer Science

public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature...

public class Mammal extends SeaCreature {
public void method1() {
System.out.println("warm-blooded");
}
}
public class SeaCreature {
public void method1() {
System.out.println("creature 1");
}
public void method2() {
System.out.println("creature 2");
}
public String toString() {
return "ocean-dwelling";
}
}

public class Whale extends Mammal {
public void method1() {
System.out.println("spout");
}
public String toString() {
return "BIG!";
}
}

public class Squid extends SeaCreature {
public void method2() {
System.out.println("tentacles");
}
public String toString() {
return "squid";
}
}

What output is produced by the following little program; (remember to pay
attention to the output formatting of the statements in your response)?

public class ByTheSea {
public static void main(String[] args) {
SeaCreature[] elements = {new Squid(), new Whale(), new SeaCreature(),
new Mammal()};
for (int i = 0; i < elements.length; i++) {
System.out.println(elements[i]);
elements[i].method1();
elements[i].method2();
System.out.println();
}
}
}

Solutions

Expert Solution

public class Mammal extends SeaCreature {
public void method1() {
System.out.println("warm-blooded");
}
}
public class SeaCreature {
public void method1() {
System.out.println("creature 1");
}
public void method2() {
System.out.println("creature 2");
}
public String toString() {
return "ocean-dwelling";
}
}

public class Whale extends Mammal {
public void method1() {
System.out.println("spout");
}
public String toString() {
return "BIG!";
}
}

public class Squid extends SeaCreature {
public void method2() {
System.out.println("tentacles");
}
public String toString() {
return "squid";
}
}


// Driver code
public class ByTheSea {
public static void main(String[] args) {
SeaCreature[] elements = {new Squid(), new Whale(), new SeaCreature(),
new Mammal()};
for (int i = 0; i < elements.length; i++) {
System.out.println(elements[i]);
elements[i].method1();
elements[i].method2();
System.out.println();
}
}
}

the output of the above program is:

This is because in a java code there can be only one Public Class. But in the above code, all the classes provided are public.

For this program to work, you have to create separate java files for these class.

Or you can remove the public keyword from all other classes.

You also have to reorder the classes because some classes are inheriting from others.

class SeaCreature {
    public void method1() {
        System.out.println("creature 1");
    }
    public void method2() {
        System.out.println("creature 2");
    }
    public String toString() {
        return "ocean-dwelling";
    }
}

class Mammal extends SeaCreature {
    
    public void method1() {
        System.out.println("warm-blooded");
    }
}

class Squid extends SeaCreature {
    
    public void method2() {
        System.out.println("tentacles");
    }
    
    public String toString() {
        return "squid";
    }
}

class Whale extends Mammal {
    
    public void method1() {
        System.out.println("spout");
    }
    
    public String toString() {
        return "BIG!";
    }
}



public class ByTheSea {
    public static void main(String[] args) {
        SeaCreature[] elements = {new Squid(), new Whale(), new SeaCreature(), new Mammal()};
        for (int i = 0; i < elements.length; i++) {
            System.out.println(elements[i]);
            elements[i].method1();
            elements[i].method2();
            System.out.println();
        }
    }
}

The output will be:

I hope this answers your question.

thanks for asking


Related Solutions

Consider the following code: public class Bay extends Lake { public void method1() { System.out.println("Bay 1");...
Consider the following code: public class Bay extends Lake { public void method1() { System.out.println("Bay 1"); super.method2(); } public void method2() { System.out.println("Bay 2"); } } //********************************* class Pond { public void method2() { System.out.println("Pond 2"); } } //************************** class Ocean extends Bay { public void method2() { System.out.println("Ocean 2"); } } //********************************* class Lake extends Pond { public void method3() { System.out.println("Lake 3"); method2(); } } //**************************** class Driver { public static void main(String[] args) { Object var4 =...
Consider the following classes: public class Clock extends Bear { public void method3() { System.out.println("Clock 3");...
Consider the following classes: public class Clock extends Bear { public void method3() { System.out.println("Clock 3"); } } public class Lamp extends Can { public void method1() { System.out.println("Lamp 1"); } public void method3() { System.out.println("Lamp 3"); } } public class Bear extends Can { public void method1() { System.out.println("Bear 1"); } public void method3() { System.out.println("Bear 3"); super.method3(); } } public class Can { public void method2() { System.out.println("Can 2"); method3(); } public void method3() { System.out.println("Can 3"); }...
import java.util.Scanner; import java.text.value; public class Rectangle { public static void main(String[]args){ System.out.println("Find the area of...
import java.util.Scanner; import java.text.value; public class Rectangle { public static void main(String[]args){ System.out.println("Find the area of a rectangle"); System.out.println("Find the perimeter of a rectangle"); System.out.println("Enter a number of choice"); } public static void main(String[]args){ int choice = //the user choice double value :0 // the value returned from the method double length; // the length of rectangle double width;// the width of rectangle Scanner scan = new Scanner(System.in); System.out.println("Enter length: "); double length = scan.nextDouble(); //the length of rectangle System.out.println("Enter...
Create a class to represent a Mammal object that inherits from (extends) the Animal class. View...
Create a class to represent a Mammal object that inherits from (extends) the Animal class. View javadoc for the Mammal class and updated Animal class in homework 4 http://comet.lehman.cuny.edu/sfakhouri/teaching/cmp/cmp326/s19/hw/hw4/ Use the description provided below in UML. Mammal - tailLength : double - numLegs : int + Mammal() + Mammal(double, int) + Mammal(String, int, double, double, char, double, int) //pass values to parent’s overloaded constructor //and assign valid values to tailLength, numLegs or -1 if invalid + setTailLength(double) : void //if...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is the subclass of ‘A’. Is it possible to define the same method public static void show( ) in the Derived class. Why give reasons? Is it run time polymorphism? (0.5 Marks) Is it possible to define the method public void show( ) (non static method) in the Derived class. What will happen and why give reasons?
List.h template class List { // List class ADT              public:    virtual void...
List.h template class List { // List class ADT              public:    virtual void clear() = 0; // Inserts an item into the list at position index    virtual bool insert(const ListItemType& newItem) = 0;    // Append "it" at the end of the list    // The client must ensure that the list's capacity is not exceeded    virtual bool append(const ListItemType& newItem) = 0;    // Deletes an item from the list at a given position...
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT