Wednesday, February 19, 2020

JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS
  1. How can you perform thread scheduling by setting priorities to threads? Explain the same with an example.
  2. Write a Java program to read from file and print file data on the user screen.
  3. Explain various states in the life cycle of an applet. And also give the syntax of each state.
  4. Explain the delegation event model of handling events with a suitable JAVA program
  5. Write short notes on
    • Different ways of creating a frame in AWT
    • AWT Layout Manager
    • JAVA AWT Checkbox 

Monday, February 17, 2020

Exercise – 16 (Swings - Continued)


Exercise – 16 (Swings - Continued)

a). Write a JAVA program that to create a single ball bouncing inside a JPanel.



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

public class BouncingBall extends JPanel {

  // Box height and width
  int width;
  int height;

  // Ball Size
  float radius = 40;
  float diameter = radius * 2;

  // Center of Call
  float X = radius + 50;
  float Y = radius + 20;

  // Direction
  float dx = 3;
  float dy = 3;

  public BouncingBall() {

    Thread thread = new Thread() {
      public void run() {
        while (true) {

          width = getWidth();
          height = getHeight();

          X = X + dx ;
          Y = Y + dy;

          if (X - radius < 0) {
            dx = -dx;
            X = radius;
          } else if (X + radius > width) {
            dx = -dx;
            X = width - radius;
          }

          if (Y - radius < 0) {
            dy = -dy;
            Y = radius;
          } else if (Y + radius > height) {
            dy = -dy;
            Y = height - radius;
          }
          repaint();

          try {
            Thread.sleep(50);
          } catch (InterruptedException ex) {
          }

        }
      }
    };
    thread.start();
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.RED);
    g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Bouncing Ball");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setContentPane(new BouncingBall());
    frame.setVisible(true);
  }
}


b). Write a JAVA program JTree as displaying a real tree upside down



import javax.swing.*; 
import javax.swing.tree.DefaultMutableTreeNode; 
public class TreeExample extends JFrame { 
JFrame f; 
TreeExample(){ 
    f=new JFrame(); 
    DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style"); 
    DefaultMutableTreeNode color=new DefaultMutableTreeNode("color"); 
    DefaultMutableTreeNode font=new DefaultMutableTreeNode("font"); 
    style.add(color); 
    style.add(font); 
    DefaultMutableTreeNode red=new DefaultMutableTreeNode("red"); 
    DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue"); 
    DefaultMutableTreeNode black=new DefaultMutableTreeNode("black"); 
    DefaultMutableTreeNode green=new DefaultMutableTreeNode("green"); 
    color.add(red); color.add(blue); color.add(black); color.add(green);     
    JTree jt=new JTree(style); 
    f.add(jt); 
    f.setSize(200,200); 
    f.setVisible(true); 

public static void main(String[] args) { 
    new TreeExample(); 
}}

Exercise - 15 (Swings)


Exercise - 15 (Swings)

a).Write a java program to built a Calculator in swings



import javax.swing.*;
import java.awt.event.*;
 
 
class Calc implements ActionListener
{
    JFrame f;
    JTextField t;
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;
 
    static double a=0,b=0,result=0;
    static int operator=0;
 
    Calc()
    {
        f=new JFrame("Calculator");
        t=new JTextField();
        b1=new JButton("1");
        b2=new JButton("2");
        b3=new JButton("3");
        b4=new JButton("4");
        b5=new JButton("5");
        b6=new JButton("6");
        b7=new JButton("7");
        b8=new JButton("8");
        b9=new JButton("9");
        b0=new JButton("0");
        bdiv=new JButton("/");
        bmul=new JButton("*");
        bsub=new JButton("-");
        badd=new JButton("+");
        bdec=new JButton(".");
        beq=new JButton("=");
        bdel=new JButton("Delete");
        bclr=new JButton("Clear");
        
        t.setBounds(30,40,280,30);
        b7.setBounds(40,100,50,40);
        b8.setBounds(110,100,50,40);
        b9.setBounds(180,100,50,40);
        bdiv.setBounds(250,100,50,40);
        
        b4.setBounds(40,170,50,40);
        b5.setBounds(110,170,50,40);
        b6.setBounds(180,170,50,40);
        bmul.setBounds(250,170,50,40);
        
        b1.setBounds(40,240,50,40);
        b2.setBounds(110,240,50,40);
        b3.setBounds(180,240,50,40);
        bsub.setBounds(250,240,50,40);
        
        bdec.setBounds(40,310,50,40);
        b0.setBounds(110,310,50,40);
        beq.setBounds(180,310,50,40);
        badd.setBounds(250,310,50,40);
        
        bdel.setBounds(60,380,100,40);
        bclr.setBounds(180,380,100,40);
        
        f.add(t);
        f.add(b7);
        f.add(b8);
        f.add(b9);
        f.add(bdiv);
        f.add(b4);
        f.add(b5);
        f.add(b6);
        f.add(bmul);
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(bsub);
        f.add(bdec);
        f.add(b0);
        f.add(beq);
        f.add(badd);
        f.add(bdel);
        f.add(bclr);
        
        f.setLayout(null);
        f.setVisible(true);
        f.setSize(350,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        b0.addActionListener(this);
        badd.addActionListener(this);
        bdiv.addActionListener(this);
        bmul.addActionListener(this);
        bsub.addActionListener(this);
        bdec.addActionListener(this);
        beq.addActionListener(this);
        bdel.addActionListener(this);
        bclr.addActionListener(this);
    }
 
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
            t.setText(t.getText().concat("1"));
        
        if(e.getSource()==b2)
            t.setText(t.getText().concat("2"));
        
        if(e.getSource()==b3)
            t.setText(t.getText().concat("3"));
        
        if(e.getSource()==b4)
            t.setText(t.getText().concat("4"));
        
        if(e.getSource()==b5)
            t.setText(t.getText().concat("5"));
        
        if(e.getSource()==b6)
            t.setText(t.getText().concat("6"));
        
        if(e.getSource()==b7)
            t.setText(t.getText().concat("7"));
        
        if(e.getSource()==b8)
            t.setText(t.getText().concat("8"));
        
        if(e.getSource()==b9)
            t.setText(t.getText().concat("9"));
        
        if(e.getSource()==b0)
            t.setText(t.getText().concat("0"));
        
        if(e.getSource()==bdec)
            t.setText(t.getText().concat("."));
        
        if(e.getSource()==badd)
        {
            a=Double.parseDouble(t.getText());
            operator=1;
            t.setText("");
        } 
        
        if(e.getSource()==bsub)
        {
            a=Double.parseDouble(t.getText());
            operator=2;
            t.setText("");
        }
        
        if(e.getSource()==bmul)
        {
            a=Double.parseDouble(t.getText());
            operator=3;
            t.setText("");
        }
        
        if(e.getSource()==bdiv)
        {
            a=Double.parseDouble(t.getText());
            operator=4;
            t.setText("");
        }
        
        if(e.getSource()==beq)
        {
            b=Double.parseDouble(t.getText());
        
            switch(operator)
            {
                case 1: result=a+b;
                    break;
        
                case 2: result=a-b;
                    break;
        
                case 3: result=a*b;
                    break;
        
                case 4: result=a/b;
                    break;
        
                default: result=0;
            }
        
            t.setText(""+result);
        }
        
        if(e.getSource()==bclr)
            t.setText("");
        
        if(e.getSource()==bdel)
        {
            String s=t.getText();
            t.setText("");
            for(int i=0;i<s.length()-1;i++)
            t.setText(t.getText()+s.charAt(i));
        }        
    }
 
    public static void main(String...s)
    {
        new Calc();
    }
}

b). Write a JAVA program to display the digital watch in swing tutorial.


import javax.swing.*; 
import java.awt.*; 
import java.text.*; 
import java.util.*; 
public class DigitalWatch implements Runnable{ 
JFrame f; 
Thread t=null; 
int hours=0, minutes=0, seconds=0; 
String timeString = ""; 
JButton b; 
 
DigitalWatch(){ 
    f=new JFrame(); 
     
    t = new Thread(this); 
        t.start(); 
     
    b=new JButton(); 
        b.setBounds(100,100,100,50); 
     
    f.add(b); 
    f.setSize(300,400); 
    f.setLayout(null); 
    f.setVisible(true); 

 
 public void run() { 
      try { 
         while (true) { 
 
            Calendar cal = Calendar.getInstance(); 
            hours = cal.get( Calendar.HOUR_OF_DAY ); 
            if ( hours > 12 ) hours -= 12; 
            minutes = cal.get( Calendar.MINUTE ); 
            seconds = cal.get( Calendar.SECOND ); 
 
            SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss"); 
            Date date = cal.getTime(); 
            timeString = formatter.format( date ); 
 
            printTime(); 
 
            t.sleep( 1000 );  // interval given in milliseconds 
         } 
      } 
      catch (Exception e) { } 
 } 
 
public void printTime(){ 
b.setText(timeString); 

 
public static void main(String[] args) { 
    new DigitalWatch(); 
         
 

Exercise - 14 (Event Handling)


Exercise - 14 (Event Handling)

a).Write a JAVA program that display the x and y position of the cursor movement using
Mouse.


import java.awt.*;

import java.awt.event.*;
import java.applet.Applet;
public class AppletMouse extends Applet implements MouseListener, MouseMotionListener
{
  int x, y;
  String str="";
  public void init()
  {
    addMouseListener(this);
    addMouseMotionListener(this);
  }
                                    // override ML 5 abstract methods
  public void mousePressed(MouseEvent e)
  {
    x = e.getX();
    y = e.getY();
    str = "Mouse Pressed";
    repaint();
  }
  public void mouseReleased(MouseEvent e)
  {
    x = e.getX();
    y = e.getY();
    str = "Mouse Released";
    repaint();
   }
   public void mouseClicked(MouseEvent e)
   {
     x = e.getX();
     y = e.getY();
     str = "Mouse Clicked";
     repaint();
   }
   public void mouseEntered(MouseEvent e)
   {
     x = e.getX();
     y = e.getY();
     str = "Mouse Entered";
     repaint();
   }
   public void mouseExited(MouseEvent e)
   {
     x = e.getX();
     y = e.getY();
     str = "Mouse Exited";
     repaint();
   }
                                    // override two abstract methods of MouseMotionListener
   public void mouseMoved(MouseEvent e)
   {
     x = e.getX();
     y = e.getY();
     str = "Mouse Moved";
     repaint();
   }
   public void mouseDragged(MouseEvent e)
   {
     x = e.getX();
     y = e.getY();
     str = "Mouse dragged";
     repaint();
   }
                                    // called by repaint() method
   public void paint(Graphics g)
   {
     g.setFont(new Font("Monospaced", Font.BOLD, 20));
     g.fillOval(x, y, 10, 10);
     g.drawString(x + "," + y,  x+10, y -10);
     g.drawString(str, x+10, y+20);
     showStatus(str + " at " + x + "," + y);
   }
}

Applet code:Applet code save as .html  file

<html>  
<body>  
<applet code="AppletMouse.class" width="300" height="300">  
</applet>  
</body>  
</html>  

b).Write a JAVA program that identifies key-up key-down event user entering text in a  Applet.



import java.applet.Applet;
import java.awt.*;

public class KeyUpDown1 extends Applet {
   private Font f;
   private String letter;
   private boolean first;

   public void init()
   {
      f = new Font( "Courier", Font.BOLD, 72 );
      first = true;
   }

   public void paint( Graphics g )
   {
      g.setFont( f );

      if ( !first )
         g.drawString( letter, 75, 70 );
   }

   public boolean keyDown( Event e, int key )
   {
      showStatus( "keyDown: the " + ( char ) key +
                  " was pressed." );

      letter = String.valueOf( ( char ) key );
      first = false;
      repaint();

      return true;   // event has been handled
   }

   public boolean keyUp( Event e, int key )
   {
      showStatus( "keyUp: the " + ( char ) key +
                  " was released." );

      return true;   // event has been handled
   }
}


Applet code:Applet code save as .html

<html>  
<body>  
<applet code="KeyUpDown1.class" width="300" height="300">  
</applet>  
</body>  
</html>  

Monday, February 10, 2020

Exercise - 13 (Applet)

Exercise - 13 (Applet)

a).Write a JAVA program to paint like paint brush in applet.

import java.awt.*;  
import java.awt.event.*;  
import java.applet.*;  
public class MouseDrag extends Applet implements MouseMotionListener
{
public void init()
{  
addMouseMotionListener(this);  
setBackground(Color.red);  
}  
public void mouseDragged(MouseEvent me)
{  
Graphics g=getGraphics();  
g.setColor(Color.white);  
g.fillOval(me.getX(),me.getY(),10,10);  // (x-position, y-postion, width, height)
}  
public void mouseMoved(MouseEvent me)
{
}  
}

Applet Code: Applet Code save as .html file.

<html>
<applet code="MouseDrag.class" height=300 width=400>
</applet>
</html>

b) Write a JAVA program to display analog clock using Applet.

import java.applet.*;  
import java.awt.*;  
import java.util.*;  
import java.text.*;  
public class MyClock extends Applet implements Runnable 
{  
int width, height;  
Thread t = null;  
boolean threadSuspended;  
int hours=0, minutes=0, seconds=0;  
String timeString = "";  
public void init() 
{  
      width = getSize().width;  
      height = getSize().height;  
setBackground( Color.black );  
    }  
public void start() 
{  
if ( t == null ) 
{  
t = new Thread( this );  
t.setPriority( Thread.MIN_PRIORITY );  
threadSuspended = false;  
t.start();  
}  
else 
{  
if ( threadSuspended ) 
{  
threadSuspended = false;  
synchronized( this ) 
{  
notify();  
            }  
          }  
      }  
    }  
    public void stop() 
{  
      threadSuspended = true;  
    }  
public void run() 
{  
      try 
{  
          while (true) 
{  

            Calendar cal = Calendar.getInstance();  
            hours = cal.get( Calendar.HOUR_OF_DAY );  
            if ( hours> 12 ) hours -= 12;  
            minutes = cal.get( Calendar.MINUTE );  
            seconds = cal.get( Calendar.SECOND );  

            SimpleDateFormat formatter = new SimpleDateFormat( "hh:mm:ss", Locale.getDefault() );  
            Date date = cal.getTime();  
            timeString = formatter.format( date );  

            // Now the thread checks to see if it should suspend itself  
            if ( threadSuspended ) 
{  
synchronized( this ) 
{  
                  while ( threadSuspended ) 
{  
wait();  
                  }  
                }  
            }  
repaint();  
t.sleep( 1000 );  // interval specified in milliseconds  
          }  
      }  
      catch (Exception e) 
}
    }  
    void drawHand( double angle, int radius, Graphics g ) 
{  
      angle -= 0.5 * Math.PI;  
      int x = (int)( radius*Math.cos(angle) );  
      int y = (int)( radius*Math.sin(angle) );  
g.drawLine( width/2, height/2, width/2 + x, height/2 + y );  
    }  
    void drawWedge( double angle, int radius, Graphics g ) 
{  
      angle -= 0.5 * Math.PI;  
      int x = (int)( radius*Math.cos(angle) );  
      int y = (int)( radius*Math.sin(angle) );  
      angle += 2*Math.PI/3;  
      int x2 = (int)( 5*Math.cos(angle) );  
      int y2 = (int)( 5*Math.sin(angle) );  
      angle += 2*Math.PI/3;  
      int x3 = (int)( 5*Math.cos(angle) );  
      int y3 = (int)( 5*Math.sin(angle) );  
g.drawLine( width/2+x2, height/2+y2, width/2 + x, height/2 + y );  
g.drawLine( width/2+x3, height/2+y3, width/2 + x, height/2 + y );  
g.drawLine( width/2+x2, height/2+y2, width/2 + x3, height/2 + y3 );  
    }  
    public void paint( Graphics g ) 
{  
g.setColor( Color.white );  
drawWedge( 2*Math.PI * hours / 12, width/5, g );  
drawWedge( 2*Math.PI * minutes / 60, width/3, g );  
drawHand( 2*Math.PI * seconds / 60, width/2, g );  
g.setColor( Color.white );  
g.drawString( timeString, 10, height-10 );  
    }  
}

Applet Code: Applet Code save as .html file.

<html>
<applet code="MyClock.class" height=300 width=400>
</applet>
</html>

c). Write a JAVA program to create different shapes and fill colors using Applet.

import java.applet.*;
import java.awt.*;
public class  ShapeColor extends Applet
{
  int x=300,y=100,r=50;
public void paint(Graphics g)
{
g.setColor(Color.red);  //Drawing line color is red
g.drawLine(3,300,200,10);
g.setColor(Color.magenta);  
g.drawString("Line",100,100);
g.drawOval(x-r,y-r,100,100);
g.setColor(Color.yellow);  //Fill the yellow color in circle
g.fillOval( x-r,y-r, 100, 100 );
g.setColor(Color.magenta);
g.drawString("Circle",275,100);
g.drawRect(400,50,200,100);
g.setColor(Color.yellow);  //Fill the yellow color in rectangel
g.fillRect( 400, 50, 200, 100 );
g.setColor(Color.magenta);
g.drawString("Rectangel",450,100);
  }
}

Applet Code: Applet Code save as .html file.

<html>
<applet code="ShapeColor.class" height=300 width=400>
</applet>
</html>

Monday, February 3, 2020

Exercise – 12 (Packages)


Exercise – 12 (Packages) 


a). Write a JAVA program illustrate class path 


import java.net.URL;

import java.net.URLClassLoader;
public class App
{
public static void main(String[] args)
{
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
for(int i=0; i< urls.length; i++)
{
System.out.println(urls[i].getFile());
}
}
}

b). Write a case study on including in class path in your os environment of your package.  

COPEING THE PATH OF JAVA:-

First of all go to MYCOMPUTER and go to the drive where the java is installed.In that go to

PROGRAM FILES and then double click on java folder.
In that we have observed there is a folder with name java jdk,double click on the java jdk folder
and then go into the bin folder.
At this time we have to copy the path of the bin folder.

SETTING THE JAVA PATH:-

Inorder to set the path of the java in our system,first of all we need to open CONTROL PANEL
in our system and go to SYSTEM SETTINGS .

In system settings we need to go into the ADVANCED SYSTEM SETTINGS settings.In

advanced system settings we just click on ENVIRONMENT VARIABLES option.
In USER VARIABLES click on NEW button and type the " path" at VARIABLE NAME.
We need to paste the previously copied path of the bin folder at the place of VARIABLE VALUE.
Finally click on OK and then OK,then close the MYCOMPUTR window.Now the java path is set.We are ready to use java facilities in our computer.


c). Write a JAVA program that import and use the defined your package in the previous  Problem


1).

package pack;

public class Addition
{
private double d1,d2;
public Addition(double a, double b)
{
d1=a;
d2=b;
}
public void sum()
{
System.out.println("Sum inside of  class Addition is:"+(d1+d2));
}
}

Compilation of the program :- javac -d . Addition.java
2).
import pack.Addition;
class Use
{
public static void main(String args[])
{
Addition obj=new Addition(10,15.5);
obj.sum();
}
}

JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS

SIR C R REDDY COLLEGE OF ENGINEERING, ELURU DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS ...