Professional Documents
Culture Documents
Java Experiments (13-35) With Output
Java Experiments (13-35) With Output
PROGRAM:
import java.util.Scanner;
class ChkPalindrome
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
str = sc.nextLine();
}
}
EXPERIMENT:17
AIM: To write a java program to show dynamic polymorphism and
interfaces.
PROGRAM:
interface Bird
{
public abstract void eat(); // I
}
class Peacock implements Bird
{
public void eat() // II
{
System.out.println("Peacock eats grains");
}
}
class Vulture implements Bird
{
public void eat() // III
{
System.out.println("Vulture eats flesh");
}
}
class Crane implements Bird
{
public void eat() // IV
{
System.out.println("Crane eats fish");
}
}
public class DynamicPolyDemo
{
public static void main(String args[])
{
Bird b1; // reference variable of interface
Peacock p1 = new Peacock(); b1 = p1; b1.eat(); // calls II
Vulture v1 = new Vulture(); b1 = v1; b1.eat(); // calls III
Crane c1 = new Crane(); b1 = c1; b1.eat(); // calls IV
}
}
EXPERIMENT:18
AIM: To Create a Customized Exception and also make use of
all 5 exception keywords.
PROGRAM:
class MyException extends Exception
{
public MyException(String s)
{
// Call constructor of parent Exception
super(s);
}
}
public class ExceptionKeywords {
try {
// call division() method
String welcomeMessage = welcomeMessage("SJ");
// print to console
System.out.println("The returned welcome message : "
+ welcomeMessage);
}
catch (NullPointerException npex){
System.out.println("Exception handled : "
+ npex.toString());
}
finally {
System.out.println("Rest of the clean-up code here");
}
}
if(name == null) {
PROGRAM:
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class Super{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}
}
EXPERIMENT-20
AIM: To implement the use of final keyword.
PROGRAM:
class FinalVariable
}
}
EXPERIMENT-21
AIM: To write an applet that computes the payment of a loan based
on the amount of the loan,the interest rate and the number of
months. It takes one parameter from the browser:Monthly
Rate,Interest Rate is per month,otherwise the interest rate is annual.
PROGRAM:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Loan" width=300 height=300>
</applet>
*/
public class Loan extends Applet
implements ActionListener,ItemListener
{
double p,r,n,total,i;
String param1;
boolean month;
Label l1,l2,l3,l4;
TextField t1,t2,t3,t4;
Button b1,b2;
CheckboxGroup cbg;
Checkbox c1,c2;
String str;
public void init()
{
l1=new Label("Balance Amount",Label.LEFT);
l2=new Label("Number of Months",Label.LEFT);
l3=new Label("Interest Rate",Label.LEFT);
l4=new Label("Total Payment",Label.LEFT);
t1=new TextField(5);
t2=new TextField(5);
t3=new TextField(15);
t4=new TextField(20);
b1=new Button("OK");
b2=new Button("Delete");
cbg=new CheckboxGroup();
c1=new Checkbox("Month Rate",cbg,true);
c2=new Checkbox("Annual Rate",cbg,true);
t1.addActionListener(this);
t2.addActionListener(this);
t3.addActionListener(this);
t4.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
c1.addItemListener(this);
c2.addItemListener(this);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
add(c1);
add(c2);
add(b1);
add(b2);
}
public void itemStateChanged(ItemEvent ie)
{
}
public void actionPerformed(ActionEvent ae)
{
str=ae.getActionCommand();
if(str.equals("OK"))
{
p=Double.parseDouble(t1.getText());
n=Double.parseDouble(t2.getText());
r=Double.parseDouble(t3.getText());
if(c2.getState())
{
n=n/12;
}
i=(p*n*r)/100;
total=p+i;
t4.setText(" "+total);
}
else if(str.equals("Delete"))
{
t1.setText(" ");
t2.setText(" ");
t3.setText(" ");
t4.setText(" ");
}
}
}
EXPERIMENT-22
AIM: To develop a program using the following AWT components in an applet-
TextField, TextArea, Button and Label
PROGRAM:
import java.awt.*;
import java.applet.*;
/* <applet code="AppletDemo.class" height=200 width=300>
</applet>*/
for(int i=0;i<6;i++)
{
b1[i]=new Button(""+op2[i]);
add(b1[i]);
b1[i].setBackground(Color.pink);
b1[i].addActionListener(this);
}
add(t);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("+")){ p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("-")){ p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("*")){ p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("%")){ p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("=")) { str1="";
if(oper.equals("+")) {
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p+q)));}
else if(oper.equals("-")) {
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p-q))); }
else if(oper.equals("*")){
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p*q))); }
else if(oper.equals("%")){
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p%q))); }
}
else{ t.setText(str1.concat(str));
str1=t.getText();
}
}
EXPERIMENT-24
AIM: Develop an analog clock using applet.
PROGRAM:
import java.applet.Applet;
import java.awt.*;
import java.util.*;
/*<applet code="analogClock.class" width=300 height=300></applet>*/
while (true)
{
repaint();
delayAnimation();
}
}
}.start();
}
private void delayAnimation()
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public void paint(Graphics g)
{
Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
int second = time.get(Calendar.SECOND);
if (hour > 12) {
hour -= 12;
}
g.setColor(Color.white);
g.fillOval(300, 100, 200, 200);
g.setColor(Color.black);
g.drawString("12", 390, 120);
g.drawString("9", 310, 200);
g.drawString("6", 400, 290);
g.drawString("3", 480, 200);
double angle;
int x, y;
angle = Math.toRadians((15 - second) * 6);
x = (int)(Math.cos(angle) * 100);
y = (int)(Math.sin(angle) * 100);
g.setColor(Color.red);
g.drawLine(400, 200, 400 + x, 200 - y);
angle = Math.toRadians((15 - minute) * 6);
x = (int)(Math.cos(angle) * 80);
y = (int)(Math.sin(angle) * 80);
g.setColor(Color.blue);
g.drawLine(400, 200, 400 + x, 200 - y);
angle = Math.toRadians((15 - (hour * 5)) * 6);
x = (int)(Math.cos(angle) * 50);
y = (int)(Math.sin(angle) * 50);
g.setColor(Color.black);
g.drawLine(400, 200, 400 + x, 200 - y);
}
}
EXPERIMENT-25
AIM:To Develop a program demonstrating the use of Grid Layout in
an applet.Write the program which creates the frame and
implements Mouse Listener.
PROGRAM:
(1)Program for Grid Layout:
import java.awt.*;
import javax.swing.*;
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
f.add(b6);f.add(b7);f.add(b8);f.add(b9);
f.setLayout(new GridLayout(3,3));
//setting grid layout of 3 rows and 3 columns
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new GridDemo();
}
}
(2)Program for Mouse Listener:
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/*
* To create a stand alone window, class should be extended from
* Frame and not from Applet class.
*/
}
public void mouseReleased(MouseEvent e) {
strEvent = "MouseReleased";
x = e.getX();
y = getY();
repaint();
}
public void mouseEntered(MouseEvent e) {
strEvent = "MouseEntered";
x = e.getX();
y = getY();
repaint();
}
public void mouseExited(MouseEvent e) {
strEvent = "MouseExited";
x = e.getX();
y = getY();
repaint();
}
public void paint(Graphics g){
g.drawString(strEvent + " at " + x + "," + y, 50,50);
}
HandleMouseListenerInWindowExample myWindow =
new HandleMouseListenerInWindowExample("Window With
Mouse Events Example");
}
MyWindowAdapter(HandleMouseListenerInWindowExample myWindow){
this.myWindow = myWindow;
}
}
EXPERIMENT-27
AIM:To develop a program to set a given string in desired font and
color.
PROGRAM:
import java.awt.event.*;
import javax.swing.*;
String line;
// Initializing counters
int countWord = 0;
int sentenceCount = 0;
int characterCount = 0;
int paragraphCount = 1;
int whitespaceCount = 0;
characterCount += line.length();
countWord += wordList.length;
whitespaceCount += countWord -1;
sentenceCount += sentenceList.length;
}
}
t1.join();
t2.join();
}
public static class PC
{
while (list.size()==capacity)
wait();
System.out.println("Producer produced-"
+ value);
list.add(value++);
notify();
Thread.sleep(1000);
}
}
}
while (list.size()==0)
wait();
System.out.println("Consumer consumed-"
+ val);
notify();
Thread.sleep(1000);
}
}
}
}
}
EXPERIMENT-35
AIM: To create three threads by extending Thread class.First thread
displays “Good Morning” every 1 sec,the second thread displays
“Hello” every 2 seconds and the third thread displays “Welcome”
every 3 seconds.
PROGRAM:
class A extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(1000);
System.out.println("good morning");
}
}
catch(Exception e)
{}
}
}
class B extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(2000);
System.out.println("hello");
}
}
catch(Exception e)
{}
}
}
class C extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(3000);
System.out.println("welcome");
}
}
catch(Exception e)
{}
}
}
class ThreeThreads
{
public static void main(String args[])
{
A t1=new A();
B t2=new B();
C t3=new C();
t1.start();
t2.start();
t3.start();
}
}