Java Practical
Java Practical
S.N
o
1 Write a java program that accepts10-digit string as a
telephone number and extracts the 3-digit are a code,
3-digit exchange and the remaining 4-digit number
as a separate string and print them.
2 Write a Java program to count the number of word sign
a given sentences.
3 Write a java code to create a 2-D array having 5
rows, with first row having 1element, second row
having 2 elements and so on. Store numbers in these
cells by taking input from user and find the sum of
the numbers ofeach row.
4 Write a program that creates an abstract class called
Shape. Create two subclasses rectangle and triangle.
Include appropriate methods for both the subclasses
that calculate and display the area of the
rectangle and triangle.
5 Write a java source code to accept elements
of an integer array from command line and
sort the array.
6 Define an exception Neg Arg Exception that is
thrown if the argument is negative. Write a
program to find the factorial of a number that
uses thisexception when the number is negative.
7 Create an exception called “No Match Exception” that
is thrown if the string is note equal to “India” .Handle
the exception in your java program.
8 Write Java code to see the all the IP addresses of
“www.google.com” using Inet Address class.
9 Write a java program to read a character file
line by line and displayits contents with line
numbers.
10 Write a java program that display the contents of a
directory passed through command line argument.
Experiment-01
if (phoneNumber.length() == 10) {
String areaCode = phoneNumber.substring(0, 3); String
exchange = phoneNumber.substring(3, 6); String
remainingNumber = phoneNumber.substring(6);
Output:-
Experiment-02
Aim:- 2 Write a Java program to count the number of word sign a givensentences.
Source Code:-
import java.util.Scanner;
scanner.close();
}
String[] words =
sentence.trim().split("\\s+");return
words.length;
}
}
Output:-
Aim:- 3 Write a java code to create a 2-D array having 5 rows, with first row having
1element, second row having 2 elements and so on. Store numbers in these cells by
taking input from user and find the sum of the numbers of each row.
Source Code:-
import java.util.Scanner;
row++) {
for (int col = 0; col < array[row].length; col++) {
");
System.out.print("Enter value for row " + (row + 1) + ", column " + (col + 1) + ":
array[row][col] = scanner.nextInt();
}
}
scanner.close();
}
}
Output:-
Experiment-04
Aim:- 4 Write a program that creates an abstract class called Shape. Create two
subclasses rectangle and triangle. Includeappropriate methods for both the subclasses
that calculate and display the area of the rectangle and triangle.
Source Code:-
abstract class Shape {
abstract double calculateArea();
double calculateArea() {
return length * width;
}
void displayArea() {
System.out.println("Rectangle Area: " + calculateArea());
}
}
double calculateArea() {
void displayArea() {
System.out.println("Triangle Area: " + calculateArea());
}
}
class ShapeDemo {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5,
10);rectangle.displayArea();
Experiment-05
Aim:- 5 Write a java source code to accept elements of an integer array fromcommand
line and sort the array.
Source Code:-
import java.util.Arrays;
Arrays.sort(array);
System.out.print("Sorted Array:
");for (int num : array) {
Experiment-06
Source Code:-
class NegArgException extends Exception {
if (n == 0 || n == 1)
{return 1;
}
args) {try {
}
}
Output:-
Experiment-07
if (n == 0 || n == 1)
{return 1;
}
Output:-
Experiment-8
Aim:- 8 Write Java code to see the all the IP addresses of “www.google.com”using
InetAddressclass.
Source Code:-
import java.net.InetAddress;
import java.net.UnknownHostException;
Experiment-9
Aim:- 9 Write a java program to read a character file line by line and display itscontents
with line numbers.
Source Code:-
import
java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
Output:-
Experiment-10
Aim:- 10 Write a java program that display the contents of a directory passedthrough
command line argument.
Source Code:-
import java.io.File;
if (!directory.exists() ||
!directory.isDirectory()) {
System.out.println("Invalid directory
path."); return;
}
if (files != null) {
for (File file : files) {
System.out.println(file.getName());
}
} else {
System.out.println("No files or directories found.");
}
}
}
Output:-