Question

In: Computer Science

Java Create an abstract Product Class Add the following private attributes: name sku (int) price (double)...

Java

Create an abstract Product Class

Add the following private attributes:

  • name

  • sku (int)

  • price (double)

Create getter/setter methods for all attributes

Create a constructor that takes in all attributes and sets them

Remove the default constructor
Override the toString() method and return a String that represents the building object that is formatted

nicely and contains all information (attributes)

Create a FoodProduct Class that extends Product

Add the following private attributes:

  • expDate (java.util.Date) for expiration date

  • refrigerationTemp (int) if 70 or above, none necessary, otherwise it will be a lower number

  • servingSize (int) in grams

  • caloriesPerServing (int)

  • allergens (ArrayList<String>)

    Create getter/setter methods for all attributes

    Create a constructor that takes in all attributes for product and food product. Call the super constructor and then set the expDate, refrigerationTemp, servingSize, caloriesPerServing and allergens.

  • Remove the default constructor

    Override the toString() method and return a String that represents the food product object that is formatted nicely and contains all information (super toString and the private attributes)

Create a CleaningProduct Class that extends Product

Add the following private attributes:

  • String chemicalName

  • String hazards

  • String precautions

  • String firstAid

  • ArrayList<String> uses (e.g. kitchen, bath, laundry, etc)

Create getter/setter methods for all attributes

Create a constructor that takes in all attributes for product and food product. Call the super constructor and then set the chemical name, hazard, precautions, firstAid and uses. TIP: try generating the constructor, it will add the super stuff for you. (Also, see Lynda inheritance videos.)

Remove the default constructor

Override the toString() method and return a String that represents the cleaning product object that is formatted nicely and contains all information (super toString() and the private attributes)

Create the interface Edible which contains the following method signatures:

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

Create the interface Chemical which contains the following method signatures

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

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

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

Create your main/test class to read products from a file, instantiate them, load them into an ArrayList and then print them nicely to the console at the end of your program. See instructor note about reading the file as the lengths of the lines may be different depending on the list of allergens. It is also comma delimited rather than by spaces. You will have to read an entire line, split its contents and then store the information into variables. The last element(s) are the allergens that you will have to store to an ArrayList.

2 note files:

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

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

Solutions

Expert Solution

/*****************************Edible.java********************/

import java.util.ArrayList;
import java.util.Date;

/**
* The Interface Edible.
*/
public interface Edible {

   /**
   * Gets the exp date.
   *
   * @return the exp date
   */
   public Date getExpDate();

   /**
   * Sets the refrigeration temp.
   *
   * @param refrigerationTemp the new refrigeration temp
   */
   public void setRefrigerationTemp(int refrigerationTemp);

   /**
   * Gets the refrigeration temp.
   *
   * @return the refrigeration temp
   */
   public int getRefrigerationTemp();

   /**
   * Sets the exp date.
   *
   * @param expDate the new exp date
   */
   public void setExpDate(Date expDate);

   /**
   * Gets the serving size.
   *
   * @return the serving size
   */
   public int getServingSize();

   /**
   * Sets the serving size.
   *
   * @param servingSize the new serving size
   */
   public void setServingSize(int servingSize);

   /**
   * Gets the calories per serving.
   *
   * @return the calories per serving
   */
   public int getCaloriesPerServing();

   /**
   * Sets the calories per serving.
   *
   * @param caloriesPerServing the new calories per serving
   */
   public void setCaloriesPerServing(int caloriesPerServing);

   /**
   * Gets the allergens.
   *
   * @return the allergens
   */
   public ArrayList<String> getAllergens();

   /**
   * Sets the allergens.
   *
   * @param allergens the new allergens
   */
   public void setAllergens(ArrayList<String> allergens);

}
/***************************Chemical.java*****************************/

import java.util.ArrayList;

/**
* The Interface Chemical.
*/
public interface Chemical {

   /**
   * Gets the chemical name.
   *
   * @return the chemical name
   */
   public String getChemicalName();

   /**
   * Sets the chemical name.
   *
   * @param chemicalName the new chemical name
   */
   public void setChemicalName(String chemicalName);

   /**
   * Gets the hazards.
   *
   * @return the hazards
   */
   public String getHazards();

   /**
   * Sets the hazards.
   *
   * @param hazards the new hazards
   */
   public void setHazards(String hazards);

   /**
   * Gets the precautions.
   *
   * @return the precautions
   */
   public String getPrecautions();

   /**
   * Sets the precautions.
   *
   * @param precautions the new precautions
   */
   public void setPrecautions(String precautions);

   /**
   * Gets the uses.
   *
   * @return the uses
   */
   public ArrayList<String> getUses();

   /**
   * Sets the uses.
   *
   * @param uses the new uses
   */
   public void setUses(ArrayList<String> uses);

   /**
   * Gets the first aid.
   *
   * @return the first aid
   */
   public String getFirstAid();

   /**
   * Sets the first aid.
   *
   * @param firstAid the new first aid
   */
   public void setFirstAid(String firstAid);
}
/*******************************Product.java********************************/

/**
* The Class Product.
*/
public abstract class Product {

   /** The name. */
   private String name;
  
   /** The sku. */
   private int sku;
  
   /** The price. */
   private double price;

   /**
   * Instantiates a new product.
   *
   * @param name the name
   * @param sku the sku
   * @param price the price
   */
   public Product(String name, int sku, double price) {
       super();
       this.name = name;
       this.sku = sku;
       this.price = price;
   }

   /**
   * Gets the name.
   *
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * Sets the name.
   *
   * @param name the new name
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * Gets the sku.
   *
   * @return the sku
   */
   public int getSku() {
       return sku;
   }

   /**
   * Sets the sku.
   *
   * @param sku the new sku
   */
   public void setSku(int sku) {
       this.sku = sku;
   }

   /**
   * Gets the price.
   *
   * @return the price
   */
   public double getPrice() {
       return price;
   }

   /**
   * Sets the price.
   *
   * @param price the new price
   */
   public void setPrice(double price) {
       this.price = price;
   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Product name=" + name + ", sku=" + sku + ", price=" + price;
   }

}
/***************************FoodProduct.java************************/

import java.util.ArrayList;
import java.util.Date;

/**
* The Class FoodProduct.
*/
public class FoodProduct extends Product implements Edible {

   /** The exp date. */
   private Date expDate;

   /** The refrigeration temp. */
   private int refrigerationTemp;

   /** The serving size. */
   private int servingSize;

   /** The calories per serving. */
   private int caloriesPerServing;

   /** The allergens. */
   private ArrayList<String> allergens;

   /**
   * Instantiates a new food product.
   *
   * @param name the name
   * @param sku the sku
   * @param price the price
   * @param expDate the exp date
   * @param refrigerationTemp the refrigeration temp
   * @param servingSize the serving size
   * @param caloriesPerServing the calories per serving
   * @param allergens the allergens
   */
   public FoodProduct(String name, int sku, double price, Date expDate, int refrigerationTemp, int servingSize,
           int caloriesPerServing, ArrayList<String> allergens) {
       super(name, sku, price);
       this.expDate = expDate;
       this.refrigerationTemp = refrigerationTemp;
       this.servingSize = servingSize;
       this.caloriesPerServing = caloriesPerServing;
       this.allergens = allergens;
   }

   /*
   * (non-Javadoc)
   *
   * @see Edible#getExpDate()
   */
   @Override
   public Date getExpDate() {
       return expDate;
   }

   /*
   * (non-Javadoc)
   *
   * @see Edible#setExpDate(Date)
   */
   @Override
   public void setExpDate(Date expDate) {
       this.expDate = expDate;
   }

   /*
   * (non-Javadoc)
   *
   * @see Edible#getRefrigerationTemp()
   */
   @Override
   public int getRefrigerationTemp() {
       return refrigerationTemp;
   }

   /*
   * (non-Javadoc)
   *
   * @see Edible#setRefrigerationTemp(int)
   */
   @Override
   public void setRefrigerationTemp(int refrigerationTemp) {
       this.refrigerationTemp = refrigerationTemp;
   }

   /*
   * (non-Javadoc)
   *
   * @see Edible#getServingSize()
   */
   @Override
   public int getServingSize() {
       return servingSize;
   }

   /*
   * (non-Javadoc)
   *
   * @see Edible#setServingSize(int)
   */
   @Override
   public void setServingSize(int servingSize) {
       this.servingSize = servingSize;
   }

   /*
   * (non-Javadoc)
   *
   * @see Edible#getCaloriesPerServing()
   */
   @Override
   public int getCaloriesPerServing() {
       return caloriesPerServing;
   }

   /*
   * (non-Javadoc)
   *
   * @see Edible#setCaloriesPerServing(int)
   */
   @Override
   public void setCaloriesPerServing(int caloriesPerServing) {
       this.caloriesPerServing = caloriesPerServing;
   }

   /*
   * (non-Javadoc)
   *
   * @see Edible#getAllergens()
   */
   @Override
   public ArrayList<String> getAllergens() {
       return allergens;
   }

   /*
   * (non-Javadoc)
   *
   * @see Edible#setAllergens(java.util.ArrayList)
   */
   @Override
   public void setAllergens(ArrayList<String> allergens) {
       this.allergens = allergens;
   }

   /*
   * (non-Javadoc)
   *
   * @see Product#toString()
   */
   @Override
   public String toString() {
       return super.toString() + " " + "expDate=" + expDate + ", refrigerationTemp=" + refrigerationTemp
               + ", servingSize=" + servingSize + ", caloriesPerServing=" + caloriesPerServing + ", allergens="
               + allergens;
   }

}
/****************************CleaningProduct.java************************************/

import java.util.ArrayList;


/**
* The Class CleaningProduct.
*/
public class CleaningProduct extends Product implements Chemical {

   /** The chemical name. */
   private String chemicalName;

   /** The hazards. */
   private String hazards;

   /** The precautions. */
   private String precautions;

   /** The first aid. */
   private String firstAid;

   /** The uses. */
   private ArrayList<String> uses;

   /**
   * Instantiates a new cleaning product.
   *
   * @param name the name
   * @param sku the sku
   * @param price the price
   * @param chemicalName the chemical name
   * @param hazards the hazards
   * @param precautions the precautions
   * @param firstAid the first aid
   * @param uses the uses
   */
   public CleaningProduct(String name, int sku, double price, String chemicalName, String hazards, String precautions,
           String firstAid, ArrayList<String> uses) {
       super(name, sku, price);
       this.chemicalName = chemicalName;
       this.hazards = hazards;
       this.precautions = precautions;
       this.firstAid = firstAid;
       this.uses = uses;
   }
  
   /* (non-Javadoc)
   * @see Chemical#getChemicalName()
   */
   @Override
   public String getChemicalName() {
       return chemicalName;
   }
  
   /* (non-Javadoc)
   * @see Chemical#setChemicalName(java.lang.String)
   */
   @Override
   public void setChemicalName(String chemicalName) {
       this.chemicalName = chemicalName;
   }
  
   /* (non-Javadoc)
   * @see Chemical#getHazards()
   */
   @Override
   public String getHazards() {
       return hazards;
   }
  
   /* (non-Javadoc)
   * @see Chemical#setHazards(java.lang.String)
   */
   @Override
   public void setHazards(String hazards) {
       this.hazards = hazards;
   }
  
   /* (non-Javadoc)
   * @see Chemical#getPrecautions()
   */
   @Override
   public String getPrecautions() {
       return precautions;
   }
  
   /* (non-Javadoc)
   * @see Chemical#setPrecautions(java.lang.String)
   */
   @Override
   public void setPrecautions(String precautions) {
       this.precautions = precautions;
   }
  
   /* (non-Javadoc)
   * @see Chemical#getFirstAid()
   */
   @Override
   public String getFirstAid() {
       return firstAid;
   }
  
   /* (non-Javadoc)
   * @see Chemical#setFirstAid(java.lang.String)
   */
   @Override
   public void setFirstAid(String firstAid) {
       this.firstAid = firstAid;
   }
  
   /* (non-Javadoc)
   * @see Chemical#getUses()
   */
   @Override
   public ArrayList<String> getUses() {
       return uses;
   }
  
   /* (non-Javadoc)
   * @see Chemical#setUses(java.util.ArrayList)
   */
   @Override
   public void setUses(ArrayList<String> uses) {
       this.uses = uses;
   }

   /* (non-Javadoc)
   * @see Product#toString()
   */
   @Override
   public String toString() {
       return super.toString()+", chemicalName=" + chemicalName + ", hazards=" + hazards + ", precautions=" + precautions
               + ", firstAid=" + firstAid + ", uses=" + uses;
   }

  
}
/*********************************************TestProduct.java**********************************/

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;


/**
* The Class TestProduct.
*/
public class TestProduct {

   /**
   * The main method.
   *
   * @param args the arguments
   */
   public static void main(String[] args) {

       ArrayList<Product> products = new ArrayList<>();
       File file = new File("food.txt");
       File file2 = new File("chemical.txt");
       try {
           Scanner scnr = new Scanner(file);
           Scanner scanner = new Scanner(file2);

           while (scnr.hasNextLine()) {

               String line = scnr.nextLine();
               String[] data = line.split(",");
               String name = data[0];
               int sku = Integer.parseInt(data[1]);
               double price = Double.parseDouble(data[2]);
               Date date = null;
               try {
                   date = new SimpleDateFormat("MM/yyyy").parse(data[3]);
               } catch (ParseException e) {
               }
               int refrigerationTemp = Integer.parseInt(data[4]);
               int servingSize = Integer.parseInt(data[5]);
               int caloriesPerServing = Integer.parseInt(data[6]);
               ArrayList<String> allergens = new ArrayList<>();
               for (int i = 7; i < data.length; i++) {

                   allergens.add(data[i]);
               }

               products.add(new FoodProduct(name, sku, price, date, refrigerationTemp, servingSize, caloriesPerServing,
                       allergens));
           }
           while (scanner.hasNextLine()) {

               String line = scanner.nextLine();
               String[] data = line.split(",");
               String name = data[0];
               int sku = Integer.parseInt(data[1]);
               double price = Double.parseDouble(data[2]);

               String chemicalName = data[3];
               String hazards = data[4];
               String precautions = data[5];
               String firstAid = data[6];
               ArrayList<String> uses = new ArrayList<>();
               for (int i = 7; i < data.length; i++) {

                   uses.add(data[i]);
               }

               products.add(new CleaningProduct(name, sku, price, chemicalName, hazards, precautions, firstAid, uses));
           }
       } catch (FileNotFoundException e) {
           System.out.println("File not found!");
       }
      
       for (Product product : products) {
          
           System.out.println(product.toString());
       }
   }
}
/***************************food.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
/************************************chemical.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
/***********************output****************************/

Product name=Frosted Mini Wheats, sku=233345667, price=4.99 expDate=Thu Oct 01 00:00:00 IST 2020, refrigerationTemp=70, servingSize=54, caloriesPerServing=190, allergens=[Wheat Ingredients Activa YoCrunch]
Product name=Yogurt, sku=33445654, price=2.5 expDate=Mon Oct 01 00:00:00 IST 2018, refrigerationTemp=35, servingSize=113, caloriesPerServing=90, allergens=[Dairy, Peanuts]
Product name=Lysol, sku=2344454, price=4.99, chemicalName=Alkyl (50%C14 40%C12 10%C16) dimethyl benzyl ammonium saccharinate, hazards=Hazard to humans and domestic animals. Contents under pressure, precautions=Causes eye irritation, firstAid=In case of eye contact immediately flush eyes thoroughly with water, uses=[ kitchen, bath]
Product name=Windex, sku=4456765, price=3.99, chemicalName=Sodium citrate (Trisodium citrate), hazards=To avoid electrical shock do not spray at or near electric lines, precautions=Undiluted product is an eye irritant, firstAid= if contact with eye occurs flush immediately with plenty of water for at least 15 to 20 minutes, uses=[ glass, upholstery, clothing]
/*********************************scrshot**********************/

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Specifications Create an abstract Employee class that provides private attributes for the first name, last name,...
Specifications Create an abstract Employee class that provides private attributes for the first name, last name, email address, and social security number. This class should provide functions that set and return the employee’s first name, last name, email address, and social security number. This class has a function: get_net_income which returns 0. Create a Manager class that inherits the Employee class. This class should add private attributes for years of experience and the annual salary. This class should also provide...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games,...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If there are...
Add the following private attributes: String publisher String title String ISBN String imageName double price Create...
Add the following private attributes: String publisher String title String ISBN String imageName double price Create getter/setter methods for all data types Create a constructor that takes in all attributes and sets them Remove the default constructor Override the toString() method and return a String that represents the book object that is formatted nicely and contains all information (attributes) In the main class: Create a main method that throws FileNotFoundException Import java.io libraries Import java.util.Scanner Create a loop to read...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If...
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name...
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name (string) Company id (string) Billing address (string) Billing city (string) Billing state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyClient that inherits from the Client class.   HourClient must use the inherited parent class variables and add in hourlyRate and hoursBilled. Your Hourly Client class should contain a constructor that calls the constructor from the Client class to initialize...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
please recode this in c++ language public abstract class BankAccount { double balance; int numOfDeposits; int...
please recode this in c++ language public abstract class BankAccount { double balance; int numOfDeposits; int numOfWithdraws; double interestRate; double annualInterest; double monSCharges; double amount; double monInterest; //constructor accepts arguments for balance and annual interest rate public BankAccount(double bal, double intrRate) { balance = bal; annualInterest = intrRate; } //sets amount public void setAmount(double myAmount) { amount = myAmount; } //method to add to balance and increment number of deposits public void deposit(double amountIn) { balance = balance + amountIn;...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT