Monday, August 14, 2017

Division of Two Numbers in Java Using Inner Class

In this article I would like to share with you how to create inner classes in Java. Inner Classes are very fundamental concepts in object oriented programming in Java programming it makes our code more easier to understand and secure. In this example our program will ask the user to give two numbers and our program will compute the quotient of the two numbers. The code is written using Netbeans IDE that is free to download over the I hope you will find my work useful. Thank you.

I hope you will find my work useful and beneficial. If you have some questions about programming, about my work please send mu an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com. People here in the Philippines can contact me at  my mobile number 09173084360.

Thank you very much and Happy Programming.







Sample Program Output


Program Listing

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package inner;

/**
 *
 * @author jake.r.pomperada
 */


import java.util.Scanner;

class Outer_Demo {
   int num1=0,num2=0;
   
   Scanner input = new Scanner(System.in);
   
   // inner class
   private class Inner_Demo {
      public void print() {
         System.out.print("===== Division of Two Numbers in Java Using Inner Class =====");
         System.out.print("\n\n");
         System.out.print("Enter First  Value : ");
         num1 = input.nextInt();
         System.out.print("Enter Second Value : ");
         num2 = input.nextInt();
         // perform division here
         num1/=num2;
         System.out.print("\n\n");
         System.out.println("The Division of two numbers is: "+num1 +".");
         System.out.print("\n\n");
         System.out.print("===== END OF PROGRAM =====");
         System.out.print("\n\n");
      }
   }
   
   // Accessing he inner class from the method within
   void display_Inner() {
      Inner_Demo inner = new Inner_Demo();
      inner.print();
   }
}
   
public class Inner {

   public static void main(String args[]) {
      // Instantiating the outer class 
      Outer_Demo outer = new Outer_Demo();
      
      // Accessing the display_Inner() method.
      outer.display_Inner();
   }
}

No comments:

Post a Comment