Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement class design: Product and TestProduct You will create the Product clas

ID: 3886251 • Letter: I

Question

Implement class design: Product and TestProduct

You will create the Product class and run with the test program. This class must implement the model in the following UML class diagram.

1. declare class fields to store the product name, price, product ID and total number of products;
2. implement a constructor that takes two parameters that populates the name and price fields; the variable total is incremented by one; the productID is populated by the current value of total;
3. implement a method getName that retrieves the product name;
4. implement a method getProductID that retrieves the product ID;
5. implement a method getPrice that retrieves the product price;
6. implement a method changePrice that changes the product price to the new price received as a parameter;
7. implement a method getTotal that retrieves the total number of products.

Design class TestProduct. Compile and run TestProduct program. You should have the following output:

Just need it done simple and to what the instructions ask :) No need for anything different or out of the ordinary, thanks in advance :)

Product -name: String price: double -productID: int total: int +Product (String, double) +getName ): string +getProductID () : int +getPrice ) :double +changePrice (double) : void +getTotal ): int

Explanation / Answer

Source Code:

import java.util.*;

import java.lang.*;

import java.io.*;

class Product

{

private String Name;

private double Price;

private int productId;

private static int total;

public Product(String name,double price)

{   

total=total+1;

Name=name;

Price=price;

}

public String getName()

{

return Name;

}

public double getPrice()

{

return Price;

}

public int getProductId()

{

return total;

}

public int getTotal()

{

return total;

}

}

class TestProduct

{

public static void main (String[] args) throws java.lang.Exception

{

String name;

double price;

int choice;

Scanner sc=new Scanner(System.in);

Product objProduct=new Product("Milk",2.6);

System.out.println("*** You have "+ objProduct.getTotal()+" Products Now");

System.out.println("*** You have "+ objProduct.getName()+"(ID="+objProduct.getProductId()+") It's price is $"+objProduct.getPrice());

Product objProduct1=new Product("Apple",6.0);

System.out.println("*** You have "+ objProduct.getTotal()+" Products Now");

System.out.println("*** You have "+ objProduct1.getName()+"(ID="+objProduct1.getProductId()+") It's price is $"+objProduct1.getPrice());

}

}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote