Question

In: Computer Science

Write a line of code that indicates to the compiler that the method being declared overrides...

Write a line of code that indicates to the compiler that the method being declared overrides a superclass method.

Write a line of code that specifies that class Fly inherits from class Insect.

Call superclass Insect's toString method from subclass Fly's tostring method.

Call superclass Insect's constructor from subclass Fly's constructor, assume that the constructor receives an integer for number of legs and a string for the ability it has.

Solutions

Expert Solution

For each problem given, the solution is provided below:

The line that shows the superclass method overrides in the derived class. The line is in bold form.

public class A{

public double compute_cost()

{

    return 50.0;

}

}

public class B extends A{

//The function of Base class

//A overrides in the derived class B.

public double compute_cost()

{

    return 55.0;

}

}

Explanation:

  • Consider a class that represents the Base class and the class B that is derived by class A.
  • The function compute_cost() is the function of the base class A which is override in the derived class B.

The line of code that specifies that class Fly inherits from class Insect is as follows:

The line is in bold form.

public class Insect{

//Body of class Insect.

}

public class Fly extends Insect{

    //Body of class Fly.

   

}

Explanation:

  • The base class is Insect and the derived class is Fly class. The Fly inherits from class Insect.
  • For inheritance, the extend keyword is used in the derived class.

The calling of superclass Insect's toString method from subclass Fly's tostring method is as follows:

public class Insect{

public String toString()

{

    return("Base class Insect toString() method.");

}

}

public class Fly extends Insect{

@Override

//The toString() function of class Insect

//is called in the dervied class.

public String toString()

{

    return (super.toString());

}

}

Explanation:

  • The Base class Insect has a method named as toString() that return the string as Base class Insect toString() method.
  • The method toString() of Base class is called in the subclass Fly by using the super keyword.
  • For calling the methods of the superclass, super keyword is used.

The calling of superclass Insect's constructor from subclass Fly's constructor. The code is provided below:

public class Insect{

//Declare the variables.

public int legs_count ;

public string ability;

//Parameterized constructor of the class Insect.

public Insect(int legs, string ability)

{

    //Assign the values.

    legs_count = legs;

    ability = ability;

}

}

public class Fly extends Insect{

//Constructor of derived class Fly.

public Fly(int legs, string insect_ability)

{

    //Calling of the parameterized constructor of

    //the base class Insect.

    super(legs, insect_ability);

}

}

Explanation:

  • The calling of the Base class methods, constructors is called by the keyword super.
  • The line super(legs, insect_ability) call the parameterized constructor of the class Insect.

Related Solutions

I have an error on my code. the compiler says 'setLastName' was not declared in this...
I have an error on my code. the compiler says 'setLastName' was not declared in this scope. this is my code : #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> using std::string; class Employee {    public :               Employee(string fname, string lname, int salary)        {            setFirstName(fname);            setLastName(lname);            setMonthlySalary(salary);        }               void setFirstName(string fname)        {       ...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this function is called with the parameter n = 5… int summarize(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += i; } return sum; } c) Compile the code using the ARM gcc 8.2 compiler. Add the –O1 compiler option, which asks the compiler to optimize for speed (at least to one level). What is...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this function is called with the parameter n = 5… int summarize(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += i; } return sum; } a) Accounting for delay slots, what is the dynamic instruction count of this routine? What is the dynamic instruction count if you add the –O1 compiler option, which asks the...
Allowance Method versus Direct Write-Off Method On April 12, Maddox Company, declared a $40,000 account receivable...
Allowance Method versus Direct Write-Off Method On April 12, Maddox Company, declared a $40,000 account receivable from the Ward Company as uncollectible and wrote off the account. On December 5, Maddox received a $28,000 payment on the account from Ward. a. Assume that Maddox uses the allowance method of handling credit losses. Prepare the journal entries to record the write-off and the subsequent recovery of Ward's account.< b. Assume that Maddox uses the direct write-off method of handling credit losses....
A straight line possibilities frontier indicates that
A straight line possibilities frontier indicates that
JAVA Copy the attached code into your IDE or an online compiler and test an additional...
JAVA Copy the attached code into your IDE or an online compiler and test an additional type with the generic class. Submit your code and execution display. JAVA The test cases for Integer and String is given in the code. Please add test cases for Character, Boolean and Double type etc. // A Simple Java program to show working of user defined // Generic classes    // We use < > to specify Parameter type class Test<T> {     //...
Write a MATLAB code for importing an image and then being able to find all the...
Write a MATLAB code for importing an image and then being able to find all the coordinates that would draw that image on a plot column by column and row by row with a continuous line.
27) Consider the student class. There is a method setSemesterGrade(int grade) In that method write code...
27) Consider the student class. There is a method setSemesterGrade(int grade) In that method write code to throw a new IllegalStateException if the average is less than 0 And an IndexOutOfBoundsException if the average is > 0 28) Consider a student represented by the variable s1 and we are trying to set the average of s1. Write a try catch routine to catch either error listed above try { s1.setSemesterGrade(-30); s1.setSemesterGrade(200);
For each of the following write the line(s) of code that: Declares and initializes (creates) an...
For each of the following write the line(s) of code that: Declares and initializes (creates) an ArrayList that holds String objects Adds to your ArrayList the words "Too" and "Fun" Verifies that your ArrayList now contains 2 elements Sets the second String in the ArrayList to "No" Verifies that your ArrayList still contains exactly 2 elements Prints the contents of the ArrayList to the screen in the following format: <element>, <element>, . . . , <element>
A 296500$ tractor-trailer is being depreciated by the straight line method over five years to a...
A 296500$ tractor-trailer is being depreciated by the straight line method over five years to a final Book Value (BV) of zero. After three years the trailer is sold for a) 249830 $ (2 points) b) 80460$. (2 points) If the effective income tax rate is 52% what is the net cash inflow from the sale for situation (a) and situation (b)? (tax saving is permitted. )
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT