Skip to main content

Posts

SIMPLE BLACKJACK GAME IN JAVA (CONSOLE)

import java.util.Scanner; class BlackJack{     public static void main(String[] args)      {         int player_random1 = 100;         int player_random2 = 100;         while(player_random1 >= 12 || player_random2 >= 12  || player_random1 < 3 || player_random2 <3)         {             player_random1 = (int)(Math.random()*100);             player_random2 = (int)(Math.random()*100);         }                  int player_total = player_random1 + player_random2;                  System.out.println("You get a "+player_random1+" and a "+player_random2);         System.out.println("Your total is "+player_total); if(player_total==21)  ...

HANGMAN USING JAVA (CONSOLE)

SOURCE CODE import java.util.Scanner; class Hangman {     public static void main(String[] args) {         String words_list = "cheese puppies hello tree mango pineapple flowers basket lamp ceiling";         String[] wordsAsArray = words_list.split(" ");         int index;                  //to pick a random integer which is less than the number of array elements and greater than or equal to 0         do{             index =(int) (Math.random() * 10);         }         while(index<0 || index>=wordsAsArray.length);                  //To pick the word that corresponds to the random index picked         String randomWord = wordsAsArray[index];             ...

TIC TAC TOE GAME IN JAVA (CONSOLE)

SOURCE CODE import java.util.Scanner; class TicTacToe {     public static void main(String[] args) {         char[][] main = new char[3][3];                  for(int i=0; i<3; i++){             for(int j=0; j<3; j++){                 main[i][j]=' ';             }         }                  for(int i=0; i<4; i++){             if(input(main, 'O')){                 return;             }             if(input(main, 'X')){                 return;             }         }         //1st...

Decimal To Binary Converter Without Using Predefined Methods (Java)

This simple Java Application will convert the given decimal value to its Binary. Following code is to be executed after the user enters a value and presses 'Convert'. (Button Action) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                  int num = Integer.parseInt(jTextField1.getText());                  if(num>=0){            int binary[] = new int[50];            int index = 0;            do{                binary[index++]=num%2;                num = num/2;            }            while(num>0);   ...

Setting up a database connection (Java+mysql)

Initially, it is essential to set up the database connection. STEP 1: Create a database in phpmyadmin and create the required table. STEP 2: Set up a new connection in Net Beans. 1. Right Click on Databases (Services tab) -> Select New Connection 2. Select MySQL  -> Select Next 3. Enter the name of your database -> TEST   CONNECTION 4. If you see the below added to your services, then the connection has been set up successfully. STEP 3: Add a library to the net beans project as below. STEP 4:     Creating the Database Connect Class 1. Create a new package under the project and create a new class called ' DBconnect ' under that package. 2. DBconnect class should look like below.  In the line,         con = (Connection) DriverManager.getConnection(" jdbc:mysql://localhost:3306/employee ","root",""); The highlighted path can be optained from the services pane ...

Implementing Data Structures using Java - Stacks

The Stack Data Structure follows the principle of Last In First Out which means the last element inserted is the one to be taken out first.  Real world example would be a stack of CDs. To take out the bottom one you will have to remove the CDs on top of it. And in this Data Structure you have to remove each element one by one. This example is implemented using an array, hence contains elements of the same data type only. A Stack comes in handy when you need to reverse a character array and this will show you how. STEP 1: The Stack class should be implemented as below. STEP 2: The GUI should be designed as preferred. Then, the Stack is created dynamically in case it needs to be accessed in different events and for the button click the below code should be added. STEP 3: An object of the JFrame Class (GUI) should be created in the main program and the visibility should be set to true. Now, you can run the program and use it to reverse any strin...

Admin panel of a Q & A Forum

In a Q & A Forum, when a user posts a question, it should be sent to the administrator for approval in case it contains inappropriate content. After approval it should be removed from this pending approval page and other users should be able to see the question afterwards. To enable this, we should maintain an approval column in our database table of records and for each record approval should be set to false by default. In the Pending approvals page only the records with approval=false should be displayed. Below is  the MySQL  statement for retrieval, $sql="SELECT * FROM topics WHERE approval=false"; To know which post was approved we should embed the post_id to the URL. And the relevant post should be updated as approval=true. Below is the complete code. <?php $sql="SELECT * FROM topics WHERE approval=false"; $query=mysqli_query($conn,$sql); echo '<form name="approve" method="p...