Question

In: Computer Science

Programming Problem 2 - Cycle [A] Create a class called “Cycle” which has two instance integer...

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. o Create an object of the Cycle class. o 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. o Create the second constructor which will receive the two arguments. • Create a separate application to test the class. o Create an object of the Cycle class. o Display the properties of the object.

Solutions

Expert Solution

Below are your programs. Let me know in comments if you need something else in this.

a)

Cycle.java

public class Cycle {

//Declaration of variables

private int numberOfWheels;

private int weight;

//Constructor

public Cycle(int numberOfWheels, int weight) {

this.numberOfWheels = numberOfWheels;

this.weight = weight;

}

//toString() method

public String toString() {

return "Cycle [numberOfWheels=" + numberOfWheels + ", weight=" + weight + "]";

}

}

MainA.java

public class MainA {

public static void main(String[] args) {

Cycle yc = new Cycle(2, 10);

System.out.println(yc);

}

}

Sample Run: -

Cycle [numberOfWheels=2, weight=10

B)

Cycle.java

public class Cycle {

// Declaration of variables

private int numberOfWheels;

private int weight;

// Constructor Parameterized

public Cycle(int numberOfWheels, int weight) {

this.numberOfWheels = numberOfWheels;

this.weight = weight;

}

// Default Constructor

public Cycle() {

this(100, 1000);

}

// toString() method

public String toString() {

return "Cycle [numberOfWheels=" + numberOfWheels + ", weight=" + weight + "]";

}

}

MainB.java

public class MainB {

public static void main(String[] args) {

Cycle yc = new Cycle();

System.out.println(yc);

}

}

Sample Output

Cycle [numberOfWheels=100, weight=1000]

Update

MainB.java

public class MainB {

public static void main(String[] args) {

Cycle yc = new Cycle();

System.out.println(yc);

}

}

class Cycle {

// Declaration of variables

private int numberOfWheels;

private int weight;

// Constructor Parameterized

public Cycle(int numberOfWheels, int weight) {

this.numberOfWheels = numberOfWheels;

this.weight = weight;

}

// Default Constructor

public Cycle() {

this(100, 1000);

}

// toString() method

public String toString() {

return "Cycle [numberOfWheels=" + numberOfWheels + ", weight=" + weight + "]";

}

}


Related Solutions

Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.”
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...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
Programming Language: C++ Create a base class called Shape which has 2 attributes: X and Y...
Programming Language: C++ Create a base class called Shape which has 2 attributes: X and Y (positions on a Cartesian coordinate system). Since a shape is amorphous, set the class up so that an object of type Shape can not be instantiated. Create three derived classes of your choice whose base class is Shape. These derived classes should have accessors/mutators for their class specific attributes, as well as methods to compute the area and the perimeter of the shape. In...
Create a class called employee which has the following instance variables: Employee ID number Salary Years...
Create a class called employee which has the following instance variables: Employee ID number Salary Years at the company Your class should have the following methods: New employee which reads in an employee’s ID number, salary and years of service Anniversary which will up the years of service by 1 You got a raise which will read in how much the raise was (a percent) and then calculate the new salary You get a bonus which gives a yearly bonus...
Create a class called Sphere. The class will contain the following    Instance data double radius...
Create a class called Sphere. The class will contain the following    Instance data double radius Methods Constructor with one parameter which will be used to set the radius instance data Getter and Setter for radius             Area - calculate the area of the sphere (4 * PI * radius * radius)             Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius) toString - returns a string with the...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The class should have one static methods: public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false. NOTE: You are not allowed to use...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT