Wednesday, November 6, 2019

Calculator Program in Java

A simple calculator program in Java programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.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.

Here in Bacolod City, Negros Occidental I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My Facebook address is https://www.facebook.com/profile.php?id=100009212511791

My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.

https://www.mindshaperspublishing.com/

https://www.unlimitedbooksph.com/

Thank you very much for your help and support.


Program Listing

Calc.java

class Calc {

public static void main (String[] args) {

try {

if (args.length == 0) {

System.out.println("\nPROGRAM ERROR: You didn't enter any arguments!");

} else if  (args.length < 3) {

System.out.println("\nPROGRAM ERROR: You passed fewer than 3 arguments.");

} else if (args.length > 3) {

System.out.println("\nPROGRAM ERROR: You passed more than 3 arguments.");

} else {

double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[2]);
String op = args[1];

// Product variables
double p = a + b;
double s = a - b;
double m = a * b;
double d = a / b;

System.out.println("\n================");
System.out.println("WELCOME TO CALCACULATOR IN JAVA!");
System.out.println("================");
System.out.println("\nYou have entered: " + a + " " + op + " " + b);

switch (op) {

case "+" :
System.out.println("\n" + a + " " + op + " " + b + " = " + p);
break;

case "-" :
System.out.println("\n" + a + " " + op + " " + b + " = " + s);
break;

case "x" :
System.out.println("\n" + a + " " + op + " " + b + " = " + m);
break;

case "/" :
System.out.println("\n" + a + " " + op + " " + b + " = " + d);
break;

default :
System.out.println("INVALID OPERATOR SPECIFIED");

}
}

} catch (NumberFormatException e) {

System.out.println("\nPROGRAM ERROR");
System.out.println("\nFirst and third arguments must be numbers!");
System.out.println("\nEXAMPLE: 2 + 2");
System.out.println("\nERROR DETAILS: " + e);

} catch (Exception e) {

System.out.println("\nPROGRAM ERROR");
throw e;

}
}

}

Tic Tac Toe in Java

A simple tic tac toe program in Java.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.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.

Here in Bacolod City, Negros Occidental I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My Facebook address is https://www.facebook.com/profile.php?id=100009212511791

My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.

https://www.mindshaperspublishing.com/

https://www.unlimitedbooksph.com/

Thank you very much for your help and support.


Program Listing

TicTacT.java

import java.util.Scanner;

class TicTacT {

// Array to hold the X or O in each space (Default value: '_')
public static char[] place = { '_','_','_','_','_','_','_','_','_' };

// Holds X or O, whichever team user chooses
public static char team = '_';

// Holds X or O, whichever team user does NOT choose!
public static char opp = '_';

// Variable to hold user input for Scanner
public static String usrStr;

public static void main (String[] args) {

// Greeting message for user
System.out.println("WELCOME TO TIC TAC TOE!");

// Run the setup method
setup();

}

public static void drawBoard() {

// Draw the game board
System.out.println("\n\t    A   B   C");
System.out.println("\t  .-----------.");
System.out.println("\t1 |_"+TicTacT.place[0]+"_|_"+TicTacT.place[1]+"_|_"+TicTacT.place[2]+"_|\n");
System.out.println("\t2 |_"+TicTacT.place[3]+"_|_"+TicTacT.place[4]+"_|_"+TicTacT.place[5]+"_|\n");
System.out.println("\t3 |_"+TicTacT.place[6]+"_|_"+TicTacT.place[7]+"_|_"+TicTacT.place[8]+"_|");
System.out.println("\t  '-----------'");

}

public static void setup() {

// Loop to reset the game board
for ( int i = 0 ; i < 9 ; i++ ) {

TicTacT.place[i] = '_';

}

// Print the game board to the console
drawBoard();

while ( (TicTacT.team != 'X') && (TicTacT.team != 'O') ) {

System.out.println("\n\nSelect Your Team! Enter 'X' or 'O' below...");

System.out.print("Enter your selection: ");
Scanner input = new Scanner(System.in);
TicTacT.usrStr = input.next();

if (TicTacT.usrStr.toUpperCase().equals("X")) {

TicTacT.team = 'X';
TicTacT.opp = 'O';

} else if (TicTacT.usrStr.toUpperCase().equals("O")) {

TicTacT.team = 'O';
TicTacT.opp = 'X';

} else {

System.out.println("You entered: " + usrStr);
System.out.println("This is not a valid option.");
System.out.println("Please enter either an X or an O to continue.");

}

}

System.out.println("\nYou are team " + TicTacT.team + "!");

// Run the game method
game();

}

public static void game() {

// Local variable to run loop
boolean loop = true;

System.out.println("IT'S YOUR TURN!");

drawBoard();

do {

System.out.println("\n\nChoose a column and row to place an " + TicTacT.team + ". (EXAMPLE: A1)\n");

System.out.print("Enter your selction: ");
Scanner input = new Scanner(System.in);
TicTacT.usrStr = input.next().toUpperCase();

switch (TicTacT.usrStr)
{
case "A1" : if (TicTacT.place[0]=='_') {

TicTacT.place[0] = TicTacT.team;
System.out.println("\nYou place an " + TicTacT.team + " in A1");
oppMove();
loop = false;

} else if (TicTacT.place[0]==TicTacT.team) {

System.out.println("\nThere is an " + TicTacT.team + " there already!");

} else if (TicTacT.place[0]==TicTacT.opp) {

System.out.println("\nThis space is already taken!");

}; break;

case "B1" : if (TicTacT.place[1]=='_') {

TicTacT.place[1] = TicTacT.team;
System.out.println("\nYou place an " + TicTacT.team + " in B1");
oppMove();
loop = false;

} else if (TicTacT.place[1]==TicTacT.team) {

System.out.println("\nThere is an " + TicTacT.team + " there already!");

} else if (TicTacT.place[1]==TicTacT.opp) {

System.out.println("\nThis space is already taken!");

}; break;

case "C1" : if (TicTacT.place[2]=='_') {

TicTacT.place[2] = TicTacT.team;
System.out.println("\nYou place an " + TicTacT.team + " in C1");
oppMove();
loop = false;

} else if (TicTacT.place[2]==TicTacT.team) {

System.out.println("\nThere is an " + TicTacT.team + " there already!");

} else if (TicTacT.place[2]==TicTacT.opp) {

System.out.println("\nThis space is already taken!");

}; break;

case "A2" : if (TicTacT.place[3]=='_') {

TicTacT.place[3] = TicTacT.team;
System.out.println("\nYou place an " + TicTacT.team + " in A2");
oppMove();
loop = false;

} else if (TicTacT.place[3]==TicTacT.team) {

System.out.println("\nThere is an " + TicTacT.team + " there already!");

} else if (TicTacT.place[3]==TicTacT.opp) {

System.out.println("\nThis space is already taken!");

}; break;

case "B2" : if (TicTacT.place[4]=='_') {

TicTacT.place[4] = TicTacT.team;
System.out.println("\nYou place an " + TicTacT.team + " in B2");
oppMove();
loop = false;

} else if (TicTacT.place[4]==TicTacT.team) {

System.out.println("\nThere is an " + TicTacT.team + " there already!");

} else if (TicTacT.place[4]==TicTacT.opp) {

System.out.println("\nThis space is already taken!");

}; break;

case "C2" : if (TicTacT.place[5]=='_') {

TicTacT.place[5] = TicTacT.team;
System.out.println("\nYou place an " + TicTacT.team + " in C2");
oppMove();
loop = false;

} else if (TicTacT.place[5]==TicTacT.team) {

System.out.println("\nThere is an " + TicTacT.team + " there already!");

} else if (TicTacT.place[5]==TicTacT.opp) {

System.out.println("\nThis space is already taken!");

}; break;

case "A3" : if (TicTacT.place[6]=='_') {

TicTacT.place[6] = TicTacT.team;
System.out.println("\nYou place an " + TicTacT.team + " in A3");
oppMove();
loop = false;

} else if (TicTacT.place[6]==TicTacT.team) {

System.out.println("\nThere is an " + TicTacT.team + " there already!");

} else if (TicTacT.place[6]==TicTacT.opp) {

System.out.println("\nThis space is already taken!");

}; break;

case "B3" : if (TicTacT.place[7]=='_') {

TicTacT.place[7] = TicTacT.team;
System.out.println("\nYou place an " + TicTacT.team + " in B3");
oppMove();
loop = false;

} else if (TicTacT.place[7]==TicTacT.team) {

System.out.println("\nThere is an " + TicTacT.team + " there already!");

} else if (TicTacT.place[7]==TicTacT.opp) {

System.out.println("\nThis space is already taken!");

}; break;

case "C3" : if (TicTacT.place[8]=='_') {

TicTacT.place[8] = TicTacT.team;
System.out.println("\nYou place an " + TicTacT.team + " in C3");
oppMove();
loop = false;

} else if (TicTacT.place[8]==TicTacT.team) {

System.out.println("\nThere is an " + TicTacT.team + " there already!");

} else if (TicTacT.place[8]==TicTacT.opp) {

System.out.println("\nThis space is already taken!");

}; break;
}

} while (loop);

checkWin();

}

public static void oppMove() {

while (true) {

if ( (TicTacT.place[0]=='_') || (TicTacT.place[1]=='_') || (TicTacT.place[2]=='_') || (TicTacT.place[3]=='_') || (TicTacT.place[4]=='_') || (TicTacT.place[5]=='_') || (TicTacT.place[6]=='_') || (TicTacT.place[7]=='_') || (TicTacT.place[8]=='_') ) {

float rn = (float) Math.random();
float m = rn * 8;
int x = Math.round(m);

if (TicTacT.place[x]=='_') {

TicTacT.place[x] = TicTacT.opp;
break;

}

} else { checkWin(); break; }

}

}

public static void checkWin() {

// See if X has won the game

if ( (TicTacT.place[0]=='X') && (TicTacT.place[1]=='X') && (TicTacT.place[2]=='X') ) {

System.out.println("\n\n\n\tX WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[3]=='X') && (TicTacT.place[4]=='X') && (TicTacT.place[5]=='X') ) {

System.out.println("\n\n\n\tX WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[6]=='X') && (TicTacT.place[7]=='X') && (TicTacT.place[8]=='X') ) {

System.out.println("\n\n\n\tX WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[0]=='X') && (TicTacT.place[3]=='X') && (TicTacT.place[6]=='X') ) {

System.out.println("\n\n\n\tX WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[1]=='X') && (TicTacT.place[4]=='X') && (TicTacT.place[7]=='X') ) {

System.out.println("\n\n\n\tX WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[2]=='X') && (TicTacT.place[5]=='X') && (TicTacT.place[8]=='X') ) {

System.out.println("\n\n\n\tX WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[0]=='X') && (TicTacT.place[4]=='X') && (TicTacT.place[8]=='X') ) {

System.out.println("\n\n\n\tX WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[6]=='X') && (TicTacT.place[4]=='X') && (TicTacT.place[2]=='X') ) {

System.out.println("\n\n\n\tX WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");
}

// Now, see if O has won!

if ( (TicTacT.place[0]=='O') && (TicTacT.place[1]=='O') && (TicTacT.place[2]=='O') ) {

System.out.println("\n\n\n\tO WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[3]=='O') && (TicTacT.place[4]=='O') && (TicTacT.place[5]=='O') ) {

System.out.println("\n\n\n\tO WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[6]=='O') && (TicTacT.place[7]=='O') && (TicTacT.place[8]=='O') ) {

System.out.println("\n\n\n\tO WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[0]=='O') && (TicTacT.place[3]=='O') && (TicTacT.place[6]=='O') ) {

System.out.println("\n\n\n\tO WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[1]=='O') && (TicTacT.place[4]=='O') && (TicTacT.place[7]=='O') ) {

System.out.println("\n\n\n\tO WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[2]=='O') && (TicTacT.place[5]=='O') && (TicTacT.place[8]=='O') ) {

System.out.println("\n\n\n\tO WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[0]=='O') && (TicTacT.place[4]=='O') && (TicTacT.place[8]=='O') ) {

System.out.println("\n\n\n\tO WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

} else if ( (TicTacT.place[6]=='O') && (TicTacT.place[4]=='O') && (TicTacT.place[2]=='O') ) {

System.out.println("\n\n\n\tO WINS!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

// Check for a tie!

} else if ( (TicTacT.place[0]!='_') && (TicTacT.place[1]!='_') && (TicTacT.place[2]!='_') && (TicTacT.place[3]!='_') && (TicTacT.place[4]!='_') && (TicTacT.place[5]!='_') && (TicTacT.place[6]!='_') && (TicTacT.place[7]!='_') && (TicTacT.place[8]!='_') ) {

System.out.println("\n\n\n\tTIE GAME!!!!"); drawBoard(); playAgain(); System.out.println("\n\n");

// If no one has won and no tie, keep playing!!

} else { game(); }

}

public static void playAgain() {

System.out.print("\n\nPlay again? [y/n]: ");
Scanner input = new Scanner(System.in);
TicTacT.usrStr = input.next().toLowerCase();

if (usrStr.equals("y")) {

TicTacT.team = '_';
TicTacT.opp = '_';
setup();

} else {

System.exit(0);

}

}


}

Tuesday, November 5, 2019

While Loop in PHP

While Loop Statement in PHP

I wrote this program to show to declare and use the while loop statement using PHP as my programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.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.

Here in Bacolod City, Negros Occidental I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My Facebook address is https://www.facebook.com/profile.php?id=100009212511791

My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.

https://www.mindshaperspublishing.com/

https://www.unlimitedbooksph.com/

If you like my video tutorials kindly click the like button and subscribe for more video tutorials on my channel.


Thank you very much for your help and support.


Sample Program Output


Program Listing

index.php

<?php
 // while loop statement in PHP
 // Author : Jake Rodriguez Pomperada
 // Date   : November 5, 2019  9:15 AM Tuesday

 $a=1;


   while ($a<=20) {
    
    echo "<br> Counting at ".$a;
    $a++;      
   }

   




If Else Statement in PHP

If Else Statement in PHP

I wrote this program to demonstrate how to declare and use the if-else conditional statement in PHP.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.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.

Here in Bacolod City, Negros Occidental I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My Facebook address is https://www.facebook.com/profile.php?id=100009212511791

My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.

https://www.mindshaperspublishing.com/

https://www.unlimitedbooksph.com/

If you like my video tutorials kindly click the like button and subscribe for more video tutorials on my channel.


Thank you very much for your help and support.




Sample Program Output



Program Listing

index.php

<?php
  // if-else statement in PHP
  // Author : Jake Rodriguez Pomperada
  // November 5, 2019    8:37 AM  Tuesday


 $age=18;

 if ($age>=18) {

  echo "Your age $age you are already an Adult.";
 } 
 else {
  echo "Your age $age you are still a Minor.";
 }





Functions in PHP

Functions in PHP

I wrote this program to demonstrate how to declare and use functions using PHP as my programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.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.

Here in Bacolod City, Negros Occidental I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My Facebook address is https://www.facebook.com/profile.php?id=100009212511791

My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.

https://www.mindshaperspublishing.com/

https://www.unlimitedbooksph.com/

If you like my video tutorials kindly click the like button and subscribe for more video tutorials on my channel.

Thank you very much for your help and support.

Sample Program Output


Program Listing

index.php

<?php

function title()
{
echo "<h2>Addition of Three Numbers Using Function in PHP </h2>";
}


function addition($a,$b,$c)
{
return($a+$b+$c);
}


echo title();
echo "<h3>The total sum is " .addition(5,10,15).".</h3>";



For Loop Statement in PHP

For Loop Statement in PHP

I wrote this program to show how to declare and use for loop statement using PHP as my programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.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.

Here in Bacolod City, Negros Occidental I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My Facebook address is https://www.facebook.com/profile.php?id=100009212511791

My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.

https://www.mindshaperspublishing.com/

https://www.unlimitedbooksph.com/

If you like my video tutorials kindly click the like button and subscribe for more video tutorials on my channel.

Thank you very much for your help and support.


Sample Program Output


Program Listing

index.php

<?php
   
   for ($a=1; $a<=10; $a++) {
    echo "<br>";
     echo "Counting at " .$a;
   }

 ?>  

Do While Loop Statement in PHP