Skip to main content

Creating Class with attributes and method and using them- Example0006

Creating Class with attributes and method and using them- Example0006
This example shows creating own Class
We will create a class called 'Product' with Attribute, Contructor and Methods
We will see how we can create an Object of that Class and use the methods in that Class. 
We can see two ways to access the attributes of the Class

Keyword: java, class, attribute, constructor, methods, object, instance

Creating own Class and use it
======================
package com.swprogramdeveloper;

//Textual presentation of an object, how it will look like in the memory
//Whatever we write in class is actually belong to object. Note: if you want something for class, need to write static
class Product {
    //Attribute
        int pid;
        String name;
        int price;

        //Constructor
        Product() {
        System.out.println(">>Product Object Constructed");
        }

        //Methods
        //Method one to initialize data
         void setProductDetails(int pid, String name, int price){
         this.pid=pid;
         this.name=name;
         this.price=price;
         }

        //Method to display data
         void showProductDetails(){
             System.out.println("-----Product ID: "+pid+"----------");
             System.out.println("Name:\t"+name);
             System.out.println("Price:\t"+price);
             System.out.println("---------------------------");
         }
        }

public class Main {

    public static void main(String[] args) {
// write your code here
        System.out.println("This is Main inside the Class Main");
     //Creating Object- Class Instance with New keyword
        Product product1 = new Product();
        product1.setProductDetails(1,"iphone",70000);
        product1.showProductDetails();
        System.out.println("Address of Product1: " + product1);

        // Accessing parameters directly
//We can access the attribute directly like following
        Product product2 = new Product();
        product2.pid=201;
        product2.name="Nike";
        product2.showProductDetails();
        System.out.println(product2);


    }
}



Output0006
========

This is Main inside the Class Main
>>Product Object Constructed
-----Product ID: 1----------
Name: iphone
Price: 70000
---------------------------
Address of Product1: com.swprogramdeveloper.Product@34c45dca
>>Product Object Constructed
-----Product ID: 201----------
Name: Nike
Price: 0
---------------------------
com.swprogramdeveloper.Product@52cc8049

Process finished with exit code 0

Comments

Popular posts from this blog

Java Program to see how to use Arrays- Example0003

Java Program to see how to use Arrays- Example0003 Example0003 =========== Description: In this example we learn to create Arrays in different  We will also see lots of methods like sort, length. We will see how to initialize data in the array. We will see how to display content of single dimension and multidimensional arrays. //Arrays package com.swprogramdeveloper; import java.awt.*; import java.util.Arrays; import java.util.Date; public class Main {     public static void main(String[] args) { // write your code here     //Arrays     int[] numbers1 = new int[5];     numbers1[0] = 5;     numbers1[3] = 9;         System.out.println("Address where valuese of numbers1 array stored : " +  numbers1);         System.out.println("Size of Array numbers1 : " + numbers1.length);         System.out.println("Content of Array numbers1 : " + Arrays.toString(numbers1));         System.out.println("Content of certain member of Array :

Basic Java Program with Variable Introduction and Class Introduction - Example0001

Basic Java Program with Variable Introduction and Class Introduction - Example0001  Example1: ========= This is example of a basic java program which shows structure of the program. It is basic java program showing different type of Java variables. Primitive types: Integer, float, double, Boolean are shown Creating an object. Reference types and methods to that object. How to print output in a screen is shown using System Class package com.swprogramdeveloper; import java.util.Date; public class Main {     public static void main(String[] args) {  // write your code here      //variable types double and integer         double age = 30;         int herage = (int) age + 20;   //Printing output using System Class         System.out.println("Hello World "+age + "---" + herage);      //float type. Note the F- it is required as the compiler will take double as default and will show error if 'F' not given         float viewCount= 567.23F;

Mortgage Calculator with User Input and Mathematical Expression - Example0005

Mortgage Calculator with User Input and Mathematical Expression - Example0005 In this code sample, we will see how mortgage(EMI) calculator can be made Mortgage formula M=P * (r(1+r)**n)/((1+r)**n-1) will be used. Scanner Class will be used to get user input. We need to get the rightly formatted output (with dollar sign as currency). For this purpose we will use Class NumberFormat:NumberFormat.getCurrencyInstance. We use NumberFormat.getCurrencyInstance method. Keyword: java, mortgage calculator, user input, scanner, currency format, dollar sign, numberformat, getcurrencyinstance package com.swprogramdeveloper; import java.text.NumberFormat; import java.util.Scanner; public class Main {     public static void main(String[] args) { // write your code here         //Mortgage Calculator         Scanner userInput = new Scanner(System.in);         System.out.print("Please Input the Principal Amount : ");         float principal = userInput.nextFloat();