Question: Make the following changes to your class from Assignment 3 -- Use the this reference in your default (no-arg) constructor call the parameterized constructor Add

Make the following changes to your class from Assignment 3

-- Use the this reference in your default (no-arg) constructor call the parameterized constructor

Add the following new method in your class

-- Create a static method to your class called createSpreadsheet ( )

-- This method will have one parameter, an array of objects of your class

-- -- Note that your array should have at least 2 objects already, from Assignment 3. Add at least 2 more objects, so you have a total of at least 4 objects.

-- The method body will simply loop through each object in the array, and create one StringBuilder object that contains the following:

-- -- The first row should be a comma separated list of all the field names (you can manually add that)

-- -- The rest of the rows should be a list of all the values of those fields

-- -- Once you have completed creating the StringBuilder object, use the PrintWriter class to write it to a file called "mydata.csv"

-- -- The method does not return any values

-- -- Be sure to include the new line character at the end of each row, so it prints it out in the file on a separate line

-- -- See example below

-- Call this method from your Demo's main method, by simply passing your array of objects to it, that you created in Assignment 7

-- -- When you open the file, it should open in an Excel spreadsheet (It must have the .csv extension for this to work. CSV stands for Comma Separated Values)

-- -- Note: If you don't have the Excel software, then as long as you can open the file in notepad and it looks like the example below (without the characters), that is fine

Example of how the StringBuilder object should look for a Person class:

Name, Age, Weight

Donald Trump, 70, 236

Kanye West, 39, 154

Kim Kardashian, 36, 120

Bill Gates, 61, 154

>>Here's my object class(Gum)<<

public class Gum

{

//Instance fields (attributes)

private String brand;

private int pieces;

private double price;

//Default constructor

public Gum()

{

brand = "";

pieces = 0;

price = 0;

}

//Parameterized constructor

public Gum(String brand, int pieces, double price)

{

this.brand = brand;

this.pieces = pieces;

this.price = price;

}

/**

* The setBrand method stores a value in the brand field

* @param brand

*/

public void setBrand(String brand)

{

this.brand = brand;

}

/**

* The setPieces method stores a value in the pieces field

* @param pieces

*/

public void setPieces(int pieces)

{

this.pieces = pieces;

}

/**

* The setPrice method stores a value in the price field

* @param price

*/

public void setPrice(double price)

{

this.price = price;

}

/**

* The getBrand method returns the Gum brand

* @return

*/

public String getBrand()

{

return brand;

}

/**

* The getPieces method returns the Gum pieces

* @return

*/

public int getPieces()

{

return pieces;

}

/**

* The getPrice method returns the Gum price

* @return

*/

public double getPrice()

{

return price;

}

/**

* This display Method simply prints out the values of all instance variables of your object

* and has no parameters and will not return a value

*/

public void display()

{

System.out.println(brand + " Gum costs $" + price + " and contains " + pieces + " pieces.");

}

}

>>Heres my Demo class<<

public class Demo

{

/**

* This main method tests the Gum.java program

* @param args

*/

public static void main(String[] args)

{

//Create an instance of your class (an object) by using the default constructor. (1 point)

Gum gum = new Gum();

//Call all your objects setter (mutator) methods to assign values to your object

gum.setBrand("Orbit");

gum.setPieces(14);

gum.setPrice(0.99);

//Call the objects display method, to print out it's values

gum.display();

//Create another instance of your class (another object) by using the parameterized constructor. (1 point)

Gum gum1 = new Gum("Five", 24, 1.99);

//Call the objects display method, to print out it's values

gum1.display();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To address your question we will make changes to both the Gum class and the Demo class as outlined in your prompt Well add a default constructor that utilizes the this reference to call the parameteri... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!