Sunday, May 7, 2017

Addition of Five Numbers in Java Using Getters and Setters

Here is a sample program that I wrote using Java to ask the user to give five numbers and then our program will compute for the total sum of the five numbers given by our user. I'm using object oriented approach and using getters and setters in Java. I hope you will find my work useful. I am using Eclipse Neon as my text editor and Jave 8 as my version of Java. 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

Addition.java

package demo;

import java.util.Scanner;

public class Addition {

public int a=0,b=0,c=0,d=0,e=0;
private int getA() {
return a;
}
private void setA(int a) {
this.a = a;
}
private int getB() {
return b;
}
private void setB(int b) {
this.b = b;
}
private int getC() {
return c;
}
private void setC(int c) {
this.c = c;
}
private int getD() {
return d;
}
private void setD(int d) {
this.d = d;
}
private int getE() {
return e;
}
private void setE(int e) {
this.e = e;
}
public int getResults(){
return(a+b+c+d+e);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Addition val = new Addition(); 
  
       System.out.println();
       System.out.println("Addition of Five Numbers 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();
       System.out.print("Enter third value : ");
       val.c = input.nextInt();
       System.out.print("Enter fourth value : ");
       val.d = input.nextInt();
       System.out.print("Enter fifth value : ");
       val.e = input.nextInt();
   val.setA(val.a);
   val.setB(val.b);
   val.setC(val.c);
   val.setD(val.d);
   val.setE(val.e);
     
   val.getA();
   val.getB();
   val.getC();
   val.getD();
   val.getE();
   
   val.getResults();
   
 System.out.println();
        System.out.print("The total sum is " + val.getResults() + ".");
        System.out.println("\n");
        System.out.print("\t END OF PROGRAM");
        input.close();
}

}



No comments:

Post a Comment