Question

7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

7.2 Write a Java program called to create an Excel spreadsheet

  1. Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs
  2. In a main method (of a different class), ask the user "How many things are you going to eat for lunch?".
  3. Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each Food object in an ArrayList
  4. Create a method called printLunch(), that will take an ArrayList of Food objects as an argument and create an Excel spreadsheet. This method should loop through the ArrayList and write the data to a file called lunch.csv (CSV stands for Coma Separated Values)
  5. The data should be printed in this format below. Be sure to include a header as the first line of code written the the file, and a total row at the end. You will see that every row will be a new row in the Excel spreadsheet, and every coma will create a new column.
  6. After collecting all the food data in your main method, pass the ArrayList to the printLunch () method.  

Hint: To append to a file, check out https://www.youtube.com/watch?v=mkqbbkBqOaU

Example lunch.csv file (You should be able to open this file in Excel, but in Notepad it will look just like this)

Name, Calories, Carbs
Pizza, 1800, 46.3
Salad, 355, 6.4
Soda, 200, 12.1
Total, 2355, 64.8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Food.java

public class Food {
   private String name;
   private int calories;
   private double carbs;

   public Food(String name, int calories, double carbs) {
       this.name = name;
       this.calories = calories;
       this.carbs = carbs;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public double getCalories() {
       return calories;
   }

   public void setCalories(int calories) {
       this.calories = calories;
   }

   public double getCarbs() {
       return carbs;
   }

   public void setCarbs(double carbs) {
       this.carbs = carbs;
   }

   @Override
   public String toString() {
       return name + ", " + calories + ", " + carbs;
   }

}
_____________________

// Test.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       String name;
       int calories;
       double carbs;
      
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       ArrayList<Food> arl=new ArrayList<Food>();
      
       System.out.print("How many things are you going to eat for lunch?");
       int size=sc.nextInt();
      
       for(int i=0;i<size;i++)
       {
           System.out.print("\nEnter Food name :");
           name=sc.next();
           System.out.print("Enter Calories :");
           calories=sc.nextInt();
           System.out.print("Enter Carbs :");
           carbs=sc.nextDouble();
           Food f=new Food(name, calories, carbs);
           arl.add(f);
       }
      
       printLunch(arl);
      

   }

   private static void printLunch(ArrayList<Food> arl) {
       int totCal=0;
       double totCarbs=0;
       try {
           FileWriter sc1=new FileWriter(new File("lunch.csv"));
           sc1.write("Name, Calories, Carbs"+"\n");
           for(int i=0;i<arl.size();i++)
           {
               sc1.write(arl.get(i)+"\n");
               totCal+=arl.get(i).getCalories();
               totCarbs+=arl.get(i).getCarbs();
           }
           sc1.write("total, "+totCal+", "+totCarbs+"\n");
           sc1.close();
       }catch (IOException e) {
           System.out.println(e);
       }
      
      
      
   }

}
________________________

Output:

How many things are you going to eat for lunch?3

Enter Food name :Pizza
Enter Calories :1800
Enter Carbs :46.3

Enter Food name :Salad
Enter Calories :355
Enter Carbs :6.4

Enter Food name :Soda
Enter Calories :200
Enter Carbs :12.1


______________________

// lunch.csv

Name, Calories, Carbs
Pizza, 1800, 46.3
Salad, 355, 6.4
Soda, 200, 12.1
total, 2355, 64.8


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in...

    create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • Write a new program called TrickOr Treat that will do the following 1. In a while...

    Write a new program called TrickOr Treat that will do the following 1. In a while loop, say "Trick or Treat!" and allow the user to type in the name of a candy and store it in an ArrayList. Once the user types "Trick", then the loop should stop. Then, print out the total number of candies on the screen. (See example below) [1 points] 2. Write a method called countSnickers that will take an ArrayList of candy as an...

  • Create a budget Spreadsheet Create a spreadsheet called Budget. using Excel (Office) or Calc (Open Office)...

    Create a budget Spreadsheet Create a spreadsheet called Budget. using Excel (Office) or Calc (Open Office) In this spreadsheet file, list 5 budget items with their -budget amount -actual amount -difference Create a pie chart that illustrates how much of your money is spent on various items. Notes: -Headings (Row 1 and Column A) must be bold -All numbers must be formatted as currency -Totals (Row 7 and Column D) must be formulas, not actual numbers -Percentage amounts (not actual...

  • Java Framework Collection Assign. Create a class called ArrayListExample Create a ArrayList object called languageList which...

    Java Framework Collection Assign. Create a class called ArrayListExample Create a ArrayList object called languageList which accept type of String Add English, French, Italian and Arabic strings into languageList array object Print languageList using Iterator object Sort ArrayList object alphabetically Print languageList using Iterator object Solution will produce following: ArrayListExample class ArrayList object called languageList ArrayListExampleTest class with main method

  • Visual C# Homework 2 You are to write a program which will create a class called...

    Visual C# Homework 2 You are to write a program which will create a class called Student Each student has a name age height and weight. These variables should be declared as private. Create the correct constructors and functions. In the main, you will create 5 students. Input the data for the 5 students from a file which already has information in it. Name the file “Information.txt”. After setting up all the data, it is time to sort based on...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • 5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create...

    5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create a method called generateSuperLottoNumbers() that returns an array of 6 random SuperLotto lottery numbers. The first 5 numbers must be from the range 1 to 47 The 6th number (the MEGA) must be from 1 to 27. Create a method called printTicket() that takes an integer array as an parameter it will loop through the integer array and print out the data Display the...

  • Write the following in Java Design a class called ReviewSystem, this class should have an attribute...

    Write the following in Java Design a class called ReviewSystem, this class should have an attribute reviewList, which is an ArrayList type. add a constructor to initialize the attribute. add a method addReviewAndComment(). This method asks from the user to provide an integer (0 to 4 for the five ratings) and a String as a comment. If the user inputs -1, it ends the loop of asking. All valid user inputs should be saved to the reviewList attribute. For example:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT