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 =...
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...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT