0% found this document useful (0 votes)
5 views34 pages

Java Record

The document outlines several Java programs, each with a specific aim and algorithm, including defining classes for products, matrices, complex numbers, students, shapes, and employee hierarchies. Each program includes source code and confirms successful output verification. The document serves as a collection of programming exercises demonstrating object-oriented principles and basic operations in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views34 pages

Java Record

The document outlines several Java programs, each with a specific aim and algorithm, including defining classes for products, matrices, complex numbers, students, shapes, and employee hierarchies. Each program includes source code and confirms successful output verification. The document serves as a collection of programming exercises demonstrating object-oriented principles and basic operations in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAM 1 DATE : 18/02/26

AIM : To define a class Product with data members pcode, pname and price. Create three objects
and find the product having the lowest price.

ALGORITHM
1. Start
2. Import Scanner class
3. Create class Product
4. Declare data members: pcode, pname, price
5. Create constructor to initialize values
6. Create a method to find minimum priced product
7. Create main class
8. Create three product objects by reading input
9. Call method to find lowest price product
10. Stop

Source Code
import [Link];

class Product {
int pcode;
String pname;
int price;

Product(int c, String n, int p) {


pcode = c;
pname = n;
price = p;
}

static void findMin(Product p1, Product p2, Product p3) {


Product min = p1;

if ([Link] < [Link])


min = p2;

1
if ([Link] < [Link])
min = p3;

[Link]("Lowest Price Product:");


[Link]("Code: " + [Link]);
[Link]("Name: " + [Link]);
[Link]("Price: " + [Link]);
}
}

public class ProductSale {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

Product p1 = new Product([Link](), [Link](), [Link]());


Product p2 = new Product([Link](), [Link](), [Link]());
Product p3 = new Product([Link](), [Link](), [Link]());

[Link](p1, p2, p3);


}
}

RESULT
Output obtained and verified successfully.

2
PROGRAM 2 DATE : 18/02/26
AIM : To read two matrices from the user and perform matrix addition.

ALGORITHM
1. Start
2. Import Scanner class
3. Read number of rows and columns of first matrix
4. Read elements of first matrix
5. Read number of rows and columns of second matrix
6. Check if dimensions are equal
7. If equal, perform addition
8. Display resultant matrix
9. Else display error message
10. Stop
SOURCE CODE

import [Link];

public class Matrix {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int r1 = [Link]();
int c1 = [Link]();

int[][] a = new int[r1][c1];

for (int i = 0; i < r1; i++)


for (int j = 0; j < c1; j++)
a[i][j] = [Link]();

int r2 = [Link]();
int c2 = [Link]();

int[][] b = new int[r2][c2];

3
for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
b[i][j] = [Link]();

if (r1 == r2 && c1 == c2) {


int[][] sum = new int[r1][c1];

for (int i = 0; i < r1; i++)


for (int j = 0; j < c1; j++)
sum[i][j] = a[i][j] + b[i][j];

for (int i = 0; i < r1; i++) {


for (int j = 0; j < c1; j++)
[Link](sum[i][j] + " ");
[Link]();
}
} else {
[Link]("Matrix addition not possible");
}
}
}

RESULT
Output obtained and verified successfully.

4
PROGRAM 3 DATE : 20/02/26
AIM : To add two complex numbers using a class.

ALGORITHM
1. Start
2. Import Scanner class
3. Create class Complex
4. Declare real and imaginary parts
5. Create constructor to initialize values
6. Create method to add complex numbers
7. Create method to display result
8. Read two complex numbers
9. Perform addition
10. Display result
11. Stop
SOURCE CODE

import [Link];

class Complex {
double real, imag;

Complex(double r, double i) {
real = r;
imag = i;
}

void add(Complex c) {
real += [Link];
imag += [Link];
}

void display() {
[Link](real + " + " + imag + "i");
}
}
5
public class AddComplex {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

Complex c1 = new Complex([Link](), [Link]());


Complex c2 = new Complex([Link](), [Link]());

[Link](c2);
[Link]();
}
}

RESULT
Output obtained and verified successfully.

6
PROGRAM 4 DATE : 23/02/26
AIM : To create a class Student with attributes student id, name and mark. Read details of
students and display the student with highest mark using array of objects.

ALGORITHM
1. Start
2. Import Scanner class
3. Create class Student
4. Declare id, name, mark
5. Create constructor
6. Create display method
7. Create array of objects
8. Read student details
9. Find highest mark
10. Display result
11. Stop

SOURCE CODE
import [Link];

class Student {
int id, mark;
String name;

Student(int id, String name, int mark) {


[Link] = id;
[Link] = name;
[Link] = mark;
}

void display() {
[Link]("ID: " + id);
[Link]("Name: " + name);
[Link]("Mark: " + mark);
}
7
}

public class StudentDetails {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int n = [Link]();
Student[] arr = new Student[n];

for (int i = 0; i < n; i++) {


arr[i] = new Student([Link](), [Link](), [Link]());
}

Student max = arr[0];

for (int i = 1; i < n; i++) {


if (arr[i].mark > [Link])
max = arr[i];
}

[Link]();
}
}

RESULT
Output obtained and verified successfully.
PROGRAM 5 DATE : 27/02/26

8
AIM : To calculate the area of rectangle, triangle and square using method overloading.

ALGORITHM
1. Start
2. Create class Shapes
3. Create method area(int,int) for rectangle
4. Create method area(float,float) for triangle
5. Create method area(int) for square
6. Create main class
7. Read inputs
8. Call respective methods
9. Display results
10. Stop
SOURCE CODE

import [Link];

class Shapes {

void area(int l, int b) {


[Link]("Rectangle Area = " + (l * b));
}

void area(float b, float h) {


[Link]("Triangle Area = " + (b * h) / 2);
}

void area(int a) {
[Link]("Square Area = " + (a * a));
}
}

public class ShapeMain {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Shapes s = new Shapes();

9
int l = [Link]();
int b = [Link]();
[Link](l, b);

float b1 = [Link]();
float h = [Link]();
[Link](b1, h);

int a = [Link]();
[Link](a);
}
}

RESULT
Output obtained and verified successfully.

10
PROGRAM 6 DATE : 10/03/26
AIM : To create a class Person with data members name, gender, address and age.
Create a class Employee that inherits Person and contains empid, company name, qualification
and [Link] another class Teacher that inherits Employee and contains subject, department
and teacher [Link] details using array of objects.

ALGORITHM
1. Start
2. Import Scanner class
3. Create class Person with attributes
4. Create constructor to initialize values
5. Create display method
6. Create class Employee inheriting Person
7. Add employee details and constructor
8. Override display method
9. Create class Teacher inheriting Employee
10. Add teacher details and constructor
11. Override display method
12. Create main class
13. Create array of Teacher objects
14. Use menu-driven approach (Insert / Display / Exit)
15. Stop
Source Code
import [Link];

class Person {
String name, gender, address;
int age;

Person(String name, String gender, String address, int age) {


[Link] = name;

11
[Link] = gender;
[Link] = address;
[Link] = age;
}

void display() {
[Link]("Name: " + name);
[Link]("Gender: " + gender);
[Link]("Address: " + address);
[Link]("Age: " + age);
}
}

class Employee extends Person {


int empid;
String company, qualification;
float salary;

Employee(String name, String gender, String address, int age,


int empid, String company, String qualification, float salary) {
super(name, gender, address, age);
[Link] = empid;
[Link] = company;
[Link] = qualification;
[Link] = salary;
}

void display() {
[Link]();
[Link]("Employee ID: " + empid);
[Link]("Company: " + company);
[Link]("Qualification: " + qualification);
[Link]("Salary: " + salary);
}
}

class Teacher extends Employee {


int tid;
String subject, dept;

Teacher(String name, String gender, String address, int age,


int empid, String company, String qualification, float salary,
int tid, String subject, String dept) {

super(name, gender, address, age, empid, company, qualification, salary);


[Link] = tid;

12
[Link] = subject;
[Link] = dept;
}

void display() {
[Link]();
[Link]("Teacher ID: " + tid);
[Link]("Subject: " + subject);
[Link]("Department: " + dept);
}
}

public class Details {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

Teacher[] t = new Teacher[10];


int i = 0;

while (true) {
[Link]("\[Link]\[Link]\[Link]");
int ch = [Link]();

switch (ch) {
case 1:
[Link]();

String name = [Link]();


String gender = [Link]();
String address = [Link]();
int age = [Link]();

int empid = [Link]();


[Link]();
String company = [Link]();
String qual = [Link]();
float salary = [Link]();

int tid = [Link]();


[Link]();
String sub = [Link]();
String dept = [Link]();
t[i++] = new Teacher(name, gender, address, age,
empid, company, qual, salary,
tid, sub, dept);
break;

13
case 2:
for (int j = 0; j < i; j++) {
t[j].display();
[Link]("----------------");
}
break;
case 3:
return;
}
}
}
}

RESULT
Output obtained and verified successfully.

14
PROGRAM 7 DATE : 13/03/26
AIM : To create a class CPU with attribute [Link] an inner class Processor with attributes
number of cores and [Link] a static nested class RAM with attributes memory and
[Link] the details.

ALGORITHM
1. Start
2. Create class CPU
3. Declare variable price
4. Create inner class Processor
5. Add attributes cores and manufacturer
6. Create static class RAM
7. Add attributes memory and manufacturer
8. Create display methods
9. Create main class
10. Create objects and display values
11. Stop
SOURCE CODE

class CPU {
double price = 3999.89;

class Processor {
int cores;
String manufacturer;

Processor(int cores, String manufacturer) {


[Link] = cores;
[Link] = manufacturer;
}

void display() {
[Link]("Processor Cores: " + cores);
[Link]("Processor Manufacturer: " + manufacturer);
}
15
}
static class RAM {
int memory;
String manufacturer;

RAM(int memory, String manufacturer) {


[Link] = memory;
[Link] = manufacturer;
}

void display() {
[Link]("RAM Memory: " + memory + "GB");
[Link]("RAM Manufacturer: " + manufacturer);
}
}
}

public class CPUDetails {


public static void main(String[] args) {

CPU cpu = new CPU();


[Link] p = [Link] Processor(8, "Intel");
[Link] r = new [Link](16, "Corsair");
[Link]("CPU Price: " + [Link]);
[Link]();
[Link]();
}
}

RESULT
Output obtained and verified successfully.

16
PROGRAM 8 DATE : 17/03/26
AIM : To write a Java program to sort a list of strings in alphabetical order.

ALGORITHM
1. Start
2. Import Scanner and Arrays classes
3. Create class for sorting
4. Read number of strings
5. Store strings in an array
6. Use sorting method (compareTo or [Link])
7. Display sorted strings
8. Stop
SOURCE CODE
import [Link].*;

class StringSort {
String[] sort(String arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i].compareTo(arr[j]) > 0) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
}

public class StringProgram {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter number of strings:");


int n = [Link]();

17
String arr[] = new String[n];
StringSort s = new StringSort();

[Link]("Enter strings:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}

arr = [Link](arr, n);

[Link]("Sorted strings:");
for (int i = 0; i < n; i++) {
[Link](arr[i]);
}
}
}

RESULT
Output obtained and verified successfully.

18
PROGRAM 9 DATE : 27/03/26
AIM : To perform various string operations like lowercase, uppercase, length, replace, trim, and
conversion to string.

ALGORITHM
1. Start
2. Read input string
3. Convert to lowercase
4. Convert to uppercase
5. Find length
6. Replace characters
7. Convert integer to string
8. Trim string
9. Display results
10. Stop

SOURCE CODE
import [Link].*;

public class StringManipulation {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter the string:");


String txt = [Link]();

[Link]("Lowercase: " + [Link]());


[Link]("Uppercase: " + [Link]());
[Link]("Length: " + [Link]());

[Link]("Enter character to replace:");


String a = [Link]();

[Link]("Enter replacement character:");


String b = [Link]();
19
String replaced = [Link](a, b);
[Link]("Replaced string: " + replaced);

[Link]("Enter integer:");
int num = [Link]();

String numStr = [Link](num);


[Link]("Integer to String: " + numStr);

[Link]("Trimmed string: " + [Link]());


}
}

RESULT
Output obtained and verified successfully.

20
PROGRAM 10 DATE : 31/03/26
AIM : To create an interface with methods area() and perimeter().Implement it using classes
Circle and Rectangle and perform operations using a menu-driven program.

ALGORITHM
1. Start
2. Create interface Shapes
3. Declare methods area() and perimeter()
4. Create class Circle implementing interface
5. Define area and perimeter methods
6. Create class Rectangle implementing interface
7. Define area and perimeter methods
8. Create main class
9. Use menu to perform operations
10. Stop

SOURCE CODE
import [Link].*;
interface Shapes {
double area();
double perimeter();
}
class Circle implements Shapes {
Scanner sc = new Scanner([Link]);
double radius;
public double area() {
[Link]("Enter radius:");
radius = [Link]();
return [Link] * radius * radius;
}

public double perimeter() {


return 2 * [Link] * radius;
}
}
21
class Rectangle implements Shapes {
Scanner sc = new Scanner([Link]);
double length, breadth;
public double area() {
[Link]("Enter length:");
length = [Link]();
[Link]("Enter breadth:");
breadth = [Link]();
return length * breadth;
}
public double perimeter() {
return 2 * (length + breadth);
}
}
public class Dimensions {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Circle c = new Circle();
Rectangle r = new Rectangle();
while (true) {
[Link]("\[Link] of Circle");
[Link]("[Link] of Circle");
[Link]("[Link] of Rectangle");
[Link]("[Link] of Rectangle");
[Link]("[Link]");
int ch = [Link]();
switch (ch) {
case 1:
[Link]("Area: " + [Link]());
break;
case 2:
[Link]("Perimeter: " + [Link]());
break;
case 3:
[Link]("Area: " + [Link]());
break;
case 4:
[Link]("Perimeter: " + [Link]());
break;
case 5:
return;
default:
[Link]("Invalid choice");
}
}

22
}
}

RESULT
Output obtained and verified successfully.

23
PROGRAM 11 DATE : 31/03/26
Aim : Create a graphics package that contains classes and interfaces for Rectangle and Circle.
Test the package by finding area and perimeter.

ALGORITHM

1. Start
2. Create a package graphics
3. Create an interface with methods area() and perimeter()
4. Create class Rectangle implementing the interface
5. Define area and perimeter methods for rectangle
6. Create class Circle implementing the interface
7. Define area and perimeter methods for circle
8. Create main class
9. Accept input and display results
10. Stop

SOURCE CODE

graphics/[Link]
package graphics;

public interface Shape {


float area();
float perimeter();
}

graphics/[Link]

package graphics;

public class Rectangle implements Shape {


float length, breadth;

public Rectangle(float l, float b) {


length = l;
breadth = b;
}

public float area() {


return length * breadth;
}

public float perimeter() {


return 2 * (length + breadth);

24
}
}

graphics/[Link]

package graphics;

public class Circle implements Shape {


float radius;
final float PI = 3.14f;

public Circle(float r) {
radius = r;
}

public float area() {


return PI * radius * radius;
}

public float perimeter() {


return 2 * PI * radius;
}
}

[Link]

import [Link];
import graphics.*;

public class FindArea {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter length and breadth:");


float l = [Link]();
float b = [Link]();

Rectangle r = new Rectangle(l, b);


[Link]("Rectangle Area = " + [Link]());
[Link]("Rectangle Perimeter = " + [Link]());

[Link]("Enter radius:");
float rad = [Link]();

Circle c = new Circle(rad);

25
[Link]("Circle Area = " + [Link]());
[Link]("Circle Perimeter = " + [Link]());
}
}

RESULT
Output obtained and verified successfully.

26
PROGRAM 12 DATE : 16/04/26

AIM : Write a user-defined exception class to authenticate username and password.

ALGORITHM

