Question

Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors •...

Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors • Be able to create accessor and mutator methods • Be able to create toString methods • Be able to refer to both global and local variables with the same name Introduction Until now, you have just simply been able to refer to any variable by its name. This works for our purposes, but what happens when there are multiple variables with the same name? In this lab, you will be learning how to refer to both global and local variables, as well as where you can access them.

Task #1 – Creating the Constuctor 1. Create a constructor for the Bike class • A default constructor is a constructor with no parameters, that is used to set up an object with default values • A non-default constructor is a constructor that uses parameters to set up an object with specific values

Task #2 Calculating the Cost 2. Create a method that calculates the cost of the Bike object • To find the cost, use the following formula cost = numGears × topSpeed • Return the value of cost to where the method was called (the constructor) by typing the return keyword and providing it with a value

package bikeshop; import java.util.Random; /** * BikeShop Lab * * @author Diego Vera */ public class BikeShopDriver { public static void main(String[] args) { Bike bike1 = new Bike(8, 30); Bike bike2 = new Bike(7, 35); Random rand = new Random(); //Task 3b: Use the "ride" method that you just created to increase the distance //travelled on each bike by a random amount, from 5 to 20, inclusive //HINT: use rand.nextInt(n) to get a random number from 0 to n. //Task 4b: Use the .toString method to print the attributes of the bike //that travelled the furthest distance. //Task 5: Use the .toString method to print the attributes of the bike //with the highest cost. //HINT: You may need to create a method of some sort to be able to //access some variables } }

package bikeshop; /** * * @author Diego Vera */ public class Bike { public final int numWheels = 2; //The final keyword means that once the variable //is set, it can never be changed public int numGears; //Between 7-11 gears public double topSpeed; //Between 20-40 mph private double distance; //Task 1: Create both the default and non-default constructor //Both constructors should initialize all global variables //Task 1a: Create the default constructor public Bike() { //This should create a generic Bike that can still be used } //Task 1b: Create the non-default constructor without changing the name of //any parameters //HINT: Use the this keyword to refer to global variables by typing //this.yourVariableName //SECOND HINT: Try using cost = calculateCost(); to set the value of cost to //the value returned in the calculateCost method public Bike(int numGears, double topSpeed) { } //Task 2: Create a method to calculate the cost of a bike, where the cost is //equal to numGears * topSpeed private double calculateCost() { } //Task 3a: Create a method that increases the distance traveled by a given amount public void ride(int distance){ } //Task 4a: Create a .toString method that prints out each bike object's attributes public String toString() { } }

Need help completing this program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//BikeShopDriver.java

import java.util.Random;
public class BikeShopDriver
{
   public static void main(String[] args)
   {

   double cost;
   Bike bike1 = new Bike(8, 30);
   Bike bike2 = new Bike(7, 35);
   Bike bike3 = new Bike();
   Random rand = new Random();
   int ridedist=rand.nextInt(20 - 5 + 1) + 5;
   System.out.println("Byke1 Cost :"+bike1.calculateCost());
   bike1.toString1();           //Display Byke1 Attributes
   System.out.println("Byke2 Cost :"+bike2.calculateCost());
   bike2.toString1();           //Display Byke2 Attributes
  
   System.out.println("Byke2 Cost :"+bike3.calculateCost());
   bike3.toString1();           //Display Byke3 Attributes

   }
}


class Bike
{
   public final int numWheels = 2;           //The final keyword means that once the variable    //is set, it can never be changed
   public int numGears;               //Between 7-11 gears
   public double topSpeed;               //Between 20-40 mph
   private double distance =40;
   public double cost;
  
       //This should create a generic Bike that can still be used
   public Bike()      
   {
       numGears=7;       //default taken 7 gears
       topSpeed=20;       //default taken 20 kmph
      
       cost= numGears * topSpeed;
              
   }
   //Parameterized constructor
   public Bike(int Gears,double Speed)          
   {
       this.numGears=Gears;      
       this.topSpeed=Speed;      
              
       cost= numGears * topSpeed;
              
   }
   //Task 2: Create a method to calculate the cost of a bike, where the cost is
   public double calculateCost()
   {
       return cost;
   }

   //Task 3a: Create a method that increases the distance traveled by a given amount
   public void ride(int d)
   {
       distance=distance+d;
   }

   //Task 4a: Create a .toString method that prints out each bike object's attributes
   public void toString1()
   {
   System.out.println("bike object's attributes :");
   System.out.println("numGears :"+numGears);
   System.out.println("numWheels :"+numWheels);
   System.out.println("topSpeed :"+topSpeed);
   System.out.println("distance :"+distance);
   System.out.println("cost :"+cost);
   }

}

Output:

javac BikeShopDriver.java
$java -Xmx128M -Xms16M BikeShopDriver
Byke1 Cost :240.0
bike object's attributes :
numGears :8
numWheels :2
topSpeed :30.0
distance :40.0
cost :240.0
Byke2 Cost :245.0
bike object's attributes :
numGears :7
numWheels :2
topSpeed :35.0
distance :40.0
cost :245.0
Byke2 Cost :140.0
bike object's attributes :
numGears :7
numWheels :2
topSpeed :20.0
distance :40.0
cost :140.0
Add a comment
Know the answer?
Add Answer to:
Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors •...
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
  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • College of Winston and Charlotte This program will calculate the amount for a semester bill at...

    College of Winston and Charlotte This program will calculate the amount for a semester bill at The College of Winston and Charlotte. Part 1: Create a class Course.java: The class has non-static instance variables: private String department private int courseNumber private int courseCredits private double courseCost The class must have a default constructor which will set values as follows: department = “unknown” courseNumber = 0 courseCost = 0 courseCredits = 0 The class must have a non-default constructor which will...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

  • fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life...

    fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life Before you begin, DOWNLOAD the files “Lab8_Bicycle.java” and “Lab8_BicycleApp.java” from Canvas to your network drive (not the local hard drive or desktop). Once you have the files saved, RENAME THE FILES Bicycle.java and BicycleApp.java then open each file. 1) Look at the Bicycle class. Two of the many data properties we could use to define a bike are its color and the gear it...

  • CS1180 Lab 9 Students will learn how to create a user-defined class. Part 1. Create a...

    CS1180 Lab 9 Students will learn how to create a user-defined class. Part 1. Create a user-defined class. 1. Follow the UML diagram below to create a user-defined class, Automobile When implementing the constructors, use the default value of 0 for numeric types and "unknown" for VehicleMake and VehicleModel when not given as a parameter Implement the toString method in a format ofyour choosing, as long as it clearly includes information for all four member variables Make sure to include...

  • LW: Class Constructors Objectives Write a default constructor for a class. Note that this const...

    LW: Class Constructors Objectives Write a default constructor for a class. Note that this constructor is used to build the object anytime an argument is not provided when it is defined. Write a parameterized constructor that takes arguments that are used to initialize the class’s member variables Labwork Download the code associated with this lab: ColorConstructor.zip Main.cpp Color.h Color.cpp Compile and run the code. The first line of output of the program should display nonsensical integers for the values of...

  • (To be written in Java code) Create a class named CollegeCourse that includes the following data...

    (To be written in Java code) Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display()...

  • I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...

    I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...

  • The function should have a class name CPizza that will have three constructors (default, type, and...

    The function should have a class name CPizza that will have three constructors (default, type, and copyl, public functions to use and implement are noted for you in the starter kit, and private member variables (all which are allocated dynamically). The private member variables include the three different sizes of pizza (Large, Medium, and Small), cost, a bool delivery, name, and delivery fee. The prices of the pizzas' are $20. $15, and $10 for large, medium, and small respectively. The...

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