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...
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...
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...
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...
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...
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...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make sure the variable names match those #words. size will be a character, either "S", "M", or "L". #extra_shots will be an integer. # #FrapOrder should have a constructor with two required #parameters, one for each of those attributes (size and #extra_shots, in that order). # #FrapOrder should also have a method called get_total. #get_total should calculate the total cost of the order. #If size...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
2. Program containing two modules: a. Create a class Code one programmer-written constructor Create several instance...
2. Program containing two modules: a. Create a class Code one programmer-written constructor Create several instance data attributes Create 3 methods which will each be invoked from the driver program - one method will not receive any parameters, but will return a value to driver program - one method will receive one parameter, and will also return a value to driver program - a display method will receive 2 parameters from driver program Define a constant properly; use the constant...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT