You are on page 1of 18

B.Sc.

IT 09-01
JAVA PROGRAMMING
Q.1) Write your own exception class. Now with a working example show
how do you make use of this exception in another class?

Ans: -
import java.lang.Exception;
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}
class TestMyException
{
public TestMyException
{
int x=5; y=1000;
try
{
float z=(float) x/(float) y;
if(z<0.01)
{
throw new MyException
(“Number is to small”);
}
}
catch(MyException e)
{
System.out.println(“Caugh MyException”);
System.out.println(e.getmessage());
}
finally
{
System.out.println(“I am always here”);
}
}
}
Q.3) Which important feature of Object Oriented approach is realized
using interfaces? Explain.

Ans: - Followings are the component of Object Oriented Program: -

 Object and Classes

 Data Abstraction and Encapsulatiory

 Inheritance

 Polymorphism

 Dynamic Binding

 Message Communication

In which Inheritance is a great feature of Object Oriented Program. It


produce a important feature , “interface”.

Java introduces the notion of interfaces of recover much of the functionality


that multiple inheritance gives you. Java chose this concept because:

 Multiple inheritance gives makes compliers either very complex(as in


C++) or very inefficient(as in Eiffel).

 With multiple inheritance there are name clashes in the base classes.
That is, if two base classes have the methods with the same name, then
this results in the name clash in the class which inherits these two base
classes.

Some uses of interfaces in Object Oriented Programming:

 Interfaces are a way of saying, “You need to plug some code in here
for this thing to fully work”. The interface specifies the exact
signatures of the methods that must be provided.

 Use the interface type as a Parameter for a method. Inside the method
you can invoke any of the methods promised by the interface
parameter. When you actually call the method, You will have to
provide an object that has all the methods promised by the interface.
It might help to think of this as a bit like passing in a subclass as an
argument and having the correct overriding methods called.
 Interfaces can be used to mix in generally useful constants. So, for
example, if an interface defines a set of constants, and then multiple
classes use those constants, the values of those constants could be
globally changed without having to modify multiple classes. That is
interfaces separate design from implemention.

Q.4) Design an applet GUI for a simple calculator.

Ans: -

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.lang.*;
/*<applet code="Cal.class" width=600 height=500></applet>*/
public class Cal extends Applet implements ActionListener
{
String x="",y="",z,w;
float rp=0.0f,sum=0.0f;
Button visible;
Button b1,b2,be,bp,bce,bexit;
Button b3,b4,b5,b6,b7,b8,b9,b0,bmu,bd,bpr,bm;
TextField tf;
Panel p1,p2;
public void init()
{
visible=new Button("Back");
bvisible.setForeground(Color.red);
Label l=new Label(" ");
Font ft=new Font("Arial",Font.BOLD,30);
bexit=new Button("Exit");
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b0=new Button("0");
bmu=new Button("x");
bd=new Button("/");
bpr=new Button("%");
bp=new Button("+");
be=new Button("=");
bm=new Button("-");
bce=new Button("ce");
//color Button//
bce.setForeground(Color.red);
bmu.setForeground(Color.red);
bm.setForeground(Color.red);
bpr.setForeground(Color.red);
bp.setForeground(Color.red);
bd.setForeground(Color.red);
be.setForeground(Color.red);
bexit.setForeground(Color.red);
//Font//
b1.setFont(ft);
b2.setFont(ft);
b3.setFont(ft);
b4.setFont(ft);
b5.setFont(ft);
b6.setFont(ft);
b7.setFont(ft);
b8.setFont(ft);
b9.setFont(ft);
b0.setFont(ft);
bp.setFont(ft);
bm.setFont(ft);
be.setFont(ft);
bmu.setFont(ft);
bce.setFont(ft);
bexit.setFont(ft);
bpr.setFont(ft);
bd.setFont(ft);
p1=new Panel();
p2=new Panel();
add(visible);
tf=new TextField(10);
tf.setText("");
tf.setFont(ft);
GridLayout g=new GridLayout(5,4,2,2);
p1.setLayout(g);
BorderLayout br=new BorderLayout(6,6);
p2.setLayout(br);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(bm);
p1.add(bpr);
p1.add(b0);
p1.add(bmu);
p1.add(bd);
p1.add(be);
p1.add(bce);
p1.add(bexit);
p2.add("North",l);
p2.add("Center",tf);
p2.add("South",p1);
add(p2);

//add border

p2.setBackground(Color.red);
p1.setBackground(Color. gray);
}
}

Q.5) For the calculator designed in the previous question, add


functionality.

Ans:-
This is functional part of previous question
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);
bm.addActionListener(this);
bd.addActionListener(this);
bpr.addActionListener(this);
bmu.addActionListener(this);
be.addActionListener(this);
bp.addActionListener(this);
bce.addActionListener(this);
bvisible.addActionListener(this);
bexit.addActionListener(this);

}//end of init() method;

public void actionPerformed(ActionEvent e)


{
if(e.getSource()==b1)
{
tf.setText(x);
x=tf.getText()+"1";
tf.setText(x);
}

else if(e.getSource()==b2)
{
tf.setText(x);
x=tf.getText()+"2";
tf.setText(x);
}
else if(e.getSource()==b3)
{
tf.setText(x);
x=tf.getText()+"3";
tf.setText(x);
}
else if(e.getSource()==b4)
{
tf.setText(x);
x=tf.getText()+"4";
tf.setText(x);
}
else if(e.getSource()==b5)
{
tf.setText(x);
x=tf.getText()+"5";
tf.setText(x);
}
else if(e.getSource()==b6)
{
tf.setText(x);
x=tf.getText()+"6";
tf.setText(x);
}
else if(e.getSource()==b7)
{
tf.setText(x);
x=tf.getText()+"7";
tf.setText(x);
}
else if(e.getSource()==b8)
{
tf.setText(x);
x=tf.getText()+"8";
tf.setText(x);
}
else if(e.getSource()==b9)
{
tf.setText(x);
x=tf.getText()+"9";
tf.setText(x);
}
else if(e.getSource()==b0)
{
tf.setText(x);
x=tf.getText()+"0";
tf.setText(x);
}

else if(e.getSource()==bp)
{
y=e.getActionCommand();
rp=Float.valueOf(tf.getText()).floatValue();
x=" ";
}
else if(e.getSource()==bm)
{
y=e.getActionCommand();
rp=Float.valueOf(tf.getText()).floatValue();
x="";
}
else if(e.getSource()==bd)
{
y=e.getActionCommand();
rp=Float.valueOf(tf.getText()).floatValue();
x="";
}
else if(e.getSource()==bmu)
{
y=e.getActionCommand();
rp=Float.valueOf(tf.getText()).floatValue();
x="";
}
else if(e.getSource()==bpr)
{
if(y=="x")
{
sum=Float.valueOf(tf.getText()).floatValu
e();
sum=sum*rp/100;
tf.setText(String.valueOf(sum));
x="";
}
else if(y=="/")
{
sum=Float.valueOf(tf.getText()).floatValue(
);
sum=(rp*100)/sum;
tf.setText(String.valueOf(sum));
}
}
else if(e.getSource()==be)
{
if(y=="+")
{
sum=Float.valueOf(tf.getText()).floatValu
e();
sum=rp+sum;
tf.setText(String.valueOf(sum));
x="";
}
else if(y=="-")
{
sum=Float.valueOf(tf.getText()).floatValue(
);
sum=rp-sum;
tf.setText(String.valueOf(sum));
x="";
}
else if(y=="x")
{
sum=Float.valueOf(tf.getText()).floatValue(
);
sum=rp*sum;
tf.setText(String.valueOf(sum));
x="";
}
}
else if(e.getSource()==bce)
{
tf.setText( " " );
x="";
}

}//end of action method

}//end of class
B.Sc.IT 09-02
JAVA PROGRAMMING
Q.1) Write a program that finds the sum of 5 numbers given through
command line.

Ans: - Following program the sum of 5 numbers given through command


line: -
class CommandLine
{
public static void main(String args[])
{
int i, sum=0;
for(i=0;i<args.length;i++)
sum += Integer.parseInt(args[i]);
System.out.println("Sum of numbers="+sum);
}
}

Q.3) With an example how interfaces are used dynamically?

Ans:- Once we have defined an interface, we can use it as the type of some
parameter to a method. Inside the method we can use that parameter to invoke the
operations that the interface promises. Whenever we make a call to that method, we
can pass it a class that implements the interface, and it will call the appropriate
method of that class. The following four steps make this clear.

i. Define an interface that promises method called run() like this.

public interface Runnable


{
public void run();
}
ii. Now we can write a class which calls this run() method, even though we
haven’t yet implemented this interface in any of the classes.

public class AudioPlayer


{
...
public void playAudio(Runnable r)
{
r.run();
}
}

This code can be compiled, and may even be part of the Java runtime library.

iii. At a later time and in another file, provide a class(or several classes)that
implements the above interface.

public class Runner implements Runnable


{
public void run()
{
//give implementation here.
...
}
...
}

Pass Runner object to the PlayAudio method:

Runner myRunner=new Runner();


PlayAudio(myRunner);
Whenever the playAudio is invoked it results in myRunner.run() being called what
you do with a function pointer in C or C++. Java callbacks are type-safe, but
function pointers might not be.

The main reason for writing your code this way is that it decouples the
calling routine from the called-back routine. You could have several different classes
that implemet the Runnable interface. Callbacks allow any of them to be sent to the
playAudio and hence to be called back. The correct class is called back, based on the
type at runtime.
Q.4) Design an applet GUI for a scientific calculator.

Ans:-

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.lang.*;
/*<applet code="Cal. class" width=600 height=500></applet>*/
public class Cal extends Applet implements ActionListener
{
String x="",y="",z,w;
float rp=0.0f,sum=0.0f;
Button visible;
Button b1,b2,be,bp,bce,bexit;
Button b3,b4,b5,b6,b7,b8,b9,b0,bmu,bd,bpr,bm;
TextField tf;
Panel p1,p2;
public void init()
{
visible visible.setForeground =new
Button("Back");
(Color.red);
Label l=new Label(" ");
Font ft=new Font("Arial",Font.BOLD,30);
bexit=new Button("Exit");
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b0=new Button("0");
bmu=new Button("x");
bd=new Button("/");
bpr=new Button("%");
bp=new Button("+");
be=new Button("=");
bm=new Button("-");
bce=new Button("ce");
//color Button//
bce.setForeground(Color.red);
bmu.setForeground(Color.red);
bm.setForeground(Color.red);
bpr.setForeground(Color. red);
bp.setForeground(Color. red);
bd.setForeground(Color. red);
be.setForeground(Color. red);
bexit.setForeground(Color. red);
//Font//
b1.setFont(ft);
b2.setFont(ft);
b3.setFont(ft);
b4.setFont(ft);
b5.setFont(ft);
b6.setFont(ft);
b7.setFont(ft);
b8.setFont(ft);
b9.setFont(ft);
b0.setFont(ft);
bp.setFont(ft);
bm.setFont(ft);
be.setFont(ft);
Bmu.setFont(ft);
bce.setFont(ft);
bexit.setFont(ft);
bpr.setFont(ft);
bd.setFont(ft);
p1=new Panel();
p2=new Panel();
add(visible);
tf=new TextField(10);
tf.setText("");
tf.setFont(ft);
GridLayout g=new GridLayout(5,4,2,2);
p1.setLayout(g);
BorderLayout br=new BorderLayout(6,6);
p2.setLayout(br);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(bm);
p1.add(bpr);
p1.add(b0);
p1.add(bmu);
p1.add(bd);
p1.add(be);
p1.add(bce);
p1.add(bexit);
p2.add("North",l);
p2.add("Center",tf);
p2.add("South",p1);
add(p2);

//add border

p2.setBackground(Color. red);
p1.setBackground(Color. gray);
}
}

Q.5) For the calculator designed in the previous question, add


functionality.

Ans:-
This is functional part of previous question
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);
bm.addActionListener(this);
bd.addActionListener(this);
bpr.addActionListener(this);
bmu.addActionListener(this);
be.addActionListener(this);
bp.addActionListener(this);
bce.addActionListener(this);
bvisible.addActionListener(this);
bexit.addActionListener(this);

}//end of init() method;

public void actionPerformed(ActionEvent e)


{
if(e.getSource()==b1)
{
tf.setText(x);
x=tf.getText()+"1";
tf.setText(x);
}

else if(e.getSource()==b2)
{
tf.setText(x);
x=tf.getText()+"2";
tf.setText(x);
}
else if(e.getSource()==b3)
{
tf.setText(x);
x=tf.getText()+"3";
tf.setText(x);
}
else if(e.getSource()==b4)
{
tf.setText(x);
x=tf.getText()+"4";
tf.setText(x);
}
else if(e.getSource()==b5)
{
tf.setText(x);
x=tf.getText()+"5";
tf.setText(x);
}
else if(e.getSource()==b6)
{
tf.setText(x);
x=tf.getText()+"6";
tf.setText(x);
}
else if(e.getSource()==b7)
{
tf.setText(x);
x=tf.getText()+"7";
tf.setText(x);
}
else if(e.getSource()==b8)
{
tf.setText(x);
x=tf.getText()+"8";
tf.setText(x);
}
else if(e.getSource()==b9)
{
tf.setText(x);
x=tf.getText()+"9";
tf.setText(x);
}
else if(e.getSource()==b0)
{
tf.setText(x);
x=tf.getText()+"0";
tf.setText(x);
}

else if(e.getSource()==bp)
{
y=e.getActionCommand();
rp=Float.valueOf(tf.getText()).floatValue();
x=" ";
}
else if(e.getSource()==bm)
{
y=e.getActionCommand();
rp=Float.valueOf(tf.getText()).floatValue();
x="";
}
else if(e.getSource()==bd)
{
y=e.getActionCommand();
rp=Float.valueOf(tf.getText()).floatValue();
x="";
}
else if(e.getSource()= = bmu)
{
y=e.getActionCommand();
rp=Float.valueOf(tf.getText()).floatValue();
x="";
}
else if(e.getSource()==bpr)
{
if(y=="x")
{
sum=Float.valueOf(tf.getText()).floatValu
e();
sum=sum*rp/100;
tf.setText(String.valueOf(sum));
x="";
}
else if(y=="/")
{
sum=Float.valueOf(tf.getText()).floatValue(
);
sum=(rp*100)/sum;
tf.setText(String.valueOf(sum));
}
}
else if(e.getSource()==be)
{
if(y=="+")
{
sum=Float.valueOf(tf.getText()).floatValu
e();
sum=rp+sum;
tf.setText(String.valueOf(sum));
x="";
}
else if(y=="-")
{
sum=Float.valueOf(tf.getText()).floatValue(
);
sum=rp-sum;
tf.setText(String.valueOf(sum));
x="";
}
else if(y=="x")
{
sum=Float.valueOf(tf.getText()).float
Value();
sum=rp*sum;
tf.setText(String.valueOf(sum));
x="";
}

}
else if(e.getSource()==bce)
{
tf.setText( " " );
x="";
}
}//end of action method

}//end of class


2.Write a program that open and reads a text file. It should also find out how many times
the word “MAHE”appears in that text file.
Ans:
Import java.io.* ;
Public class Fbmit
{
public static void main(String args[ ])
{
int=0; int count=0;
int m=0; m2=0; m3=0; m4=0;
FileInputStream F=new FileInputStream(“Fbmit.txt”);
In length1=F.length();
For (int j=0;j<=length1;j++)
{
char ch;
ch=(char) F.read()
if(ch= =” “)
i++;
else if(ch= =’M’&& i= =1)
m++;
else if(ch= =’A’&& m= =1)
m2++;
else if(ch= =’H’&& m2= =1)
m3++;
else if (ch= =’E’&& m3= =1)
m4++;

elseif ch= =” “&& m4= =1

Count++;
else
i=0; m=0; m2=0; m3=0; m4=0;
System.out.println (“no. Of \”MAHE\”in my text file” +count);
}
}

You might also like