EX.
NO: USER INPUT WITH CLASS OBJECT CREATION
DATE:
AIM:
To implement the program of user input with class object
creation in java.
ALGORITHM:
Step1: start the program.
Step2:initialize the class.
Step3:create the function of print().
Step4:Get a value from the user using scanner.
Step5:create obj for the class and call the print().
Step6:print the value.
Step8:stop the program.
PROGRAM:
import java.util.*;
public class input
void print(int in)
{
OUTPUT:
System.out.print("the input is="+in);
public static void main(String[] args)
input obj=new input();
Scanner sc=new Scanner(System.in);
System.out.print("enter a number:");
int n=sc.nextInt();
obj.print(n);
RESULT:
Thus the implementation to the program of user input with class
object creation was executed successfully and output was verified.
EX.NO: IMPLEMENTING LOOPING AND CONDITIONAL
DATE: STATEMENTS
AIM:
To implement the program of looping and conditional statement
in java.
ALGORITHM:
1.IF…ELSE
Step1:Start the program.
Step2: Get the n value from the user using scanner operation.
Step3:check condition if(n%2==0) then print n is even.
Step4:else print n is odd.
Step5:stop the program.
PROGRAM:
import java.util.*;
public class if_loop
public static void main(String[] args)
int n;
OUTPUT:
Scanner sc=new Scanner(System.in);
System.out.print("enter a number:");
n=sc.nextInt();
if (n%2==0)
System.out.print("even");
else
System.out.print("odd");
2. WHILE AND DOWHILE LOOP
ALGORITHM:
Step1:Start the program.
Step2:Get n value from the user.
Step3: initialized value of i=0 and check condition(i<=n) is true
print the i value and i is incremented.
Step4:Repeat the process until the condition become false.
Step5:Stop the program.
PROGRAM (WHILE):
import java.util.*;
OUTPUT:
public class while_loop
public static void main(String[] args)
int n,i=1;
Scanner sc=new Scanner(System.in);
System.out.print("enter a number:");
n=sc.nextInt();
while(i<=n)
System.out.print(i+" ");
i+=1;
PROGRAM (DO..WHILE):
import java.util.*;
public class do_while_loop
public static void main(String[] args)
OUTPUT:
int n,i=1;
Scanner sc=new Scanner(System.in);
System.out.print("enter a number:");
n=sc.nextInt();
do
System.out.print(i+" ");
i+=1;
}while(i<=n);
ALGORITHM:
3.FOR LOOP:
Step1:Start the program.
Step2:get the n value from the user.
Step3: print I value until i<=n using for loop.
Step4:Stop the program.
PROGRAM:
import java.util.*;
public class for_loop
{
public static void main(String[] args)
int n;
Scanner sc=new Scanner(System.in);
System.out.print("enter a number:");
n=sc.nextInt();
for(int i=1;i<=n;i++)
System.out.print(i+" ");
ALGORITHM:
4. SWITCHCASE
Step1:Start the program
Step2:Get n1 and n2 value from the user.
Step3:Initilize the case1 to add operation and case2 to subtraction
operation using switch .
Step4:Enter choice for perform the operation.
Step5:if choice is 1 then print the value is addition of two number.
if choice is 2 then print the value is subtraction of two number. If
choice is 3 then print multiplication of two numbers. If choice is 4
then print division of two numbers.
OUTPUT:
Step6:Stop the program.
PROGRAM:
import java.util.*;
public class switch_condition
public static void main(String[] args)
int n1,n2,opt;
Scanner sc=new Scanner(System.in);
System.out.println("1.addition:");
System.out.println("2.subtraction:");
System.out.println("3.multiplication:");
System.out.println("4.division:");
System.out.println("enter a numbers:");
n1=sc.nextInt();
n2=sc.nextInt();
System.out.print("enter option:");
opt=sc.nextInt();
switch(opt)
{
case 1:
System.out.println("answer="+(n1+n2));
break;
case 2:
System.out.println("answer="+(n1-n2));
break;
case 3:
System.out.println("answer="+(n1*n2));
break;
case 4:
System.out.println("answer="+(n1/n2));
break;
default:
System.out.println("invalid selection");
break;
RESULT:
Thus the looping and conditional statements are executed and
verified successfully.
Ex.NO: IMPLEMENTING STRING OPERATION
DATE:
AIM:
To implement the strings operation.
ALGORITHM:
Step1:Start the program.
Step2:Initialized the strings value.
Step3:perform the strings operation are string
length,concated,repace,substring,uppercase,lowercase,trim.
Step4:print the strings operations.
Step5:stop the program.
PROGRAM:
public class string_operations
public static void main(String[] args)
String
str_ans,string1="anand",string2="ramasamy",string3=" ram
";
int int_ans;
OUTPUT:
System.out.println("string1="+string1+"\nstring2="+strin
g2+"\nstring3="+string3);
int_ans=string1.length();
System.out.println("length of the string "+string1+"
is="+int_ans);
str_ans=string1.concat(string2);
System.out.println("concatenation of the string
"+string1+" and "+"string2"+" is="+str_ans);
str_ans=string1.replace('n','N');
System.out.println("after replacing 'n' with 'N'
"+string1+" is="+str_ans);
str_ans=string2.substring(0,3);
System.out.println("substring of the string "+string2+"
between the index 0,3 is="+str_ans);
str_ans=string1.toLowerCase();
System.out.println("lower case of "+string1+"
is="+str_ans);
str_ans=string1.toUpperCase();
System.out.println("upper case of "+string1+"
is="+str_ans);
str_ans=string3.trim();
System.out.println("after trimming the string
"+string1+" ="+str_ans);
}
}
RESULT:
Thus the implementation of string operation was executed
successfully and ouput was verified.
Ex.NO: MATRIX MULTIPLICATION USING TWO
DATE: DIMENSIONAL ARRAYS
AIM:
To implement the matrix multiplication using 2D arrays.
ALGORITHM:
Step1:Start the program.
Step2:get base index of the square matrix.
Step3:get the input data for two matrices.
Step4:perform the multiplication of the two dimensional arrays
using for loop.
Step5:print the result.
Step6:stop the program.
PROGRAM:
import java.util.Scanner;
public class matrix
public static void main(String args[])
int n;
Scanner sc = new Scanner(System.in);
OUTPUT:
System.out.println("Enter the base of squared
matrices");
n = sc.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
int[][] c = new int[n][n];
System.out.println("Enter the elements of 1st martix
row wise \n");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
a[i][j] = sc.nextInt();
System.out.println("Enter the elements of 2nd martix
row wise \n");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
b[i][j] = sc.nextInt();
}
System.out.println("Multiplying the matrices...");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
System.out.println("The product is:");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
System.out.print(c[i][j] + " ");
System.out.println();
}
sc.close();
} input.close();
}
RESULT:
Thus the implemented of matrix multiplication using 2D array
was successfully executed and then output was verified.
EX.NO: IMPLEMENTING THE INHERITANCE
DATE:
AIM:
To implement the inheritance using java.
ALGORITHM:
1.SINGLE INHERITANCE
Step1: start the program.
Step2:create base class of class1.
Step3:create derived class single_inheritance.
Step4:print in class1.
Step5:stop the program.
PROGRAM:
class class1
public void one()
System.out.print("single inheritance was successfully
implemented");
public class single_inheritance extends class1
OUTPUT:
{
public static void main(String[] args)
single_inheritance obj=new single_inheritance();
obj.one();
ALGORITHM:
2. IMPLEMENT THE MULTILEVEL INHERITANCE:
Step1: start the program.
Step2:create base class of class1.
Step3:create derived class class2 that extends class1.
Step4:create derived class multilevel_inheritance extends
class2.
Step5:stop the program.
PROGRAM:
class class1
public void one(int a)
{
System.out.println("this is class "+a);
OUTPUT:
}
class class2 extends class1
public void two(int a)
System.out.println("this is class "+a);
public class multilevel_inheritance extends class2
public static void main(String[] args)
multilevel_inheritance obj=new
multilevel_inheritance();
obj.one(1);
obj.two(2);
System.out.println("this is main class explains
multilevel inheritance");
ALGORITHM:
3.HYBRID INHERITANCE
Step1: start the program.
Step2:create base class of hybr.
Step3:create derived class B,C,D.
Step4:create a object in derived class hybrid_inheritance
amd access all the super classes.
Step5:stop the program.
PROGRAM:
class C
public void fun_c()
{
System.out.println("this is in class C");
class A extends C
public void fun_a()
System.out.println("this is in class A");
OUTPUT:
}
class B extends C
public void fun_b()
System.out.println("this is in class B");
}
class hybrid_inheritance extends A
public void fun_d()
System.out.println("this is in main and derived class that explains
hybrid inheritance");
public static void main(String args[])
hybrid_inheritance obj = new hybrid_inheritance();
obj.fun_a();
System.out.println("class B is another derived class that cannot be
accessed from this derived class");
obj.fun_c();
obj.fun_d();
}
RESULT:
Thus the inheritance program was executed and output was
verified successfully.
Ex.NO: IMPLEMENTING ABSTRACT
DATE:
AIM:
To write a java program to implement abstract class.
ALGORITHM:
Step 1:start the program.
Step 2:create an abstract class class_a using abstract
keyword.
Step 3:create an abstract method implement().
Step 4:create main class abstract_class which extends the
abstract class class_A.
Step 5:invoke the abstract method inside the main class.
Step 6:stop the program.
PROGRAM:
abstract class class_a
{
abstract void implement();
public class abstract_class
void implement()
System.out.print("this class is abstracted from superclass");
OUTPUT:
}
public static void main(String[] args)
abstract_class obj=new abstract_class();
obj.implement();
RESULT:
Thus the implementation of abstract class using java has been
executed successfully.
Ex.NO: IMPLEMENTING INTERFACES
DATE:
AIM:
To write a java program to implement interface.
ALGORITHM:
Step 1: start the program.
Step 2: create an interface class_a using interface keyword.
Step 3: create an method implement() inside the interface.
Step 4: create main class iinterfaces which implements the
interface class_a.
Step 5: invoke the method implement() inside the main class.
Step 6: stop the program.
PROGRAM:
interface class_a
void implement();
public class interfaces implements class_a
public void implement()
{
System.out.print("this class is implemented from
a interface");
public static void main(String[] args)
interfaces obj=new interfaces();
obj.implement();
OUTPUT:
RESULT:
Thus the implementation of interface using java has been
executed successfully.
Ex.NO: IMPLEMENTING PACKAGES
DATE:
AIM:
To write a java program to implement packages.
ALGORITHM:
Step 1:start the program.
Step 2:create a package mypack with the class square.
Step 3:create another file with class packages.
Step 4:import the the package mypack to retrieve the
operations.
Step 5:call the method find and print the square value.
Step 6:stop the program.
PROGRAM:
Square.java
package mypack;
public class square
public void find(int n)
System.out.print("square of the number is="+(n*n));
Packages.java
import java.util.Scanner;
import mypack.*;
class packages
public static void main(String[] args)
Scanner apk=new Scanner(System.in);
square obj=new square();
System.out.print("enter a number:");
int n=apk.nextInt();
obj.find(n);
}
OUTPUT:
RESULT:
Thus the implementation of packages using java has been
executed successfully.
Ex.NO: IMPLEMENTING THREADS
DATE:
AIM:
To write a java program to implement threads.
ALGORITHM:
Step 1:start the program.
Step 2:create a class multi_threading which extends thread.
Step 3:create methods inside the class.
Step 4:call the methods using threads.
Step 5:stop the program.
PROGRAM:
class Anand extends Thread
public void run()
for (int no_of_times=1;no_of_times<=10;no_of_times++)
System.out.println("Anand");
try
Thread.sleep(1000);
catch(Exception e)
{}
}
class Ram extends Thread
public void run()
for (int no_of_times=1;no_of_times<=10;no_of_times++)
System.out.println("Ram");
try
Thread.sleep(1000);
catch(Exception e)
{}
public class multi_threading
{
public static void main(String[] args)
Anand object_anand=new Anand();
Ram object_ram=new Ram();
object_anand.start();
try
Thread.sleep(10);
catch(Exception e)
{}
object_ram.start();
OUTPUT:
RESULT:
Thus the implementation of thread using java has been executed
successfully.
Ex.NO: DESIGNING A SHEET WITH JAVA APPLET
DATE:
AIM:
To design a sheet with java applet.
ALGORITHM:
Step 1:start the program.
Step 2:import java.applet.*
Step 3: import java.awt.*.
Step 3:create class applet_program which extends applet.
Step 4:create method paint(graphics.g).
Step 5:use g.drawstring to print the content inside the applet
using some font types.
Step 6:draw rectangels and lines using some colors
Step 6:stop the program.
PROGRAM:
import java.applet.*;
import java.awt.*;
public class applet_program extends Applet
public void paint(Graphics g)
Font big_font=new Font("segoe print",Font.BOLD,32);
Font small_font=new Font("segoe
print",Font.BOLD,24);
g.setColor(Color.blue);
g.fillRect(0,0,320,480);
g.setColor(Color.white);
g.drawLine(0,80,320,80);
g.setFont(small_font);
g.drawString("hello everyone",75,120);
g.drawLine(0,160,320,160);
g.setColor(Color.white);
g.fillRect(30,240,100,60);
g.fillRect(190,240,100,60);
g.fillRect(30,360,260,60);
g.setFont(big_font);
g.setColor(Color.black);
g.drawLine(70,270,90,270);
g.drawLine(230,270,250,270);
g.drawLine(120,400,180,395);
g.drawLine(180,395,200,380);
//<applet code="applet_program" height="480"
width="320"></applet>
}
OUTPUT:
RESULT:
Thus the implementation of designing sheet with java applet has
been done and executed successfully.