In this and final part of the course work you are required to change the definition of the Item_in_Stock class to make it an abstract class and change the getItemCat(), getItemName() and getItemDescription() definitions to make them abstract methods. You are then required to design and implement three classes which are derived from Item_in_Stock class to fully demonstrate the concept of inheritance and polymorphism. Implementation of HP_Laptop class in part II should have given you an idea of inheritance and polymorphism. Three sub classes, one class against each category (Computers, Laptops and Accessories), should contain appropriate constructors, instance variables, setter and getters methods and overridden methods for getItemName(), getItemDescription() and get_Item_details() method. You should be creative and come up with your own ideas of sub-classes. Task 3.1. Draw a UML class diagram with all the classes and relationships. Task 3.2. Write code in Java with all these classes implementation and a program called Polymorphism_works with member method specific_Item which takes one instance of Item_in_Stock class as a parameter. Perform functionality of adding items, selling items and changing item price and displaying items details against specific item instance. There should be a main () method which declares an array of objects containing instance of sub classes of Item_in_Stock class and then calls methods of each of the implemented classes. Part 1 code : import java.util.Scanner; public class ItemInStock { private String itemCode; private String itemName; private String itemDescription; private int quantity; private double itemPrice; private String item_category; public ItemInStock(String itemCode, int quantity, double itemPrice) { this.itemCode = itemCode; this.quantity = quantity; this.itemPrice = itemPrice; } public String getItemCode() { return this.itemCode; } public void setItemCode(String itemCode) { this.itemCode = itemCode; } public String getItemName() { return this.itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public String getItemDescription() { return this.itemDescription; } public void setItemDescription(String itemDescription) { this.itemDescription = itemDescription; } public int getQuantity() { return this.quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public double getItemPrice() { return this.itemPrice; } public void setItemPrice(double itemPrice) { this.itemPrice = itemPrice; } public String getItem_category() { return item_category ; } public void setItem_category(String item_category) { this.item_category = item_category; } public void addItem() { if (this.quantity + 1 > 25) { System.out.println("The error: Item stock must not exceed 25 items"); } else { this.quantity++; } } public void itemSell() { Scanner scanner = new Scanner(System.in); System.out.print("Enter the quantity of items to sell: "); int sellQuantity = scanner.nextInt(); if (sellQuantity > this.quantity) { System.out.println("Error: Not enough items in stock"); } else { this.quantity -= sellQuantity; } } public double taxOnItem() { return this.itemPrice * 0.05; } public double getItemPriceWithTax() { return this.itemPrice + this.taxOnItem(); } public void getItemDetails() { System.out.println("Item Category: " + this.getItem_category()); System.out.println("Item Name: " + this.getItemName()); System.out.println("Description: " + this.getItemDescription()); System.out.println("StockCode: " + this.getItemCode()); System.out.println("Price Without Tax: R. O " + this.getItemPrice()); System.out.println("Price With Tax: R.O " + this.getItemPriceWithTax()); System.out.println("Total quantity in store: " + this.getQuantity()); } } Part 2 code public class HP_Laptop extends ItemInStock { private String processor; private int memory; private int storage; public HP_Laptop(String itemCode, int quantity, double itemPrice, String processor, int memory, int storage) { super(itemCode, quantity, itemPrice); this.processor = processor; this.memory = memory; this.storage = storage; } @Override public String getItem_category() { return "Laptop"; } @Override public String getItemName() { return "HP Laptop"; } @Override public String getItemDescription() { return "HP " + processor + " " + memory + "GB " + storage + "GB"; } @Override public void getItemDetails() { super.getItemDetails(); System.out.println("Processor: " + processor); System.out.println("Memory: " + memory + "GB"); System.out.println("Storage: " + storage + "GB"); } }
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
Answer this qustion with refer to the part 1 and part 2 codes only an eample how to write in pic:
In this and final part of the course work you are required to change the definition of the Item_in_Stock class to make it an abstract class and change the getItemCat(), getItemName() and getItemDescription() definitions to make them abstract methods.
You are then required to design and implement three classes which are derived from Item_in_Stock class to fully demonstrate the concept of inheritance and polymorphism. Implementation of HP_Laptop class in part II should have given you an idea of inheritance and polymorphism.
Three sub classes, one class against each category (Computers, Laptops and Accessories), should contain appropriate constructors, instance variables, setter and getters methods and overridden methods for getItemName(), getItemDescription() and get_Item_details() method.
You should be creative and come up with your own ideas of sub-classes.
Task 3.1. Draw a UML class diagram with all the classes and relationships.
Task 3.2. Write code in Java with all these classes implementation and a program called Polymorphism_works with member method specific_Item which takes one instance of Item_in_Stock class as a parameter. Perform functionality of adding items, selling items and changing item price and displaying items details against specific item instance. There should be a main () method which declares an array of objects containing instance of sub classes of Item_in_Stock class and then calls methods of each of the implemented classes.
Part 1 code :
import java.util.Scanner;
public class ItemInStock {
private String itemCode;
private String itemName;
private String itemDescription;
private int quantity;
private double itemPrice;
private String item_category;
public ItemInStock(String itemCode, int quantity, double itemPrice) {
this.itemCode = itemCode;
this.quantity = quantity;
this.itemPrice = itemPrice;
}
public String getItemCode() {
return this.itemCode;
}
public void setItemCode(String itemCode) {
this.itemCode = itemCode;
}
public String getItemName() {
return this.itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemDescription() {
return this.itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public int getQuantity() {
return this.quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getItemPrice() {
return this.itemPrice;
}
public void setItemPrice(double itemPrice) {
this.itemPrice = itemPrice;
}
public String getItem_category() {
return item_category ;
}
public void setItem_category(String item_category) {
this.item_category = item_category;
}
public void addItem() {
if (this.quantity + 1 > 25) {
System.out.println("The error: Item stock must not exceed 25 items");
} else {
this.quantity++;
}
}
public void itemSell() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the quantity of items to sell: ");
int sellQuantity = scanner.nextInt();
if (sellQuantity > this.quantity) {
System.out.println("Error: Not enough items in stock");
} else {
this.quantity -= sellQuantity;
}
}
public double taxOnItem() {
return this.itemPrice * 0.05;
}
public double getItemPriceWithTax() {
return this.itemPrice + this.taxOnItem();
}
public void getItemDetails() {
System.out.println("Item Category: " + this.getItem_category());
System.out.println("Item Name: " + this.getItemName());
System.out.println("Description: " + this.getItemDescription());
System.out.println("StockCode: " + this.getItemCode());
System.out.println("Price Without Tax: R. O " + this.getItemPrice());
System.out.println("Price With Tax: R.O " + this.getItemPriceWithTax());
System.out.println("Total quantity in store: " + this.getQuantity());
}
}
Part 2 code
public class HP_Laptop extends ItemInStock {
private String processor;
private int memory;
private int storage;
public HP_Laptop(String itemCode, int quantity, double itemPrice, String processor, int memory, int storage) {
super(itemCode, quantity, itemPrice);
this.processor = processor;
this.memory = memory;
this.storage = storage;
}
@Override
public String getItem_category() {
return "Laptop";
}
@Override
public String getItemName() {
return "HP Laptop";
}
@Override
public String getItemDescription() {
return "HP " + processor + " " + memory + "GB " + storage + "GB";
}
@Override
public void getItemDetails() {
super.getItemDetails();
System.out.println("Processor: " + processor);
System.out.println("Memory: " + memory + "GB");
System.out.println("Storage: " + storage + "GB");
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images