Wednesday, March 28, 2018

PRIME NUMBER CHECKER USING EXCEPTION IN JAVA

A simple program to check if the given number by a user is prime number or not that have exception handling capabilities.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.


Sample Program Output


Program Listing

PrimeNumber.java


package exceptiondemo;

import java.util.Scanner;
import java.util.InputMismatchException;


/**
 * NetBeans IDE 8.2
 * @author Mr. Jake R. Pomperada
 * March 20, 2018  Tuesday
 * Bacolod City, Negros Occidental
 */
public class PrimeNumber {

   public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
               boolean flag = false;
               boolean goodData = false;
                
              while(!goodData) {
              try {
                System.out.print("PRIME NUMBER CHECKER USING EXCEPTION");
                System.out.println("\n");
                System.out.print("Give a number  : ");
int num_value =sc.nextInt();
                
                 for(int i = 2; i <= num_value/2; ++i)
        {
            // condition for nonprime number
            if(num_value % i == 0)
            {
                flag = true;
                break;
            }
        }

        if (!flag) {
            System.out.println("\n");
            System.out.println("The given " + num_value
                    + " is a Prime Number.");
        }
        else{
            System.out.println("\n");
            System.out.println("The given " + num_value
                    + " is Not a Prime Number.");
        }   
                
                System.out.println("\n");
goodData = true;
       
              } catch(InputMismatchException e) {
            
                sc.next();
                System.out.println("You Entered a Bad Data." );
                System.out.println("Please Try Again." );
                System.out.println("\n");
                }
            }  // while loop end
System.out.print("\t END OF PROGRAM");
        System.out.println("\n");
     }
}

 

AREA OF A CIRCLE USING EXCEPTION IN JAVA

A simple program that I wrote to compute the area of the circle with exception handling capabilities.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.



Sample Program Output


Program Listing

AreaCircle.java


package exceptiondemo;

import java.util.Scanner;
import java.util.InputMismatchException;
import java.text.DecimalFormat;

/**
 * NetBeans IDE 8.2
 * @author Mr. Jake R. Pomperada
 * March 20, 2018  Tuesday
 * Bacolod City, Negros Occidental
 */
public class AreaCircle {

    public static final DecimalFormat TWO_DECIMAL = new DecimalFormat(".##");
     
    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
      
               final double PI = 3.14159;
               boolean goodData = false;
                
              while(!goodData) {
              try {
                System.out.print("AREA OF A CIRCLE USING EXCEPTION");
                System.out.println("\n");
                System.out.print("What is the area?  : ");
double radius=sc.nextDouble();
                
                 double area = (PI * radius * radius);
                
                System.out.println("\n");
                System.out.println("The Area of Circle is " 
                        + TWO_DECIMAL.format(area) +".");
                System.out.println("\n");
goodData = true;
       
              } catch(InputMismatchException e) {
            
                sc.next();
                System.out.println("You Entered a Bad Data." );
                System.out.println("Please Try Again." );
                System.out.println("\n");
                }
            }  // while loop end
System.out.print("\t END OF PROGRAM");
        System.out.println("\n");
     }
}



ADDITION OF THREE NUMBERS USING EXCEPTION IN JAVA

In this article I wrote a program in Java to ask the user to give three numbers and the program will compute the sum of the three number with exception handling capabilities to check for invalid entries.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.



Sample Program Output


Program Listing

Addition.java


package exceptiondemo;


import java.util.Scanner;
import java.util.InputMismatchException;

/**
 * NetBeans IDE 8.2
 * @author Mr. Jake R. Pomperada
 * March 20, 2018  Tuesday
 * Bacolod City, Negros Occidental
 */
public class Addition {

   
    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
                int sum =0;
                boolean goodData = false;
                
              while(!goodData) {
              try {
                System.out.print("ADDITION OF THREE NUMBERS USING EXCEPTION");
                System.out.println("\n");
                System.out.print("Enter First Value  : ");
int num1=sc.nextInt();
                System.out.print("Enter Second Value : ");
int num2=sc.nextInt();
                System.out.print("Enter Third Value  : ");
int num3=sc.nextInt();
                 
                sum = (num1+num2+num3);
                System.out.println("\n");
                System.out.println("The sum of "+ num1 + ", " +num2 + " and " 
                        + num3 + " is " + sum +".");
                System.out.println("\n");
goodData = true;
       
              } catch(InputMismatchException e) {
            
                sc.next();
                System.out.println("You Entered a Bad Data." );
                System.out.println("Please Try Again." );
                System.out.println("\n");
                }
            }  // while loop end
System.out.print("\t END OF PROGRAM");
        System.out.println("\n");
     }
}

 

SQUARE AND CUBE A NUMBER USING EXCEPTION IN JAVA


In this article I would like to share with you a program that I wrote in Java that will ask the user to give a number and then our program will compute the square and cube of the given number I added exception handling routine to check for invalid entries.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.



Sample Program Output


Program Listing

 ExceptionDemo.java

package exceptiondemo;


import java.util.Scanner;
import java.util.InputMismatchException;

/**
 * NetBeans IDE 8.2
 * @author Mr. Jake R. Pomperada
 * March 20, 2018  Tuesday
 * Bacolod City, Negros Occidental
 */
public class ExceptionDemo {

   
    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
                boolean goodData = false;
                
              while(!goodData) {
              try {
                System.out.print("SQUARE AND CUBE A NUMBER USING EXCEPTION");
                System.out.println("\n");
                System.out.print("Give a Number : ");
int num=sc.nextInt();
                 
                System.out.println("Square of "+ num + " is: "+ Math.pow(num, 2));
System.out.println("Cube of "+ num + " is: "+ Math.pow(num, 3));
                System.out.println("\n");
goodData = true;
       
              } catch(InputMismatchException e) {
            
                sc.next();
                System.out.println("You Entered a Bad Data." );
                System.out.println("Please Try Again." );
                System.out.println("\n");
                }
            }  // while loop end
System.out.print("\t END OF PROGRAM");
        System.out.println("\n");
     }
}




CRUD in JDBC and MySQL

A simple CRUD program that I wrote using Java JDBC and MySQL. The code is very short and easy to understand.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.


Prorgam Listing

Jdbc.java

package com.jdbc.demo;

import java.sql.*;

public class Jdbc {

    private static Connection connection;

    // create table script
    // create table item(id int primary key auto_increment, name varchar(20), price double);

    public static void main(String[] args) throws Exception {

        String dbURL = "jdbc:mysql://localhost:3306/product";
        String username = "root";
        String password = "";

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            connection = DriverManager.getConnection(dbURL, username, password);

            if (connection != null) {
                System.out.println("Connected");
            }

            // insert record
            //insertItem("Pencil",5.50);
            //insertItem("Ballpen",9.00);

            //updateItem("Pencil",6.00);

            deleteItem("Pencil");
            // select records
            selectItems();


            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void deleteItem(String name) throws Exception {
        String sql = "DELETE FROM Item WHERE name=?";

        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, name);

        int rowsDeleted = statement.executeUpdate();
        if (rowsDeleted > 0) {
            System.out.println("An item was deleted successfully!");
        }
    }

    private static void updateItem(String name, Double price) throws Exception {
        String sql = "UPDATE Item SET price=? WHERE name=?";

        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setDouble(1, price);
        statement.setString(2, name);

        int rowsUpdated = statement.executeUpdate();
        if (rowsUpdated > 0) {
            System.out.println("An existing item was updated successfully!");
        }
    }
    private static void selectItems() throws Exception {
        String sql = "SELECT * FROM Item";

        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery(sql);

        int count = 0;

        while (result.next()){
            String name = result.getString(2);
            Double price = result.getDouble(3);

            String output = "Item #%d: %s - %s";
            System.out.println(String.format(output, ++count, name, price));
        }
    }

    private static void insertItem(String name, Double price) throws Exception {
        String sql = "INSERT INTO Item (name, price) VALUES (?, ?)";

        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, name);
        statement.setDouble(2, price);

        int rowsInserted = statement.executeUpdate();
        if (rowsInserted > 0) {
            System.out.println("A new item was inserted successfully!");
        }
    }



}


Friday, March 16, 2018

