Thursday, July 30, 2015

Age Calculator in Java

I have wrote a simple program that will ask the user its birth month, birth day and birth year and then the program will check and determine the present age of the user. The code uses calendar libraries in Java programming language.

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

 // age_calculator.java
// Programmer : Mr. Jake R. Pomperada, MAED-IT
// Date       : July 30, 2015
// Tools      : Netbeans 8.0.2
// Email      : jakerpomperada@yahoo.com and jakerpomperada@gmail.com
// Write a program that will determine the age of the person.


 import java.util.Scanner;  
 import java.util.GregorianCalendar;  
 import java.util.Calendar;  
   
 public class age_calculator {  
   
   private static int age_checker(int y, int m, int d) {  
     Calendar cal = new GregorianCalendar(y, m, d);  
     Calendar now = new GregorianCalendar();  
     
     int age_result = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);  
     if((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))  
       || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH)  
       && cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH)))  
     {  
        age_result--;  
     }  
     return age_result;  
   }  
   
   public static void main(String [] args) {  

      Scanner input = new Scanner(System.in); 
        char ch;
    do {
      int month=0,day=0,year=0;
      System.out.println();
      System.out.print("\t Age Checker Program ");
      System.out.println("\n");
      System.out.print("Enter Birth Month : "); 
      month = input.nextInt();
      System.out.print("Enter Birth Day   : "); 
      day = input.nextInt();
      System.out.print("Enter Birth Year  : "); 
      year = input.nextInt();
      System.out.println("Your present age is " 
              + age_checker(year,month,day)+ " years old.");
      System.out.println();
      System.out.print("\nDo you want to continue (Type y or n) : ");
      ch = input.next().charAt(0);                        
     } while (ch == 'Y'|| ch == 'y');     
      System.out.println();
      System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
      System.out.println("\n\n");
    }
 }   


Sunday, July 26, 2015

Smallest Number in Java

In this simple program that I wrote it will allows the user to give a series of numbers and then it will check and display the smallest number from the given number by the user using Java as our programming language.

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

/*
smallest_number_array.java
Programmer By: Mr. Jake R. Pomperada, MAED-IT
Date : July 23, 2015
Tools: NetBeans 8.0.2, Java SE 8.0

Problem No. 7:
  
Write a program that will ask the user to give five numbers and then
it will search for the smallest number in the given list from the user.
user.
*/
              

import java.util.Scanner;

class smallest_number_array{
    
public static void main(String[] args) {
      
     Scanner input = new Scanner(System.in);
     char ch;
                   
  do { 
      int [] values; 
      values  = new int[5];  
      int x=0;
      int smallest = Integer.MAX_VALUE;
      System.out.print("SMALLEST NUMBER PROGRAM");
      System.out.println();
      for (int a=0; a<5; a++)
      {
         x=1;
         x+=a;
         System.out.print("Enter value in item no. " + x + " : ");
         values[a] = input.nextInt();
        }       
         System.out.println(); 
         for(int a =0;a<5;a++) {
           if(smallest > values[a]) 
           {
            smallest = values[a];
            }
        }  
     System.out.println("The smallest number is " + smallest +".");
      System.out.print("\nDo you want to continue (Type y or n) : ");
     ch = input.next().charAt(0);                        
     } while (ch == 'Y'|| ch == 'y');  
     System.out.println();
     System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
     System.out.println("\n\n");
  }
} // End of Code
    


Square and Cube Number Program in Java

This simple program  ask the user to enter a number and then our program will generate the corresponding square and cube value of the given number using for loop statement in Java. The code is very basic for beginners that are new in Java programming.

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 Prorgam Output

Program Listing

/*
square_array.java
Programmer By: Mr. Jake R. Pomperada, MAED-IT
Date : July 23, 2015
Tools: NetBeans 8.0.2, Java SE 8.0

Problem No. 6:
  
Write a program that will ask the user to give five numbers and then
it will display the square and cube value of the given number by the
user.
*/
              

import java.util.Scanner;

class square_array{
    
public static void main(String[] args) {
      
     Scanner input = new Scanner(System.in);
     char ch;
        
  do { 
      int [] values; 
      values  = new int[5];  
      int x=0;
      System.out.print("SQUARE AND CUBE NUMBER PROGRAM");
      System.out.println();
      for (int a=0; a<5; a++)
      {
         x=1;
         x+=a;
         System.out.print("Enter value in item no. " + x + " : ");
         values[a] = input.nextInt();
        }       
         System.out.println("\n\n"); 
         System.out.print("\tNUMBER     SQUARE      CUBE ");
         for (int a=0; a<5; a++)
          {
             x=1;
             x+=a;
             int square = (values[a] * values[a]);
             int cube  = (values[a] * values[a] * values[a]);
             System.out.println(); 
             System.out.println("\t  "+x+"\t      " + square + " \t" +cube);
          }
     System.out.println(); 
     System.out.print("\nDo you want to continue (Type y or n) : ");
     ch = input.next().charAt(0);                        
     } while (ch == 'Y'|| ch == 'y');  
     System.out.println();
     System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
     System.out.println("\n\n");
  }
} // End of Code
    

Saturday, July 25, 2015

Addition of Five Numbers Using Java Servlet

Learning to program in Java is a journey not a destination.  This program will allow the user to enter five numbers in a form and then it will display the total sum of five numbers using Java Servlet.

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

index.html

<!DOCTYPE html>
<!--
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.
-->
<html>
    <head>
        <title>Addition of Five Numbers</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
       body {
           background:lightgreen; 
           font-family: arial;
        }
        h2 {
            font-family: arial;
        }  
    </style>  </head>  
    <body>
  <h2> Addition of Five Numbers in Java Servlet </h2>
<form name='addition' action='http://localhost:8080/WebApplication1/addition' method='get'>
Enter First  Value <input type='text' name='value1'><br>
Enter Second Value <input type='text' name='value2'><br>
Enter Third  Value <input type='text' name='value3'><br>
Enter Fourth Value <input type='text' name='value4'><br>
Enter Fifth Value <input type='text' name='value5'><br><br>
<input type='submit' name='submit' value='Add Values' title='Click here to find the sum.'>
</form>
</body>
</html>


additional.java

/*
 * 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.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author jake.r.pomperada
 */
@WebServlet(urlPatterns = {"/addition"})
public class addition extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException , ServletException
{
PrintWriter pr=res.getWriter();
res.setContentType("text/html");
try
{
int a=Integer.parseInt(req.getParameter("value1"));
int b=Integer.parseInt(req.getParameter("value2"));
int c=Integer.parseInt(req.getParameter("value3"));
int d=Integer.parseInt(req.getParameter("value4"));
int e=Integer.parseInt(req.getParameter("value5"));

int total_sum=(a+b+c+d+e);
pr.println("<body bgcolor='lightgreen'>");    
pr.println("<h1> THE RESULT </h1>");
pr.println("<br>");
pr.println("<h2> The sum of " +a + "," 
          + b + ", " + c + ", " + d + ", "
          + e + " is "+ total_sum +". </h2");
pr.println("</body>");

}
catch(Exception e)
{
pr.println("<body bgcolor='lightgreen'>");    
pr.println("<font face='aria'>Invalid Input Value Try Again");
pr.println("</font> </body>");
}
} }

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>addition</servlet-name>
        <servlet-class>addition</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>addition</servlet-name>
        <url-pattern>/addition</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file> index.html</welcome-file>
     </welcome-file-list>   
</web-app>




Sum of Numbers Using While Loop

A simple program that will sum up the  series of number using while looping statement in Java.

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

/*
while_loops.java
Programmer By: Mr. Jake R. Pomperada, MAED-IT
Date : July 21, 2015

Problem No. 1:
  
Design a program that calculates the sum of the input given
number of N using while loop statement.

*/


import java.util.Scanner;

class while_loops {
    
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int sum=0,b=0;
    System.out.print("\t Sum of Numbers Using While Loop");
    System.out.println();
    System.out.print("Enter your desired looping number : ");
    int value = input.nextInt();
    while (b<=value) {
      System.out.print(" " + b + " ");
        sum+=b;
        b++;
    }   
    System.out.println();
    System.out.println("The total sum is  " + sum +".");
    System.out.println();
    System.out.println(" END OF PROGRAM"); 
    System.out.println();
  }
}

Word Count in Java

A program that I wrote in Java that will ask the user to enter a sentence and then our program will count the number of words in a given sentence.

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

/* word_count.java */
/* Written By: Mr. Jake R. Pomperada, MAED-IT */
/* July 15, 2015 */
/* This program will count the number of words in a given sentence by the user */

import java.util.Scanner;

class word_count {

    
    private static int word_count(String phrase)
    {
    if (phrase == null)
       return 0;
    return phrase.trim().split("\\s+").length;
    }

public static void main(String args[])
   {
      String words;
      char ch;

      do {
          Scanner input = new Scanner(System.in);
          System.out.println();
          System.out.print("\t WORD COUNT PROGRAM ");
          System.out.println("\n");
          System.out.print("Kindly enter a word :=> ");
          words= input.nextLine();
          System.out.println();
          System.out.print("The total number of words is " + word_count(words) +".");
          System.out.println();
          System.out.print("\nDo you want to continue (Type y or n) : ");
          ch = input.next().charAt(0);                        
     } while (ch == 'Y'|| ch == 'y');  
          System.out.println();
          System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
          System.out.println("\n\n");    
   }
}


Database Connection in JSP and MySQL

In this article I would like to share with you a code to check whether JSP and MySQL is connected to one another or not. This code is very useful if you want to know if JSP and MySQL are working together.

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.


Program Listing


dbconnect.jsp


<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<html> 
<head> 
<title>Connection with mysql database</title>
</head> 
<body>
<h1>Connection status</h1>
<%
Connection conn=null;
ResultSet result=null;
Statement stmt=null;
ResultSetMetaData rsmd=null;
try {
  Class c=Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){
  out.write("Error!!!!!!" + e);
}
try {
  conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/users",
   "root","");
  out.write("Connected!");
}
catch(SQLException e) {
  System.out.println("Error!!!!!!" + e);
}
%>
</body> 
</html>

Login and Registration in JSP and MySQL

In this article I am very happy to share with you my readers of my site a simple program that I wrote how to write a system using Java Server Pages and MySQL to login and register a user for our website. Basically an JSP code or snipplets is also a servlet used primarily for presentation in Java. This program will allow the visitor of the website to register their user name and password. After they are registered they can able to login in the website.

I make sure the code is not very long and very easy to understand. I hope you will find my wok useful in your programming projects and assignments.

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

<!-- login.jsp -->
<!-- Written By: Mr. Jake R. Pomperada,MAED-IT -->
<!-- Date      : July 24, 2015                 -->
<!-- Tools     : Netbeans IDE 8.0.2, Java SE 8 and Apache Tomcat Server -->
<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
 alert("Enter Username!");
  return false;
}
if(password==""){
 alert("Enter Password!");
  return false;
}
return true;
}
</script>
<style>
    table, td, th,h3 {
    border: 1px solid green;
    font-family: arial;
    color: blue;
}

table {
    background-color: lightgreen;
   }
</style>
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
    <h3> Login System and Registration System in JSP and MySQL </h3>  
<table>
<tr><td>Username:</td><td><input type="text" name="user"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Login"></td></tr>
<tr><td></td><td><a href="register.jsp" title="Click here to register account">
Register </a></td></tr>
</table>
</form>
</html>


// check.jsp
// Written By: Mr. Jake R. Pomperada,MAED-IT
// Date      : July 24, 201
// Tools     : Netbeans IDE 8.0.2, Java SE 8 and Apache Tomcat Server

<%@page import="java.sql.*"%>
<%
try{
String user=request.getParameter("user");
String pass=request.getParameter("pass");
 Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","");  
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'");
           int count=0;
           while(rs.next()){
           count++;
          }
          if(count>0){
           out.println("<body bgcolor='lightgreen'>");   
           out.println("<h1> Welcome  "+ user + " in the website. </h1>");
           out.println("</body>");
          }
          else{
           response.sendRedirect("login.jsp");
          }
        }
    catch(Exception e){
    System.out.println(e);
}
%>


<!-- register.jsp -->
<!-- Written By: Mr. Jake R. Pomperada,MAED-IT -->
<!-- Date      : July 24, 2015                 -->
<!-- Tools     : Netbeans IDE 8.0.2, Java SE 8 and Apache Tomcat Server -->

<%@ page language="java" import="java.sql.*,java.util.*,java.text.*" %>
<html>
<head>
<title>Registration Page </title>
</head>
<body>
<style>
    table, td, th {
    border: 1px solid green;
    font-family: arial;
    color: blue;
}

table {
    background-color: lightgreen;
   }
</style>
<%
out.println("<style>  p {font-family: arial;"
           + " color: red; font-size: 16;   }; "
           + "</style>");
out.println("<style>  a,b {font-family: arial;"
           + " color: blue; font-size: 16;   }; "
           + "</style>");

%>

<table border="1" width="50%">
<tr>
<td width="100%">
<form method="POST" action="save_login.jsp">

<h2 align="center">Register User Name and Password</h2>
<table border="1" width="100%">
<tr>
<td width="50%"><b>Username:</b></td>
<td width="50%"><input type="text" name="username" size="20"/> </td>
</tr>
<tr>
<td width="50%"><b>Password:</b></td>
<td width="50%"><input type="text" name="password" size="20"></td>
</tr>
<tr>
</table>
<p><input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset"></p>
</form>
</td>
</tr>
</table>
<% out.println("<br>");
out.println("<a href='login.jsp'> RETURN TO LOGIN PAGE </a>");
%>
</body>
</html>

// save_login.jsp
// Written By: Mr. Jake R. Pomperada,MAED-IT
// Date      : July 24, 201
// Tools     : Netbeans IDE 8.0.2, Java SE 8 and Apache Tomcat Server

<%@ page language="java" import="java.sql.*,java.util.*,java.text.*" %>

<%
out.println("<style>  p {font-family: arial;"
           + " color: red; font-size: 16;   }; "
           + "</style>");
out.println("<style>  a,b {font-family: arial;"
           + " color: blue; font-size: 16;   }; "
           + "</style>");

%>
<%
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "users";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","");
try{
Statement st = con.createStatement();
String username=request.getParameter("username");
String password=request.getParameter("password");


int val = st.executeUpdate("INSERT login "
        + "VALUES(id,'"+username+"','"+password+"')");

con.close();
out.println("<p> The record is successfully saved. </p>");
out.println("<br>");
out.println("<a href='login.jsp'> RETURN TO LOGIN PAGE </a>");
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}

%>