Question

In: Computer Science

In Java, please write a tester code. Here's my code: public class Bicycle {     public...

In Java, please write a tester code.

Here's my code:

public class Bicycle {

    public int cadence;
public int gear;
  public int speed;

    public Bicycle(int startCadence, int startSpeed, int startGear) {

        gear = startGear;
  cadence = startCadence;
speed = startSpeed;

    }

    public void setCadence(int newValue) {

        cadence = newValue;

    }

    public void setGear(int newValue) {

        gear = newValue;

    }

    public void applyBrake(int decrement) {

        speed -= decrement;

    }

    public void speedUp(int increment) {

        speed += increment;

    }

}

public class MountainBike extends Bicycle {

public int seatHeight;

public MountainBike(int startGear, int startCadence, int startSpeed, int startHeight) {

super(startGear, startCadence, startSpeed);
seatHeight = startHeight;

}

public void setHeight(int newValue) {

seatHeight = newValue;

}

}

Solutions

Expert Solution

1. Bicycle constructor parameter order should be correct
super(startGear, startCadence, startSpeed);   // wrong parameters order
super(startCadence, startSpeed, startGear);   // correct order

2. To retrieve the details of the bike, ideally we should define
separate method in MountainBike class (I've added it)
   public String getInfo() {
       String info = "startGear= " +    gear + "\n";
       info += "startCadence= " +    cadence + "\n";
       info += "startSpeed= " +    speed + "\n";
       info += "startHeight= " +    seatHeight + "\n";  
      
       return info;      
   }

//-------- Tester class BicycleTester.java
public class BicycleTester {
        
        public static void main (String args[]) {
                
                //create object of sub/child class
                MountainBike mb = new MountainBike(1,2,3,4);
                
                System.out.println(mb.getInfo()); //call method of mb
                
                                
                // if we dont want to use getInfo() method then we have to
                //get info using MountainBike object mb, as follows             
                /*
                String  info = "MountainBike info: \n";
                info += "startGear= " +         mb.gear + "\n";
                info += "startCadence= " +      mb.cadence + "\n";
                info += "startSpeed= " +        mb.speed + "\n";
                info += "startHeight= " +       mb.seatHeight + "\n";   
                
                System.out.println(info);
                */
                
        }
}

//-------- end of BicycleTester.java
//-------- Bicycle.java
public class Bicycle {

    public int cadence;
    public int gear;
    public int speed;

    public Bicycle(int startCadence, int startSpeed, int startGear) {

        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;

    }

    public void setCadence(int newValue) {

        cadence = newValue;

    }

    public void setGear(int newValue) {

        gear = newValue;

    }

    public void applyBrake(int decrement) {

        speed -= decrement;

    }

    public void speedUp(int increment) {

        speed += increment;

    }

}

//-------- end of Bicycle.java
//-------- MountainBike.java
public class MountainBike extends Bicycle {

    public int seatHeight;

    public MountainBike(int startGear, int startCadence, int startSpeed, int startHeight) {

        //super(startGear, startCadence, startSpeed);   wrong parameters order
        super(startCadence, startSpeed, startGear);

        seatHeight = startHeight;

    }

    public void setHeight(int newValue) {

        seatHeight = newValue;

    }

    //added this method to get bike details
    public String getInfo() {
        String info = "MountainBike info: \n";
        info += "startGear= " + gear + "\n";
        info += "startCadence= " + cadence + "\n";
        info += "startSpeed= " + speed + "\n";
        info += "startHeight= " + seatHeight + "\n";

        return info;
    }

}

//-------- end of MountainBike.java

Output

Thanks...


Related Solutions

I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
IN JAVA. I have the following code (please also implement the Tester to test the methods...
IN JAVA. I have the following code (please also implement the Tester to test the methods ) And I need to add a method called public int remove() that should remove the first integer of the array and return it at the dame time saving the first integer and bring down all other elements. After it should decrease the size of the array, and after return and save the integer. package ourVector; import java.util.Scanner; public class ourVector { private int[]...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT