1. Display Welcome Message.
public class welcome
public static void main(String[] s)
[Link]("Hello, world");
[Link]("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;
[Link] ("x is " + x + ", y is " + y);
[Link] ("x + y = " + (x + y));
[Link] ("x - y = " + (x – y));
[Link] ("x * y = " + (x * y));
[Link] ("x / y = " + (x / y));
[Link] ("x % y = " + (x % y));
x = -6;
[Link] ("x % y = " + (x % y));
y=-4;
[Link] ("x % y = " + (x % y));
x=6; y=-4;
[Link] ("x % y = " + (x % y));
[Link] ("a is " + a + ", b is " + b);
[Link] ("a / b = " + (a / b));
[Link] ("a / x = " + (a / x));
[Link] ("a % x = " + (a*x));
[Link] ("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
[Link] Bigger value between 3 numbers.
class big3
public static void main (String[] args)
int a=100;
int b=150;
int c=200;
[Link](“Value of A is "+a);
[Link](“Value of B is "+b);
[Link](“Value of C is "+c);
if (a>b && a>c)
[Link]("A is bigger");
else if(b>c)
[Link]("B is biger");
else
[Link]("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)
[Link]("Counting is:");
for (int i =1; i <=10; i++)
[Link] (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
[Link](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)
[Link](i++);
Output:
10
[Link] a program of Switch Case .
class week
public static void main(String[] args)
int a=3;
switch(a)
{
case 1:
[Link]("Sunday");
break;
case 2:
[Link]("Monday");
break;
case 3:
[Link]("Tuesday");
break;
case 4:
[Link]("Wednesday");
break;
case 5:
[Link]("Thursday");
break;
case 6:
[Link]("Friday");
break;
case 7:
[Link]("Saturday");
break;
default:
[Link]("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()
[Link]("Your id is "+id);
[Link]("Your name is "+n);
[Link]("------------");
class cons // main class
public static void main(String[] args)
student s1=new student(5,"Chirag");
student s2=new student(7,"Rahul");
[Link]();
[Link]();
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();
[Link]("Area of room without height is="+i);
[Link]("Area of room with height is="+j);
Output:
Area of room without height is=20
Area of room with height is=60
[Link] a program of String function.
String Function
import [Link].*;
class sf
public static void main(String args[])
String s = "COre Java ";
String s1 = "CORE";
String s2 = "JAVA";
[Link]("Upper Case- "+[Link]());
[Link]("Lower Case- "+[Link]());
[Link]("As it is - "+s);
[Link]("The Starts With() and ends With() METHOD CALLS
HERE");
[Link]("Start result- "+[Link]("CO"));
[Link]("End result- "+[Link]("VA"));
String s3=[Link](s2);
[Link](s3);
[Link]([Link](s2));
[Link](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
[Link] 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)
[Link]("Excpetion Occurs................."+e);
finally
{
[Link]("Finally block executed");
[Link]("Rest of the code will be executed");
Output:
Finally block executed
Exception in thread "main" [Link]: 5 at