Swap of Two Numbers using Reference in C++

A very simple program that I wrote to show you how to swap two numbers using the concept of reference in C++.


I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.





Sample Program Output


Program Listing

swap.cpp


#include <iostream>

using namespace std;


        void swap(int &x, int &y);

       int main()
       {
          
         int x=0,y=0;
         
  cout << "Swap of Two Numbers using Reference in C++";
   cout << "\n\n";
   cout << "Created By Mr. Jake R. Pomperada, MAED-IT";
   cout << "\n\n";
   cout << "Enter values for x and y : ";
   cin >> x >> y;

       cout << "Main. Before swap, x: " << x << " y: " << y << "\n";
         swap(x,y);
         cout << "Main. After swap, x: " << x << " y: " << y << "\n";
        cout << "\n\n";
   cout << " End of Program ";  
           }


          void swap (int &ax, int &ay)
        {
             int temp;

                cout << "Swap. Before swap, ax: " << ax << " ay: " << ay << "\n";
                temp = ax;
                ax = ay;
              ay = temp;

                cout << "Swap. After swap, ax: " << ax << " ay: " << ay << "\n";
                
            }


Temperature Converter Using Function in C++


In this article I would like to share with you a sample program that I wrote using C++ to demonstrate how to use function to convert temperature the code is very easy to understand to use.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.




Sample Program Output


Program Listing


temp.cpp


#include <iostream>
#include <iomanip>

using namespace std;

     float Convert(float);
    int main()
     {
       float TempFer=0.00;
       float TempCel=0.00;
       cout << std::fixed;
       cout << std::setprecision(2);
   cout << "Temperature Converter Using Function in C++";
   cout << "\n\n";
   cout << "Created By Mr. Jake R. Pomperada, MAED-IT";
   cout << "\n\n";
       cout << "Enter temperature in Fahrenheit: ";
       cin >> TempFer;
       TempCel = Convert(TempFer);
       cout << "\nHere's the temperature in Celsius: ";
       cout << TempCel;
   cout << "\n\n";
   cout << " End of Program ";    
     }

    float Convert(float Fer)
     {
        float Cel;
        Cel = ((Fer - 32) * 5) / 9;
        return Cel;
   }

Monday, March 12, 2018

Table Creation and Insert of Record in SQLite

A very simple code to show you how to create a table and insert a record using SQLite. 

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.



Sample Program Output




Program Listing

--
-- File generated with SQLiteStudio v3.1.1 on Mon Mar 12 13:54:08 2018
--
-- Text encoding used: System
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;

-- Table: users
DROP TABLE IF EXISTS users;

CREATE TABLE users (
    user_id INTEGER    PRIMARY KEY,
    name    TEXT (100),
    age     INTEGER
);

INSERT INTO users (user_id, name, age) VALUES (1, 'jake', 39);
INSERT INTO users (user_id, name, age) VALUES (2, 'iya', 3);
INSERT INTO users (user_id, name, age) VALUES (3, 'allie', 46);
INSERT INTO users (user_id, name, age) VALUES (4, 'jacob', 4);

-- Index: 
DROP INDEX IF EXISTS "";

CREATE INDEX "" ON users (
    user_id
);


COMMIT TRANSACTION;
PRAGMA foreign_keys = on;




Friday, March 9, 2018

Sum of a Number in Java

I wrote this program to ask the user to give a series of numbers and then our program will sum up all the numbers using Java as my programming language.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.






Sample Program Output


Program Listing

sum.java


import java.util.Scanner;

class sum {

public static void main(String[] args) {
   Scanner input = new Scanner(System.in);

   System.out.print("Sum of a Number in Java");
   System.out.print("\n\n");
   System.out.print("Created By Mr. Jake R. Pomperada");
   System.out.print("\n\n");
  System.out.print("Give a Number : ");
  int n = input.nextInt();

 int number = n;
 int sum = 0;
 int num = number;

while (num > 0) {
        int lastDigit = num % 10;
        sum += lastDigit;
        num /= 10;
    }
    System.out.print("\n\n");
    System.out.println("Sum of digits is  "+sum +".");
    System.out.print("\n\n");
}

}