JAVA PROGRAMS
1.Write a java program to display any 4 images one by
one using getImage() of
Applet with respect to mouse pressed
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="Appimage"
width=800 height=300>
</applet>*/
public class Appimage extends Applet implements ActionListener{
Image i;
Button b=new Button("click");
public void init()
{
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b)
{
i=getImage(getDocumentBase(),"cs.png");
}
}
public void paint(Graphics g) {
g.drawImage(i,
30,30, this);
}
}
2.Write a java program to
display four buttons in a user window with the names as North,South,East,West
in four corners and display the name of the button when a user clicks the
button.
import
java.applet.*;import java.awt.*;
import
java.awt.event.*;
import
java.awt.Button;
/*<applet
code="But" width="500" height="500">
</applet>*/
public
class But extends Applet implements ActionListener
{
Button
b=new Button("North");
Button
b1=new Button("South");
Button
b2=new Button("East");
Button
b3=new Button("West");
TextField
t=new TextField(15);
public
void init()
{
setLayout(new
BorderLayout());
add(b,BorderLayout.NORTH);
add(b1,BorderLayout.SOUTH);
add(b2,BorderLayout.EAST);
add(b3,BorderLayout.WEST);
add(t);
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public
void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
t.setText("North
pressed");
if(e.getSource()==b1)
t.setText("South
pressed");
if(e.getSource()==b2)
t.setText("East
pressed");
if(e.getSource()==b3)
t.setText("West
pressed");
}
}
3.A sequence of Fibonacci Strings is
generated as follows:
S0 = “a”, S1 = “b”, Sn
= S(n-1) + S(n-2) where ‘+’ denotes concatenation. Thus
the sequence is:a, b, ba, bab, babba, babbabab, ………. n terms.Design a class FiboString to generate
Fibonacci strings.
package strings;
import java.util.*;
public class str_fibbo {
public static void main(String ar[])throws Exception
{
String x,y,z;
int n=Integer.parseInt(ar[0]);
x="a";
y="b";
z="ba";
System.out.print(x+","+y);
for(int i=0;i<=n-2;i++)
{
System.out.print(","+z);
x=y;
y=z;
z=y.concat(x);
}
}
}
4.A Palindrome is a word that may be
read the same way in either direction.Accept a sentence in UPPER CASE which is
terminated by either ” . “, ” ? ” or ” ! “.Each word of the sentence is
separated by a single blank space.Perform the following task:
(a) Display the Palindromic words in the
sentence.Example of palindromic words: MADAM, ARORA, NOON
package strings;
import java.util.*;
public class Palind {
public static void main(String args[]){
String str="mom
and dad";
int count=0;
//str=sc.nextLine();
str=str.replace("."," ");
str=str.replace("?"," ");
str=str.replace("!"," ");
String answer="", ar[]= str.split(" ");
for(int i=0;i < ar.length;i++){
if(isPalindrome(ar[i])){
count++;
answer += ar[i] + "
";
}
System.out.println("OUTPUT:\t\t"+answer+"\nNUMBER
OF PALINDROMIC WORDS: "+count);
}
public static boolean isPalindrome(String str){
char ch;
int len=str.length(),half=len/2;
for(int i=0;i < half;i++){
if(str.charAt(i)!=str.charAt(len-i-1)) return false;
}
return true;
}
}
5.Input
a word that may be any length or it may be any case i.e.,Upper case,Lower case
or Camel case and receive the input through a[0] as string buffer. Then replace
the contents of string buffer from 0 to 5 by a[1].
package strings;
import java.util.*;
public class replace {
public static void main(String arg[])
{
StringBuffer
s=new StringBuffer(arg[0]);
System.out.println(s.replace(0, 5, "go"));
}
}
6.Input a
word in uppercase and check for the position of the first occurring vowel and
perform the following operation.
i). Words that begin with a vowel are
concatenated with “Y” . For example, EUROPE becomes EUROPEY.
ii). Words
that contain a vowel in between should have the first part from the position of
the vowel till end, followed by the part of the string from beginning till
position of the vowel and is concatenated by “C”. For example PROJECT becomes
OJECTPRC.
iii). Words which do not contain a vowel are
concatenated with “N”. For example, SKY becomes SKYN
package strings;
import java.util.*;
public class Nvowel {
public static void main(String ar[]){
String s=ar[0],c=" ";
int len=0;
int i,x=0;
s=s.toUpperCase();
len=s.length()-1;
for(i=0;i<=len;i++){
switch(s.charAt(i)){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
x++;
}
if(x>0){
break;
}
}
if(x==0){
c=s+"NO";
}
else if(i==0){
c=s+"Y";
}
else{
c=s.substring(i);
c=c+s.substring(0, i)+"C";
}
System.out.println(c);
}
}
7.Store the set of student’s marks in
array and handle the Exception if marks>100 or<0
Print invalid.
package tes;
import java.util.*;
import java.io.*;
public class Excep {
public static void main(String ar[])
{
int a[]={10,20,11,-1};
try{
for(int i=0;i<a.length;i++)
{
if(a[i]>100||a[i]<0)
{
throw
new Exception("invalid");
}
else
{
System.out.print("");
}
}
}
catch(Exception e)
{System.out.println(e);
}
}
}
8.Create a method add(*,*) of class
C1 in a package p1 , Create a method mul(*,*) of class C2 in a package p1.p2
and also Create a method sub(*,*) of class Demo (which containing main()
method) in a package p3. Write a program to implement the above said
specification.
Note:
* represents a integer value
which could be get through command line arguments
Sample input : 10 20
Output: Addition=30
Subtraction=-10
Multiplication=200
A.java:
package p1;
interface i1
{void disp();
}
public class A implements i1{
public void disp()
{
System.out.println("no");
}
public void m1(int a,int b)
{
System.out.println(a+b);
}
}
B.java
package p1.p2;
public class B {
public void m2(int a,int b)
{
System.out.println(a*b);
}
}
C.java
package p3;
import p1.*;
import p1.p2.*;
import java.util.*;
public class C {
public void m3(int a,int b)
{System.out.println(b/a);
}
public static void main(String ar[])
{
A a=new
A();
B b=new
B();
C c=new
C();
a.disp();
a.m1(Integer.parseInt(ar[0]),Integer.parseInt(ar[1]));
b.m2(Integer.parseInt(ar[0]),Integer.parseInt(ar[1]));
c.m3(Integer.parseInt(ar[0]),Integer.parseInt(ar[1]));
}
}

No comments