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

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;   ...
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...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT