Saturday, August 26, 2017

Product of Two in Java Using Getters and Setters

A very simple program that I wrote in Java to demonstrate how to use getters and setters concepts in object oriented programming. The code will ask the user to give two numbers and then our program will compute that product of the two numbers given by our users. 

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


Product_Two.java


// Written By: Mr. Jake R. Pomperada, MAED-IT
// Date : August 26, 2017  Sunday 7:36 AM
// Language: Java
// Place of Origin:  Mandaluyong City, Metro Manila Philippines.


package product_number;

import java.util.Scanner;

public class Product_Two {

int val_a;
int val_b;
public int getVal_a() {
return val_a;
}


public void setVal_a(int val_a) {
this.val_a = val_a;
}


public int getVal_b() {
return val_b;
}


public void setVal_b(int val_b) {
this.val_b = val_b;
}


public int solveProduct()
{
return(val_a * val_b);
}
public static void main(String[] args) {
  
Scanner input = new Scanner(System.in);
  
  Product_Two num = new Product_Two(); 
    
       System.out.println();
       System.out.println("Product of Two in Java Using Getters and Setters");
       System.out.println();
       System.out.print("Enter first value : ");
       num.val_a = input.nextInt();
       System.out.print("Enter second value : ");
       num.val_b = input.nextInt();

        num.setVal_a(num.val_a);
        num.setVal_b(num.val_b);
        num.getVal_a();
        num.getVal_b();
        
        num.solveProduct();
     
        System.out.println();
        System.out.print("The product of " +num.val_a + " and " + num.val_b + " is " + num.solveProduct() + ".");
        System.out.println("\n");
        System.out.print("\t END OF PROGRAM");
        input.close();
}

}



No comments:

Post a Comment