0% found this document useful (0 votes)
102 views

Cp-Iii (Java Programming) Branch-Cs / Me (Cs-306 / Me-306)

1. The document provides 10 examples of Java programs covering topics like: printing multiple statements, checking even/odd numbers, checking vowels/constants, square roots, multiple classes, type casting, constructors, exception handling, method overloading, and creating an applet. 2. The examples range from basic programs that print statements or check numbers, to more advanced concepts like classes, exceptions, overloading and applets. 3. For each example, the code and expected output is provided.

Uploaded by

badholiya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Cp-Iii (Java Programming) Branch-Cs / Me (Cs-306 / Me-306)

1. The document provides 10 examples of Java programs covering topics like: printing multiple statements, checking even/odd numbers, checking vowels/constants, square roots, multiple classes, type casting, constructors, exception handling, method overloading, and creating an applet. 2. The examples range from basic programs that print statements or check numbers, to more advanced concepts like classes, exceptions, overloading and applets. 3. For each example, the code and expected output is provided.

Uploaded by

badholiya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

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 [ ]) { System.out.println ("This is my program..."); System.out.println ("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= Integer.parseInt(args [0]); if(a= =0) { System.out.println ("ZERO"); } else if (a%2= = 0) { System.out.println (a+ "It is even"); } else if (a%2= = 1) { System.out.println ("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': System.out.println ("It is a vowel"); break; case 'A': System.out.println ("It is a vowel"); break; case 'e': System.out.println ("It is a vowel"); break; case 'E':

System.out.println ("It is a vowel"); case 'i': System.out.println ("It is a vowel"); break; case 'I': System.out.println("It is a vowel"); break; case 'o': System.out.println("It is a vowel"); 5 break;

case 'O': System.out.println("It is a vowel"); break; case 'u': System.out.println("It is a vowel"); break; case 'U': System.out.println("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 java.lang.Math; Class squareroot { public static void main(String args [ ] ) { Double x = 25 double y=Math.sqrt(x); System.out.println ("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 [ ]) { System.out.println("**beginning execution**");

Greeter greeter = new Greeter(); System.out.println("**created Greeter**"); greeter.greet(); } } class Greeter { private static Message s_message = new Message("Hello, World!"); public void greet() { s_message.print(System.out); } } class Message { private String m_text; public Message(String text){ m_text = text; } public void print(java.io.PrintStream ps){ ps.println(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 ; System.out.println("i="+i); System.out.println("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 =rect.rect1.rectarea( ); System.out.println(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) System.out.println("Division by zero"); } y = a/(b+c); System.out.println("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 java.applet.*; import java.awt.*; class Hello Java extends Applet { public void paint(Graphics g) { setBackground(Color.GREEN); g.setColor(Color.WHITE); g.drawString("Hello Java", 10,100); } } <html> <applet code=" Hello Java t" width=10 height=100></applet> </html>

Output

100 Hello Java_______ 10

You might also like