0% found this document useful (0 votes)
4 views43 pages

Java Record

The document outlines various Java programming exercises, including finding the maximum of three numbers, converting Fahrenheit to Celsius, and using data types and operators. It also covers selection and iteration control structures, classes and objects, methods, constructors, and encapsulation. Each section includes algorithms, sample programs, and results indicating successful execution.

Uploaded by

nk0131458
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)
4 views43 pages

Java Record

The document outlines various Java programming exercises, including finding the maximum of three numbers, converting Fahrenheit to Celsius, and using data types and operators. It also covers selection and iteration control structures, classes and objects, methods, constructors, and encapsulation. Each section includes algorithms, sample programs, and results indicating successful execution.

Uploaded by

nk0131458
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

EX.

NO : 1 DATE:

Develop Basic JAVA Programs

A) To Find Maximum among three numbers

Aim:

To write a java program to find Maximum of Three Numbers

Algorithm:

1. Start the program


2. Declare variables a, b, c
3. Using if condition find
• if(a>b) if a is greater than b then if(a>c) if a greater than c then
print “A is Maximum”
• if a is not greater than b then check if(b>c) then print “B is
Maximum”
• else print “C is Maximum”
4. Stop the Program

Program:

public class Maximum{


public static void main(String[] args){
int a=10,b=25,c=14;
if(a>b && a>c){
[Link]("A is Maximum");
}
else if(b>a && b>c){
[Link]("B is Maximum");
}
else{
[Link]("C is Maximum");
}
}}

1
Output :

Result :
Thus the java program was executed successfully.

2
B) To Convert temperature from Fahrenheit to Celsius

Algorithm:

1. Start the program


2. Declare variable temperature
3. Prompt user to enter the value of temperature
4. Calculate Celsius using the formula ((temperature -32)*5)/9
5. Print the value of Celsius
6. Stop the Program

Program:

import [Link];

public class TemperatureConverter {


public static void main(String[] args) {
double temperature;
Scanner scanner = new Scanner([Link]);
[Link]("Enter the temperature in Fahrenheit: ");
temperature = [Link]();
double celsius = ((temperature - 32) * 5) / 9;
[Link]("Temperature in Celsius: " + celsius);
[Link]();
}
}
Output:

Result :

Thus the java program was executed successfully.

3
[Link]: 2 DATE:

Develop JAVA Programs using Data types & Operators


A) Develop JAVA Programs using Data types
Aim:

To write a java program to develop JAVA Programs using Data types

Algorithm:

1. Start the Program


2. Create a scanner object to get user input
3. Prompt the user to get values using various Datatypes
4. Print the Values
5. Stop the Program

Program:

public class Data_type{


public static void main(String [] args){
byte by = 100;
short sh = 32000;
int in = 123456;
long l = 987342679l;
float f = 5.65f;
char ch = 'N';
double db = 19.65;
boolean bn = true;
[Link]("Byte :"+by);
[Link]("Short :"+sh);
[Link]("Integer :"+in);
[Link]("Long :"+l);
[Link]("Float :"+f);
[Link]("Character :"+ch);
[Link]("Double :"+db);
[Link]("Boolean :"+bn);

}
}

4
Output:

Result :

Thus Java program to print values using Datatypes has been successfully
executed and verified

5
B) Develop JAVA Programs using operators
Aim:

To write a java program to develop JAVA Programs using Operators

Algorithm:

1. Start the Program


2. Create a scanner object to get user input
3. Prompt the user to get values for a and b
4. Calculate using various operators
5. Print the Values
6. Stop the Program

Program:
public class Operator{
public static void main(String[]args){
int a=10,b=5,c;
boolean x=true,y=false;
[Link]("Arithmetic operator");
[Link]("A+B="+(a+b));
[Link]("A-B="+(a-b));
[Link]("A*B="+(a*b));
[Link]("A/B="+(a/b));
[Link]("A%B="+(a%b));
[Link]("Relation Operator");
[Link]("A==B"+(a==b));
[Link]("A!=B"+(a!=b));
[Link]("A>B"+(a>b));
[Link]("A>=B"+(a>=b));
[Link]("A<B"+(a<b));
[Link]("A<=B"+(a<=b));
[Link]("Logical Operator");
[Link]("X&&Y:"+(x&&y));
[Link]("X || Y:"+(x||y));
[Link]("!X:"+(!x));
[Link]("Assignment Operator");
c=a;
[Link]("C=A"+c);
c+=b;
[Link]("C+=B"+c);
c-=b;
[Link]("C-=B"+c);
c*=b;
[Link]("C*=B"+c);

6
c/=b;
[Link]("C/=B"+c);
c%=b;
[Link]("C%=B"+c);
[Link]("Bitwise Operator");
[Link]("A&B="+(a&b));
[Link]("A/B="+(a/b));
[Link]("A^B="+(a^b));
[Link]("~A="+(~a));
[Link]("A<<2="+(a<<2));
[Link]("A>>2="+(a>>2));
}
}

Output :

Result :

Thus Java program to print values using operators has been successfully
executed and verified

7
[Link]: 3 DATE:

Develop JAVA Programs on Selection & Iteration control structures

A) Develop JAVA Programs on Selection Structures

Aim:

To write a java program to develop JAVA Programs on Selection


Structures

Algorithm:

1. Start the Program


2. Create a scanner object to get user input
3. Declare variable named number and assign the value
• If the remainder of the number is 0 then print the number is Even.
• Otherwise , print the number is Odd.

4. Declare and prompt the user to enter “day of the week” using switch
• if user enters 1 then print “Monday”
• if user enters 2 then print “Tuesday”
• if user enters 3 then print “Wednesday”
• if user enters 4 then print “Thursday”
• if user enters 5 then print “Friday”
• if user enters 6 then print “Saturday”
• if user enters 7 then print “Sunday”
• else print “Invalid day number”
[Link] the Program

Program
public class Selection
{
public static void main(String [] args)
{
int num = 10;
if (num%2==0)
{
[Link]("Given number is Even");
}else{
[Link]("Given number is Odd");
}

8
int day = 6;
String dayname;
switch(day)
{
case 1:
dayname = "Sunday";
break;
case 2:
dayname = "Monday";
break;
case 3:
dayname = "Tuesday";
break;
case 4:
dayname = "Wednesday";
break;
case 5:
dayname = "Thursday";
break;
case 6:
dayname = "Friday";
break;
case 7:
dayname = "Saturday";
break;
default:
dayname = "Invalid day";
}
[Link]("Day name is : " + dayname);
}
}

Output:

Result :

Thus Java program to print values using conditional structures has been successfully
executed and verified

9
B) Develop JAVA Programs on Iteration Structures

Aim:

To write a java program to develop JAVA Programs on Iteration


Structures

Algorithm:

1. Start the Program


2. Create a scanner object to get user input
3. Declare variable named rows and Prompt user to enter the value
4. Declare variable named column and Prompt user to enter the value
5. Use outer loop to control the rows
6. Use inner loop to control the column
7. Print the value of outer loop or inner loop
8. Initialize variable i and assign a value in it
9. Using do-while loop print the value of i
10. Initialize variable j and assign a value in it
11. Using while loop print the value of j
12. Stop the Program

Program:

For loop

public class ForLoop{


public static void main (String[]args){
[Link]("USIND FOR LOOP");
for(int i=1;i<=5;i++){
[Link](i);
}
}
}

10
Do While Loop

public class DoWhileLoop{


public static void main(String [] args){
[Link]("Using Do While Loop");
int i=1;
do{
[Link](i);
i++;
}while(i<=5);
}
}

While Loop :

public class WhileLoop{


public static void main(String [] args){
[Link]("Using While Loop");
int i=1;
while(i<5){
[Link](i);
i++;
}
}
}

Output :

11
Result :
Thus Java program to print values using Iteration structures has been successfully
executed and verified.

12
[Link]: 4 DATE:

Develop JAVA Programs using Classes and Objects

Aim:

To write a java program to develop JAVA Programs using Classes and


Objects

Algorithm:

1. Start the Program


2. Create a sub class and Main class
3. Initialize variables under sub class
4. Create a method to display the values of variables in the subclass
5. Create a object under Main class for Sub class
6. Assign values to variables in sub class in Main class
7. Call the method in subclass in Main class
8. Stop the Program
Program:

class detail{
int id = 123456;
String name = "Nithish";
String fname = "Murugesan";
String mname = "Rajalakshmi";
void studentdata(){
[Link](id);
[Link](name);
}
void parentsdata(){
[Link](fname);
[Link](mname);
}
}
public class Student{
public static void main(String [] args){
detail d1=new detail();
[Link]();
[Link]();
}
}

13
Output :

Result :

Thus Java program to print values using class and objects has been
successfully executed and verified

14
[Link]: 5 DATE:

Develop JAVA Programs using Methods

Aim:

To write a java program to develop JAVA Programs using Methods

Algorithm:

1. Start the Program


2. Create a class
3. Create a methods for Addition, Subtraction, Multiplication, Division and
Modulus separately
4. Create a method to display the values of variables in the subclass
5. Create a object under Main class for Sub class
6. Assign values to variables in sub class in Main class
7. Call the method in subclass in Main class
8. Stop the Program

Program:

import [Link];
public class ArithmeticOperation{
public static int add(int a, int b){
return a+b;
}
public static int sub(int a, int b){
return a-b;
}
public static int multiply(int a, int b){
return a*b;
}
public static double divide(int a, int b){
if(b!=0){
return(double) a/b;
}else{
[Link]("Error:Division by Zero Error");
return 0;
}
}

15
public static void main(String[]args){
Scanner scan = new Scanner([Link]);
[Link]("Enter Number One");
int n1=[Link]();
[Link]("Enter Number Two");
int n2=[Link]();
[Link]("Addition:"+add(n1,n2));
[Link]("Subtraction:"+sub(n1,n2));
[Link]("Multiplication:"+multiply(n1,n2));
[Link]("Division:"+divide(n1,n2));
[Link]();
}
}

Output :

Result :

Thus To write a java program to develop JAVA Programs using Methods has been successfully
executed and verified.

16
[Link]: 6 DATE:

Develop JAVA Programs using constructors and this


keyword

Aim:

To write a java program to develop JAVA Programs using Constructors


and this keyword

Algorithm:

1. Start the Program


2. Define the Book class:
• Create two instance variables: title and price.
• Define two constructors: a default constructor and a
parameterized constructor
3. Use the this Keyword:
• Use this to refer to the current instance variables.
• Use this in the default constructor to call the parameterized
constructor.
4. Define a Method to Display Details:
• Add a displayDetails method to print the book's title and price.
5. Create the Main class:
• Instantiate Book objects using both constructors.
• Call the displayDetails method to show the book information.
6. Stop the Program

Program:

class Book {
private String title;
private double price;
public Book() {
this("Unknown Title", 0.0);
}
public Book(String title, double price) {
[Link] = title;
[Link] = price;
}

17
public void displayDetails() {
[Link]("Book Title: " + title);
[Link]("Book Price: $" + price);
}
}
public class Constructors {
public static void main(String[] args) {
Book defaultBook = new Book();
Book customBook = new Book("Java Programming", 29.99);
[Link]("Default Book Details:");
[Link]();
[Link]("\nCustom Book Details:");
[Link]();
}
}

Output :

Result :

Thus To write a java program to develop JAVA Programs using Constructors and

this keyword has been successfully executed and verified

18
[Link]: 7 DATE:

Develop JAVA Programs using Encapsulation

Aim:

To write a java program to develop JAVA Programs using Encapsulation

Algorithm:

1. Start the Program


2. Define the Person class:
• Declare two private instance variables: name and age.
• Create public getter and setter methods for each variable.
3. Encapsulate the Fields:
• Use the getter methods to retrieve the values of the private fields.
• Use the setter methods to modify the values of the private fields,
ensuring data validation where necessary.
4. Create the Main class:
• Instantiate the Person class.
• Use the setter methods to set the values of name and age.
• Use the getter methods to display the values.
5. Stop the Program

Program:

class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
if (name == null || [Link]()) {
[Link] = "Unknown";
} else {
[Link] = name;
}
}
19
public int getAge() {
return age;
}

public void setAge(int age) {


if (age < 0 || age > 150) {
[Link] = 0;
} else {
[Link] = age; // Set the value of age
}
}
}

public class Encapsulation {


public static void main(String[] args) {
Person person = new Person();
[Link]("Nithish");
[Link](18);
[Link]("Person Details:");
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
}
}

Output :

Result :

Thus To write a java program to develop JAVA Programs using


Encapsulation has been successfully executed and verified.

20
[Link]: 8 DATE:

Develop JAVA Programs using Array

Aim:

To write and develop a java program Programs using Array

Algorithm:

1. Start the Program


2. Prompt the user to enter the dimensions of two matrices (matrix1 and
matrix2).
3. Ensure the number of columns in matrix1 matches the number of rows in
matrix2 for multiplication.
4. Create two 2D arrays (matrix1 and matrix2) based on the dimensions
provided.
5. Prompt the user to enter the elements of both matrices.
6. Create a result matrix (result) with dimensions:
• Rows = rows of matrix1.
• Columns = columns of matrix2.
7. Perform matrix multiplication:
• For each row in matrix1:
• For each column in matrix2:
• Compute the dot product of the corresponding row from matrix1
and column from matrix2.
8. Display the resulting matrix.
9. Stop the Program

Program:

import [Link];
public class MatrixMultiplication{
public static void main(String[]args){
Scanner n = new Scanner([Link]);
[Link]("Enter the no. of Rows");
int row = [Link]();
[Link]("Enter the no. of Column");
int cols = [Link]();
if(row!=cols){
[Link]("Multiplication is not possible");
}

21
int[][]matrix1=new int [row][cols];
int[][]matrix2=new int [row][cols];
int[][]result=new int [row][cols];
[Link]("Enter element for Matrix 1");
for(int i=0;i<row;i++){
for(int j=0;j<cols;j++){
matrix1[i][j]=[Link]();
}
}
[Link]("Enter element for Matrix2");
for(int i=0;i<row;i++){
for(int j=0;j<cols;j++){
matrix2[i][j]=[Link]();
}
}
for(int i=0;i<row;i++){
for(int j=0;j<cols;j++){
result[i][j]=0;
for(int k=0;k<cols;k++){
result[i][j]+=matrix1[i][j]*matrix2[i][j];
}
}
}
[Link]("Resulting Matrix");
for(int i=0;i<row;i++){
for(int j=0;j<cols;j++){
[Link](result[i][j]+" ");
}
[Link]();
}
[Link]();
}
}

22
Output :

Result :

Thus To write a java program to develop JAVA Programs using Array has been successfully
executed and verified

23
[Link]: 9 DATE:

Develop JAVA Programs using Inheritance

Aim:

To write and develop a java program Programs using Inheritance

Algorithm:

1. Start the Program


2. Create a base class Person:
• Define attributes name and age.
• Add methods to set and display name and age.
3. Create a derived class Student that extends Person:
• Add an additional attribute studentID.
• Add methods to set and display studentID.
4. In the Main method:
• Prompt the user to input name, age, and studentID.
• Create an object of the Student class.
• Use the object to set the values for name, age, and studentID.
• Display the details of the student.
5. Stop the Program

Program:

import [Link];
class person{
private String name;
private int age;
public void setdetails(String name,int age){
[Link]=name;
[Link]=age;
}
public void displaydetails(){
[Link]("Name:"+name);
[Link]("Age:"+age);
}
}

24
class Student extends person{
String studentID;
public void setstudentID(String studentID){
[Link]=studentID;
}
public void displaystudentID(){
[Link]("Student ID:"+studentID);
}
}
public class Inheritance{
public static void main(String[]args){
Scanner scanner=new Scanner([Link]);
[Link]("Enter Name:");
String name=[Link]();
[Link]("Enter Age:");
int age=[Link]();
[Link]("Enter Student ID");
String studentID=[Link]();
Student student=new Student();
[Link](name,age);
[Link](studentID);
[Link]("Student Details");
[Link]();
[Link]();
[Link]();
}
}

Output:

Result :

Thus To write a java program to develop JAVA Programs using


Inheritance has been successfully executed and verified.

25
[Link]: 10 DATE:

Develop JAVA Programs using Polymorphism

Aim:

To write and develop a java program Programs using Polymorphism

Algorithm:

1. Start the Program


2. Define a base class Shape:
3. Create a method calculateArea() that calculates the area of a generic
shape.
4. Create derived classes Circle and Rectangle:
5. Override the calculateArea() method in each subclass to calculate their
respective areas.
6. In the main method:
1) Use polymorphism (method overriding) to call the calculateArea()
method based on the object type.
7. Demonstrate method overloading by implementing methods to handle
different types of inputs for the area calculation.
8. Use a Scanner to get user input for different shapes (circle and rectangle).
9. Stop the Program

Program:
import [Link];
class Shape {
public double calculateArea() {
[Link]("Calculating area of a generic shape.");
return 0;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
[Link] = radius;
}
@Override
public double calculateArea() {
return [Link] * radius * radius;
}

26
public double calculateArea(double radius) {
return [Link] * radius * radius;
}
}
class Rectangle extends Shape {
private double length, width;
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}
@Override
public double calculateArea() {
return length * width;
}
public double calculateArea(double length, double width) {
return length * width;
}
}
public class Polymorphism {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Choose the shape to calculate area:");
[Link]("1. Circle");
[Link]("2. Rectangle");
[Link]("Enter your choice (1/2): ");
int choice = [Link]();
Shape shape = null;
if (choice == 1) {
[Link]("Enter the radius of the Circle: ");
double radius = [Link]();
shape = new Circle(radius);
} else if (choice == 2) {
[Link]("Enter the length of the Rectangle: ");
double length = [Link]();
[Link]("Enter the width of the Rectangle: ");
double width = [Link]();
shape = new Rectangle(length, width);
} else {
[Link]("Invalid choice!");
[Link]();
return;
}
[Link]("The area of the shape is: " + [Link]());

[Link]();
}
}

27
Output:

Result :

Thus To write a java program to develop JAVA Programs using


Polymorphism has been successfully executed and verified

28
DATE:
[Link]

DEVELOP JAVA PROGRAMS USING INTERFACES

Aim:
To write and develop a java program Programs using Interfaces

Algorithm:
1. Start the Program
2. Define the Interface:
i. Create an interface Shape with methods calculateArea() andcalculatePerimeter().
3. Implement the Interface:
i. Create two classes, Circle and Rectangle, that implement the Shape interface.
ii. Override the methods to calculate the area and perimeter for each shape.
4. User Input:
i. Prompt the user to select a shape (circle or rectangle).
ii. Based on the selection, ask for the necessary dimensions (e.g., radius for a circle, length
and width for a rectangle).
5. Perform Calculations:
i. Instantiate the appropriate shape class based on user input.
ii. Call the methods to calculate and display the area and perimeter.
6. Display Results:
7. Print the calculated area and perimeter to the console.
8. Define a base class Shape.
9. Stop the Program

Program:
import [Link];
interface Shape {
double CalculateArea();
double CalculatePerimeter();
}
abstract class BaseShape {
abstract void display();
}
class Circle extends BaseShape implements Shape {
private double radius;
public Circle(double radius) {
[Link] = radius;
}
@Override
public double CalculateArea() {
return [Link] * radius * radius;
}

29
@Override
public double CalculatePerimeter() {
return 2 * [Link] * radius;
}
@Override
void display() {
[Link]("Circle with Radius: " +
radius);
}
}
class Rectangle extends BaseShape implements Shape {
private double length, width;

public Rectangle(double length, double width) {


[Link] = length;
[Link] = width;
}
@Override
public double CalculateArea() {
return length * width;
}
@Override
public double CalculatePerimeter() {
return 2 * (length + width);
}
@Override
void display() {
[Link]("Rectangle with Length: " + length + " and Width: " + width);
}
}
public class Interfaces {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Select Shape 1. Circle 2. Rectangle");
int choice = [Link]();
Shape shape = null;
BaseShape baseShape = null;
switch (choice) {
case 1:
[Link]("Enter Radius:");
double radius = [Link]();
shape = new Circle(radius);
baseShape = (Circle) shape;
break;

30
case 2:
[Link]("Enter Length:");
double length = [Link]();
[Link]("Enter Width:");
double width = [Link]();
shape = new Rectangle(length, width);
baseShape = (Rectangle) shape;
break;
default:
[Link]("Invalid Choice");
return;
}
if (baseShape != null) {
[Link]();
}
if (shape != null) {
[Link]("Area: " + [Link]());
[Link]("Perimeter: " + [Link]());
}
[Link]();
}
}

Output:

Result:
Thus to write a java program using Interfaces has been successfully executed and verified.

31
[Link]: 12 (a) DATE:

DEVELOP JAVA PROGRAMS USING ARRAY LIST AND LINKEDLIST

Develop Java Programs using Array List

Aim:

To write and develop a java program Programs using Array List

Algorithm:
1. Start the Program
2. Add an Element (add()):
• Input a string element to be added.
3. Append the element at the end of the ArrayList.
4. Remove an Element (remove()):
• Input a string element to be removed.
5. Search for the element and remove it if found.
6. Search for an Element (contains()):
• Input a string element to search for.
• Iterate through the list to check for the
presence of the element.
7. Display the ArrayList:
8. InputNone (just display all elements in the list).
9. Iterate through the list and print each element.
10. Stop the Program

Program:
import [Link];
import [Link];
public class Array {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Scanner scanner = new Scanner([Link]);
int choice;
do {
[Link]("\nMenu:");
[Link]("1. Add an Element");
[Link]("2. Remove an Element");
[Link]("3. Search for an Element");
[Link]("4. Display the ArrayList");
[Link]("5. Stop the Program");
[Link]("Enter your choice: ");

32
choice = [Link]();
[Link]();
switch (choice) {
case 1:
[Link]("Enter element to add: ");
String addElement = [Link]();
[Link](addElement);
[Link]("Element added successfully!");
break;
case 2:
[Link]("Enter element to remove: ");
String removeElement = [Link]();
if ([Link](removeElement)) {
[Link]("Element removed successfully!");
} else {
[Link]("Element not found!");
}
break;
case 3:
[Link]("Enter element to search: ");
String searchElement = [Link]();
if ([Link](searchElement)) {
[Link]("Element found in the list!");
} else {
[Link]("Element not found!");
}
break;
case 4:
[Link]("ArrayList Elements: " + list);
break;
case 5:
[Link]("Exiting the program...");
break;
default:
[Link]("Invalid choice! Please try again.");
}
} while (choice != 5);
[Link]();
}
}

33
Output:

Result:
Thus To write a java program using Array List has been successfully executed and
verified

34
[Link]: 12 (b) DATE:

Develop Java Programs using Linked List

Aim:
To write and develop a java program Programs using Linked List.

Algorithm:
1. Start the Program
2. Adding an Element (add()):
i. Input a string element to be added.
ii. Append the element to the end of the LinkedList.
3. Removing an Element (remove()):
i. Input a string element to be removed.
ii. Search for the element in the list and remove it if
found.
4. Searching for an Element (contains()):
i. Input a string element to search for.
[Link] through the list to check for the presence of the element
5. Display the LinkedList.
6. Stop the Program.

Program:
import [Link];
import [Link];

public class LinkedListManager {


public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
Scanner scanner = new Scanner([Link]);
int choice;

do {
[Link]("\nMenu:");
[Link]("1. Add an Element");
[Link]("2. Remove an Element");
[Link]("3. Search for an Element");
[Link]("4. Display the LinkedList");
[Link]("5. Stop the Program");
[Link]("Enter your choice: ");
choice = [Link]();
[Link]();

35
switch (choice) {
case 1:
[Link]("Enter element to add: ");
String addElement = [Link]();
[Link](addElement);
[Link]("Element added successfully!");
break;
case 2:
[Link]("Enter element to remove: ");
String removeElement = [Link]();
if ([Link](removeElement)) {
[Link]("Element removed successfully!");
} else {
[Link]("Element not found!");
}
break;
case 3:
[Link]("Enter element to search: ");
String searchElement = [Link]();
if ([Link](searchElement)) {
[Link]("Element found in the list!");
} else {
[Link]("Element not found!");
}
break;
case 4:
[Link]("LinkedList Elements: " + list);
break;
case 5:
[Link]("Exiting the program...");
break;
default:
[Link]("Invalid choice! Please try again.");
}
} while (choice != 5);
[Link]();
}
}

36
Output:

Result:
Thus to write a java program using Linked List has been successfully executed
and verified.

37
[Link]: 13 DATE:

DEVELOP JAVA PROGRAMS USING PACKAGES

Aim:
To write and develop a java program Programs using Packages

Algorithm:
1. Start the Program
2. Define a Package:
i. Create a folder structure corresponding to the package name.
ii. Write a Java class inside the package folder.
3. Use a Package:
i. Import the package in another Java program using the import keyword.
ii. Call the methods or classes defined in the package.
4. Compile:
i. Compile the package class using javac -d .
[Link] to ensure proper placement in the directory structure.
5. Run:
i. Use the fully qualified name when running the program.
6. Stop the Program

Program:

package mypackage;

public class MyClass {


public void showMessage() {
[Link]("Hello from MyClass inside mypackage!");
}
}

import [Link];

public class MainProgram {


public static void main(String[] args) {
MyClass obj = new MyClass();
[Link]();
}
}

38
Output:

Result :
Thus to write a java program using Packages has been successfully executed and
verified

39
[Link]: 14 DATE:

DEVELOP JAVA PROGRAMS USING EXCEPTIONS

Aim:
To write and develop a java program Programs using exception

Algorithm:
1. Start the Program
2. Prompt the user for input.
3. Ask for two numbers to perform a division operation.
4. Try to execute the operation:
5. Read the inputs.
6. Perform the division.
7. Catch exceptions:
• If the user enters invalid input (e.g., non-numeric), catch
InputMismatchException.
• If the user attempts to divide by zero, catch
ArithmeticException.
8. Display appropriate messages for exceptions.
9. Allow the user to retry or exit the program.
10. Stop the Program

Program:

import [Link];
import [Link];
public class DivisionCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
boolean continueProgram = true;
while (continueProgram) {
try {
[Link]("Enter the numerator: ");
int numerator = [Link]();
[Link]("Enter the denominator: ");
int denominator = [Link]();
int result = numerator / denominator;
[Link]("Result: " + result);
continueProgram = false;
}

40
catch (InputMismatchException e) {
[Link]("Error: Invalid input! Please enter numeric values.");
[Link]();
} catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed.");
}
[Link]("Do you want to try again? (yes/no): ");
String choice = [Link]().toLowerCase();
if (![Link]("yes")) {
continueProgram = false;
}
}
[Link]("Program terminated.");
[Link]();
}
}

OUTPUT:

Result:
Thus to write a java programs using Exceptions has been successfully executed and
verified.

41
[Link]: 15 DATE:

DEVELOP JAVA PROGRAMS USING STRINGS

Aim:
To write and develop a java program Programs using Strings

Algorithm:
1. Start the Program
2. Prompt the user for input.
3. Ask for two numbers to perform a division operation.
4. Try to execute the operation:
5. Read the inputs.
6. Perform the division.
7. Catch exceptions:
• If the user enters invalid input (e.g., non-numeric), catch
InputMismatchException.
• If the user attempts to divide by zero, catch ArithmeticException.
8. Display appropriate messages for exceptions.
9. Allow the user to retry or exit the program.
10. Stop the Program

Program:

import [Link];
public class StringDivisionCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
boolean continueProgram = true;
while (continueProgram) {
try {
[Link]("Enter the numerator: ");
String numStr = [Link]();
[Link]("Enter the denominator: ");
String denomStr = [Link]();
int numerator = [Link](numStr);
int denominator = [Link](denomStr);
int result = numerator / denominator;
[Link]("Result: " + result);
continueProgram = false;
}

42
catch (NumberFormatException e) {
[Link]("Error: Invalid input! Please enter numeric values.");
} catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed.");
}
[Link]("Do you want to try again? (yes/no): ");
String choice = [Link]().toLowerCase();
if (![Link]("yes")) {
continueProgram = false;
}
}
[Link]("Program terminated.");
[Link]();
}
}

OUTPUT:

Result:
Thus to write a java program using strings has been successfully executed and
verified.

43

You might also like