Sunday, June 11, 2017

Sum of Two in Java Using Getters and Setters

In this article I would like to share with you a sample program that I wrote in Java to ask the user to give two numbers and then our program will compute the sum of two numbers using getters and setters in Java. The code is very simple and easy to understand the concepts of Object Oriented Programming in Java. I hope you will find my work useful. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.


Sample Program Output


Program Listing

SumTwo.java

// Written By: Mr. Jake R. Pomperada, MAED-IT
// Date : June 11, 2017  Sunday 12:07 AM
// Language: Java
// Place of Origin:  Mandaluyong City, Metro Manila Philippines.


package demo;

import java.util.Scanner;

class SumTwo {
public int a;
public int b;

public void setA(int a) {
this.a = a;
}
public void setB(int b) {
this.b = b;
}
public int getA() {
return a;
}
public int getB() {
return b;
}
public int getSum()
{
return(a+b);
}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
SumTwo val = new SumTwo(); 
  
      System.out.println();
      System.out.println("Sum of Two in Java Using Getters and Setters");
      System.out.println();
      System.out.print("Enter first value : ");
      val.a = input.nextInt();
      System.out.print("Enter second value : ");
      val.b = input.nextInt();

       val.setA(val.a);
   val.setB(val.b);
   val.getA();
   val.getB();
   val.getSum();
   
   System.out.println();
       System.out.print("The sum of " +val.a + " and " + val.b + " is " + val.getSum() + ".");
       System.out.println("\n");
       System.out.print("\t END OF PROGRAM");
       input.close();
}

}







No comments:

Post a Comment