1. Start
2. Create class Authenticate extending Exception
3. Define constructors
4. Create main class
5. Create array to store users
6. Show menu (Signup / Login / Exit)
7. If signup → store username & password
8. If login → check credentials
9. If mismatch → throw exception
10. Catch and display error
11. Stop

SOURCE CODE

import [Link];

class Authenticate extends Exception {


String uid, pswd;

Authenticate(String msg) {
super(msg);
}

Authenticate(String u, String p) {
uid = u;
pswd = p;
}
}

public class Login {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


Authenticate[] users = new Authenticate[20];

int i = -1;

try {
while (true) {

27
[Link]("1. Sign Up");
[Link]("2. Login");
[Link]("3. Exit");

int ch = [Link]();

switch (ch) {

case 1:
[Link]("Enter username:");
String u = [Link]();

[Link]("Enter password:");
String p = [Link]();

users[++i] = new Authenticate(u, p);


[Link]("User added");
break;

case 2:
[Link]("Enter username:");
String lu = [Link]();

[Link]("Enter password:");
String lp = [Link]();

boolean found = false;

for (int j = 0; j <= i; j++) {


if (users[j].[Link](lu) &&
users[j].[Link](lp)) {

[Link]("Login Success!");
found = true;
break;
}
}

if (!found) {
throw new Authenticate("Invalid Credentials");
}
break;

case 3:
[Link](0);

28
default:
[Link]("Invalid choice");
}
}

} catch (Authenticate e) {
[Link]([Link]());
}
}
}

RESULT
Output obtained and verified successfully.

29
PROGRAM 13 DATE : 20/04/26

AIM : Define two classes:


 one for generating Fibonacci numbers
 one for displaying even numbers in a given range
Implement using threads (Runnable interface)

ALGORITHM

1. Start
2. Create class Fibonacci implementing Runnable
3. Read number of terms
4. Generate Fibonacci series inside run()
5. Create class Even implementing Runnable
6. Read start and end range
7. Display even numbers inside run()
8. Create main class
9. Create thread objects
10. Start threads
11. Stop

SOURCE CODE

import [Link];

class Fibonacci implements Runnable {


int n;

Fibonacci(int n) {
this.n = n;
}

public void run() {


int a = 0, b = 1;

[Link]("Fibonacci Series:");
for (int i = 0; i < n; i++) {
[Link](a + " ");
int c = a + b;
a = b;
b = c;
}
[Link]();
}
}

30
class Even implements Runnable {
int start, end;

Even(int s, int e) {
start = s;
end = e;
}

public void run() {


[Link]("Even Numbers:");
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
[Link](i + " ");
}
}
[Link]();
}
}

public class ThreadDemo {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter number of Fibonacci terms:");


int n = [Link]();

[Link]("Enter start and end range:");


int s = [Link]();
int e = [Link]();

Thread t1 = new Thread(new Fibonacci(n));


Thread t2 = new Thread(new Even(s, e));

[Link]();
[Link]();
}
}

31
RESULT
Output obtained and verified successfully.

32
PROGRAM 14 DATE : 21/04/26

AIM : Write a program to write data to a file and then read from the file and display the
contents.

ALGORITHM

1. Start
2. Import [Link].*
3. Create main class
4. Create FileOutputStream
5. Convert string into byte array
6. Write data into file
7. Close output stream
8. Open file using FileInputStream
9. Read byte-by-byte until -1
10. Display characters
11. Close input stream
12. Stop

SOURCE CODE

import [Link].*;

public class FileBasicByte {

public static void main(String[] args) {

try {
// Writing to file
FileOutputStream fout = new FileOutputStream("[Link]");

String data = "This is the implementation of write and read operation";


byte[] b = [Link]();

[Link](b);
[Link]();

// Reading from file


FileInputStream fin = new FileInputStream("[Link]");

int ch;
while ((ch = [Link]()) != -1) {
[Link]((char) ch);
}

33
[Link]();

} catch (IOException e) {
[Link]("Error: " + e);
}
}
}

RESULT
Output obtained and verified successfully.

34

You might also like