Java Record
Java Record
NO : 1 DATE:
Aim:
Algorithm:
Program:
1
Output :
Result :
Thus the java program was executed successfully.
2
B) To Convert temperature from Fahrenheit to Celsius
Algorithm:
Program:
import [Link];
Result :
3
[Link]: 2 DATE:
Algorithm:
Program:
}
}
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:
Algorithm:
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:
Aim:
Algorithm:
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:
Algorithm:
Program:
For loop
10
Do While Loop
While Loop :
Output :
11
Result :
Thus Java program to print values using Iteration structures has been successfully
executed and verified.
12
[Link]: 4 DATE:
Aim:
Algorithm:
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:
Aim:
Algorithm:
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:
Aim:
Algorithm:
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
18
[Link]: 7 DATE:
Aim:
Algorithm:
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;
}
Output :
Result :
20
[Link]: 8 DATE:
Aim:
Algorithm:
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:
Aim:
Algorithm:
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 :
25
[Link]: 10 DATE:
Aim:
Algorithm:
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 :
28
DATE:
[Link]
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;
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:
Aim:
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:
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];
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:
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;
import [Link];
38
Output:
Result :
Thus to write a java program using Packages has been successfully executed and
verified
39
[Link]: 14 DATE:
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 () {
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:
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 () {
continueProgram = false;
}
}
[Link]("Program terminated.");
[Link]();
}
}
OUTPUT:
Result:
Thus to write a java program using strings has been successfully executed and
verified.
43