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

use java please Implement a class according to its UML class symbol: Product -na

ID: 3826855 • Letter: U

Question


use java please

Implement a class according to its UML class symbol: Product -name: String -price: double -scanCode: int -numObjects: int = 0 +Product (String, double) +getName(): String +getScanCode: int +getPrice(): double +changePrice (double): void +getNumObjects(): int +getNumberInstock(): int The constructor shall take two parameters to initialise the name and price fields. The variable numObjects shall be incremented by the constructor every time a new object is instattiated. The scanCode is a 4-digit integer value 1000 +numObjects initialised by the constructor. Test the defined class with the following program: class TestProduct {public static void main (String[] args) {System.out.println ("There are " + Product.getNumberInStock() + "items"); Product pr1 = new Product ("Computer", 1500.0); System.out.println ("There are" + Product.getNumberInstock() + "items"); Product pr2 = new Product ("Printer", 600.0); Product pr3 = new Product ("Monitor", 240.0); System.out.println ("There are" + Product.getNumberInstock() + "items"); Product pr2 = new Product ("Printer", 600.0); Product pr3 = new Product ("Monitor", 240.0); System.out.println ("There are" +Product.getNumberInstock () + "items"); pr2.changePrice (550.0); System.out.println("-- Product info -"); System.out.println (" Name: " + pr2. getName ()); System.out.println (" Scan code: " + pr2.getNumObjects ()); System.out.println ("price: " + pr2.getPrice ());}

Explanation / Answer


public class Product {
   private String name;
   private double price;
   private int scancode;
   private static int numOfObjects;
  
   public Product(){
       setNumOfObjects(0);
   }

   public Product(String name,double price){
       setNumOfObjects(getNumOfObjects()+1);
       setName(name);
       setPrice(price);
       setScancode(1000+getNumOfObjects());
   }
   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;
   }

   public int getScancode() {
       return scancode;
   }

   public void setScancode(int scancode) {
       this.scancode = scancode;
   }

   public int getNumOfObjects() {
       return numOfObjects;
   }

   public void setNumOfObjects(int numOfObjects) {
       Product.numOfObjects = numOfObjects;
   }
}

class TestProcuct{
  
   public static void mian(String[] argv){
       System.out.println("There are : "+Product.getNumberInStock()+" items");
      
       Product pr1 = new Product("computer", 1500.0);
       System.out.println("There are : "+Product.getNumberInStock()+" items");
          
       Product pr2 = new Product("Printer", 600.0);
       Product pr3 = new Product("Moniter", 240.0);
       System.out.println("There are : "+Product.getNumberInStock()+" items");
      
       pr3.setPrice(550.0);
      
       System.out.println("-- Product info --");
       System.out.println("Name : "+pr2.getName());
       System.out.println("Scan Code : "+pr2.getScancode());
       System.out.println("Price : "+pr2.getPrice());
      
   }
}