Question

How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

How to solve and code the following requirements (below) using the JAVA program?

1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface.

2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface.

3. Create main class to read products from a file, instantiate them, load them into an ArrayList and then print them to the console.

4. The last element are the allergens that you will have to store to an ArrayList.

5. Please show the screenshot of the output.

1. Product.java

public abstract class Product {
    // attributes
   
private String name;
    private int sku;
    private double price;

    // constructor
   
public Product(String name, int sku, double price) {
        this.name = name;
        this.sku = sku;
        this.price = price;
    }


    // getters ans setters
   
public String getName() {
        return name;
    }

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

    public int getSku() {
        return sku;
    }

    public void setSku(int sku) {
        this.sku = sku;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    // toString override
   
@Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", sku=" + sku +
                ", price=" + price +
                '}';
    }
}

2. FoodProduct.java

import java.util.ArrayList;

import java.util.Date;



public class FoodProduct extends Product {



    // constructors

    public FoodProduct(String name, int sku, double price, Date expDate, int refrigeration, int servingSize, int caloriesPerServing, ArrayList allergens) {

        super(name, sku, price);

        this.expDate = expDate;

        this.refrigeration = refrigeration;

        this.servingSize = servingSize;

        this.caloriesPerServing = caloriesPerServing;

        this.allergens = allergens;

    }



    // attributes

    private Date expDate;

    private int refrigeration;

    private int servingSize;

    private int caloriesPerServing;

    private ArrayList allergens;





    // getters and setters

    public Date getExpDate() {

        return expDate;

    }



    public void setExpDate(Date expDate) {

        this.expDate = expDate;

    }



    public int getRefrigeration() {

        return refrigeration;

    }



    public void setRefrigeration(int refrigeration) {

        this.refrigeration = refrigeration;

    }



    public int getServingSize() {

        return servingSize;

    }



    public void setServingSize(int servingSize) {

        this.servingSize = servingSize;

    }



    public int getCaloriesPerServing() {

        return caloriesPerServing;

    }



    public void setCaloriesPerServing(int caloriesPerServing) {

        this.caloriesPerServing = caloriesPerServing;

    }



    public ArrayList getAllergens() {

        return allergens;

    }



    public void setAllergens(ArrayList allergens) {

        this.allergens = allergens;

    }





    // toString override

    @Override

    public String toString() {

        return "FoodProduct{" +

                super.toString() +

                "expDate=" + expDate +

                ", refrigeration=" + refrigeration +

                ", servingSize=" + servingSize +

                ", caloriesPerServing=" + caloriesPerServing +

                ", allergens=" + allergens +

                '}';

    }

}
 

3. CleaningProduct.java

 
import java.util.ArrayList;



public class CleaningProduct extends Product {



   // constructors

    public CleaningProduct(String name, int sku, double price, String chemicalName, String hazards, String precautions, String firstAid, ArrayList uses) {

        super(name, sku, price);

        this.chemicalName = chemicalName;

        this.hazards = hazards;

        this.precautions = precautions;

        this.firstAid = firstAid;

        this.uses = uses;

    }



    // attributes

    private String chemicalName;

    private String hazards;

    private String precautions;

    private String firstAid;

    private ArrayList uses;





    // getters and setters

    public String getChemicalName() {

        return chemicalName;

    }



    public void setChemicalName(String chemicalName) {

        this.chemicalName = chemicalName;

    }



    public String getHazards() {

        return hazards;

    }



    public void setHazards(String hazards) {

        this.hazards = hazards;

    }



    public String getPrecautions() {

        return precautions;

    }



    public void setPrecautions(String precautions) {

        this.precautions = precautions;

    }



    public String getFirstAid() {

        return firstAid;

    }



    public void setFirstAid(String firstAid) {

        this.firstAid = firstAid;

    }



    public ArrayList getUses() {

        return uses;

    }



    public void setUses(ArrayList uses) {

        this.uses = uses;

    }



    // toString override





    @Override

    public String toString() {

        return "CleaningProduct{" +

                super.toString() +

                "chemicalName='" + chemicalName + '\'' +

                ", hazards='" + hazards + '\'' +

                ", precautions='" + precautions + '\'' +

                ", firstAid='" + firstAid + '\'' +

                ", uses=" + uses +

                '}';

    }

}
 

4. Edible-Interface

 
import java.util.ArrayList;

import java.util.Date;





public interface Edible {



    public Date getExpDate();

    public void setRefrigerationTemp(int refrigerationTemp);

    public int getRefrigerationTemp();

    public void setExpDate(Date expDate);

    public int getServingSize();

    public void setServingSize(int servingSize);

    public int getCaloriesPerServing();

    public void setCaloriesPerServing(int caloriesPerServing);

    public String getAllergens();

    public void setAllergens(ArrayList<String>allergens);



}
 

5. Chemical-Interface

import java.util.ArrayList;



public interface Chemical {



    public String getChemicalName();

    public void setChemicalName(String chemicalName);

    public String getHazards();

    public void setHazards(String hazards);

    public String getPrecautions();

    public void setPrecautions(String precautions);

