131k views
5 votes
Implement a class Product. A product has a name and a price, for example new Product("Toaster", 29.95). Supply methods getName, getPrice. Supply a program ProductDemo that makes two products and prints each name and price.

User Pranag
by
3.8k points

2 Answers

5 votes

Answer:

================================================================

//Create class definition for Product class

public class Product {

//Product class has a name and a price property

//Declare those properties as follows

String name;

double price;

//The Product class has a constructor to initialize the name and price

//properties .

//Declare the constructor as follows

public Product(String name, double price){

//Initialize the variables

this.name = name;

this.price = price;

}

//Write a getName method to retrieve the name of a Product object

public String getName() {

//This method returns the name value of the Product object

return name;

}

//Write a getPrice method to retrieve the price of a Product object

public double getPrice() {

//This method returns the price value of the Product object

return price;

}

}

===============================================================

//Create the definition for the ProductDemo class

public class ProductDemo {

//Create a main method for the application

public static void main(String[] args) {

//Create an object of the Product class

Product product1 = new Product("Toaster", 29.95);

//Create another object of the Product class

Product product2 = new Product("Machine", 89.89);

//Print out the name of the first product

System.out.println("Name of product 1 " + product1.getName());

//Print out the price of the first product

System.out.println("Price of product 1 " + product1.getPrice());

//Print out the name of the second product

System.out.println("Name of product 2 " + product2.getName());

//Print out the price of the second product

System.out.println("Price of product 2 " + product2.getPrice());

} // End of main method

} // End of class definition

=================================================================

Sample Output:

>> Name of product 1 : Toaster

>> Price of product 1 : 29.95

>> Name of product 2 : Machine

>> Price of product 2 : 89.89

=================================================================

Step-by-step explanation:

The above program has been written in Java and contains comments explaining every part of the program. Please go through the comments.

The program contains two classes and these classes have been written above. The first class is the Product class and the second is the ProductDemo class.

The actual lines of the code have been written in bold face to enhance readability and distinguish from comments.

Sample output of a run of the program has been provided also.

User Satish Reddy
by
4.2k points
2 votes

Answer:

class Product{

private String name;

private double price;

public Product(String name, double price) {

this.name = name;

this.price = price;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

}

Step-by-step explanation:

The class Product is implemented above in Java with the set and get methods as well the constructor.

The ProductDemo program is given below:

public class num9 {

public static void main(String[] args) {

Product product1 = new Product("Paties", 4.55);

Product product2 = new Product ("jamies", 5.99);

System.out.println("Product 1 is "+product1.getName()+" The price is " +

""+product1.getPrice());

System.out.println("Product 2 is "+product2.getName()+" The price is " +

""+product2.getPrice());

}

}

User AndriusZ
by
4.6k points