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"); }...
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...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
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...
write the following programs: Ram.java, Truck.java, LandVehicle.java and Vehicle.java. public class Ram extends Truck { public...
write the following programs: Ram.java, Truck.java, LandVehicle.java and Vehicle.java. public class Ram extends Truck { public static void main(String[] args) { // your implementation } public Ram() { // your implementation } } public class Truck extends LandVehicle { public Truck() { // your implementation } public Truck(String s) { // your implementation } } public class LandVehicle extends Vehicle { public LandVehicle() { // your implementation } } public class Vehicle { public Vehicle() { // your implementation
public class Flight extends java.lang.Object This class represents a single flight within the travel agency system....
public class Flight extends java.lang.Object This class represents a single flight within the travel agency system. Constructor Summary Constructors Constructor and Description Flight(java.lang.String airline, int flightNum, java.lang.String from, java.lang.String to, java.util.Calendar leavesAt, java.util.Calendar arrives, double price) Creates a new flight leg in the system. Method Summary All Methods Instance Methods Concrete Methods Modifier and Type Method and Description double getPrice() Retrieves the price of this flight. java.lang.String toString() Retrieves a formatted string summarizing this Flight. Methods inherited from class java.lang.Object...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[]...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[] args) { //Fixme(2) create an ArrayList of integers and name the ArrayList list. //Fixme(3) add the following numbers to the list: 10, 15, 7, -5, 73, -11, 100, 20, 5, -1    displayList(list); System.out.println(); displayListBackwards(list);       }    public static void displayList(ArrayList<Integer> list) { for(Integer i: list) System.out.print(i + " ");    } //Fixme(4) define a method displayListBackwards, which takes an ArrayList as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT