Question

The purpose of this homework is to practice OOP programming covering Inheritance in java. The scenario...

The purpose of this homework is to practice OOP programming covering Inheritance in java.

The scenario is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket) and is mostly concentrated on the product registration system.

Design and draw a UML diagram, and write the code for the following classes:

  • ID: The identification number must start with 1 and follows with 6 numbers, e.g., “1123456”.

  • Description, e.g. “Banana”

  • Recommended Price per unit, e.g. 5.54. Note that this is the suggested price, it is not the actual price

    in the online shopping system.

  • Unit (by default is 1)

  • Weight (in gram)

  • Unit Type, e.g. “Each, Kilogram, Bunch, Box and Pack”

  • The packing or baking date (it should include the date, month, year and time), this information is not

    required for some product, hence, it default may be set to 1/1/1900.

  • The expiry date is (it should include the date, month, year)

  1. The purpose of this class is to maintain the general information of fresh or daily product such as meat, seafood, bread, milk and vegetable.

  2. The second product category is a packaged product such as can, source, snack, etc. It contains the following attributes.

  • ID: The identification number must start with 2 and follows with 6 numbers, e.g., “2123456”.

  • Description, e.g. “Corn cream can”

  • Recommended Price per unit, e.g. 5.54.

  • Unit (by default is 1)

  • Weight (in gram)

  • The dimension of the package (including height, width, deep). If a product doesn’t provide this

    information, then it set to (0, 0, 0) as a default value.

  • Nutrition Information

  • o Quantityper100gor100ml

  • o Energy/kilojoules

  • o Protein

  • o Fat

  • o Sugars(mg)

  • o Sodium/salt(mg)

  • Unit Type for this class is limited to only “Box, Bottle and Pack”

  • Packing or manufacturing date (it should include the date, month, year and time), this information is

    not required for some product, hence, it default may be set to 1/1/1900.

  • Expire date is (it should include the date, month, year), its default may be set to 1/1/3000 for the

    product that doesn’t have an expiry date.

  1. The purpose of this class is to maintain the general information of package product such as can, sauce, instant noodle, flour and snack.

  2. Noted that all classes should override and implement the toString methods to transform the information of the object into the string variable.

  3. Ensure that all classes are well designed with practical attributes (either primitive type, class object or Enumerated Data type) and have proper methods to handle all tasks (Encapsulation).

  4. Task 1: Class design (Code and UML).

  1. Design and write the java code for the above two categories.

  2. Design 2 subclass with 2 additional attributes for each category.

  1. Task 2: Your classes should be able to provide methods allowing one to

  2. (verifyUnitType) Check whether the input arguments (a unit type) passed to the method matches with

    the user type stored in the object or not.

  3. (getAttributeDescriptionForSavingTofile) output the description of all attributes to a String format

    that can be used to save it to a CVS format file directly, e.g. “ID, Description, .... , Expire date”.

  4. (getInformationForSavingToFile) Take all attributes in the object and output them to a String format

    that can be used to save it to a CVS format file directly, e.g. “1222344, Corn Cream,.... ,

    20/11/2019”.

  5. Choose the appropriate input or/and output type for each method.

  6. Test Code: write the test code (a separate class/file called “TestCode” with the Main method in it), create the object of each class, and create an ArrayList to keep all the above classes. For each object, call methods in Task 2 to check whether the method works properly. Display the output of the methods to the console. Finally, display information of all the object in the ArrayList to the console.

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

package inheritance;

import java.util.*;

// Defines super class Product

class Product

{

// Instance variable to store product information

String id;

String description;

double price;

double weight;

Date packingDate;

Date expiryDate;

// Default constructor to assign default values to instance variables

@SuppressWarnings("deprecation")

Product()

{

id = description = "";

price = weight = 0.0;

packingDate.setDate(1);

packingDate.setMonth(1);

packingDate.setYear(1900);

expiryDate.setDate(0);

expiryDate.setMonth(0);

expiryDate.setYear(0);

}// End of default constructor

// Parameterized constructor to assign parameter values to instance variables

Product(String ID, String des, double pr, double we, Date pd, Date ex)

{

id = ID;

description = des;

price = pr;

weight = we;

packingDate = pd;

expiryDate = ex;

}// End of parameterized constructor

// Method to set id

void setId(String ID)

{

id = ID;

}// End of method

// Method to set price

void setPrice(double pr)

{

price = pr;

}// End of method

// Method to set weight

void setWeight(double we)

{

weight = we;

}// End of method

// Method to set packing date

void setPackingDate(Date pd)

{

packingDate = pd;

}// End of method

// Method to set expiry date

void setExpiryDate(Date ed)

{

expiryDate = ed;

}// End of method

// Method to return id

String getId()

{

return id;

}// End of method

// Method to return price

double getPrice()

{

return price;

}// End of method

// Method to return weight

double getWeight()

{

return weight;

}// End of method

// Method to return packing date

Date getPackingDate()

{

return packingDate;

}// End of method

// Method to return expiry date

Date getExpiryDate()

{

return expiryDate;

}// End of method

// Overrides toString method to return product information

public String toString()

{

return "\n\n Product Id: " + id + "\n Price: " + price +

"\n Weight: " + weight + "\n Packing Date: " + packingDate +

"\n Expiry Date: " + expiryDate;

}// End of method

}// End of class Product

// Derived a class DailyProduct from super class Product

class DailyProduct extends Product

{

// Instance variable to store daily product information

String unitType;

// Default constructor to assign default values to instance variables

DailyProduct()

{

// Calls the base class constructor

super();

unitType = "";

}// End of default constructor

// Parameterized constructor to assign parameter values to instance variables

DailyProduct(String ID, String des, double pr, double we,

Date pd, Date ex, String ut)

{

// Calls the base class constructor

super(ID, des, pr, we, pd, ex);

unitType = ut;

}// End of parameterized constructor

// Method to set unit type

void setUnitType(String ut)

{

unitType = ut;

}// End of method

// Method to return unit type

String getUnitType()

{

return unitType;

}// End of method

// Overrides toString method to return daily product information

public String toString()

{

return super.toString() + "\n Unit Type: " + unitType;

}// End of method

}// End of class DailyProduct

// Derived a class PackageProduct from super class Product

class PackageProduct extends Product

{

// Instance variable to store package product information

int packageWdith;

int packageHeight;

int packageDeep;

String unitType;

String nutritionInfo;

// Default constructor to assign default values to instance variables

PackageProduct()

{

// Calls the base class constructor

super();

packageWdith = packageHeight = packageDeep = 0;

unitType = nutritionInfo = "";

}// End of default constructor

// Parameterized constructor to assign parameter values to instance variables

PackageProduct(String ID, String des, double pr, double we,

Date pd, Date ex, int pw, int ph, int pde, String ut, String ni)

{

// Calls the base class constructor

super(ID, des, pr, we, pd, ex);

packageWdith = pw;

packageHeight = ph;

packageDeep = pde;

unitType = ut;

nutritionInfo = ni;

}// End of parameterized constructor

// Method to set width

void setPackageWdith(int pw)

{

packageWdith = pw;

}// End of method

// Method to set height

void setPackageHeight(int ph)

{

packageHeight = ph;

}// End of method

// Method to set depth

void setPackageDeep(int pd)

{

packageDeep = pd;

}// End of method

// Method to set unit type

void setUnitType(String ut)

{

unitType = ut;

}// End of method

// Method to set nutrition

void setNutritionInfo(String ni)

{

nutritionInfo = ni;

}// End of method

// Method to return width

int getPackageWdith()

{

return packageWdith;

}// End of method

// Method to return height

int getPackageHeight()

{

return packageHeight;

}// End of method

// Method to return depth

int getPackageDeep()

{

return packageDeep;

}// End of method

// Method to return unit type

String getUnitType()

{

return unitType;

}// End of method

// Method to return nutrition

String getNutritionInfo()

{

return nutritionInfo;

}// End of method

// Overrides toString method to return packing product information

public String toString()

{

return super.toString() + "\n Packing Width: " + packageWdith +

"\n Packing Height: " + packageHeight +

"\n Packing Deepth: " + packageDeep +

"\n Unit Type: " + unitType + "\n Nutrition: " + nutritionInfo;

}// End of method

}// End of class PackageProduct

// Defines driver class FoodsSupermarket

public class FoodsSupermarket

{

// main method definition

@SuppressWarnings("deprecation")

public static void main(String st[])

{

// Declares an array list to store products

ArrayList Product = new ArrayList();

// Declares an array of objects for DailyProduct

DailyProduct dp[] = new DailyProduct[3];

// Declares an array of objects for PackageProduct

PackageProduct pp[] = new PackageProduct[3];

// Creates daily product using parameterized constructor

dp[0] = new DailyProduct("1123456", "Banana", 2.56, 1.2, new Date(1, 1, 1998),

new Date(5,1,198), "Each");

dp[1] = new DailyProduct("1123458", "Orange", 89.36, 22.23,

new Date(1, 3, 1999), new Date(5,3,198), "Kilogram");

// Creates package product using parameterized constructor

pp[0] = new PackageProduct("2123477", "noodle", 2.56, 1.2,

new Date(1, 1, 1998), new Date(5,1,198), 12, 10, 5, "Each", "Protein");

pp[1] = new PackageProduct("2123488", "snack", 45.44, 31.11,

new Date(1, 1, 1998), new Date(5,1,198), 22, 12, 3, "Box", "Sodium");

// Adds the daily product objects

Product.add(dp[0]);

Product.add(dp[1]);

// Adds the package product objects

Product.add(pp[0]);

Product.add(pp[1]);

// Loops to display all product information

for(int c = 0; c < Product.size(); c++)

System.out.println(Product.get(c));

}// End of main method

}// End of driver class FoodsSupermarket

Sample Output:

Product Id: 1123456
Price: 2.56
Weight: 1.2
Packing Date: Sun Jul 22 00:00:00 IST 1906
Expiry Date: Thu Aug 17 00:00:00 IST 1905
Unit Type: Each


Product Id: 1123458
Price: 89.36
Weight: 22.23
Packing Date: Thu Sep 20 00:00:00 IST 1906
Expiry Date: Sun Oct 15 00:00:00 IST 1905
Unit Type: Kilogram


Product Id: 2123477
Price: 2.56
Weight: 1.2
Packing Date: Sun Jul 22 00:00:00 IST 1906
Expiry Date: Thu Aug 17 00:00:00 IST 1905
Packing Width: 12
Packing Height: 10
Packing Deepth: 5
Unit Type: Each
Nutrition: Protein


Product Id: 2123488
Price: 45.44
Weight: 31.11
Packing Date: Sun Jul 22 00:00:00 IST 1906
Expiry Date: Thu Aug 17 00:00:00 IST 1905
Packing Width: 22
Packing Height: 12
Packing Deepth: 3
Unit Type: Box
Nutrition: Sodium

Add a comment
Know the answer?
Add Answer to:
The purpose of this homework is to practice OOP programming covering Inheritance in java. The scenario...
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
  • Write java program The purpose of this assignment is to practice OOP with Array and Arraylist,...

    Write java program The purpose of this assignment is to practice OOP with Array and Arraylist, Class design, Interfaces and Polymorphism. Create a NetBeans project named HW3_Yourld. Develop classes for the required solutions. Important: Apply good programming practices Use meaningful variable and constant names. Provide comment describing your program purpose and major steps of your solution. Show your name, university id and section number as a comment at the start of each class. Submit to Moodle a compressed folder of...

  • Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student...

    Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student will demonstrate the ability to use inheritance in Java programs. Program Specifications: Programming assignment: Classes and Inheritance You are to pick your own theme for a new Parent class. You can pick from one of the following: Person Automobile Animal Based on your choice: If you choose Person, you will create a subclass of Person called Student. If you choose Automobile, you will create...

  • How do I start this Java: Inheritance problem? • person.java • student.java • studentAthlete.java • app.java...

    How do I start this Java: Inheritance problem? • person.java • student.java • studentAthlete.java • app.java • Students should apply consistent indenting in all submissions. This can be done via the NetBeans Source menu. Contents person First name Last name app Creates 1 student-athlete object Hometown Retinfo) 1-Displays the complete information about the student-athlete object student New attributes Maior attributes getinfo) method from the superlas person Student-athlete student's rankine Overrides the method from the superclass student Video from Professor Fisher...

  • Please use Java to write the program The purpose of this lab is to practice using...

    Please use Java to write the program The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company. Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc. Insurance policies have a lot of data and behavior in common. But they also have differences. You want to build each insurance policy class so that you can reuse as much code as possible. Avoid duplicating code....

  • JAVA - Abstraction and Encapsulation are one pillar of OOP (Object Oriented Programming). Another is inheritance...

    JAVA - Abstraction and Encapsulation are one pillar of OOP (Object Oriented Programming). Another is inheritance and polymorphism. In this assignment we will use inheritance and polymorphism to solve a problem. Part (a) of the figure below shows a symbolic representation of an electric circuit called an amplifier. The input to the amplifier is the voltage vi and the output is the voltage vo. The output of an amplifier is proportional to the input. The constant of proportionality is called...

  • Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person...

    Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee. A Person object has a name, address, phone number, and email address (all Strings). A Student Object has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable. An Employee Object has an office number, salary (both ints ), and a date hired. Use...

  • Computer Science 111 Introduction to Algorithms and Programming: Java Programming Net Beans Project #4 - Classes...

    Computer Science 111 Introduction to Algorithms and Programming: Java Programming Net Beans Project #4 - Classes and Objects (15 Points) You will create 3 new classes for this project, two will be chosen (THE TWO CHOSEN ARE TENNIS SHOE AND TREE) from the list below and one will be an entirely new class you invent. Here is the list: Cellphone Clothes JuiceDrink Book MusicBand Bike GameConsole Tree Automobile Baseball MusicPlayer Laptop TennisShoe Cartoon EnergyDrink TabletComputer RealityShow HalloweenCostume Design First Create...

  • Use Java please... Activity 1. Suppose we want to have an object-oriented design for an invoicing...

    Use Java please... Activity 1. Suppose we want to have an object-oriented design for an invoicing system, that includes invoices, one or more line items in each invoice, address of the invoice, list of products, etc. Try to design this system by indicating the required classes, responsibilities of each class, and the collaborating classes to fulfill each responsibility, and draw the CRC cards as well as UML diagram of the classes and their relationships. Then, indicate the methods of each...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • In Python a class can inherit from more than one class (Java does not allow this)....

    In Python a class can inherit from more than one class (Java does not allow this). The resulting class will have all the methods and attributes from the parent classes. Do the following: • Create a class called Person. In the class, define variables for storing date of birth, place of birth, and male/female attributes. In the class, define the constructor method, as well as methods for returning current values of the class attributes. • Create a class called Employee....

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