Wednesday, 9 October 2013

java program for Bouncing Ball GAME

java program for Bouncing Ball GAME
This program does a simple animation. Animation is done by creating a timer which calls an ActionListener at fixed intervals (eg, every 35 milliseconds). The listener tells the ball to move it's coordinates a little, then it repaints the panel. repaint() indirectly calls ourpaintComponent() method, which then draws the ball with the updated coordinates.

BBDemo.java - The main program and window creation

// File: animation/bb/BBDemo.java
// Description: Illustrates animation with a ball bouncing in a box
//              Possible extensions: faster/slower button,
// Author: Fred Swartz
// Date:   February 2005 ...

import javax.swing.*;

/////////////////////////////////////////////////////////////// BBDemo
public class BBDemo extends JApplet {
    
    //============================================== applet constructor
    public BBDemo() {
        add(new BBPanel());
    }
    
    //============================================================ main
    public static void main(String[] args) {
        JFrame win = new JFrame("Bouncing Ball Demo");
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        win.setContentPane(new BBPanel());
        
        win.pack();
        win.setVisible(true); 
    }
}//endclass BBDemo

BBPanel.java - The JPanel which organizes the GUI

// File:  animation/bb/BBPanel.java
// Description: Panel to layout buttons and graphics area.
// Author: Fred Swartz
// Date:   February 2005

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/////////////////////////////////////////////////////////////////// BBPanel
class BBPanel extends JPanel {
    BallInBox m_bb;   // The bouncing ball panel
    
    //========================================================== constructor
    /** Creates a panel with the controls and bouncing ball display. */
    BBPanel() {
        //... Create components
        m_bb = new BallInBox();        
        JButton startButton = new JButton("Start");        
        JButton stopButton  = new JButton("Stop");
        
        //... Add Listeners
        startButton.addActionListener(new StartAction());
        stopButton.addActionListener(new StopAction());
        
        //... Layout inner panel with two buttons horizontally
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(startButton);
        buttonPanel.add(stopButton);
        
        //... Layout outer panel with button panel above bouncing ball
        this.setLayout(new BorderLayout());
        this.add(buttonPanel, BorderLayout.NORTH);
        this.add(m_bb       , BorderLayout.CENTER);
    }//end constructor
    
    
    ////////////////////////////////////// inner listener class StartAction
    class StartAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            m_bb.setAnimation(true);
        }
    }
    
    
    //////////////////////////////////////// inner listener class StopAction
    class StopAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            m_bb.setAnimation(false);
        }
    }
}//endclass BBPanel

BallInBox.java - The graphics panel that does the animation.

// File:   animation/bb/BouncingBall.java
// Description: This Graphics panel simulates a ball bouncing in a box.
//         Animation is done by changing instance variables
//         in the timer's actionListener, then calling repaint().
//         * Flicker can be reduced by drawing into a BufferedImage, 
//           and/or using a clip region.
//         * The edge of the oval could be antialiased (using Graphics2).
// Author: Fred Swartz
// Date:   February 2005

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/////////////////////////////////////////////////////////////// BouncingBall
public class BallInBox extends JPanel {
    //============================================== fields
    //... Instance variables representing the ball.
    private Ball m_ball         = new Ball(0, 0, 2, 3);
    
    //... Instance variables for the animiation
    private int   m_interval  = 35;  // Milliseconds between updates.
    private Timer m_timer;           // Timer fires to anmimate one step.

    //========================================================== constructor
    /** Set panel size and creates timer. */
    public BallInBox() {
        setPreferredSize(new Dimension(200, 80));
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        m_timer = new Timer(m_interval, new TimerAction());
    }
    
    //========================================================= setAnimation
    /** Turn animation on or off.
     *@param turnOnOff Specifies state of animation.
     */
    public void setAnimation(boolean turnOnOff) {
        if (turnOnOff) {
            m_timer.start();  // start animation by starting the timer.
        } else {
            m_timer.stop();   // stop timer
        }
    }

    //======================================================= paintComponent
    public void paintComponent(Graphics g) {
        super.paintComponent(g);  // Paint background, border
        m_ball.draw(g);           // Draw the ball.
    }
    
    //////////////////////////////////// inner listener class ActionListener
    class TimerAction implements ActionListener {
        //================================================== actionPerformed
        /** ActionListener of the timer.  Each time this is called,
         *  the ball's position is updated, creating the appearance of
         *  movement.
         *@param e This ActionEvent parameter is unused.
         */
        public void actionPerformed(ActionEvent e) {
            m_ball.setBounds(getWidth(), getHeight());
            m_ball.move();  // Move the ball.
            repaint();      // Repaint indirectly calls paintComponent.
        }
    }
}//endclass

Ball.java - The logic/model of the ball

This class holds the information about the ball, its diameter, position, and velocity. Other attributes are possible (eg, color). This ball knows nothing about animation, only about its current state, how to update its coordinates, and how to draw itself.
// File: animation/bb/BallModel.java
// Description: The logic / model of a ball.
// Author: Fred Swartz
// Date:   February 2005

import java.awt.*;

///////////////////////////////////////////////////////////////// BallModel
public class Ball {
    //... Constants
    final static int DIAMETER = 21;
    
    //... Instance variables
    private int m_x;           // x and y coordinates upper left
    private int m_y;
    
    private int m_velocityX;   // Pixels to move each time move() is called.
    private int m_velocityY;
    
    private int m_rightBound;  // Maximum permissible x, y values.
    private int m_bottomBound;
    
    //======================================================== constructor
    public Ball(int x, int y, int velocityX, int velocityY) {
        m_x = x;
        m_y = y;
        m_velocityX = velocityX;
        m_velocityY = velocityY;
    }
    
    //======================================================== setBounds
    public void setBounds(int width, int height) {
        m_rightBound  = width  - DIAMETER;
        m_bottomBound = height - DIAMETER;
    }
    
    //============================================================== move
    public void move() {
        //... Move the ball at the give velocity.
        m_x += m_velocityX;
        m_y += m_velocityY;        
        
        //... Bounce the ball off the walls if necessary.
        if (m_x < 0) {                  // If at or beyond left side
            m_x         = 0;            // Place against edge and
            m_velocityX = -m_velocityX; // reverse direction.
            
        } else if (m_x > m_rightBound) { // If at or beyond right side
            m_x         = m_rightBound;    // Place against right edge.
            m_velocityX = -m_velocityX;  // Reverse direction.
        }
        
        if (m_y < 0) {                 // if we're at top
            m_y       = 0;
            m_velocityY = -m_velocityY;
            
        } else if (m_y > m_bottomBound) { // if we're at bottom
            m_y       =  m_bottomBound;
            m_velocityY = -m_velocityY;
        }
    }
    
    //============================================================== draw
    public void draw(Graphics g) {
        g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
    }
    
    //============================================= getDiameter, getX, getY
    public int  getDiameter() { return DIAMETER;}
    public int  getX()        { return m_x;}
    public int  getY()        { return m_y;}
    
    //======================================================== setPosition
    public void setPosition(int x, int y) {
        m_x = x;
        m_y = y;
    }
}

JAVA PROGRAMS 8

Write a Java program that:
i)     Implements stack ADT.

PROGRAM:

import java.util.*;

public class StackADT
{
        int a[];
        int top;
            public StackADT(int n)         
            {         
                        a = new int[n];
                        top = -1;         
            }
            public void push(int item)
            {
                if(isFull())
                        {
                                    System.out.println("Stack is full");
                        return;
                        }
                a[++top] = item;
                System.out.println("Item Pushed");
            }
            public int pop()
            {
                if(isEmpty()) 
                        {         
                                    System.out.println("Stack is empty");
                        return 0;
                        }  
                return a[top--];
            }
            public int topElement()   
            {         
                if(isEmpty())           return 0;
                else                          return a[top];
            }
            public boolean isEmpty()    
            {         
                return (top <= -1);
            }
        public boolean isFull()
        {
                return (top >= a.length-1);
        }
        public int count()
        { 
                        return top+1;
         }
        public void display()
        {
                 System.out.print("Items In Stack Are: ");
                 for(int i=0;i<=top;i++)
                        System.out.print(" "+a[i]);
        }
        public static void main(String args[])
        {
                int x,n,ch=0;
                Scanner sc=new Scanner(System.in);
                System.out.println("Enter Stack Size:");
                n=sc.nextInt();
                StackADT st=new StackADT(n);
                System.out.println("\t-----LIST OF OPERATIONS------");
                System.out.println("\t\t[1].Push");
                System.out.println("\t\t[2].Pop");
                System.out.println("\t\t[3].Top Element");
                System.out.println("\t\t[4].Count");
                System.out.println("\t\t[5].Empty");
                System.out.println("\t\t[6].Full");
                System.out.println("\t\t[7].Display");
                System.out.println("\t\t[0].EXIT");
                do
                {
                System.out.print("\nEnter Choice Of Operation:");
                ch=sc.nextInt();
                switch(ch)
                {
                        case 1: System.out.print("Enter Element: ");
                                x=sc.nextInt();
                                st.push(x);
                                break;
                        case 2: x=st.pop();
                                if(x!=0)                System.out.println("Item Poped: "+x);
                                break;
                        case 3: x=st.topElement();
                                if(x!=0)                System.out.println("Top Item: "+x);
                                else                      System.out.println("Stack Is Empty");
                                break;
                        case 4: System.out.println("Number Of Items In Stack: "+st.count());
                                break;
                        case 5: if(st.isEmpty()) System.out.println("Stack Is Empty");
                                else                        System.out.println("Stack Is Not Empty");
                                break;
                        case 6: if(st.isFull())     System.out.println("Stack Is Full");
                                else                        System.out.println("Stack Is Not Full");
                                break;
                        case 7: if(st.isEmpty()) System.out.println("Stack Is Empty");
                                else                        st.display();
                        case 0: break;
                        default: System.out.println("Invalid Choice Of Operation");
                        }
                }while(ch!=0);              }    }

Output:
   -----LIST OF OPERATIONS------
                [1].Push
                [2].Pop
                [3].Top Element
                [4].Count
                [5].Empty
                [6].Full
                [7].Display
                [0].EXIT

Enter Stack Size:
10
Enter Choice Of Operation:1
Enter Element: 10
Item Pushed
Enter Choice Of Operation:1
Enter Element: 20
Item Pushed
Enter Choice Of Operation:7
Items In Stack Are:  10 20
Enter Choice Of Operation:3
Top Item: 10
Enter Choice Of Operation:4
Number Of Items In Stack: 1

Enter Choice Of Operation:0

JAVA PROGRAMS 7

a)    Write a Java program for sorting a given list of names in ascending order.

Program:

import java.io.*;
class Ascend
{
        public static void main(String args[]) throws IOException
        {
                int n,i,j;
                String temp;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("\nEnter Number Of Names:");
                n=Integer.parseInt(br.readLine());
                String s[]=new String[n];
                System.out.println("Enter "+n+" Names:");
                for(i=0;i<n;i++)
                        s[i]=br.readLine();
                for(i=0;i<n;i++)
                        {
                                    for(j=0;j<n;j++)
                                    {
                                                if((s[j].compareTo(s[i]))>0)
                                                {
                                                            temp=s[i];
                                                            s[i]=s[j];
                                                            s[j]=temp;
                                                }
                                    }
                        }
                System.out.println("\nAscending Order Of Names:");
                        for(i=0;i<n;i++)
                                     System.out.println(s[i]);
        }
}

Output:

Enter Number Of Names:2
Enter 2 Names:
vineela
aswini
Ascending Order Of Names:
aswini

vineela

JAVA PROG 6

Week-3:
a)    Write a Java program that checks whether a given string is a palindrome or not.


Program:

import java.io.*;
class Palindrome
{
        public static void main(String args[]) throws IOException
        {
                int i,j;
                boolean flag=true;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("\nEnter String:");
                String s=br.readLine();
                j=s.length()-1;
                for(i=0;i<=j;i++)
                {
                    if(s.charAt(i)!=s.charAt(j--))
                                flag=false;

                }
                if(flag==false)         System.out.println("\n"+s+" Is Not Palindrome"); 
                else                          System.out.println("\n"+s+" Is Palindrome");
        }
}


Output:

Enter String: malayalam


malayalam Is Palindrome

JAVA PROGRAMS 5

a)    Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use StringTokenizer class of java.util)

Program:

import java.io.*;
import java.util.*;

class SumOfInt
{
        public static void main(String args[]) throws IOException
        {
                int sum=0;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("\nEnter String Of Integers:");
                String s=br.readLine();
                StringTokenizer st=new StringTokenizer(s,",");
                while(st.hasMoreTokens())
                {
                        int i=Integer.parseInt(st.nextToken());
                        sum=sum+i;
                        System.out.println(i);
                }
                System.out.println("\nSum Of Integers Is: "+sum);

        }
}
Output:

Enter String Of Integers:12,5,3,1,2
12
5
3
1
2


Sum Of Integers Is: 23

FREE HIT COUNTERS