Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Make the following changes to your class from Assignment 3 -- Use the this refer

ID: 3860129 • Letter: M

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 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();

}

}

Explanation / Answer

Given below is the modified files for the question. The Demo class now makes an array and creates a spreadsheet. Please do rate the answer if it helped. Thank You.

Gum.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class Gum
{
   //Instance fields (attributes)
   private String brand;
   private int pieces;
   private double price;
   //Default constructor
   public Gum()
   {
       this("", 0, 0.0); //use this to call parameterized constructor of this class
   }
   //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.");
   }
  
   /**
   * This method receives an array of objects and writes them into mydata.csv. The file generated
   * is a csv file where 1st row represents the column names and rest of the rows are the field
   * values separated by a comma.
   */
   public static void createSpreadSheet(Gum []gumList)
   {
       StringBuilder strBuilder = new StringBuilder();
       //column names
       strBuilder.append("Brand,Pieces,Price ");
       //all field values separated by comma, each object on a new line
       for(int i = 0; i < gumList.length; i++)
           strBuilder.append(gumList[i].brand + "," + gumList[i].pieces + "," + gumList[i].price + " ");
  
       try {
           PrintWriter writer = new PrintWriter(new File("mydata.csv"));
           writer.write(strBuilder.toString());
           writer.close();
           System.out.println("Spreadsheet mydata.csv generated.");
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       }
      
   }
}

Demo.java


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 gum1 = new Gum();
//Call all your objects setter (mutator) methods to assign values to your object
gum1.setBrand("Orbit");
gum1.setPieces(14);
gum1.setPrice(0.99);
//Call the objects display method, to print out it's values
gum1.display();
//Create another instance of your class (another object) by using the parameterized constructor. (1 point)
Gum gum2 = new Gum("Five", 24, 1.99);
//Call the objects display method, to print out it's values
gum2.display();

//create an array of Gum objects
Gum[] gums = new Gum[4];
gums[0] = gum1;
gums[1] = gum2;
gums[2] = new Gum("Doublemint", 10, 1.49);
gums[3] = new Gum("Bubble Yum", 8, 1.25);

gums[2].display();
gums[3].display();

System.out.println("Calling createSpreadSheet....");
Gum.createSpreadSheet(gums);
}
}

output

Orbit Gum costs $0.99 and contains 14 pieces.
Five Gum costs $1.99 and contains 24 pieces.
Doublemint Gum costs $1.49 and contains 10 pieces.
Bubble Yum Gum costs $1.25 and contains 8 pieces.
Calling createSpreadSheet....
Spreadsheet mydata.csv generated.

ouputfile : mydata.csv

Brand,Pieces,Price
Orbit,14,0.99
Five,24,1.99
Doublemint,10,1.49
Bubble Yum,8,1.25

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote