231k views
3 votes
g You have a class called Product (with both an empty constructor and a second constructor that takes a quantity and a price as parameters.) It has a quantity and price as internal data and has get() and set() methods for them. Write the code to create two objects of type Product, and set their two data values to some test data of your choosing. (4 pojnts) --below that also do this-- Later on in the program you created with the code from the question above, you need to display the items for the user. Write the lines of code to display the two Product objects data (that you created above) to the user. (4 points)

User Ylu
by
3.3k points

1 Answer

2 votes

Answer:

The answer using java programming language is in the explanation section

See detailed explanations in the comments within the code

Step-by-step explanation:

//The class Product begins here

public class Product {

private int quantity;

private double price;

//First Constructor No argument

public Product() {

}

//Second Constructor Two argument

public Product(int quantity, double price) {

this.quantity = quantity;

this.price = price;

}

//Get Methods

public int getQuantity() {

return quantity;

}

public double getPrice() {

return price;

}

//Set Methods

public void setQuantity(int quantity) {

this.quantity = quantity;

}

public void setPrice(double price) {

this.price = price;

}

}

//End of Product Class

//Test class containing the main method begins here

class ProductTest{

public static void main(String[] args) {

//Creating two objects of Product

Product productOne = new Product(1000, 50.5);

Product productTwo = new Product (5000, 60);

//Displaying the product object's data

System.out.println("Product one quantity is "+productOne.getQuantity()+" and the unit price is" +

"is "+productOne.getPrice());

System.out.println("Product Two quantity is "+productTwo.getQuantity()+" and the unit price is" +

"is "+productTwo.getPrice());

}

}

User Sherene
by
3.3k points