CP-III (JAVA PROGRAMMING) BRANCH-CS / ME (CS-306 / ME306)
1. Write a program to show multiple statements in java. Class statements { Public static void main (String args [ ]) { [Link] ("This is my program..."); [Link] ("I have a copy right for it..."); } }
Output This is my program... I have a copy right for it...
2. Write a program to accept a no. and check whether it is even or odd. Cass check { public static void main (String args [ ]) { int a= [Link](args [0]); if(a= =0) { [Link] ("ZERO"); } else if (a%2= = 0) { [Link] (a+ "It is even"); } else if (a%2= = 1) { [Link] ("It is odd"); } }} Output
If enter a =0 then print ZERO If enter a =2 then print even If enter a =1 then print odd
3. Write a program to accept a character and check whether its constant or vowel. (Using switch statements) class paper { public void main(char character) { switch(character) { case 'a': [Link] ("It is a vowel"); break; case 'A': [Link] ("It is a vowel"); break; case 'e': [Link] ("It is a vowel"); break; case 'E':
[Link] ("It is a vowel"); case 'i': [Link] ("It is a vowel"); break; case 'I': [Link]("It is a vowel"); break; case 'o': [Link]("It is a vowel"); 5 break;
case 'O': [Link]("It is a vowel"); break; case 'u': [Link]("It is a vowel"); break; case 'U': [Link]("It is a vowel"); break; } } }
Output If a was entered It is a vowel Any other character entered.. It is a consonant
4. Write a program to square root of any value. Import [Link]; Class squareroot { public static void main(String args [ ] ) { Double x = 25 double y=[Link](x); [Link] ("The square root is "+y); }
Output The square root is 5.0
5. Write a program to concept of multiple classes in java. Class demo { public static void main(String args [ ]) { [Link]("**beginning execution**");
Greeter greeter = new Greeter(); [Link]("**created Greeter**"); [Link](); } } class Greeter { private static Message s_message = new Message("Hello, World!"); public void greet() { s_message.print([Link]); } } class Message { private String m_text; public Message(String text){ m_text = text; } public void print([Link] ps){ [Link](m_text); } }
Output **beginning execution**
**created Greeter** Hello, World!
6. Write a program to show type casting in java. Class casting { public static void main(String args [ ] ) { float sum; int i; sum = 0.0f; for (i=1; i<=10; i++) { Sum = sum+1 / (float)i ; [Link]("i="+i); [Link]("sum ="+sum); } } } Output i= 1 sum =1 i= 2 sum =1.5 i= 3 sum =1.83333 i= 4 sum =2.08333 i= 5 sum =2.28333 i= 6 sum =2.45 i= 7 sum =2.59286 i=8 sum =2.71786
i= 9 sum =2.82897 i= 10 sum =2.92897
7. Write a program to show use and advantages of constructor. Class Rectangle { int length, width; Rectangle (int x, int y) { length = x; width = y; } int rectarea( ) { Return(length*width); } } Class Rectangle area { Public static void main (String args [ ]) { Rectangle rect1 = new Rectangle (15, 10); constructor //calling
int area1 =[Link]( ); [Link](Area1 = +area);
Output Area1 = 150
8. Write a program to show how exception handling is in java. Class error { Public static void main (String args [ ] ) { int a =10,b=5,c=5,x,y; try { x = a/(b-c); } Catch (Arithmetic exception) [Link]("Division by zero"); } y = a/(b+c); [Link]("y="+y); } } Output Division by zero y=1 // exception
9. Write a program to show method overloading in java. Class Room { float length; float bridth; Room(float x ,float y ) { length = x; breadth y; } Room(float x) // constructor 2 (Overloading of method) { length = breadth = x ; } int area ( ) { Return (length*breadth); } } Output Room room1 =new Room (25.0, 15.0); Room room2 =new Room (20.0); //using constructor 1 //using constructor 1 // constructor 1
10. Write a program to show Hello Java in explorer using applet. import [Link].*; import [Link].*; class Hello Java extends Applet { public void paint(Graphics g) { setBackground([Link]); [Link]([Link]); [Link]("Hello Java", 10,100); } } <html> <applet code=" Hello Java t" width=10 height=100></applet> </html>
Output
100 Hello Java_______ 10