Create a public class called MedicalItem with the following specifications:It sh
ID: 3672108 • Letter: C
Question
Create a public class called MedicalItem with the following specifications:It should have the following private instance variables:
An item number (int)
An item name (String)
Cost of the item (double)
It should have the following constructors:
A public no-args constructor that creates an Item with default values, item number = 0, item name = “” and item cost = 0.0
A public constructor that creates an item with the specified item number, item name and item cost
It should have the following public accessor (get) methods:
A method that returns the item number
A method that returns the item name
A method that returns the cost of the item
It should have the following public mutator (set) methods:
A method that changes the item cost to a specified new cost
It should have a toString() method that returns a string that describes the item, i.e. its number, name and cost.
Create a public class called TestMedicalItem with the following specifications:
It has a main method which creates 5 items with the following item numbers, item names and item costs:
1, “Bandage Gauze” , 0.50,
2, “Surgical Mask”, 1.00
3, “Tongue Depressors”, 0.75
4, “III Catheter”, 1.25
5, “IV Catheter”, 1.50
Displays the number, name and cost of the items by using the toString() method
Adds sales tax of 5% to the cost of each item and displays on screen the total amount for each item, i.e. item cost + sales tax
Explanation / Answer
Solution:
package com.nancy.chegg.qa;
public class MedicalItem {
private int number;
private String name;
private double cost;
MedicalItem() {
this.number = 0;
this.name = "";
this.cost = 0.0;
}
MedicalItem(int number, String name, double cost) {
this.number = number;
this.name = name;
this.cost = cost;
}
public int getNumber() {
return number;
}
public String getName() {
return name;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost + cost * 0.05;
}
//Calculating the total cost here
public double totalAmt(double cost) {
setCost(cost);
return getCost();
}
public String toString() {
return "num : " + number + " Name : " + name + " Cost : " + cost + " Total Cost : " + totalAmt(cost);
}
}
__________________________________________________________________________
package com.nancy.chegg.qa;
public class TestMedicalItem {
public static void main(String[] args) {
MedicalItem obj1 = new MedicalItem(1, "Bandage Gauze" , 0.50);
MedicalItem obj2 = new MedicalItem(2, "Surgical Mask", 1.00);
MedicalItem obj3 = new MedicalItem(3, "Tongue Depressors", 0.75);
MedicalItem obj4 = new MedicalItem(4, "III Catheter", 1.25);
MedicalItem obj5 = new MedicalItem(5, "IV Catheter", 1.50);
//implicitly call toString method.
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
System.out.println(obj4);
System.out.println(obj5);
}
}
_________________________________________________________________________
Sample run:
num : 1 Name : Bandage Gauze Cost : 0.5 Total Cost : 0.525
num : 2 Name : Surgical Mask Cost : 1.0 Total Cost : 1.05
num : 3 Name : Tongue Depressors Cost : 0.75 Total Cost : 0.7875
num : 4 Name : III Catheter Cost : 1.25 Total Cost : 1.3125
num : 5 Name : IV Catheter Cost : 1.5 Total Cost : 1.575
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.