In: Computer Science
Programming Problem 2 - Cycle
[A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list.
[B] Edit your class Cycle by adding a default constructor which will assign the default values of 100 to represent the numberOfWheels, and 1000 to represent the weight, by invoking a call to the other constructor. Modify your application created in [A] to test the class.
Directions
[A] Create a class called Cycle
Declare integer instance variables numberOfWheels and weight as private.
Include a toString() method in the Cycle class. No set or get methods are included.
Create a constructor with two parameters, using the same variable names numberOfWheels and weight in the parameter list. Complete the constructor as necessary.
Create a separate application to test the class.
Create an object of the Cycle class.
Display the properties of the object.
[B] Add a default constructor
Edit the default constructor such that the default constructor will invoke the existing constructor with the default values of 100 to represent the numberOfWheels, and 1000 to represent the weight. Invoke the constructor using the “this” reference.
Create the second constructor which will receive the two arguments.
Create a separate application to test the class.
Create an object of the Cycle class.
Display the properties of the object.
Grading Rubric
Task | Points |
Class created and named | 2 |
Constructor(s) completed correctly | 2 |
toString() methods written correctly | 1 |
Successful invocation (constructor to constructor) | 2 |
Properties output correctly | 1 |
Proper documentation / Pseudocode | 1 |
Program works effectively | 1 |
Total | 10 |
Here is the first part of the question
"Programming Problem 2 - Cycle [A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. "
Here is the code
------------------------------------------------------------------------------------------------------------------------------------
//Create a class called “Cycle”
public class Cycle {
//two instance integer variables as properties,
int numberOfWheels;
double weight;
//Create a constructor with two parameters, using the same variable names in the parameter list.
public Cycle(int numberOfWheels, double weight) {
//Assign each variable to numberOfWheels” and “weight” respectively.
this.numberOfWheels=numberOfWheels;
this.weight=weight;
}
}
----------------------------------------------------------------------------------------------------------------------------------------
"Edit your class Cycle by adding a default constructor which will assign the default values of 100 to represent the numberOfWheels, and 1000 to represent the weight, by invoking a call to the other constructor. "
here is the code below :
-------------------------------------------------------------------------------------------------------------------------------------
//Create a class called “Cycle”
public class Cycle {
//two instance integer variables as properties,
int numberOfWheels;
double weight;
//Create a constructor with two parameters, using the same variable names in the parameter list.
public Cycle(int numberOfWheels, double weight) {
//Assign each variable to numberOfWheels” and “weight” respectively.
this.numberOfWheels=numberOfWheels;
this.weight=weight;
}
//Edit your class Cycle by adding a default constructor which will assign the default values of 100 to represent the numberOfWheels,
//and 1000 to represent the weight, by invoking a call to the other constructor.
// we call another constructor using the this keyword and passing the parameter values
public Cycle() {
this(100,1000.00);
}
}
---------------------------------------------------------------------------------------------------------------------------------------
" Create a constructor with two parameters, using the same variable names numberOfWheels and weight in the parameter list. Complete the constructor as necessary. • Create a separate application to test the class. o Create an object of the Cycle class. o Display the properties of the object."
here is the java class
---------------------------------------------------------------------------------------------------------------------------------
//Create a class called “Cycle”
public class Cycle {
//two instance integer variables as properties,
private int numberOfWheels; // we are marking the instance variable as private
private double weight; // we are marking the instance variable as private
//Create a constructor with two parameters, using the same variable names in the parameter list.
public Cycle(int numberOfWheels, double weight) {
//Assign each variable to numberOfWheels” and “weight” respectively.
this.numberOfWheels=numberOfWheels;
this.weight=weight;
}
//Include a toString() method in the Cycle class.
public String toString() {
return "";
}
}
---------------------------------------------------------------------------------------------------------------------------------------
" Add a default constructor • Edit the default constructor such that the default constructor will invoke the existing constructor with the default values of 100 to represent the numberOfWheels, and 1000 to represent the weight. Invoke the constructor using the “this” reference. o Create the second constructor which will receive the two arguments. •"
here is the java class
--------------------------------------------------------------------------------------------------------------------------------------
//Create a class called “Cycle”
public class Cycle {
//two instance integer variables as properties,
private int numberOfWheels; // we are marking the instance variable as private
private double weight; // we are marking the instance variable as private
//Create a constructor with two parameters, using the same variable names in the parameter list.
public Cycle(int numberOfWheels, double weight) {
//Assign each variable to numberOfWheels” and “weight” respectively.
this.numberOfWheels=numberOfWheels;
this.weight=weight;
}
//Edit your class Cycle by adding a default constructor which will assign the default values of 100 to represent the numberOfWheels,
//and 1000 to represent the weight, by invoking a call to the other constructor.
// we call another constructor using the this keyword and passing the parameter values
public Cycle() {
this(100,1000.00);
}
//Include a toString() method in the Cycle class.
public String toString() {
return "";
}
}
---------------------------------------------------------------------------------------------------------------------------------------
You can test all the above classes using a driver class as below -
public class TestCycle {
public static void main(String[] args) {
Cycle bicycle = new Cycle();
Cycle tricycle = new Cycle(3,3000);
}
}