    public ArrayList<String> getUses();

    public void setUses(ArrayList<String> uses );

    public String getFirstAid();

    public void setFirstAid(String firstAid);



}

6. Main

import java.io.*;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Formatter;

import java.util.Scanner;

import java.util.ArrayList;



public class ProductMain {



    public static void main (String[] args) throws FileNotFoundException{

        File fileInput = new File ("src/FoodProduct");

        Scanner sc = new Scanner(fileInput);



        String name; // product

        String sku;  // product

        double price; // product



        Date expDate = new Date(); // food product

        int refrigerationTemperature; // food product

        int servingSize; // food product

        int caloriesPerServing; // food product



        ArrayList allergens = new ArrayList<String>();

        while(sc.hasNextLine()){

            String line = sc.nextLine();

            String[] productSpec = line.split(",");

            name = productSpec[0];

            sku = productSpec[1];

            price = Double.parseDouble(productSpec[2]);



            String testDate = productSpec[3];

            SimpleDateFormat formatter = new SimpleDateFormat("MM/yyyy");

            formatter.format(expDate);



            refrigerationTemperature = Integer.parseInt(productSpec[4]);

            servingSize = Integer.parseInt(productSpec[5]);

            caloriesPerServing = Integer.parseInt(productSpec[6]);



            if(productSpec.length > 5){

                for (int i=7; i<productSpec.length; i++){

                    allergens.add(productSpec[i]);



                }

            }



            System.out.println( name + " "

                    + sku + " "

                    + price + " "

                    + testDate + " "

                    + refrigerationTemperature + " "

                    + servingSize + " "

                    + caloriesPerServing + " "

                    + allergens + "\n"



            );



        }



    }

}
 
7. FoodProduct.txt
 
Frosted Mini Wheats,233345667,4.99,10/2020,70,54,190,Wheat Ingredients

Activa YoCrunch Yogurt,33445654,2.50,10/2018,35,113,90,Dairy,Peanuts
8. CleaningProduct.txt
Lysol,2344454,4.99,Alkyl (50%C14 40%C12 10%C16) dimethyl benzyl ammonium saccharinate,Hazard to humans and domestic animals. Contents under pressure,Causes eye irritation,In case of eye contact immediately flush eyes thoroughly with water, kitchen, bath

Windex,4456765,3.99,Sodium citrate (Trisodium citrate),To avoid electrical shock do not spray at or near electric lines,Undiluted product is an eye irritant, if contact with eye occurs flush immediately with plenty of water for at least 15 to 20 minutes, glass,

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

Seems all other classes are already implemented except FoodProduct.java. So, posting code of only that class. I haven't made much modifications here, just added 3 @override methods (which were not defined). Rest all methods of Edibile interface were already defined. Infact some of the methods which are similar to the 3 methods which I've added are already implemented. So, I just delegated the method calls to the methods you've already written.

One more change which I did was I added type to the allergens. It was just raw ArrayList which were depreciated long time ago. Use of rawtype is not recommended, so typed the allergens list to String type.

Seems your solution is little messed up. However, the end-to-end program seems to be working fine as I tested with the main program and even got some output. Let me know if you need anymore help on this.

FoodProduct.java

package product;

import java.util.ArrayList;

import java.util.Date;

public class FoodProduct extends Product implements Edible {

// constructors

public FoodProduct(String name, int sku, double price, Date expDate, int refrigeration, int servingSize, int caloriesPerServing, ArrayList<String> allergens) {

super(name, sku, price);

this.expDate = expDate;

this.refrigeration = refrigeration;

this.servingSize = servingSize;

this.caloriesPerServing = caloriesPerServing;

this.allergens = allergens;

}

// attributes

private Date expDate;

private int refrigeration;

private int servingSize;

private int caloriesPerServing;

private ArrayList<String> allergens;

  

  

  

// added these three @override methods here.

@Override

public void setRefrigerationTemp(int refrigerationTemp) {

this.setRefrigerationTemp(refrigerationTemp);

}

@Override

public int getRefrigerationTemp() {

return this.getRefrigerationTemp();

}

@Override

public String getAllergens() {

String allergensString = "";

for(String allergen: this.allergens)

{

allergensString = allergensString + ", " + allergen;

}

return allergensString;

}

// getters and setters

public Date getExpDate() {

return expDate;

}

public void setExpDate(Date expDate) {

this.expDate = expDate;

}

public int getRefrigeration() {

return refrigeration;

}

public void setRefrigeration(int refrigeration) {

this.refrigeration = refrigeration;

}

public int getServingSize() {

return servingSize;

}

public void setServingSize(int servingSize) {

this.servingSize = servingSize;

}

public int getCaloriesPerServing() {

return caloriesPerServing;

}

public void setCaloriesPerServing(int caloriesPerServing) {

this.caloriesPerServing = caloriesPerServing;

}

// public ArrayList getAllergens() {

// return allergens;

// }

public void setAllergens(ArrayList<String> allergens) {

this.allergens = allergens;

}

// toString override

@Override

public String toString() {

return "FoodProduct{" +

super.toString() +

"expDate=" + expDate +

", refrigeration=" + refrigeration +

", servingSize=" + servingSize +

", caloriesPerServing=" + caloriesPerServing +

", allergens=" + allergens +

'}';

}

}

Code screenshots:

FoodProduct.java /Documents/workspace-sts-3.9.5.RELEASE/product/src/product Open package product; import java.util.ArrayList; Save E import java.util.Date public class FoodProduct extends Product imple ents Edible // constructors public FoodProduct (String name, int sku, double price, Date expDate, int refrigeration, int servingSize, int caloriesPerServing, ArrayList<string> allergens) super (name, sku, price); this.expDate expDate; this.refrigerationrefrigeration; this.servingSize-servingSize; this.caloriesPerServingcaloriesPerServing.; this.allergens-allergens; // attributes private Date expDate; private int refrigeration; private int servingSize; private int caloriesPerServing; private ArrayList<string> allergens; // added these three @override methods here @override public void setRefrigerationTemp (int refrigerationTemp) this.setRefrigerationTemp (refrigerationTemp); @Override public int getRefrigerationTemp() return this.getRefrigerationTemp); Java▼ Tab width: 8▼ し 1, col 1 INS

Output:

Add a comment
Know the answer?
Add Answer to:
How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...
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
  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in...

    Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author fenghui */ public class Course {     private String cName;     private ArrayList<Subject> cores;     private ArrayList<Major> majors;     private ArrayList<Subject> electives;     private int cCredit;          public Course(String n, int cc){         cName = n;         cCredit = cc;         cores = new ArrayList<Subject>(0);         majors =...

  • How to solve my code for my Deliv C program?

    ProgramIntro.pngProgramIntro2.pngProgramIntro1.pngProgram1.pngProgram2.pngGraph Class:import java.util.ArrayList;//Graph is a class whose objects represent graphs. public class Graph {    ArrayList<Node> nodeList;    ArrayList<Edge> edgeList;       public Graph() {        nodeList = new ArrayList<Node>();        edgeList = new ArrayList<Edge>();       }       public ArrayList<Node> getNodeList() {        return nodeList;   }   public ArrayList<Edge> getEdgeList() {        return edgeList;   }   public void addNode(Node n) {        nodeList.add(n);   }   public void addEdge(Edge e) {        edgeList.add(e);   }   public String...

  • Can the folllowing be done in Java, can code for all classes and code for the...

    Can the folllowing be done in Java, can code for all classes and code for the interface be shown please. Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometriObject class for finding the larger of two GeometricObject objects. Write a test program that uses the max method to find the larger of two circles, the larger of two rectangles. The GeometricObject class is provided below: public abstract class GeometricObject { private...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • Add appropriate descriptive comments to each line of the code, explaining why the code is in...

    Add appropriate descriptive comments to each line of the code, explaining why the code is in the application. import java.text.NumberFormat; public class LineItem implements Cloneable { private Product product; private int quantity; private double total; public LineItem() { this.product = new Product(); this.quantity = 0; this.total = 0; } public LineItem(Product product, int quantity) { this.product = product; this.quantity = quantity; } public void setProduct(Product product) { this.product = product; } public Product getProduct() { return product; } public void...

  • How can I solved my Java Program for DelivC

    //Graph Class: import java.util.ArrayList; //Graph is a class whose objects represent graphs.  public class Graph {     ArrayList<Node> nodeList;     ArrayList<Edge> edgeList;         public Graph() {         nodeList = new ArrayList<Node>();         edgeList = new ArrayList<Edge>();         }         public ArrayList<Node> getNodeList() {         return nodeList;    }    public ArrayList<Edge> getEdgeList() {         return edgeList;    }    public void addNode(Node n) {         nodeList.add(n);    }    public void addEdge(Edge e) {         edgeList.add(e);    }    public String toString() {         String s = "Graph g.\n";         if (nodeList.size() > 0) {             for (Node n : nodeList) {         // Print node info         String t = "\nNode " + n.getName() + ", abbrev " + n.getAbbrev() + ", value " + n.getVal() + "\n";         s = s.concat(t);         }         s = s.concat("\n");             }         return s;     }  } // Node Class: import java.util.ArrayList;  // Node is a class whose objects represent nodes (a.k.a., vertices) in the graph.  public class Node {    String name;     String val; // The value of the Node     String abbrev; // The abbreviation for the Node     ArrayList<Edge> outgoingEdges;     ArrayList<Edge> incomingEdges;             String color; //Create the color of the TYPE Node List     int start; //Create the Starting Time     int end; //Create the Ending Time             public Node( String theAbbrev ) {         setAbbrev( theAbbrev );         val = null;         name = null;         outgoingEdges = new ArrayList<Edge>();         incomingEdges = new ArrayList<Edge>();     }         public String getAbbrev() {         return abbrev;     }         public String getName() {         return name;     }         public String getVal() {         return val;     }         public ArrayList<Edge> getOutgoingEdges() {         return outgoingEdges;     }         public ArrayList<Edge> getIncomingEdges() {         return incomingEdges;...

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