1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ray Of Light [21]
2 years ago
14

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.
Computers and Technology
2 answers:
IrinaK [193]2 years ago
6 0
<h2>Answer:</h2><h2></h2>

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

//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

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

<h2 /><h2>Sample Output:</h2><h2 />

>> Name of product 1 : Toaster

>> Price of product 1 : 29.95

>> Name of product 2 : Machine

>> Price of product 2 : 89.89

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

<h2>Explanation:</h2>

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 <em>Product</em> class and the second is the <em>ProductDemo</em> 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.

soldier1979 [14.2K]2 years ago
4 0

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;

   }

}

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());

}

}

You might be interested in
The first person to say something in here will get brainliested.
Stels [109]

something

xyjxyjmhcfgjhnmckghnm vgj,

8 0
3 years ago
Read 2 more answers
A cell reference that has only one $ is referred to as a(n) ____ cell reference. alternative mixed relative absolute
Allushta [10]
An absolute cell reference.
4 0
3 years ago
Which type of password would be considered secure
max2010maxim [7]
Try using at least one capital letter and multiple number and symbols
Example: [email protected]#3
5 0
3 years ago
Read 2 more answers
Which engineer may design a GPS for a vehicle?
Crank

Explanation:

Mechanical engineer may design a gps for a vehicle.

4 0
2 years ago
User Interface Design ensures that the interface has elements that are easy to ________________. (Choose all that apply)
Gnesinka [82]

Answer:

A, C, D

Explanation:

User Interface (UI) Design focuses on anticipating what users might need to do and ensuring that the interface has elements that are easy to access, understand, and use to facilitate those actions.

4 0
3 years ago
Read 2 more answers
Other questions:
  • Top/bottom rules allows user to apply conditional formatting cells that fall within the top or bottom numbers or percentile. how
    7·1 answer
  • Which guideline should you use when downloading software from the Internet?
    15·1 answer
  • What is not true about contracts?
    12·2 answers
  • To display the Color gallery, with the graphic selected, click the Color button in the ____ group on the PICTURE TOOLS FORMAT ta
    14·1 answer
  • If you want the input gear to spin in the same direction as the output gear you must use a _______ gear.
    13·1 answer
  • Add me on Fortn!te <br> TsarBacon (imma girl)
    11·1 answer
  • QUESTION 10
    11·1 answer
  • Do small companies need computers? why?<br>​
    12·1 answer
  • What is output?<br> x = 2<br> y = 3<br> print (x * y + 2)<br> 4<br> 2<br> 8<br> 10
    7·1 answer
  • Mack signs into his laptop and gets a message saying all his files have been encrypted and will only be released if he sends mon
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!