1. Java program to display “hello world” and display the size of all the data types.
public class datatypesizes
{
public static void main (String[] args)
{
[Link]("Hello World!");
[Link](“size of data types in bits:”);
[Link]("byte:"+[Link]+"bites");
[Link]("short:"+[Link]+"bites");
[Link]("integer:"+[Link]+"bites");
[Link]("long:"+[Link]+"bites");
[Link](" float:"+[Link]+"bites");
[Link]("double:"+[Link]+"bites");
[Link]("character:"+[Link]+"bites");
[Link](" boolean:size is jvm dependent(typically 8 bits)");
}
}
2. Java program to find the maximum of three numbers.
import [Link];
public class maxofthreenum
{
public static void main(String[]args)
{
Scanner s=new Scanner([Link]);
[Link]("enter the first number:");
int n1=[Link]();
[Link]("enter the second number:");
int n2=[Link]();
[Link]("enter the third number:");
int n3=[Link]();
if(n1>=n2&&n1>=n3)
[Link](n1+"is the maximum number");
else if(n2>=n1&&n2>=n3)
[Link](n2+"is the maximum number");
else
[Link](n3+"is the maximum number");
}
}
3. Java program to check whether the number is even or odd.
import [Link];
public class oddoreven
{
public static void main(String[] args)
{
Scanner S= new Scanner([Link]);
[Link]("Enter the number");
int n1=[Link]();
if(n1%2==0)
[Link](n1+"is the even number");
else
[Link](n1+"is the odd number");
}
}
4. Java program to implement string operations string length,string concatenate,substring
class stringoperation
{
public static void main(String[] args){
String firstname="gfgc";
String middlename="vijaynagar";
String lastname="bangalore";
String name=[Link](middlename).concat(lastname);
[Link]("name is:"+name);
[Link]("length is:"+[Link]());
[Link]("substring of name is :"+[Link](4,14));
}
}
5. Write a program to demonstrate a division by zero exception
class demo3
{
public static void main(String[] args)
{
int n1=10,n2=0;
int result;
try{
result=n1/n2;
[Link]("The result is"+" "+ result);
}
catch(ArithmeticException e)
{
[Link]("Can't divide by zero");
}
}
}
6. Write a small program to catch Negative Array Size Exception. This
exception is caused when the array is initialized to negative values.
public class negativearraysize
{
public static void main(String[] args)
{
try{
int[] array=new int[-10];
}
catch(NegativeArraySizeException obj)
{
[Link]();
}
[Link]("Exception Caught and Continuing Exceution....");
}
}
[Link] PROGRAM TO CHECK WHETHER A NUMBER IS
PALINDROME OR NOT.
import [Link];
class palindromeeg{
public static void main(String[] args)
{
int r,temp,sum=0;
Scanner s=new Scanner ([Link]);
[Link]("enter the number");
int n=[Link]();
temp=n;
while(n>0)
{
r=n%10;
sum=( sum * 10) + r ;
n=n/10;
}
if(temp==sum)
[Link]("palindrome number");
else
[Link]("not a palindrome number");
}
}
8. JAVA PROGRAM TO ADD TWO INTEGER AND TWO FLOAT
NUMBERS WHEN NO
ARGUMENTS ARE SUPPLIED GIVEN A DEFAULT VALUE TO
CALCULATE SUM USING METHOD
[Link] overloading
{
int add(int x,int y)
{
return(x+y);
}
double add(double a, double b)
{
return(a+b);
}
int add()
{
return add(10,20);
}
}
class sum
{
public static void main(String[] args)
{
overloading d=new overloading();
[Link]([Link](4,5));
[Link]([Link] (4.5,5.5));
[Link]([Link]());
}
}
[Link] program to implement an array of objects
import [Link];
class student
{
public String name;
public int regno;
public student(String name,int regno){
[Link]=name;
[Link]=regno;
}
public void printdetails()
{
[Link](name+":"+regno);
}
}
class main
{
public static void main(String[] args)
{
student s[]= {
new student("tanu",111),
new student("manu",112),
new student("sanu",113),
new student("anu",114)};
for(student temp:s)
[Link]();
}
}
[Link] program to implement the usage of static,local and global/instance variable.
public class variabledemo
{
int iv=10;
static int sv=20;
public void display()
{
int lv=30;
[Link]("local variable="+lv);
iv++;
[Link]("global/instance variable="+iv);
sv++;
}
public static voidmain(String[] args)
{
variabledemo obj1=new variabledemo();
variabledemo obj2=new variabledemo();
[Link]();
[Link]();
[Link]("first object:static variable="+sv);
[Link]("second object:static variable="+sv);
}
}
11. Java program to implement default and parameterized constructor.
class car
{
String model;
int year;
public car()
{
model="i20";
year=2020;
[Link]("default constructor called");
}
public car(String model,int year)
{
[Link]=model;
[Link]=year;
[Link]("parameterized constructor called");
}
public void display()
{
[Link]("car model:"+model);
[Link]("car year:"+year);
}
public static void main(String[] args)
{
car c1=new car();
[Link]();
car c2=new car("punch",2023);
[Link]();
}
}
12. Java program to implement single inheritance
class employee
{
int eid=123 ;
String name="manu";
double salary=45000.00;
}
class bonus extends employee
{
double benefits=salary*0.15;
double newsalary=salary+benefits;
void display()
{
[Link]("employee id:"+eid);
[Link]("employee name:"+name);
[Link]("old salary="+salary);
[Link]("bonus="+benefits);
[Link]("new salary="+newsalary);
}
public static void main(String[] args)
{
bonus b1= new bonus();
[Link]();
}
}