
Concept explainers
7.23 LAB: Product class
Given main(), define the Product class (in file Product.java) that will manage product inventory. Product class has three private member fields: a product code (String), the product's price (double), and the number count of product in inventory (int).
Implement the following Constructor and member methods:
- public Product(String code, double price, int count) - set the member fields using the three parameters
- public void setCode(String code) - set the product code (i.e. SKU234) to parameter code
- public String getCode() - return the product code
- public void setPrice(double p) - set the price to parameter p
- public double getPrice() - return the price
- public void setCount(int num) - set the number of items in inventory to parameter num
- public int getCount() - return the count
- public void addInventory(int amt) - increase inventory by parameter amt
- public void sellInventory(int amt) - decrease inventory by parameter amt
Ex. If a new Product object is created with code set to "Apple", price set to 0.40, and the count set to 3, the output is:
Name: AppleEx. If 10 apples are added to the Product object's inventory, but then 5 are sold, the output is:
Name: AppleEx. If the Product object's code is set to "Golden Delicious", price is set to 0.55, and count is set to 4, the output is:
Name: Golden Deliciouspublic class LabProgram {
public static void main(String args[]){
String name = "Apple";
double price = 0.40;
int num = 3;
Product p = new Product(name, price, num);
// Test 1 - Are instance variables set/returned properly?
System.out.println("Name: " + p.getCode()); // Should be 'Apple'
System.out.printf("Price: %.2f\n", p.getPrice()); // Should be '0.40'
System.out.println("Count: " + p.getCount()); // Should be 3
System.out.println();
// Test 2 - Are instance variables set/returned properly after adding and selling?
num = 10;
p.addInventory(num);
num = 5;
p.sellInventory(num);
System.out.println("Name: " + p.getCode()); // Should be 'Apple'
System.out.printf("Price: %.2f\n", p.getPrice()); // Should be '0.40'
System.out.println("Count: " + p.getCount()); // Should be 8
System.out.println();
// Test 3 - Do setters work properly?
name = "Golden Delicious";
p.setCode(name);
price = 0.55;
p.setPrice(price);
num = 4;
p.setCount(num);
System.out.println("Name: " + p.getCode()); // Should be 'Golden Delicious'
System.out.printf("Price: %.2f\n", p.getPrice()); // Should be '0.55'
System.out.println("Count: " + p.getCount()); // Should be 4
}
}

Trending nowThis is a popular solution!
Step by stepSolved in 5 steps with 3 images

First time on this site. I don't see the anwser to this problem. Has it been anwsered?
First time on this site. I don't see the anwser to this problem. Has it been anwsered?
- Only Number 21arrow_forward4:12 ull ? AA A 1-xythos.content.blackboardcdn.com ITCS114 Assignment 2 Part (1) Create a class named Item, that includes two private members: Type (String), and Price(double). The Item class should also include: one set method, two get methods and default constructor to initialize Type to "None" and Price to 0.0. Part(2) Write another java program to do the following: a. Write a static method named findMax that takes as parameter an array of type Item. The method should find and output the Type of the item with the maximum price of all items in the array. The method prototype is: public static void findMax (Item [] list) b. Write the main method to do the following: Declare an array of type Item of size 5; 1. Ask the user to enter type and price for each item in the array 2. 3. Call the findMax method.arrow_forwarda. Write a class BumbleBee that inherits from Insect. Add the public member function void sting() const . This function simply prints "sting!" and a newline. b. Write a class GrassHopper that inherits from Insect. Add the public member function void hop() const . This function simply prints "hop!" and a newline. When you are done your program output should look exactly like the output provided at the end of the file. Do not modify the main program. Do not modify the Insect class.arrow_forward
- Instructions-Java Assignment is to define a class named Address. The Address class will have three private instance variables: an int named street_number a String named street_name and a String named state. Write three constructors for the Address class: an empty constructor (no input parameters) that initializes the three instance variables with default values of your choice, a constructor that takes the street values as input but defaults the state to "Arizona", and a constructor that takes all three pieces of information as input Next create a driver class named Main.java. Put public static void main here and test out your class by creating three instances of Address, one using each of the constructors. You can choose the particular address values that are used. I recommend you make them up and do not use actual addresses. Run your code to make sure it works. Next add the following public methods to the Address class and test them from main as you go: Write getters and…arrow_forward3- Write a program to implement a class "student" with the following members. Name of the student. Marks of the student obtained in three subjects. Function to assign initial values. Function to compute total average. Function to display the student's name and the total marks. Write an appropriate main() function to demonstrate the functioning of the above.arrow_forwardJavaarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





