1. Strings Sorting Problem : A program to sort a list of strings.
Algorithm: 1. Start
2. Print “Enter how many strings” 3. Read n
4. Repeat for i=1 to n 5. print “Enter a name” 6. Read name[i]
7. End for
8. Repeat for i=1 to n 9. Repeat for j= i+1 to n
10. if(name[j].compareTo(name[i])<0) then 1. temp=name[j]; 2. name[j]=name[i]; 3. name[i]=temp; 11. End for i 12. End for j
13. print “Sorted names” 14. Repeat for i=1 to n 15. print name[i]
16. End
Program:
import java.util.*; import java.io.*; class stringsort {
public static void main(String args[]) {
String name[]=new String[10]; String temp;
int i,j,n=1;
DataInputStream in=new DataInputStream(System.in); System.out.print("\n How Many names to sort:"); try { n=Integer.parseInt(in.readLine()); for(i=0;i<n;i++) { System.out.println("Enter a name:"); name[i]=in.readLine(); } } catch(Exception e) { } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(name[j].compareTo(name[i])<0) { temp=name[j]; name[j]=name[i]; name[i]=temp; } } }
System.out.println("\nsorted Names are \n"); for(i=0;i<n;i++)
System.out.println(name[i]); }
}
Output :
How Many names to sort:3 Enter a name: Kiran Enter a name: ashok Enter a name: sree ram sorted Names are
ashok kiran sree ram
2. Lower case to Upper case
Problem : A program to convert a string from Lower case to Upper case without using a string methods.
Algorithm: 1. Start
2. Print “Enter a lower case string” 3. Read str1
4. StringBuffer str2=new StringBuffer(str1) 5. Repeat for i=0 to str1.length
1. if(str2.charAt(i)>='a' && str2.charAt(i)<='z') then 2. str2.setCharAt(i,(char)(str2.charAt(i)-32));
6. End for i
7. print "Upper Case String:",str2 8. End
Program:
import java.lang.String; import java.io.*;
public class lowertoupper{
public static void main(String args[]) {
String str1=new String(" ");
DataInputStream in=new DataInputStream(System.in); System.out.print("\nEnter Lower Case String:"); try
{
str1=in.readLine(); } catch(Exception e) { }
StringBuffer str2=new StringBuffer(str1); for(int i=0;i<str1.length();i++) {
if(str2.charAt(i)>='a' && str2.charAt(i)<='z') str2.setCharAt(i,(char)(str2.charAt(i)-32)); }
System.out.println("Upper Case String:"+str2); }
}
Output :
Enter Lower Case String:svarts Upper Case String:SVARTS
Problem : A program on command-line arguments to accept input. Problem solution
Algorithm: 1. Start
2. Count=args.length
3. Print “ number of arguments”,count 4. Repeat while ( i<count)
5. item=args[i]
6. print “java is:”,item 7. i=i+1 8. End while 9. End Program: class comline {
public static void main(String args[ ]) { int count,i=0; String item; count=args.length; System.out.println("No of arguments="+count); while(i<count) { item=args[i]; System.out.println("Java is:"+item); i=i+1; } } } Output:
Compile: javac comline.java
Run: java comline Robust secure multithread dynamic No of arguments=3
Java is: Robust Java is: secure
Java is: multithread Java is: dynamic
4. Interface implementation problem: A program to define and implement an interface.
problem solution: Algorithm: Interface area 1. begin 2. pi=3.14 3. compute(x,y) 4. end.
Class Rectangle implements area 1. Begin
2. compute(x,y) 1. return (x*y) 3. End
class circle implements area 1. Begin 2. compute(x,y) 1. return(pi*x*x) 3. End Interface test.java 1. Begin 2. rectangle r1 3. circle c1
4. print “area of rectangle:”, r1.compute(5.0,4.0) 5. print “area of circle:”,c1.compute(4.0,0)
6. End Program code: interface Area {
final static double PI=3.14;
double compute(double x,double y); }
class rectangle implements Area {
public double compute(double x,double y) {
return(x*y); }
class circle implements Area {
public double compute(double x,double y) { return(PI*x*x); } } class interfacetest {
public static void main(String args[]) {
rectangle r1=new rectangle(); circle c1=new circle();
Area area; area=r1; System.out.println("Area of rectangle="+ area.compute(5.0,4.0)); area=c1; System.out.println("Area of circle="+ area.compute(4.0,0)); } } Output : Area of rectangle=20.0 Area of circle=50.24
5. Multiple Inheritance
Problem: A program to implement multiple inheritance concepts in java. Problem solution: Algorithm: Class student: 1. start 2. getrno(n) 3. putrno(n) 4. stop
class test extends student 1. Start 2. getmarks(m1,m2) 3. putmarks() 3.1 print “part1=”,p1 3.2 print “part2=”,p2 4. stop interface sports 1. start 2. sportswt=6 3. putwt( ) 4. stop
class result extends test implements sports 1. start
2. putwt( )
2.1 print “sports weight=”,sportswt 3. void display( ) 3.1 total=p1+p2+sportswt 3.2 purtrno( ) 3.3 putmarks( ) 3.4 putwt( ) 4. stop class main 1. start 2. result r1 3. r1.getrno(1234) 4. r1.getmarks(44,55) 5. r1.display() 6. stop program: class student { int rno; void getrno(int n) { rno=n; } void putrno( ) { System.out.println("Roll number:"+rno); } }
class test extends student {
double p1,p2;
void getmarks(double m1,double m2) { p1=m1; p2=m2; } void putmarks( ) { System.out.println("marks:"); System.out.println("part1="+p1); System.out.println("part2="+p2); } } interface sports { double sportswt=6.0; void putwt(); }
class result extends test implements sports { double total;
public void putwt() { System.out.println("sportswt="+sportswt); } void display() { total=p1+p2+sportswt; putrno(); putmarks(); putwt(); } } class multileval
{ public static void main(String args[]) {
result r1=new result(); r1.getrno(121); r1.getmarks(44.5,55.6); r1.display(); } } Output : Roll number:121 marks: part1=44.5 part2=55.6 sportswt=6.0
6 Constructors Overloading
Problem: A program to implement a class with constructor overloading Problem solution:
Algorithm: ADT Room {
Room (double x, double y) 1. begin 2. length =x 3. width =y 4. end Room(double x) 1. begin 2. length=width =x 3. end Area( ) 1. Begin 2. return length*width 3. End } Main method 1. Begin
2. Room R1=new Room(4.5,6.5) 3. Room R2=new Room(4.5) 4. Print “Area of R1=",R1.Area() 5. Print “Area of R2="+R2.Area() 6. End
Program class room { double l,w;
room(double x,double y) //consructor1 { l=x; w=y; } room(double x) //consrtuctor2 { l=w=x; } double Area( ) { return(l*w); } } class roomarea {
public static void main(String args[]) { room R1=new room(4.5,6.5);
room R2=new room(4.5);
System.out.println("area="+R1.Area()); System.out.println("area="+R2.Area()); } } Output : Area of R1=29.25 area of R2=20.25
7 Nesting of Methods
Problem: A program to implement a class with constructor overloading Problem solution:
Algorithm: ADT Big {
Big (int x, int y) 1. Begin 2. a =x 3. b =y 4. end Largest( ) method 1. Begin 2. if a>b then i. return a
ii. otherwise return b iii. end if
3. End
Display( ) Method 1. Begin
2. large= Largest()
3. print “largest value=”,large 4. End
}
Main method 1. Begin
2. Big t=new Big(5,7) 3. t.display( ) 4. End Program: class big { int a,b; big(int x,int y) { a=x; b=y; } int largest() { if(a>b) return a; else return b; } void display( ) { int large=largest();
System.out.println("biggest number : "+large); } }
class biggest {
public static void main(String args[]) { big t=new big(5,7);
t.display(); }
}
8. Exception Handling
Problem: A program to throw an own exception when marks is greater than100
Problem solution: Algorithm:
1. Begin
2. print "enter student number" 3. input sno
4. print "enter student name" 5. input sname
6. print "enter marks" 7. input marks
8. if marks>100 then
8.1 throw Myexception("Invalid, marks between 0-100") 9. print "student number=",sno
10. print "student name=",sname 11. print "marks=",marks
12. end Program
import java.lang.Exception; import java.io.*;
class MyException extends Exception { public MyException(String msg) { super(msg); } } class testmarksexception {
public static void main(String[] args)throws MyException {
DataInputStream in=new DataInputStream(System.in); int sno=0,marks=0;
String sname=new String(); try{
System.out.print("\nEnter student Number:"); sno=Integer.parseInt(in.readLine());
System.out.print("\nEnter Student Name:"); sname=in.readLine();;
System.out.print("\nEnter Student Marks:"); marks=Integer.parseInt(in.readLine());
if(marks>100)
throw new MyException("invalid,Marks between0-100"); System.out.println("\n\nGiven Student Details:"); System.out.println("\nStudent Number:"+sno);
System.out.println("\nStudent Name :"+sname); System.out.println("\nMarks :"+marks);
catch(Exception e) {
System.out.print("\nCaught Marks Out of Bound"); System.out.print("\n"+e.getMessage());
} } }
Output :
Enter student Number:100 Enter Student Name:Krishna Enter Student Marks:80 Given Student Details: Student Number:100 Student Name :Krishna Marks :80
9. Armstrong number
Problem: A program to define a class Armstrong and check the given number is Armstrong or not.
Problem solution: Algorithm:
Step 1: Begin
2: print “Enter a number” 3: read n
4: repeat while n>0 1. n1=n
2. rem=Mod(n,10)
3. sd=sd+ rem * rem * rem 4. n=n/10
5. end while 5: if sd==n1 then
a. print “The given number is Armstrong” b. otherwise
c. print “The given number is not armstrong” d. End if 6: End Program: import java.util.*; class armstrong { int sumdig(int n) { int rem,sd=0; while(n>0) { rem=n%10; sd=sd+rem*rem*rem; n=n/10; } return(sd); } void display(int n) { int n1=n; if(sumdig(n)==n1)
System.out.println("The given number is armstrong"); else
System.out.println("Not armstrong"); }
public static void main(String args[]) throws Exception { int n;
Scanner in = new Scanner(System.in); System.out.println("Enter a number"); n=in.nextInt();
armstrong obj=new armstrong(); obj.display(n);
} } Output :
10. Threads
Problem: write a problem to implement threads Problem solution
Algorithm:
1.class A extends Thread 1. for i=1 to 10
2. print “ from thread A”,i 3. Print “Exit from threadA” 2.class B extends Thread 1. for j=1 to 10
2. print “ from thread B”,j 3. Print “Exit from thread B” 3.class C extends Thread 1. for k=1 to 10
2. print “ from thread C”, 3. Print “Exit from threadC” 4.procedure main( ) 1. Begin 2. A ThreadA=new A(); 3. B ThreadB=new B(); 4. C ThreadC=new C(); 5. ThreadA.start(); 6. ThreadB.start(); 7. ThreadC.start(); 8. End Program import java.util.*; class A extends Thread { int i;
public void run() {
for(i=1;i<=5;i++)
System.out.println("from thread A:"+i); System.out.println("Exit from thread A"); }
class B extends Thread { int j;
public void run() {
for(j=1;j<=5;j++)
System.out.println("from thread B:"+j); System.out.println("Exit from thread B"); }
}
class C extends Thread { int k;
public void run() {
for(k=1;k<=5;k++)
System.out.println("from thread C:"+k); System.out.println("Exit from thread C"); }
}
class Threaddemo {
public static void main(String args[])throws Exception {
System.out.println("Start of main thread"); A ThreadA=new A(); B ThreadB=new B(); C ThreadC=new C(); ThreadA.start(); ThreadB.start(); ThreadC.start(); } }
11. Using Applets Problem: A program on applets to get input from the user Algorithm:
class userin extends Applet 1. begin
2. TextField text1,text2; 3. public void init( )
3.1: text1=new TextField(8); 3.2: text2=new TextField(8); 3.3: add(text1);
3.4: add(text2);
4 public void paint(Graphics g) 4.1: x=0,y=0,z=0;
4.2: g.drawString("input a number in textbox",10,50); 4.3: try { s1=text1.getText(); x=Integer.parseInt(s1); s2=text2.getText(); y=Integer.parseInt(s2); } catch(Exception e) { } 4.4: z=x+y; 4.5: s=String.valueOf(z);
4.6: g.drawString("the sum is:",10,75); 4.7: g.drawString(s,80,75);
5. public boolean action(Event event,Object object) 5.1: repaint(); 5.2: return true; 6. end Program code : //userin.java import java.lang.*; import java.awt.*; import java.applet.*; import java.io.*;
public class userin extends Applet {
TextField text1,text2; public void init( ) { text1=new TextField(8); text2=new TextField(8); add(text1); add(text2); }
public void paint(Graphics g) {
int x=0,y=0,z=0; String s1,s2,s;
g.drawString("input a number in textbox",10,50); try { s1=text1.getText(); x=Integer.parseInt(s1); s2=text2.getText(); y=Integer.parseInt(s2); }catch(Exception e) { } z=x+y; s=String.valueOf(z);
g.drawString("the sum is:",10,75); g.drawString(s,80,75);
}
public boolean action(Event event,Object object) { repaint(); return true; } } userin.html <html> <applet code="userin.class" width=300 height=300> </applet> </html> Output:
12. Swings JFrame Problem: write a program to implement a Frame using swings Problem solution
Program:
// A simple example of the JFrame class. import javax.swing.*;
public class SimpleFrame {
public static void main(String args[]) {
JFrame f = new JFrame("Simple Frame"); f.setSize(400,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true);
JLabel label = new JLabel("sv arts and science collge gudur"); f.getContentPane().add(label);
f.pack(); }
}
13. Creating UserInterface
Problem: write a program to implement a user interface using Applets Problem solution
Program:
import java.awt.*; import java.applet.*;
public class guidemo extends Applet {
public void init() {
Label l1=new Label("Enter u r name"); TextField t1=new TextField(20);
Label l2=new Label("Select u r course"); Checkbox c1=new Checkbox("MCA",true); Checkbox c2=new Checkbox("MBA");
Checkbox c3=new Checkbox("MSC");
Label l3=new Label("Select u r place"); List ls=new List(1);
ls.add("Nellore"); ls.add("Naidupet"); ls.add("gudur"); ls.add("spet");
Label l4=new Label("What do u like"); Button b1=new Button("submit");
Button b2=new Button("cancel"); add(l1); add(t1); add(l2); add(c1); add(c2); add(c3); add(l3); add(ls); add(l4); add(b1); add(b2); } }