Java Program to test Constructors and methods of a class

Define a class called Product, each product has a name, a product code and manufacturer name.
define variables, methods and constructors, for the Product class. Write a class called
TestProduct, with the main method to test the methods and constructors of the Product class.

Program: 

public class TestProduct
{
  public static void main(String args[])
  {
    System.out.println("P code \t Product Name \t Manufc Name");

    Product hp = new Product();

    Product silver5 = new Product(12345, "mobile", "Micromax");
   
    hp.func("Disk Drive");                        //if function is static use Classname.func()
                            //if function is non static use Classobject.func()

    hp.func(45);
  }
}


class Product
{

    int pCode;
    String pName, mName;
    static int c = 1000;
        public Product()
    {
        pCode = 1234;
        pName = "Laptop";
        mName = "Lenovo";
        System.out.println(pCode+"\t"+pName+"\t\t"+mName);
    }
    public Product(int code, String itemName, String manName)
    {
        pCode = code;
        pName = itemName;
        mName = manName;
        c++;
        display();
    }

    public void func(String name)
    {
       
        pCode = 2345;
        pName = name;
        mName = "Acer";
        display();
    }

    public void func(int pc)
    {
        pCode = pc;
        pName ="Pencil";
        mName ="Apsara";
        display();
    }
   
    void display()
    {
        System.out.println(pCode+"\t"+pName+"\t\t"+mName);
    }
}

Comments

Popular Posts