0% found this document useful (0 votes)
3 views13 pages

Java Programs

The document contains a series of Java programming examples demonstrating basic concepts such as displaying messages, arithmetic operations, conditional statements, loops, switch cases, constructors, inheritance, string functions, and exception handling. Each example includes the code and its corresponding output. The examples are aimed at illustrating fundamental programming techniques in Java.

Uploaded by

Bhavana Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Java Programs

The document contains a series of Java programming examples demonstrating basic concepts such as displaying messages, arithmetic operations, conditional statements, loops, switch cases, constructors, inheritance, string functions, and exception handling. Each example includes the code and its corresponding output. The examples are aimed at illustrating fundamental programming techniques in Java.

Uploaded by

Bhavana Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1. Display Welcome Message.

public class welcome

public static void main(String[] s)

System.out.println("Hello, world");

System.out.println("This is Core Java");

Output:

Hello world

This is Core Java

2. Display all arithmetic operators.

class ao

public static void main (String[] args)

short x = 6;

int y = 4;

float a = 12.5f;

float b = 7.2f;

System.out.println ("x is " + x + ", y is " + y);

System.out.println ("x + y = " + (x + y));

System.out.println ("x - y = " + (x – y));

System.out.println ("x * y = " + (x * y));

System.out.println ("x / y = " + (x / y));


System.out.println ("x % y = " + (x % y));

x = -6;

System.out.println ("x % y = " + (x % y));

y=-4;

System.out.println ("x % y = " + (x % y));

x=6; y=-4;

System.out.println ("x % y = " + (x % y));

System.out.println ("a is " + a + ", b is " + b);

System.out.println ("a / b = " + (a / b));

System.out.println ("a / x = " + (a / x));

System.out.println ("a % x = " + (a*x));

System.out.println ("a % b = " + (a%b));

Output:

x is 6, y is 4

x + y = 10

x-y=2

x/y=1

x%y=2

x % y = -2

x % y = -2

x%y=2

a is 12.5, b is 7.2

a / b = 1.7361112

a / x = 2.0833333

a % x = 0.5
a % b = 5.3

3.Calculate Bigger value between 3 numbers.

class big3

public static void main (String[] args)

int a=100;

int b=150;

int c=200;

System.out.println(“Value of A is "+a);

System.out.println(“Value of B is "+b);

System.out.println(“Value of C is "+c);

if (a>b && a>c)

System.out.println("A is bigger");

else if(b>c)

System.out.println("B is biger");

else

System.out.println("C is bigger");

}
Output:

Value of A is 100

Value of B is 150

Value of C is 200

C is bigger

4. Print 1 to 10 counting (for loop).

class count

public static void main (String[] s)

System.out.println("Counting is:");

for (int i =1; i <=10; i++)

System.out.println (i);

Output:

16

27

38

49

5 10

5. Print 1 to 10 counting (do while loop).


class count

public static void main(String[] args)

int i=1;

do

System.out.println(i++);

while (i<=10);

Output:

10

8(c). Print 1 to 10 counting (while loop).

class while1

public static void main(String[] args)


{

int i=1;

while (i<=10)

System.out.println(i++);

Output:

10

6.write a program of Switch Case .

class week

public static void main(String[] args)

int a=3;

switch(a)
{

case 1:

System.out.println("Sunday");

break;

case 2:

System.out.println("Monday");

break;

case 3:

System.out.println("Tuesday");

break;

case 4:

System.out.println("Wednesday");

break;

case 5:

System.out.println("Thursday");

break;

case 6:

System.out.println("Friday");

break;

case 7:

System.out.println("Saturday");

break;

default:

System.out.println("Invalid Number");

}
Output

Tuesday

7. Using Constructors Print the sutdents id and name. (save your file
with the name of main class)

class student

int id;

String n;

student(int i,String name)

id=i;

n=name;

void display()

System.out.println("Your id is "+id);

System.out.println("Your name is "+n);

System.out.println("------------");

class cons // main class

public static void main(String[] args)

student s1=new student(5,"Chirag");


student s2=new student(7,"Rahul");

s1.display();

s2.display();

Output: -

Your id is 5

Your name is Chirag

-------------------------

Your id is 7

Your name is Rahul

8. Using Inheritance find the ara of a room with height and without
height.

(save your file with the name of main class)

class room // parent class//

int l,w;

room(int x,int y) //constructor of room class//

l=x; w=y;

int a()

return (l*w);

}
class bed extends room

int h;

bed(int x,int y,int z)// constructor of bed class//

super(x,y); // To call super class variable.

h=z;

int b()

return (l*w*h);

class house // Main class

public static void main(String[] args)

int i,j;

bed b1=new bed(5,4,3);

i=b1.a();

j=b1.b();

System.out.println("Area of room without height is="+i);

System.out.println("Area of room with height is="+j);

Output:
Area of room without height is=20

Area of room with height is=60

9.write a program of String function.

String Function

import java.io.*;

class sf

public static void main(String args[])

String s = "COre Java ";

String s1 = "CORE";

String s2 = "JAVA";

System.out.println("Upper Case- "+s.toUpperCase());

System.out.println("Lower Case- "+s.toLowerCase());

System.out.println("As it is - "+s);

System.out.println("The Starts With() and ends With() METHOD CALLS


HERE");

System.out.println("Start result- "+s.startsWith("CO"));

System.out.println("End result- "+s.endsWith("VA"));

String s3=s1.concat(s2);

System.out.println(s3);

System.out.println(s1.compareTo(s2));

System.out.println(s1==s2);

}
Output

Upper Case- CORE JAVA

Lower Case- core

As it is- COre Java

The Starts With() and ends With() METHOD CALLS HERE

Start result- true

End result- false

COREJAVA

-7

False

10.write a program of String handling

Exception Handling : try, catch and final block.

class Five

public static void main(String args[])

try

int arr[]=new int[5];

arr[5]=25;

catch(ArrayIndexOutOfBoundsException e)

System.out.println("Excpetion Occurs................."+e);

finally
{

System.out.println("Finally block executed");

System.out.println("Rest of the code will be executed");

Output:

Finally block executed

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at

You might also like