INDEX
Sr. No. Practical Date Remark
Write A Program To Find The
1. Average Of Three Positive Numbers.
Write A Program To Accept Two
2. Arguments From The Command Line
And Display On The Console.
Write A Program To Check Whether
3. The Given Number Is Prime or Non-
Prime (Using Scanner Class).
Write A Program To Reverse A Given
4. Number.
Write A Program To Print The Given
5. Pattern.
Write A Program To Find The Area
6. Of A Rectangle Using Constructor.
A Program To Find The Largest Value
7. In An Array Of Integer.
Write A Program To Find The Length
8. Of A Given String.
Write a program to demonstrate
9. method overriding.
Write a program to display the use of
10. abstract.
PROGRAM-01
OBJECT:- Write a Program to find the Average of three Positive Integer.
Program:-
class Program
public static void main(String[] args)
int a=10,b=20,c=30;
float avg= (a+b+c)/3;
[Link]("Average = "+avg);
Output-
PROGRAM-02
OBJECT:- Write a Program to accept two arguments from the command line
and display on the console.
Program:
class Program
public static void main(String[] args)
int a=[Link](args[0]);
int b=[Link](args[1]);
[Link]("A is:"+a);
[Link]("B is:"+b);
Output:-
PROGRAM-03
OBJECT:- Write a Program to check whether the given
number is prime or non-prime(using Scanner class).
Program:-
Import [Link];
public class Test
public static void main(String[] args)
int num, i, count=0;
Scanner br = new Scanner([Link]);
[Link]("Enter a Number: ");
num = [Link]();
for(i=2; i<num; i++)
if(num%i == 0)
count++;
break;
if(count==0){
[Link](" It is a Prime Number.");
else{
[Link]("It is not a Prime Number.");
Output:-
PROGRAM-04
OBJECT:- Write a program to Reverse a given number.
Program:-
import [Link].*;
class Program4{
public static void main(String[] args){
Scanner sc=new Scanner([Link]); int a,
rev=0,rem;
[Link]("Enter any Number: ");
a=[Link]();
while(a!=0){
rem=a%10;
rev=(rev*10)+rem;
a=a/10;
}
[Link]("Reversed number is:"+rev);
}}
Output:-
PROGRAM-05
OBJECT:- Write a program to print the given pattern.
Program:-
import [Link].*;
public class Program5 {
public static void main(String[] args)
{
Scanner br =new Scanner([Link]);
[Link]("Enter the value of Row(N)");
int n=[Link]();
for(int i=1;i<=n;i++) {
int num=i;
for(int j=1;j<=i;j++) {
[Link](num+" ");
num=num+n-j;
}
[Link]();
}
}
}
Output:-
PROGRAM-06
OBJECT:- Write a program to find area of rectangle using
constructor.
Program:-
public class Program6 {
int rec=0;
Program6(int a,int b){
int x=a;
int y=b;
rec=x*y;
}
void disp(){
[Link]("area of rectangle is:"+rec);
}
public static void main(String[] args){
Program6 br=new Program6(25,50);
[Link]();
}
}
Output:-
PROGRAM-07
OBJECT:- Write a program to find the largest value in
an array of integer.
Program:-
public class Program7 {
public static void main(String[] args){
int[] array=new int[] {200,304,450,349,846,1129};
int max=array[0];
for(int i=1;i<[Link];i++){
if(array[i]>max){
max=array[i];
}
}
[Link]("The Given Array Element is:");
for(int i=1;i<[Link];i++){
[Link](array[i]);
}
[Link]("From the array element largest Number is
"+max);
}
}
Output:-
PROGRAM-08
OBJECT:- Write a program to find the length of a
given string.
Program:-
import [Link].*;
public class Program8 {
public static void main(String[] args) {
Scanner br=new Scanner([Link]);
[Link]("Enter A String");
String name=[Link]();
[Link]("String Length is: "+[Link]());
}
}
Output:-
PROGRAM-09
OBJECT:- Write a program to demonstrate method
overriding.
Program:-
// Superclass
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
// Subclass
class Dog extends Animal {
// Overriding the sound method
@Override
void sound() {
[Link]("Dog barks");
}
}
// Main class to test
public class MethodOverridingDemo {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Reference and object of
Animal
Animal myDog = new Dog(); // Reference of Animal but
object of Dog
[Link](); // Output: Animal makes a sound
[Link](); // Output: Dog barks (method overridden)
}
}
Output-
+--------------------------------------------------+
| C:\Users\VIJAY\workspace\YourProject\> |
| |
| Animal makes a sound |
| Dog barks |
| |
| C:\Users\VIJAY\workspace\YourProject\> |
+--------------------------------------------------+
PROGRAM-10
OBJECT:- Write a program to display the use of abstract.
class.
Program:-
// Abstract class
abstract class Shape {
// Abstract method (no body)
abstract void draw();
// Concrete method
void display() {
[Link]("Displaying shape...");
}
}
// Subclass inherits from Shape and provides implementation
class Circle extends Shape {
@Override
void draw() {
[Link]("Drawing a Circle");
}
}
// Main class to test
public class AbstractDemo {
public static void main(String[] args) {
Shape s = new Circle(); // Polymorphism
[Link](); // Calls concrete method from abstract class
[Link](); // Calls overridden abstract method
}}
Output-
+-------------------------------------------------+
| C:\Users\VIJAY\workspace\CollegeHomework\> |
| |
| Displaying shape... |
| Drawing a Circle |
| |
| C:\Users\VIJAY\workspace\CollegeHomework\> |
+-------------------------------------------------+