0% found this document useful (0 votes)
0 views14 pages

Java Practical File

The document contains a list of programming exercises with their respective objectives and Java code implementations. Each program demonstrates various programming concepts such as calculating averages, handling command-line arguments, checking for prime numbers, reversing numbers, and using object-oriented principles like method overriding and abstract classes. The document serves as a practical guide for learning Java programming through hands-on examples.

Uploaded by

shabaghel19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views14 pages

Java Practical File

The document contains a list of programming exercises with their respective objectives and Java code implementations. Each program demonstrates various programming concepts such as calculating averages, handling command-line arguments, checking for prime numbers, reversing numbers, and using object-oriented principles like method overriding and abstract classes. The document serves as a practical guide for learning Java programming through hands-on examples.

Uploaded by

shabaghel19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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;

System.out.println("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=Integer.parseInt(args[0]);

int b=Integer.parseInt(args[1]);

System.out.println("A is:"+a);

System.out.println("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 java.util.Scanner;

public class Test

public static void main(String[] args)

int num, i, count=0;

Scanner br = new Scanner(System.in);

System.out.println("Enter a Number: ");

num = br.nextInt();

for(i=2; i<num; i++)

if(num%i == 0)

count++;

break;

if(count==0){

System.out.println(" It is a Prime Number.");

else{
System.out.println("It is not a Prime Number.");

Output:-
PROGRAM-04

OBJECT:- Write a program to Reverse a given number.

Program:-
import java.util.*;
class Program4{
public static void main(String[] args){
Scanner sc=new Scanner(System.in); int a,
rev=0,rem;
System.out.print("Enter any Number: ");
a=sc.nextlnt();
while(a!=0){
rem=a%10;
rev=(rev*10)+rem;
a=a/10;
}
System.out.println("Reversed number is:"+rev);
}}

Output:-
PROGRAM-05
OBJECT:- Write a program to print the given pattern.

Program:-

import java.util.*;
public class Program5 {
public static void main(String[] args)
{
Scanner br =new Scanner(System.in);
System.out.println("Enter the value of Row(N)");
int n=br.nextInt();
for(int i=1;i<=n;i++) {
int num=i;
for(int j=1;j<=i;j++) {
System.out.println(num+" ");
num=num+n-j;
}
System.out.println();
}
}
}
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(){
System.out.println("area of rectangle is:"+rec);
}
public static void main(String[] args){
Program6 br=new Program6(25,50);
br.disp();
}
}
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<array.length;i++){
if(array[i]>max){
max=array[i];
}
}
System.out.println("The Given Array Element is:");
for(int i=1;i<array.length;i++){
System.out.println(array[i]);
}
System.out.println("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 java.util.*;
public class Program8 {
public static void main(String[] args) {
Scanner br=new Scanner(System.in);
System.out.println("Enter A String");
String name=br.nextLine();
System.out.println("String Length is: "+name.length());
}
}
Output:-
PROGRAM-09
OBJECT:- Write a program to demonstrate method
overriding.
Program:-

// Superclass
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

// Subclass
class Dog extends Animal {
// Overriding the sound method
@Override
void sound() {
System.out.println("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

myAnimal.sound(); // Output: Animal makes a sound


myDog.sound(); // 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() {
System.out.println("Displaying shape...");
}
}

// Subclass inherits from Shape and provides implementation


class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a Circle");
}
}

// Main class to test


public class AbstractDemo {
public static void main(String[] args) {
Shape s = new Circle(); // Polymorphism

s.display(); // Calls concrete method from abstract class


s.draw(); // Calls overridden abstract method
}}
Output-
+-------------------------------------------------+
| C:\Users\VIJAY\workspace\CollegeHomework\> |
| |
| Displaying shape... |
| Drawing a Circle |
| |
| C:\Users\VIJAY\workspace\CollegeHomework\> |
+-------------------------------------------------+

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy