You are on page 1of 64

1. Create a Hello, World program that simply prints out that statement .

class Ankita1 { public static void main(String args[]) {System.out.println("HELLO WORLD"); System.out.println("created by Ankita Dalmia"); } } OUTPUT:

2.Write a program that print three arguments taken from the command line. class Ankita2 { public static void main(String args[]) { int count,i=0; String string; count=args.length; for(i=0;i<count;i++) { string=args[i]; System.out.println(i + ":" + string); System.out.println("Print three Arguments"); } } }

3.Write a program that prints values from 1 to 100.

class Ankita3 { public static void main(String args[]) { int i; System.out.println("PRINT VALUES FROM 1 TO 100"); for(i=1;i<=100;i++) { System.out.print( i + " "); } } }

OUTPUT:

4.Create a class with a default constructor(one that takes no arguments)that prints a message.Create an object of this class.

class Printt { Printt() { System.out.println("DEFAULT CONSTRUCTOR"); System.out.println("create an object of this class"); } } class Example4 { public static void main(String args[]) { Printt M=new Printt(); } }

OUTPUT:

5.Write java assignment statements to evaluate the following equations. i)Energy=mass(acceleration*height+(velocity)sqr(2)/2)

class Ankita5 { public static void main(String a[]) { double m=4,acc=6,ht=9,ve=200,en; System.out.println("IF Mass:"+m+" grams"); System.out.println("Accelaration:"+acc+" mt/sec.sec"); System.out.println("at Height:"+ht+" mt"); System.out.println("with a velocity of:"+ve+" mt/sec"); en=m*((acc*ht+(ve*ve))/2); System.out.println("then the Energy generated is:"+en+" joule"); } }

OUTPUT:

6.Design and write a java program to define a class called Rectangle that contains members for representating its length and breadth.Provide members to get and set these attributes.

class Rectangle { int l,b; void set(int x,int y) { l=x; b=y; } void get() { System.out.println("Length of Rectangle"+ " " +l); System.out.println("Breadth of Rectangle"+" "+b); } } class Ankita6 { public static void main(String ar[]) { Rectangle rec=new Rectangle(); rec.set(10,57); rec.get(); } }

OUTPUT:

7. Design a class to represent a bank account. Include the following members: Data members: Name of the depositor Account number Type of account Balance amount in the account Methods: To assign initial values To deposit an amount To withdraw an amount after checking balance To display the name and balance

import java.io.*; class bankDoc { String name; String type; int accno; int balam; int depam; int widam; BufferedReader br=new InputStreamReader(System.in)); public void init() { try { System.out.println("Enter The Name"); name=br.readLine(); System.out.println("Enter The Account Type"); type=br.readLine(); System.out.println("Enter The Account Number"); accno=Integer.parseInt(br.readLine()); System.out.println("Enter The Initial Balance"); balam=Integer.parseInt(br.readLine()); } catch(Exception e) { System.out.println("ERROR: : "+" "+e); }

BufferedReader(new

} public void dep() { try { System.out.println("Enter The Name"); name=br.readLine(); System.out.println("Enter The Account Type"); type=br.readLine(); System.out.println("Enter The Account Number"); accno=Integer.parseInt(br.readLine()); System.out.println("Enter The Deposit Amount"); depam=Integer.parseInt(br.readLine()); } catch(Exception e) { System.out.println("ERROR: : "+" "+e); } } public void wid() { try { System.out.println("Enter The Name"); name=br.readLine(); System.out.println("Enter The Account Type"); type=br.readLine(); System.out.println("Enter The Account Number"); accno=Integer.parseInt(br.readLine()); System.out.println("Enter The Withdraw Amount"); widam=Integer.parseInt(br.readLine()); } catch(Exception e) { System.out.println("ERROR: : "+" "+e); } } public void display() { try

{ System.out.println("NAME::"+" " +name); System.out.println("BALANCE::"+" "+balam); } catch(Exception e) { System.out.println("ERROR: :"+" "+e); } } } class Ankita7 { public static void main(String ar[]) throws IOException { bankDoc bd=new bankDoc(); System.out.println("AVAILABLE CHOICES::"); System.out.println("PRESS 1 TO OPEN NEW ACCOUNT::"); System.out.println("PRESS 2 TO DEPOSIT AMOUNT::"); System.out.println("PRESS 3 TO WITHDRAW ACCOUNT::"); System.out.println("PRESS 4 TO DISPLAY YOUR ACCOUNT::"); System.out.println("PRESS 5 TO EXIT::"); BufferedReader brr=new BufferedReader(new InputStreamReader(System.in)); char ch; System.out.println("ENTER YOUR CHOICE::"); ch=(char)brr.read(); switch(ch) { case'1': bd.init(); break; case'2': bd.dep(); break; case'3': bd.wid(); break; case'4': bd.display(); break; case'5':

System.exit(0); break; default: System.out.println("INVALID SELECTION::"); } } }

OUTPUT:

8.Write simple program to calculate the sum of digits of any number.

import java.io.*; class Ankita8 { public static void main(String a[]) { int x; try { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter any no."); String s=br.readLine(); x=Integer.parseInt(s); int i,sum=0; while(x>0) { i=x%10; sum+=i; x/=10; } System.out.println("The sum is:"+sum); } catch(IOException ie) { System.out.println(ie); } } }

OUTPUT:

9.Write a simple program to display a * I triangle shape. Output will be like this * *** *****

class Ankita9 { public static void main(String args[]) { int l,n,m,x=1; for(n=2;n>=0;n--) { for(l=n;l>=0;l--) {System.out.print(" "); } for(m=1;m<=x;m++) { System.out.print("*"); } System.out.println(""); x=x+2; } } }

OUTPUT:

10.Write a simple program to call a method called simple from a main function.The method simple should accept an integer as an argument and calculate the square of the number in the method simple.

class Ankita10 { public void simple(String b) { int sq=Integer.parseInt(b)*Integer.parseInt(b); System.out.println("The square of:"+b+" is:"+sq); } public static void main(String a[]) { prog10 pro=new prog10(); pro.simple(a[2]); } } OUTPUT: The square of 2 is: 4

11.Write a java program to add two integers and two float numbers.When no arguments are supplied,give a default value to calculate the sum .Use method overloading to achieve this.

class Sum { int Sum(int a,int b) { int c; c=a+b; return c; } float Sum(float a,float b) { float c; c=a+b; return c; } } class Ankita11 { public static void main(String a[]) { Sum s=new Sum();

System.out.println("Sum of two integer is"+s.Sum(4,5)); System.out.println("sum of two float number is"+s.Sum(4.5f,5.5f)); } }

OUTPUT:

12.Write a program to perform mathematical operations. Create a class called AddSub with methods to add and subtract. Create another class called MultDiv that extends from AddSub class to use the member data of the superclass. MultDiv should have methods to multiply and divide. A main method should access the method and perform the mathematical operations.

class AddSub { int Add(int a,int b) { int c; c=a+b; return c; } int Sub(int a,int b) { int c=a-b; return c; } } class MulDiv extends AddSub { int Mul(int a,int b) {

int c=a*b; return c; } int Div(int a,int b) { int c=a/b; return c; } } class Ankita12 { public static void main(String a[]) { MulDiv md=new MulDiv(); System.out.println("Add::"+md.Add(4,5)); System.out.println("mul::"+md.Mul(4,5)); } } OUTPUT:

13. Write an interface with a method called display. Implement this method I a class to display two names interface MyInterface { public void display(); } class MyClass implements MyInterface { public void display() { System.out.println("\n\n.....APPLICATION 13....."); System.out.println("<Made By : ankita dalmia> \n\n"); System.out.println("Neha bhushan"); System.out.println("Priya"); System.out.println("Sonam chadha"); System.out.println("Saloni mangal"); } } public class Ankita13 { public static void main(String[] args) { MyClass mc = new MyClass();

mc.display(); System.out.println("\n\n"); } }

OUTPUT:

14. Write an interface that has two methods called push and pop of a stack. Write a class to implement the two methods for a fixed size stack creation

interface Stacks { public void push(int val) throws Exception; public int pop() throws Exception; } class Stack implements Stacks { int [] myStack = new int[20]; int stackCount = 0; public void push(int val) throws Exception { if (stackCount < 20) { myStack[stackCount] = val; stackCount++; } else { Exception ex = new Exception("Stack Overflow"); throw ex;

} } public int pop() throws Exception { if(stackCount>0) { return myStack[--stackCount]; } else { Exception ex = new Exception("Stack Underflow"); throw ex; } } } public class Ankita14 { public static void main(String[] args) { System.out.println("\n\n.....APPLICATION 14....."); System.out.println("<Made By : ankita dalmia> \n\n"); Stack myStack = new Stack(); try { System.out.println("Pushing 10"); myStack.push(10); System.out.println("Pushing 15"); myStack.push(15); System.out.println("Pushing 20"); myStack.push(20); System.out.println("Pushing 25"); myStack.push(25); System.out.println("Popping..." + myStack.pop()); System.out.println("Popping..." + myStack.pop()); System.out.println("Popping..." + myStack.pop() + "\n\n"); } catch(Exception ex) { ex.printStackTrace(); } } }

OUTPUT:

15. Write a small program to catch NegativeArraySizeException. This exception is caused when the array is initialized to negative values class Ankita15 { public static void main(String a[]) { try { int arr[]=new int[-2]; System.out.println("1st Element"+arr[0]); } catch(Exception e) {System.out.println(e); } } }

OUTPUT:

16. Write a program to handle NullPointerException and use the finally clause to display a message to the user. import java.io.*; class Ankita16 { public static void main(String a[]) { try { InputStream f1=null; int size=f1.available(); for(int i=0;i<size;i++) { System.out.println((char)f1.read()); } }

catch(Exception e)

{ System.out.println(e); } finally { System.out.println("Error"); } } }

OUTPUT:

17. Write a Java program that takes a string and converts it into uppercase and lowercase letters import java.io.*; class Ankita17 { public static void main(String a[]) throws IOException { System.out.println("Enter Any Character"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s; s=br.readLine(); System.out.println("Lower case output::"+s.toLowerCase()); System.out.println("Upper case output::"+s.toUpperCase()); } }

OUTPUT:

18. Write a Java program to find the volume of a sphere and a cone.

import java.io.*; class Ankita18 { public static void main(String a[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int ros,roc,hoc; double vos,voc; double pi=3.14; System.out.println("Enter the Radius of Sphere::"); ros=Integer.parseInt(br.readLine()); System.out.println("Enter the Radius of cone::"); roc=Integer.parseInt(br.readLine()); System.out.println("Enter the Height of cone::"); hoc=Integer.parseInt(br.readLine()); vos=(4f/3f)*pi*ros*ros*ros;

voc=(1f/3f)*pi*roc*roc*hoc; System.out.println("Volume of Sphere "+" " +vos); System.out.println("Volume of Cone "+" " +voc); } }

OUTPUT:

19. Write a Java program to convert rupees to dollars.

import java.io.*; class Ankita19 { public static void main(String a[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter The Ruppes"); int rup=Integer.parseInt(br.readLine()); float dol=rup/45; System.out.println("Dollar"+" " +dol); } }

OUTPUT:

20. Write a Java program to find x to the power y. Use overloading for different cases when x and y are combinations of integer and floating point numbers.

import java.math.*; class Ankita20 { int power(int x,int y) { int pow; if (y==1) return x; else if(y==0) {return 1; } else pow=x*(power(x,y-1)); return pow; }

public static void main(String ar[]) { Ankita20 j=new Ankita20(); System.out.println(j.power(256.8,4)); } double power(double x,double y) { return Math.pow(x,y); } }

OUTPUT:

21. Create an abstract class called Figure that has an abstract method called draw (). Make the subclasses called Filled_Rectangle, Filled_Arc and override the draw method in which you would print the message regarding the current object.

abstract class filled { abstract void draw(); } class Filled_Rectangle extends filled { void draw() {System.out.println("it is an rectangle"); } } class Filled_Arc extends filled { void draw() {System.out.println("it is an arc"); } } class Ankita21 { public static void main(String a[]) { Filled_Rectangle fr=new Filled_Rectangle(); Filled_Arc fa=new Filled_Arc(); fr.draw(); fa.draw(); } }

OUTPUT:

22. Write a Java program that has integer variables a, b, c and result as float. Store some values in them and apply the formula result = a/(b-c). Catch the probable exception. import java.math.*; class Ankita22 { public static void main(String ar[]) { try { int a=3; int b=4; int c=4; float result; result=a/(b-c); System.out.println("result"+result); } catch(Exception e) { System.out.println(e); } } }

OUTPUT:

23. . Write a Java program that accepts two strings as command line arguments. It checks for the number of command line arguments. If they are less or more it throws an exception giving an appropriate message.

import java.io.*; class Ankita23 { public static void main(String ar[]) { try { if(ar.length>=3) {System.out.println("Exception"+" " + "Number of argument is more than two"); } else if(ar.length<2) {System.out.println("Exception"+" "+"number of argumnet less than two"); } else {System.out.println("1st Argument"+" " +ar[0]+" "+"2nd Argumnet"+" " +ar[1]); } } catch(Exception e) { System.out.println(e); } } }

OUTPUT:

24. Write applets to draw the following shapes: (i) Cone (ii) Cylinder (iii) Cube (iv) Square inside a circle (v) Circle inside a square i) import java.awt.*; import java.applet.*; public class Example24a extends Applet { public void paint(Graphics g) { g.drawString("A CONE",40,20); g.drawOval(240,280,70,40); g.drawLine(275,100,240,300); g.drawLine(275,100,310,300); } } HTML DOCUMENT: <HTML> <HEAD> <TITLE> </HEAD> <BODY> <Applet code="Example24a" width="500" height="400"> </APPLET> </BODY> </HTML>

OUTPUT:

ii) import java.awt.*; import java.applet.*; public class Example24b extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("A Cylinder",40,20); g.drawOval(240,120,70,40); g.drawOval(240,280,70,40); g.drawLine(240,140,240,300); g.drawLine(310,140,310,300); } } HTML DOCUMENT: <HTML> <HEAD> <TITLE> </HEAD> <BODY> <Applet code="Example24b" width="500" height="500"> </APPLET> </BODY> </HTML>

OUTPUT:

iii) import java.awt.*; import java.applet.*; public class prog24b extends Applet { public void paint(Graphics g) { g.drawString("A Cube",20,40); g.drawRect(80,160,80,80); g.drawRect(40,120,80,80); g.drawLine(40,120,80,160); g.drawLine(40,200,80,240); g.drawLine(120,120,160,160); g.drawLine(120,200,160,240); } } HTML DOCUMENT: <HTML> <HEAD> <TITLE> </HEAD> <BODY> <Applet code="prog24b" width="500" height="500"> </APPLET> </BODY> </HTML>

OUTPUT:

iv) import java.awt.*; import java.applet.*; public class prog24c extends Applet { public void paint(Graphics g) { g.drawRect(80,160,80,80); g.drawOval(81,161,78,78); } } HTML DOCUMENT: <HTML> <HEAD> <TITLE> </HEAD> <BODY> <Applet code="prog24c" width="500" height="500"> </APPLET> </BODY> </HTML>

OUTPUT:

V) import java.awt.*; import java.applet.*; public class prog24d extends Applet { public void paint(Graphics g) { g.drawString("Square inside a Circle",40,20); g.drawOval(40,158,118,118); g.drawRect(60,179,80,80); } } HTML DOCUMENT; <HTML> <HEAD> <TITLE> </HEAD> <BODY> <Applet code="prog24d" width="500" height="500"> </APPLET> </BODY> </HTML>

OUTPUT:

26. Write an applet to display a face.

import java.awt.*; import java.applet.*; public class prog25 extends Applet { int position=60; public void paint(Graphics g) { g.drawString(" FACE",50,30); g.setColor(Color.yellow); g.fillOval(position,60,200,200); g.setColor(Color.green); g.fillOval(position+30,120,50,20); g.fillOval(position+130,120,50,20); g.drawLine(position+105,125,position+105,175); g.drawLine(position+105,175,position+90,160); g.drawArc(position+50,130,95,95,0,-180); } }

HTML DOCUMENT: <HTML> <HEAD> <TITLE> </HEAD> <BODY> <Applet code="prog25" width="500" height="500"> </APPLET> </BODY> </HTML>

OUTPUT:

27.Write an applet to display five buttons.

import java.awt.*; import java.applet.*; public class prog26 extends Applet { public void init() { add(new Button("One")); add(new Button("Two")); add(new Button("Three")); add(new Button("Four")); add(new Button("Five")); } } HTML DOCUMENT: <HTML> <HEAD> <TITLE> </HEAD> <BODY> <Applet code="prog26" width="500" height="500"> </APPLET> </BODY> </HTML>

OUTPUT:

28. Write an applet to Illustrate BorderLayout import java.awt.*; import java.applet.*; public class Example28 extends Applet { public void init() { setLayout(new BorderLayout(5,5)); add ("South",new Button("BOTTOM")); add("North",new Button("TOP")); add("Center",new TextArea("At The Center")); } public Insets getInsets() { return new Insets(20,20,10,10); } } HTML DOCUMENT: <HTML> <HEAD> <TITLE> </HEAD> <BODY> <Applet code="Example28" width="500" height="500"> </APPLET> </BODY> </HTML>

OUTPUT:

29. Write a Java program to create 5 threads by extending Thread class

class pro extends Thread { String name; Thread t; pro(String s) { name=s; t=new Thread(this,s); System.out.println("NEW:"+t); t.start(); } public void run() { try { for(int i=8;i>0;i--) { System.out.println(name+" "+i); Thread.sleep(500); } } catch(Exception e) {} System.out.println(name+"exiting..."); } }

class Example29 { public static void main(String arr[]) { pro t1=new pro("1"); pro t2=new pro("2"); pro t3=new pro("3"); pro t4=new pro("4"); pro t5=new pro("5"); System.out.println("Thread t1 is alive: "+t1.t.isAlive()); System.out.println("Thread t2 is alive: "+t2.t.isAlive());

System.out.println("Thread t3 is alive: "+t3.t.isAlive()); System.out.println("Thread t4 is alive: "+t4.t.isAlive()); System.out.println("Thread t5 is alive: "+t5.t.isAlive()); try { System.out.println("WAITIN FOR THREAD TO FINISH.."); t1.t.join(); t2.t.join(); t3.t.join(); t4.t.join(); t5.t.join(); } catch(Exception e) {} System.out.println("Thread t1 is alive:"+t1.t.isAlive()); System.out.println("Thread t2 is alive:"+t2.t.isAlive()); System.out.println("Thread t3 is alive:"+t3.t.isAlive()); System.out.println("Thread t4 is alive:"+t4.t.isAlive()); System.out.println("Thread t5 is alive:"+t5.t.isAlive()); } }

OUTPUT:

30. Write a Java program to create 5 threads by implementing Runnable interface. class prom implements Runnable { String name; Thread m; prom(String s) { name=s; m=new Thread(this,s); System.out.println("new:"+m); m.start(); } public void run() { try { for(int i=0;i<8;i++) { System.out.println(name+" " +i); Thread.sleep(500); } } catch(Exception e) {} System.out.println(name+"exiting....."); } } class Example30 { public static void main(String arr[]) { prom m1=new prom("1"); prom m2=new prom("2"); prom m3=new prom("3"); prom m4=new prom("4"); prom m5=new prom("5"); System.out.println("Thread m1 is alive"+m1.m.isAlive()); System.out.println("Thread m2 is alive"+m2.m.isAlive()); System.out.println("Thread m3 is alive"+m3.m.isAlive()); System.out.println("Thread m4 is alive"+m4.m.isAlive()); System.out.println("Thread m5 is alive"+m5.m.isAlive());

try { System.out.println("Waiting for thread to alive.."); m1.m.join(); m2.m.join(); m3.m.join(); m4.m.join(); m5.m.join(); } catch(Exception e) {} System.out.println("Thread m1 is alive.."+m1.m.isAlive()); System.out.println("Thread m2 is alive.."+m2.m.isAlive()); System.out.println("Thread m3 is alive.."+m3.m.isAlive()); System.out.println("Thread m4 is alive.."+m4.m.isAlive()); System.out.println("Thread m5 is alive.."+m5.m.isAlive()); System.out.println("Main Thread Exiting.."); } }

OUTPUT:

You might also like