Question

The Bike and EBike classes are implemented as shown by the code below. public class Bike...

The Bike and EBike classes are implemented as shown by the code below.

public class Bike
{
  private String make;
  private int numGears;
  private double tirePressure;
  
  public Bike(String m, int g, double t)
  {
    make = m;
    numGears = g;
    tirePressure = t;
  }
  
  public void pumpTire()
  {
    tirePressure += 5.0;
  }
}

public class EBike extends Bike
{
  private int batteryLevel;
  
  public EBike(String m, int g, double t, int b)
  {
    super(m, g, t);
    batteryLevel = b;
  }
  
  public void chargeBattery()
  {
    batteryLevel++;
  }
}

Suppose that the following declarations are made in a separate class.

Bike bike1 = new Bike(“Clasic Cycle”, 16, 65.0);
Bike bike2 = new EBike(“Big EZ”, 8, 72.0, 96);
EBike bike3 = new Bike(“E-xtreme”, 12, 80.0, 44);

Assuming the above three lines compile without error, which of the following statements will cause an error when compiled?

bike3.pumpTire();
bike2.pumpTire();
bike2.chargeBattery();
bike1.pumpTire();
bike3.chargeBattery();
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The statement that will cause an error is bike2.chargeBattery().

This is so because bike 2 is an object of class Bike and not EBike ( Bike bike2 = new EBike(“Big EZ”, 8, 72.0, 96) ) and the parent class cannot call the methods of child class ( though the reverse is possible ) . As chargeBattery() is a method of class Ebike so the given statement will throw an error method not found.

Add a comment
Know the answer?
Add Answer to:
The Bike and EBike classes are implemented as shown by the code below. public class Bike...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • The method m() of class B overrides the m() method of class A, true or false?...

    The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...

  • Question 2 What is the EXACT output of this code: class B { public B{ System.out.print("B");...

    Question 2 What is the EXACT output of this code: class B { public B{ System.out.print("B"); } } class A extends B{ public A(int t) { super(); System.out.print("A"); } } public class Test{ public static void main(String[] args) { A a = new A(3); } }

  • List the unique classes/ADTs in the following code segment: (hint: there are 6) public class LinkedQueueclass...

    List the unique classes/ADTs in the following code segment: (hint: there are 6) public class LinkedQueueclass protected class QueueNode int info; QueueNode link; private QueueNode queue Front; private QueueNode queue Rear; public class QueueOverflowException extends Queue Exception public QueueOverflowException() super("Queue Overflow"); public QueueOverflowException (String msg) super (msg); System.out.println("An exception occurred");

  • Analyze the following code in these 2 Java classes and choose the correct statement below: public...

    Analyze the following code in these 2 Java classes and choose the correct statement below: public class Test { public static void main(String[] args) { Time1 time = new Time1(); time.print(); } } public class Time1 {    private String showTime;    public Time1(String newTime) { showTime = newTime; }    public void print() { System.out.printf("%s", showTime); } } a. The program will compile and run if you change: Time1 time = new Time1();  → Time1 time = new Time1("5:15"); b....

  • What is output? public abstract class People { protected string name; protected int age; public abstract...

    What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...

  • class x { x () {} private void one () {} } public class Y extends...

    class x { x () {} private void one () {} } public class Y extends x { Y() {} private void two () { one(); } public static void main (String[] args) { new Y().two(); } } What changes will make this code compile? a. Adding the public modifier to the declaration of class x b. Adding the protected modifier to the x() constructor c. Changing the private modifier on the declaration of the one() method to protected d....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT