Question

In: Computer Science

Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...

Program in Java using Inheritence

The purpose of this assignment is to practice OOP programming covering Inheritance.

Core Level Requirements (up to 6 marks)

The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh product, it contains the following attributes. - 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) The purpose of this class is to maintain the general information of fresh or daily product such as meat, seafood, bread, milk and vegetable. 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. CSIT121 Spring 2019 - Nutrition Information o Quantity per 100 g or 100 ml 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. The purpose of this class is to maintain the general information of package product such as can, sauce, instant noodle, flour and snack. Noted that all classes should override and implement the toString methods to transform the information of the object into the string variable. Ensure that your 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). Students are allowed to add any classes to the design, it is necessary to perfect the design. 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. Task 2: Your classes should be able to provide methods allowing one to 1. (verifyUnitType) Check whether the input arguments (a unit type) passed to the method matches with the user type stored in the object or not. 2. (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”. 3. (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”. (0.5 marks for each feature) Choose the appropriate input or/and output type for each method. 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.5 marks)

Solutions

Expert Solution

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


Related Solutions

The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers. Instructions You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT