1) Objective: To write a simple java program that prints Hello to the console.
class Program1
{
public static void main(String []arg)
{
[Link]("Hello Friends ");
}
}
Compilation & Execution steps:
1. Save the program as: “[Link]”
2. Compile it using command: javac Program1. Java
3. Run program using command: java Program1
Output:
2) Objective: To write a simple java program to demonstrate the use of
variable in java.
class VariableExample
{
int a;
static int b=20;
VariableExample()
{
[Link]("Instance Variable default value, a="+a);
}
VariableExample(int a )
{
this.a=a;
[Link]("Instance variable value given by me, a="+a);
}
public void add()
{
int c=30, d; //Local Variable
d=a+b+c;
[Link]("sum="+d);
}
public static void main(String []arg)
{
[Link]("Static Variable, b="+b);
VariableExample ve1= new VariableExample();
VariableExample ve2= new VariableExample(50);
[Link]();
[Link]();
}
}
Compilation & Execution steps:
1. Save the program as: “[Link]”
2. Compile it using command: javac VariableExample. Java
3. Run program using command: java VariableExample
Output:
3) Objective: To write a simple java program to demonstrate the use of
Arithmetic operators in java.
class ArithmeticOperator
{
public static void main(String []arg)
{
int a=10, b=3;
[Link]("Addition= "+(a+b));
[Link]("Sub= "+(a-b));
[Link]("Mul= "+(a*b));
[Link]("Div= "+a/b);
[Link]("Remainder= "+a%b);
}
}
Output:
3) Objective: To write a simple java program to demonstrate the use of
Relational operators in java.
class RelationalOperator
{
public static void main(String []arg)
{
int a=10, b=3;
[Link]("a > b: "+(a>b));
[Link]("a < b: "+(a<b));
[Link]("a >= b: "+(a>=b));
[Link]("a <= b: "+(a<=b));
[Link]("a == b: "+(a==b));
[Link]("a != b: "+(a!=b));
}
}
Output:
4) Objective: To write a simple java program to demonstrate the use of
Logical operators in java.
class LogicalOperator
{
public static void main(String [] args){
int n1= 5, n2= 7;
boolean a , b ;
a = (n1>n2) && (n2>5);
b = (n1>n2) || (n2>5);
[Link]("\nValue of a : "+a);
[Link]("Value of b : "+b);
[Link](n1!=4);
}
}
Output:
5) Objective: To write a simple java program to demonstrate the use of
Assignment operators in java.
class AssignmentOperator
{
public static void main(String []arg)
{
int a=10, b=3;
[Link]("a = "+a);
[Link]("b = "+b);
[Link]("a += b : "+(a+=b));
[Link]("a -= b : "+(a-=b));
[Link]("a *= b : "+(a*=b));
[Link]("a /= b : "+(a/=b));
}
}
Output:
6) Objective: To write a simple java program to demonstrate the use of
Bitwise operators in java.
class BitwiseExample
{
public static void main(String []arg)
{
int a=2, b = 3;
[Link]("a&b = "+(a&b));
[Link]("a|b = "+(a|b));
[Link]("~a = "+(~a));
[Link]("a^b = "+(a^b));
[Link]("a>>2 = "+(a>>2));
[Link]("b<<2 = "+(b<<2));
}
}
Output:
7) Objective: To write a simple java program to demonstrate the structure of a
class in java.
class ClassExample
{
int n1,n2;
ClassExample()
{
[Link]("Default Constructor\n n1 = "+ n1+" \n n2 = "+n2);
}
ClassExample(int n1, int n2)
{
[Link]("Parameterize Constructor");
this.n1 = n1;
this.n2 = n2;
[Link]("n1 = "+this.n1);
[Link]("n2 = "+this.n2);
}
public void sum()
{
[Link]("Sum= "+(n1+n2));
}
public static void main(String []arg)
{
ClassExample e1= new ClassExample();
[Link]();
ClassExample e2 = new ClassExample(10,32);
[Link]();
}
}
Output:
8) Objective: To write a simple java program to demonstrate the concept of
Array in java.
import [Link];
class ArrayExample
{
public static void main(String[] args) {
// Declaring an integer array
int[] arr = new int[5];
// accepting array elements by user
[Link]("Enter Array Elements ");
Scanner s = new Scanner([Link]);
for(int i = 0; i<[Link];i++)
arr[i] = [Link]();
// Iterating through the array using a for loop
[Link]("All elements in the array are :");
for (int i = 0; i < [Link]; i++)
{
[Link]("Element at index " + i + ": " + arr[i]);
}
}
}
Output:
9) Objective: To write a simple java program to demonstrate the concept of
String in java.
public class StringMethodsDemo
{
public static void main(String[] args)
{
String s = "Hello Friends";
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link](2));
[Link]([Link](" Welcome"));
[Link]([Link]("l"));
[Link]([Link]("e"));
[Link]([Link](6, 9));
}
}
10) Objective: To write a simple java program to demonstrate the concept of
inheritance in java.
class Parent
{
void m1(int x){
[Link]("Parent m1 int method: "+x);
}
void m1(String msg){
[Link]("Parent m1 string method: "+msg);
}
}
class Child extends Parent
{
void greet()
{
[Link]("Child greet method: Hello Everyone");
}
public static void main(String []arg)
{
Parent p1 = new Child();
p1.m1(10);
p1.m1("Hi");
Child c1 = new Child();
c1.m1(10);
c1.m1("Hi");
[Link]();
}
}
11) Objective: To write a simple java program to demonstrate the concept of
Overriding in java.
class NewParent
{
void m1(){
[Link]("Inside Parent m1 method");
}
}
class NewChild extends NewParent
{
void m1(){
[Link]("Inside child m1 method");
}
void greet()
{
[Link]("Child greet method: Hello Everyone");
}
public static void main(String []arg)
{
NewParent p1 = new NewParent();
p1.m1();
NewParent p2 = new NewChild();
p2.m1();
//[Link](); // parent ref. can hold child object but can only call
parents or overriden methods
NewChild c1 = new NewChild();
c1.m1();
[Link]();
/*NewChild c2 = new NewParent(); //child reference can't
hold parent object-Incompatible type
c2.m1();
[Link]();
*/
}
}