Monday, April 07, 2014

WBUT 5years Java solutions for BCA ,BTech,MCA Course


1.         Write an applet program to change the background color

import java.awt.*;



public class color5 extends java.applet.Applet {



    public void init() {

        Color lightOrange = new Color(230, 220, 0);

        setBackground(lightOrange);

    }




    public void paint(Graphics screen) {

        Color darkRed = new Color(235, 50, 50);

        screen.setColor(darkRed);

        screen.drawString("Welcome", 5, 50);

            Font currentFont = new Font("TimesRoman", Font.PLAIN, 20);

        screen.setFont(currentFont);

        screen.drawString("Welcome", 5, 150);

        currentFont = new Font("TimesRoman", Font.ITALIC, 40);

        screen.setFont(currentFont);

        screen.drawString("Welcome", 5, 250);



    }

}

2.         What is exception? Write a java program to implement try, catch nested try, throw and throws method.


A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. That method may choose to handle the exception itself, or pass it on. Either way, at some point, the exception is caught and processed.

 
i) Nested try block
 
class NestedTry

{

    public static void main(String args[])

    {

        try

        {

            int num = args.length;

            try

            {

                int numValue = Integer.parseInt(args[0]);

                System.out.println("The square is " + numValue * numValue);

            }

        catch(NumberFormatException nb)

        {

            System.out.println("Not a number! ");

        }

    }

        catch(ArrayIndexOutOfBoundsException ne)

        {

            System.out.println("No arguments given! ");

        }

    }

}





ii) try –catch block
 
mport java.io.*;
public class ExcepTest{
 
   public static void main(String args[]){
      try{
         int a[] = new int[2];
         System.out.println("Access element three :" + a[3]);
      }catch(ArrayIndexOutOfBoundsException e){
         System.out.println("Exception thrown  :" + e);
      }
      System.out.println("Out of the block");
   }
}



iii) throws & thriw block
 
import java.io.*;
public class className
{
   public void deposit(double amount) throws RemoteException
   {
      // Method implementation
      throw new RemoteException();
   }
   //Remainder of class definition
}





3.     i)Write a small applet program to draw lines.

ii) Write asmall applet program to draw rectangles.

iii)Write a small applet program to draw ellipses and circles.

iv)Write a small applet program to draw arcs.

v)Write a small applet program to draw polygons.



Sol:

i)
import java.awt.*;
import java.applet.*;
public class line1 extends Applet
{
            public void paint(Graphics g)
            {
                        g.drawLine(40,25,250,180);
            }
}
ii)
import java.awt.*;
import java.applet.*;
public class rect extends Applet
{
            public void paint(Graphics g)
            {
                        g.drawRect(10,10,60,50);
            }
}
iii)
import java.awt.*;
import java.applet.*;
public class circle_ellipse extends Applet
{
            public void paint(Graphics g)
            {
                        g.drawOval(10,10,50,50);//circle
                        g.drawOval(190,10,90,30);// ellipse
            }
}
iv) import java.awt.*;
import java.applet.*;
public class arcs extends Applet
{
            public void paint(Graphics g)
            {
                        g.drawArc(10,100,70,80,0,175);
            }
}

v)

import java.awt.*;

import java.applet.*;

public class polygon extends Applet

{

            public void paint(Graphics g)

            {

                        int xpoints[]={30,200,30,200,30};

                        int ypoints[]={30,30,200,200,30};

                        int num=5;

                        g.drawPolygon(xpoints,ypoints,num);

            }

}

4.          What are different types of controls supported by abstract window toolkit? Explain at least.


Sol:

The Abstract Windowing Toolkit (AWT), or "another windowing toolkit," as some people affectionately call it, provides a large collection of classes for building graphical user interfaces in Java. With AWT, you can create windows, draw, work with images, and use components like buttons, scrollbars, and pull-down menus in a platform independant way. The java.awt package contains the AWT GUI classes. The java.awt.image package provides some additional tools for working with images.



Structure of the AWT


The structure of the AWT is rather simple: Components are added to and then laid out by layoutmanagers in Containers. We have a variety of event handling, menu, fonts, graphics classes in addition to those two, but they'll be only briefly discussed here.
Nothing prevents us from using threads, audio, I/O, networking classes alongside the AWT. The AWT by itself is rather bare and empty. Imagine a text editor that couldn't save! As a side note, the AWT can be used in a stand-alone Java application or in an applet that is embedded within a HTML document.

The AWT hierarchy


Classes:

·         BorderLayout

·         CardLayout

·         CheckboxGroup

·         Color

·         Component

o        Button

o        Canvas

o        Checkbox

o        Choice

o        Container

§         Panel

§         Window

§         Dialog

§         Frame

o        Label

o        List

o        Scrollbar

o        TextCompoment

§         TextArea

§         TextField

·         Dimension

·         Event

·         FileDialog

·         FlowLayout

·         Font

·         FontMetrics

·         Graphics

·         GridLayout

·         GridBagConstraints

·         GridBagLayout

·         Image

·         Insets

·         MediaTracker

·         MenuComponent

o        MenuBar

o        MenuItem

§         CheckboxMenuItem

§         Menu

·         Point

·         Polygon

·         Rectangle

·         Toolkit



5.         What do you mean by command line argument?


A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched.

The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. For example, suppose a Java application called Sort sorts lines in a file. To sort the data in a file named friends.txt, a user would enter:

java Sort friends.txt

When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. In the previous example, the command-line arguments passed to the Sort application in an array that contains a single String: "friends.txt".

Echoing Command-Line Arguments


The Echo example displays each of its command-line arguments on a line by itself:

 
public class Echo {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
}
 

The following example shows how a user might run Echo. User input is in italics.

java Echo Drink Hot Java
Drink
Hot
Java

Note that the application displays each word — Drink, Hot, and Java — on a line by itself. This is because the space character separates command-line arguments. To have Drink, Hot, and Java interpreted as a single argument, the user would join them by enclosing them within quotation marks.

java Echo "Drink Hot Java"
Drink Hot Java


No comments:

Post a Comment