Wednesday, January 8, 2014


patterns using java:-

1>

import java.io.*;
class pattern
{
    public static void main()throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
       
        System.out.println("enter limit");
        String a = br.readLine();
        int x = Integer.parseInt(a);
       
        for(int i=1 ; i<=x ; i++)
        {
            for(int j=1 ; j<=i ; j++)
            {
                System.out.print("1");
            }
            System.out.println();
        }
    }
}

2>

import java.io.*;
class pattern2
{
    public static void main()throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
       
        System.out.println("enter limit");
        String a = br.readLine();
        int x = Integer.parseInt(a);
       
        for(int i=1 ; i<=x ; i++)
        {
            for(int j=1 ; j<=i ; j++)
            {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

3>

import java.io.*;
class pattern3
{
    public static void main()throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
       
        System.out.println("enter limit");
        String a = br.readLine();
        int x = Integer.parseInt(a);
       
        for(int i=1 ; i<=x ; i++)
        {
            for(int j=1 ; j<=i ; j++)
            {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

4>

import java.io.*;
class pattern4
{
    public static void main() throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
       
        System.out.println("enter limit");
        String s = br.readLine();
        int x = Integer.parseInt(s);
       
        for(int i=x ; i>=0 ; i--)
        {
            for(int j=1 ; j<=i ; j++)
            {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

5>

import java.io.*;
class pattern5
{
    public static void main() throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
       
        System.out.println("enter limit");
        String s = br.readLine();
        int x = Integer.parseInt(s);
       
        for(int i=x ; i>=0 ; i--)
        {
            for(int j=1 ; j<=i ; j++)
            {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

6>

import java.io.*;
class pattern6
{
    public static void main() throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
       
        System.out.println("enter limit");
        String s = br.readLine();
        int x = Integer.parseInt(s);
       
        for(int i=1 ; i<=x ; i++)
        {
            for(int j=1; j<=x ; j++)
            {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}
           
       
        

TIC-TAC-TOE game in java:-


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class strings implements ActionListener    {
    final String VERSION = "1.0";
//Setting up ALL the variables
    JFrame window = new JFrame("TIC_TAC_TOE game " + VERSION);
  
    JMenuBar mnuMain = new JMenuBar();
    JMenuItem     mnuNewGame = new JMenuItem("New Game"),  
                    mnuInstruction = new JMenuItem("Instructions"),
                    mnuExit = new JMenuItem("Exit"),
                    mnuAbout = new JMenuItem("About");
  
    JButton     btn1v1 = new JButton("Player vs Player"),
                btn1vCPU = new JButton("Player vs CPU"),
                btnBack = new JButton("<--back");
    JButton btnEmpty[] = new JButton[10];
  
    JPanel     pnlNewGame = new JPanel(),
                pnlNorth = new JPanel(),
                pnlSouth = new JPanel(),
                pnlTop = new JPanel(),
                pnlBottom = new JPanel(),
                pnlPlayingField = new JPanel();
    JLabel lblTitle = new JLabel("Tic-Tac-Toe");
    JTextArea txtMessage = new JTextArea();
  
    final int winCombo[][] = new int[][]    {
        {1, 2, 3},             {1, 4, 7},         {1, 5, 9},
        {4, 5, 6},             {2, 5, 8},         {3, 5, 7},
        {7, 8, 9},             {3, 6, 9}
/*Horizontal Wins*/    /*Vertical Wins*/ /*Diagonal Wins*/
    };
    final int X = 412, Y = 268, color = 190;
    boolean inGame = false;
    boolean win = false;
    boolean btnEmptyClicked = false;
    String message;
    int turn = 1;
    int wonNumber1 = 1, wonNumber2 = 1, wonNumber3 = 1;
  
    public strings()    {    //Setting game properties and layout and sytle...
    //Setting window properties:
        window.setSize(X, Y);
        window.setLocation(450, 260);
        window.setResizable(false);
        window.setLayout(new BorderLayout());
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
    //Setting Panel layouts and properties
        pnlNewGame.setLayout(new GridLayout(2, 1, 2, 10));
        pnlNorth.setLayout(new FlowLayout(FlowLayout.CENTER));
        pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
      
        pnlNorth.setBackground(new Color(color-20, color-20, color-20));
        pnlSouth.setBackground(new Color(color, color, color));
      
        pnlTop.setBackground(new Color(color, color, color));
        pnlBottom.setBackground(new Color(color, color, color));
      
        pnlTop.setLayout(new FlowLayout(FlowLayout.CENTER));
        pnlBottom.setLayout(new FlowLayout(FlowLayout.CENTER));
        pnlNewGame.setBackground(Color.blue);
      
    //Adding menu items to menu bar
        mnuMain.add(mnuNewGame);
        mnuMain.add(mnuInstruction);
        mnuMain.add(mnuAbout);
        mnuMain.add(mnuExit);//---->Menu Bar Complete
      
    //Adding buttons to NewGame panel
        pnlNewGame.add(btn1v1);
        pnlNewGame.add(btn1vCPU);
      
    //Adding Action Listener to all the Buttons and Menu Items
        mnuNewGame.addActionListener(this);
        mnuExit.addActionListener(this);
        mnuInstruction.addActionListener(this);
        mnuAbout.addActionListener(this);
        btn1v1.addActionListener(this);
        btn1vCPU.addActionListener(this);
        btnBack.addActionListener(this);
  
    //Setting up the playing field
        pnlPlayingField.setLayout(new GridLayout(3, 3, 2, 2));
        pnlPlayingField.setBackground(Color.black);
        for(int i=1; i<=9; i++)    {
            btnEmpty[i] = new JButton();
            btnEmpty[i].setBackground(new Color(220, 220, 220));
            btnEmpty[i].addActionListener(this);
            pnlPlayingField.add(btnEmpty[i]);
        }
    //Adding everything needed to pnlNorth and pnlSouth
        pnlNorth.add(mnuMain);
        pnlSouth.add(lblTitle);
      
    //Adding to window and Showing window
        window.add(pnlNorth, BorderLayout.NORTH);
        window.add(pnlSouth, BorderLayout.CENTER);
        window.setVisible(true);
    }

//-------------------START OF ACTION PERFORMED CLASS-------------------------//  
    public void actionPerformed(ActionEvent click)    {
        Object source = click.getSource();
        for(int i=1; i<=9; i++)    {
            if(source == btnEmpty[i] && turn <    10)    {
                btnEmptyClicked = true;
                if(!(turn % 2 == 0))
                    btnEmpty[i].setText("X");
                else
                    btnEmpty[i].setText("O");
                btnEmpty[i].setEnabled(false);
                pnlPlayingField.requestFocus();
                turn++;
            }
        }
        if(btnEmptyClicked)    {
            checkWin();
            btnEmptyClicked = false;
        }
        if(source == mnuNewGame)    {
            clearPanelSouth();
            pnlSouth.setLayout(new GridLayout(2, 1, 2, 5));
            pnlTop.add(pnlNewGame);
            pnlBottom.add(btnBack);
            pnlSouth.add(pnlTop);
            pnlSouth.add(pnlBottom);
          
        }
        else if(source == btn1v1)    {
            if(inGame)    {
                int option = JOptionPane.showConfirmDialog(null, "If you start a new game," +
                     "your current game will be lost..." + "\n" +
                    "Are you sure you want to continue?",
                    "Quit Game?" ,JOptionPane.YES_NO_OPTION);
                if(option == JOptionPane.YES_OPTION)    {
                    inGame = false;
                }
            }
            if(!inGame)    {
                btnEmpty[wonNumber1].setBackground(new Color(220, 220, 220));
                btnEmpty[wonNumber2].setBackground(new Color(220, 220, 220));
                btnEmpty[wonNumber3].setBackground(new Color(220, 220, 220));
                turn = 1;
                for(int i=1; i<10; i++)    {
                    btnEmpty[i].setText("");
                    btnEmpty[i].setEnabled(true);
                }
                win = false;
                showGame();
              
            }
        }
        else if(source == btn1vCPU)    {
            JOptionPane.showMessageDialog(null, "Comming soon");
        }
        else if(source == mnuExit)    {
            int option = JOptionPane.showConfirmDialog(null, "Are You Sure You want to exit?",
            "get out" ,JOptionPane.YES_NO_OPTION);
            if(option == JOptionPane.YES_OPTION)
                System.exit(0);
        }
        else if(source == mnuInstruction || source == mnuAbout)    {
            clearPanelSouth();
            String message = "";
            txtMessage.setBackground(new Color(color, color, color));
            if(source == mnuInstruction)    {
                message =     "Instructions:\n\n" +
                                "GooGle TIC TAC TOE" +
                                "I m feelin lucky";
            } else    {
                message =     "about:\n\n" +
                                "Title: Tic-Tac-Toe\n" +
                                "Author: CodeLearning JAva\n" +
                                "Version: " + VERSION + "\n";
            }
            txtMessage.setEditable(false);
            txtMessage.setText(message);
            pnlSouth.setLayout(new GridLayout(2, 1, 2, 5));
            pnlTop.add(txtMessage);
            pnlBottom.add(btnBack);
            pnlSouth.add(pnlTop);
            pnlSouth.add(pnlBottom);
        }
        else if(source == btnBack)    {
            if(inGame)
                showGame();
            else    {
                clearPanelSouth();
                pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
                pnlNorth.setVisible(true);
                pnlSouth.add(lblTitle);
            }
        }
        pnlSouth.setVisible(false);
        pnlSouth.setVisible(true);
    }
//-------------------END OF ACTION PERFORMED CLASS-------------------------//

/*
        ----------------------------------
        Start of all the other methods.    |
        ----------------------------------
*/
    public void showGame()    {    //    Shows the Playing Field
                                        //    *IMPORTANT*- Does not start out brand new (meaning just shows what it had before)
        clearPanelSouth();
        inGame = true;
        pnlSouth.setLayout(new BorderLayout());
        pnlSouth.add(pnlPlayingField, BorderLayout.CENTER);
        pnlPlayingField.requestFocus();
    }
  
    public void checkWin()    {    //    checks if there are 3 symbols in a row vertically, diagonally, or horizontally.
                                        //    then shows a message and disables buttons.
        for(int i=0; i<7; i++)    {
            if(
                    !btnEmpty[winCombo[i][0]].getText().equals("") &&
                    btnEmpty[winCombo[i][0]].getText().equals(btnEmpty[winCombo[i][1]].getText()) &&
                    //                                if {1 == 2 && 2 == 3}
                    btnEmpty[winCombo[i][1]].getText().equals(btnEmpty[winCombo[i][2]].getText())
                    /*
                        The way this checks the if someone won is:
                        First: it checks if the btnEmpty[x] is not equal to an empty string-    x being the array number
                            inside the multi-dementional array winCombo[checks inside each of the 7 sets][the first number]
                        Secong: it checks if btnEmpty[x] is equal to btnEmpty[y]- x being winCombo[each set][the first number]
                            y being winCombo[each set the same as x][the second number] (So basically checks if the first and
                            second number in each set is equal to each other)
                        Third: it checks if btnEmtpy[y] is eual to btnEmpty[z]- y being the same y as last time and z being
                            winCombo[each set as y][the third number]
                        Conclusion:    So basically it checks if it is equal to the btnEmpty is equal to each set of numbers
                    */
                )    {
                win = true;
                wonNumber1 = winCombo[i][0];
                wonNumber2 = winCombo[i][1];
                wonNumber3 = winCombo[i][2];
                btnEmpty[wonNumber1].setBackground(Color.white);
                btnEmpty[wonNumber2].setBackground(Color.white);
                btnEmpty[wonNumber3].setBackground(Color.white);
                break;
            }
        }
        if(win || (!win && turn>9))    {
            if(win)    {
                if(turn % 2 == 0)
                    message = "PLayer 1 won d game!";
                else  
                    message = "Player 2won the game!";
                win = false;
            }    else if(!win && turn>9)    {
                message = "DRAW";
            }
                JOptionPane.showMessageDialog(null, message);
            for(int i=1; i<=9; i++)    {
                btnEmpty[i].setEnabled(false);
            }
        }
    }
  
    public void clearPanelSouth()    {    //Removes all the possible panels
                                                //that pnlSouth, pnlTop, pnlBottom
                                                //could have.  
        pnlSouth.remove(lblTitle);
        pnlSouth.remove(pnlTop);
        pnlSouth.remove(pnlBottom);
        pnlSouth.remove(pnlPlayingField);
        pnlTop.remove(pnlNewGame);
        pnlTop.remove(txtMessage);
        pnlBottom.remove(btnBack);
    }
  
    public static void main(String[] args)    {
        new strings();//    Calling the class construtor.
    }
}
If anyone requires any java program, you can surely drop me a message i the comment section below. I will surely get back to you as fast as possible.

IF ANY ISC/ICSE COMPUTER SCIENCE CANDIDATE REQUIRES ANY PROGRAM/ ANY HELP/ PROGRAMS AND ALGO FOR MAKING A YEARFILE, PLEASE DROP A COMMENT BELOW WITH YOUR e-mail ADDRESS AND I WILL SEND YOU THE LINK TO DOWNLOAD ONE.
SOME BASIC JAVA PROGRAMS:-

1. DIVISION OF TWO NUMBERS

import java.io.*;
class division
{
  public static void main()throws IOException
  {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
     
      System.out.println("enter a no.");
      String s = br.readLine();
      float a = Float.parseFloat(s);
     
      System.out.println("enter another no.");
      String p = br.readLine();
      float b = Float.parseFloat(p);
     
      float c = a/b;
      float d = a%b;
      System.out.println("the quotient is "+c);
      System.out.println("the remainder is "+d);
    }
}
     

2. DIVISION AND FINDING INTEGRAL QUOTIENT:-

import java.io.*;
class division2
{
  public static void main()throws IOException
  {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
     
      System.out.println("enter a no.(N)");
      String s = br.readLine();
      int n = Integer.parseInt(s);
     
      System.out.println("enter another no.(D)");
      String p = br.readLine();
      int d = Integer.parseInt(p);
     
      double a = new Double(n).doubleValue()/d;
      int b = n/d;
      float r = n%d;
     
      System.out.println("the quotient is "+a);
      System.out.println("the integral quotient is "+b);
      System.out.println("the remainder is "+r);
    }
}
     
     
     
3. CHECK IF A STUDENT HAS PASSED IN 3 SUBJECTS:-
(CALCULATING AVERAGE AND CHECKING IF AVERAGE>50%)
 
import java.io.*;
class result
{  
    public static void main()throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);      
        BufferedReader br = new BufferedReader (isr);
       
        System.out.println("Enter marks in Maths");
        String a = br.readLine();
        float p = Float.parseFloat(a);
       
        System.out.println("Enter marks in English");
        String b = br.readLine();
        float q = Float.parseFloat(b);
       
        System.out.println("Enter marks in Maths");
        String c = br.readLine();
        float r = Float.parseFloat(c);
       
        float n = (p+q+r)/3;
       
        if (n>=50)
        {
                System.out.println("you have passed");
            }
            else
            {
                System.out.println("you have failed");
            }
        }
    }
       
4. PRINTING OF EVEN NUMBERS FROM 1 TO 50
import java.io.*;
class even_numbers
{  
    public static void main()
    {
        for(int i=2 ; i<=50 ; i=i+2)
        {
            System.out.print(i+"  ");
        }
    }
}
       
5. PRINTING OF ODD NUMBERS FROM 1 TO 50

import java.io.*;
class odd_numbers
{  
    public static void main()
    {
        InputStreamReader isr = new InputStreamReader(System.in); 
       
        for(int i=1 ; i<=50 ; i=i+2)
        {
            System.out.println(i);
        }
    }
}
Now, here are some simple java programs you would need for very basic level programming.

1. SUM OF TWO NUMBERS:-

import java.io.*;
class sum
{
  public static void main()throws IOException
  {
      InputStreamReader isr= new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);


      System.out.println("enter a no.");
      String s = br.readLine();
      int a = Integer.parseInt(s);


      System.out.println("enter another no.");
      String p = br.readLine();
      int b = Integer.parseInt(p);


      int c = a+b;
      System.out.println("the sum is "+c);
    }
}    

 
Now, here:-
import.java.io.* calls the Input-Output Package of Java
class sum assigns the name of the class
public static void main() is the main function
InputStreamReader and BufferedReader are parts of the io package of java
System.out.println() is for printing.
 

General OOP Concepts

Java, like many other programming languages is a OOP language. OOP stands for Object Oriented Programming. Java offers many ways to make ypur programme bug free and make your programme easy to maintain. It is a more reliable than other languages and that is the reason why it gained huge popularity.
We all know that a computer can understand only machine languge or assembly language, in which instructions are written in binary code(i.e. 0 and 1s). The Machine language is the only language an computer and execute. Now, it is not possible for us to write programs on machine languge, so the tedious work of converting the code you write into the machine language is done by the java compiler.

Monday, October 7, 2013

Getting Started

To start programming, the first things that you need are a java development kit (JDK)  and a devloping tool. For beginners, i would suggest you to get BlueJ software.

Download Jdk from- www.oracle.com/technetwork/java/javase/downloads/index.html

And blueJ from www.bluej.org