0% found this document useful (0 votes)
156 views

Java Lab 4th Sem

The document outlines 8 programs related to object oriented programming in Java. The programs cover topics like creating classes with methods and objects, inheritance, method overriding, multithreading, and sorting arrays. Each program is accompanied by the code and output for that program.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Java Lab 4th Sem

The document outlines 8 programs related to object oriented programming in Java. The programs cover topics like creating classes with methods and objects, inheritance, method overriding, multithreading, and sorting arrays. Each program is accompanied by the code and output for that program.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

OBJECT ORIENTED THROUGH JAVA lab programs

S NO PROGRAM NAME DATE


1 Write a Program to print the area of triangle. Save it with name 06/05/2022
Area.java in your folder
2 Write a java Program to check the number is Prime or not. 09/05/2022
3 Write a program to create a class Student with data ‘name, city and 13/05/2022
age’ along with method print Data to display the data. Create the
two objects s1 , s2 to declare and access the values.
4 Write a program in JAVA to demonstrate the method 17/05/2022
and constructor overloading
5 Write a program in JAVA to create a class Bird also declares 03/06/2022
the different parameterized constructor to display the name of
Birds.

6 06/06/2022
Java program to illustrate the Inheritance& Method overriding
7 Read array of numbers through command line and sort in 10/06/2022
ascending order

8 Write a java program for multithread in which user thread and 17/06/2022
thread started from main method invoked at a time each
thread sleep for 1 sec.

Program 1

Write a Program to print the area of triangle. Save it with name Area.java in your folder.
class Area
{
public static void main(String args[])
{
int height =10, base=6;
float area=0.5F*base* height;
System.out.println(“area of triangle = ”+area);
}
}
Output:

C:\Users\acer\Desktop>javac Area.java

C:\Users\acer\Desktop>java Area
area of triangle = 30.0
Program 2
Write a java Program to check the number is Prime or
not. import java.util.Scanner;
class Prime
{
public static void main(String arr[])
{
int c;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number to be tested for prime
"); int n=in.nextInt();
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
System.out.println(n+">>>>> not prime");
break;
}
}
if ( c == n )
System.out.println(n+ ">>>>Number is prime.");
}

}
Output:

C:\Users\acer\Desktop>javac Prime.java

C:\Users\acer\Desktop>java Prime
Enter the number to be tested for prime
83
83>>>>Number is prime.

C:\Users\acer\Desktop>java Prime
Enter the number to be tested for prime
42
42>>>>> not prime

Program 3
Write a program to create a class Student with data ‘name, city and age’ along with method print
Data to display the data. Create the two objects s1 , s2 to declare and access the values.
class Student
{
String name, city;
int age;
staticint m;
voidprintData()
{
System.out.println("Student name = "+name);
System.out.println("Student city = "+city);
System.out.println("Student age = "+age);
}
}
classStest
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.name="Amit";
s1.city="Dehradun";
s1.age=22;
s2.name="Kapil";
s2.city="Delhi";
s2.age=23;
s2.printData();
s1.printData();
}
}
Output:

C:\Users\acer\Desktop>javac Stest.java

C:\Users\acer\Desktop>java Stest
Student name = Kapil
Student city = Delhi
Student age = 23
Student name = Amit
Student city = Dehradun
Student age = 22
Program 4
Write a program in JAVA to demonstrate the method and constructor overloading.

class Cs
{
intp,q;
public Cs()
{}
public Cs(int x, int y)
{
p=x;
q=y;
}
publicint add(int i, int j)
{
return (i+j);
}
publicint add(int i, int j, int k)
{
return (i+j+k);
}
public float add(float f1, float f2)
{
return (f1+f2);
}
public void printData()
{
System.out.print("p = "+p);
System.out.println(" q = "+q);
}
}
classConstructorOverlaoding
{
public static void main(String args[])
{
int x=2, y=3, z=4;
Cs c=new Cs();
Cs c1=new Cs(x, z );
c1.printData();
float m=7.2F, n=5.2F;
int k=c.add(x,y);
int t=c.add(x,y,z);
floatft=c.add(m, n);
System.out.println("k = "+k);
System.out.println("t = "+t);
System.out.println("ft = "+ft);
}

C:\Users\acer\Desktop>javac COverloading.java

C:\Users\acer\Desktop>java COverloading
p=2q=4
k=5
t=9
ft = 12.4

Program 5
Write a program in JAVA to create a class Bird also declares the different parameterized
constructor to display the name of Birds.
class Bird
{
int age;
String name;
Bird()
{
System.out.println("this is the parrot");
}
Bird(String x)
{
name=x;
System.out.println("this is the "+name);
}
Bird(inty,String z)
{
age=y;
name=z;
System.out.println("this is the "+age+"years\t"+name);
}
public static void main(String arr[])
{
Bird a=new Bird();
a.Bird();
Bird b=new Bird("maina");
Bird c=new Bird(20,"sparrow");
}
}
Output:

C:\Users\acer\Desktop>javac Bird.java

C:\Users\acer\Desktop>java Bird
this is the parrot
this is the maina
this is the 20years sparrow

Program 6
*Java program to illustrate the Inheritance& Method overriding

import java.util.*;
import java.lang.*;
import java.io.*;
class one
{
Public void print_geek()
{
System.out.println("Geeks");
}
}
Class two extends one
{
Public void print_for()
{
System.out.println("for");
}
}
// Driver class
Public class Main
{
Public static void main(String[] args)
{
two g = new two();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Output:
C:\Users\acer\Desktop>javac Main.java

C:\Users\acer\Desktop>java Main
Geeks
for
Geeks

Program 7
Read array of numbers through command line and sort in ascending order

class SortAsc1
{
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
//Displaying elements of original array
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
//Sort the array in ascending order
for (int i = 0; i < arr.length; i++)
{

for (int j = i+1; j < arr.length; j++)


{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

System.out.println();
//Displaying elements of array after sorting
System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}
OUTPUT:
C:\Users\acer\Desktop>java SortAsc1
Elements of original array:
52871
Elements of array sorted in ascending order:
12578

Program 8
Write a java program for multithread in which user thread and thread started from main
method invoked at a time each thread sleep for 1 sec.

class UserThread extends Thread


{
public void run()
{
Thread t=Thread.currentThread();
System.out.println("run() is invoked in"+t.getName()
+"thread...");
for(int i=1;i<=10;i++)
{
System.out.println("run:"+i);
try
{
Thread.sleep(1000);
}
catch(Exception e){}
}
System.out.println("run() is completed");
}
}
class MultiThread
{
public static void main(String arr[])
{
System.out.println("main() started creating an object of user Thread......");
UserThread t=new UserThread();
System.out.println("directly invoking run() of user thread");
t.run();
System.out.println("control back in main()....");
System.out.println("launching new thread for run() of user thread....");
t.start();
for(int i=10;i>0;i--)
{
System.out.println("main:"+i);
try
{
Thread.sleep(1000);
}
catch(Exception e){}
}
System.out.println("main() completed");
}
}

Output:
C:\Users\acer\Desktop>java MultiThread1
main() started creating an object of user Thread......
directly invoking run() of user thread
run() is invoked inmainthread...
run:1
run:2
run:3
run:4
run:5
run:6
run:7
run:8
run:9
run:10
run() is completed
control back in main()....
launching new thread for run() of user thread....
main:10
run() is invoked inThread-0thread...
run:1
run:2
main:9
run:3
main:8
main:7
run:4
main:6
run:5
main:5
run:6
main:4
run:7
main:3
run:8
main:2
run:9
main:1
run:10
main() completed
run() is completed

You might also like