Question

In: Computer Science

IN JAVA 1.Write a class Item with these following requirements ØHas two properties: itemName (text) and...

IN JAVA 1.Write a class Item with these following requirements

ØHas two properties: itemName (text) and MSRP (decimal)

ØHas a getTax() method that has no implementation. This method returns a decimal value

ØHas a finalPrice() method that returns the price after tax

ØThis class cannot be instantiated

ØThis class is encapsulated

2.Write a class Electronics that inherits Item with these following requirements

ØHas two properties: manufacturer (text) and tax (decimal). tax value is shared among all electronic items

ØHas a constructor that initializes all attributes

ØProvide an implementation for getTax()

ØThis class is encapsulated

3.Write a class Shop that sells electronic items with these following requirements

ØHas two properties: shopName (text) and itemSold (collection). itemSold stores all the items that the shop sold. You can choose any Java built-in collection type that you like

ØHas a constructor (you can decide which input to have)

ØHas a method sellItem() that add a sold item to itemSold

ØHas a method getSoldList() that returns the names of all items sold in an array. This method shows an error when the collection is empty

ØThis class is encapsulated

4.Write a class Test that creates a Shop, sells a few items, then test getSoldList() and sortSoldList()

ØAssuming tax rate is 7%

Solutions

Expert Solution

Item.java

// abstract class Item ( can not instantiate abstract class)
public abstract class Item {
   // instance variables
   private String itemName;
   private double MSRP;
   // default constructor
   public Item() {
       super();
   }
   // parameterized constructor
   public Item(String itemName, double mSRP) {
       super();
       this.itemName = itemName;
       MSRP = mSRP;
   }
   // abstract method
   public abstract double getTax();
   // method to calculate and return final price of item
   public double finalPrice() {
       return MSRP+this.getTax();
   }
   // setter and getter methods
   public String getItemName() {
       return itemName;
   }
   public void setItemName(String itemName) {
       this.itemName = itemName;
   }
   public double getMSRP() {
       return MSRP;
   }
   public void setMSRP(double mSRP) {
       MSRP = mSRP;
   }
  
}

Electronics.java


public class Electronics extends Item {
   // instance variable
   private String manufacturer;
   private static double tax;
   // default constructor
   public Electronics() {
       super();
       tax=7;
   }
   // parameterized constructor
   public Electronics(String itemName, double mSRP, String manufacturer) {
       super(itemName, mSRP); // calling super class constructor
       this.manufacturer = manufacturer;
       tax=7; // tax rate 7%
   }

   @Override
   public double getTax() {
       return getMSRP()*tax/100;
   }
   // setter and getter
   public String getManufacturer() {
       return manufacturer;
   }
   public void setManufacturer(String manufacturer) {
       this.manufacturer = manufacturer;
   }
}

Shop.java

import java.util.ArrayList;
import java.util.List;

public class Shop {
   // instance varibales
   private String shopName;
   private List<Item> itemSold;
   // parameterized constructor
   public Shop(String shopName) {
       super();
       this.shopName = shopName;
       this.itemSold = new ArrayList<Item>(); // Items List
   }
   // method to add sold item to sold items list
   public void sellItem(Item item) {
       itemSold.add(item);
   }
   // method to return sold items list
   public List<Item> getSoldList() {
       return itemSold;
   }
}

Test.java

import java.util.List;

public class Test {
   public static void main(String[] args) {
       // Shop object
       Shop shop=new Shop("Electronics Store");
       // creating Item objects
       Item mouse=new Electronics("Mouse", 120.5, "HP");
       Item hardDisk=new Electronics("Hard Disk", 428.0, "Toshiba");
       Item monitor=new Electronics("Monitor", 520.0, "Intel");
       Item keyboard=new Electronics("Keyboard", 120.5, "Dell");
       Item laptop=new Electronics("Laptop", 1200.0, "Apple");
       // sell some items
       shop.sellItem(laptop);
       shop.sellItem(hardDisk);
       shop.sellItem(keyboard);
       // get all sold items list
       List<Item> soldItemList=shop.getSoldList();
       // print all sold items list with selling price including tax
       for (Item item : soldItemList) {
           System.out.println(item.getItemName()+" sold for $"+item.finalPrice());
       }
   }
}

Sample output:


Related Solutions

in java: Write a class responsible for storing data in a text file. The class should...
in java: Write a class responsible for storing data in a text file. The class should include a method that accepts a string and appends it to a file.
in java Write a class named “Stock” to model a stock. The properties and methods of...
in java Write a class named “Stock” to model a stock. The properties and methods of the class are shown in figure below. The method “ percentage ” computes the percentage of the change of the current price vs the previous closing price. Write a program to test the “Stock” class. In the program, create a Stock object with the stock symbol SUND, name Sun Microsystem V, previous closing price of 100. Set a new current price randomly and display...
JAVA Write a class to implement a list with the ability to insert a new item...
JAVA Write a class to implement a list with the ability to insert a new item at any location within the list, remove an item, and search for an item. The list should use a linked list implementation. This implementation should make use of a dummy node at the head of the list and should have explict references head and previous. Add a method to the list that inserts elements in acsending order assuming the list is already sorted before...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
USE JAVA Develop the classes for the following requirements: 1. A class named Employee (general, for...
USE JAVA Develop the classes for the following requirements: 1. A class named Employee (general, for college) 2. A class named Instructor (more specific, for college) 3. A class named Staff (more specific, for college, HR officer, Marking staff) Tasks: 1. Figure out the relationships among the classes; 2. Determine the abstract class, and the child classes; 3. For the abstract class, determine at least one abstract method; 4. Each class should at least two data members and one extra...
Write a Java application that implements the following class(es) as per business requirements mentioned below: Create...
Write a Java application that implements the following class(es) as per business requirements mentioned below: Create an abstract Insurance class (Insurance.java) that has the following instance variables: - Insurance number, - customer name, - start date of policy ( This should be represented by an object of predefined date class in java), - customer address [( Create a new Address class ( Address.java ) having following instance data members: House number , Street name, City, Province, Zip code. Implement getter...
1. Write a Java program from scratch that meets the following requirements: a. The program is...
1. Write a Java program from scratch that meets the following requirements: a. The program is in a file called Duplicates.java that defines a class called Duplicates (upper/lower case matters) b. The program includes a Java method called noDuplicates that takes an array of integers and returns true if all the integers in that array are distinct (i.e., no duplicates). Otherwise it returns false. Make sure the method is public static. example tests: noDuplicates({}) returns true noDuplicates({-1, 1}) returns true...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
. List the steps that used to write the text and change text properties inside the...
. List the steps that used to write the text and change text properties inside the drawing object.
**********Java language************ 1-      (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods:...
**********Java language************ 1-      (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods: /** Convert from Celsius to Fahrenheit */ public static double celsiusToFahrenheit(double celsius) /** Convert from Fahrenheit to Celsius */ public static double fahrenheitToCelsius(double fahrenheit) The formula for the conversion is: Celsius = (5.0 / 9) * (Fahrenheit – 32) Fahrenheit = (9.0 / 5) * Celsius + 32 Program output should be like: If you want to convert from Fahrenheit To Celsius press 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT