Java Record
Java Record
Source Code
import java.util.Scanner;
if (op == 1) {
double simpleInterest = (principal * rate * time) / 100;
System.out.println("\nSimple Interest: " + simpleInterest);
}
// Calculate simple interest
else {
double compoundInterest = principal * Math.pow((1 + rate / 100),
time) - principal;
System.out.println("Compound Interest: " + compoundInterest);
}
scanner.close();
}
}
Output
FIBONACCI SERIES
Source Code
import java.util.Scanner;
Source Code
import java.util.Scanner;
Source Code
import java.util.*;
void CalculatorAddition() {
int m1[][] = new int[20][20];
int m2[][] = new int[20][20];
int row, col;
System.out.print("\nEnter the number of rows and columns of the
matrIces : ");
row = scanner.nextInt();
col = scanner.nextInt();
System.out.print("\nEnter the matrix 1 : ");
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
m1[i][j] = scanner.nextInt();
}
}
System.out.print("\nEnter the matrix 2 : ");
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
m2[i][j] = scanner.nextInt();
} }
System.out.println("\nADDITION RESULT : ");
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
m1[i][j] += m2[i][j];
System.out.print(m1[i][j] + "\t");
}
System.out.println();
}
void CalculatorMultiplication() {
int m1[][] = new int[20][20];
int m2[][] = new int[20][20];
int result[][] = new int[20][20];
int row1, col1, row2, col2;
System.out.print("\nEnter the number of rows and columns of the matrix
1 : ");
row1 = scanner.nextInt();
col1 = scanner.nextInt();
System.out.print("\nEnter the matrix 1 : ");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
m1[i][j] = scanner.nextInt();
}
}
System.out.print("\nEnter the number of rows and columns of the matrix
2 : ");
row2 = scanner.nextInt();
col2 = scanner.nextInt();
if (col1 != row2) {
System.out.println("ERRORRRR!!!!!!!");
} else {
}
}
public static void main(String args[]) {
MatrixOperations mo = new MatrixOperations();
Scanner scanner = new Scanner(System.in);
int choice;
System.out.println(
"1. Matrix Addition \n2. Matrix Multiplication \n\nSelect your
choice :: ");
choice = scanner.nextInt();
if (choice == 2)
mo.CalculatorMultiplication();
else
mo.CalculatorAddition();
}
}
Output
CLASS AND OBJECT
Aim: Create a class Rectangle with member variable length and breadth, and a
member function area ().Find the area of rectangle using class and object
concepts
Source Code
import java.util.Scanner;
Aim: Write a program to find the area of square and rectangle using method
overloading
Source Code
import java.util.Scanner;
Aim: Write a program to illustrate single level inheritance with the super class
named player and subclass named CricketPlayer. The super class contains the
methods read_player and display_player().The read_player() reads the id and
name of the player and display_player displays the id and name of the player.
Source Code
import java.util.Scanner;
class Player {
int id;
String name;
void readPlayer() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter player ID: ");
id = scanner.nextInt();
System.out.print("Enter player name: ");
name = scanner.next();
}
void displayPlayer() {
System.out.println("Player Details");
System.out.println("Player ID: " + id);
System.out.println("Player Name: " + name);
}
}
Source Code
import java.util.Scanner;
interface Bonus {
double bonusPercentage = 0.1;
void computeBonus();
}
class Person {
String name;
int age;
void readPersonDetails() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter person's name: ");
name = scanner.nextLine();
System.out.print("Enter person's age: ");
age = scanner.nextInt();
}
void displayPersonDetails() {
System.out.println("\n\nDetails of the Person");
System.out.println(“Person’s Name: “ + name);
System.out.println(“Person’s Age: “ + age);
}
}
void readEmployeeDetails() {
readPersonDetails();
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter employee ID: “);
employeeId = scanner.nextInt();
}
void displayEmployeeDetails() {
displayPersonDetails();
System.out.println(“Employee ID: “ + employeeId);
}
}
void readFacultyDetails() {
readEmployeeDetails();
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter faculty’s salary: “);
salary = scanner.nextDouble();
void displayFacultyDetails() {
displayEmployeeDetails();
System.out.println(“Faculty’s Salary: “ + salary);
}
@Override
public void computeBonus() {
double bonusAmount = salary * bonusPercentage;
System.out.println(“Bonus Amount: “ + bonusAmount);
}
}
class College {
public static void main(String[] args) {
Faculty faculty = new Faculty();
faculty.readFacultyDetails();
faculty.displayFacultyDetails();
faculty.computeBonus();
}
}
Output
USER DEFINED EXCEPTION
Source Code
import java.util.Scanner;
try {
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
Aim: Create a class Figure that stores the two dimensions. Create an abstract
method area in figure. Derive two subclasses Rectangle and Triangle and
compute area
Source Code
import java.util.Scanner;
abstract class Figure {
double dimension1;
double dimension2;
scanner.close();
}
}
Output
PACKAGES
Source Code
ComplexNumber.java
// ComplexNumber package
package complex;
// ComplexNumber class
public class ComplexNumber {
private double real;
private double imaginary;
// Constructor
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
// Addition method
public ComplexNumber add(ComplexNumber other) {
double sumReal = this.real + other.real;
double sumImaginary = this.imaginary + other.imaginary;
return new ComplexNumber(sumReal, sumImaginary);
}
// Subtraction method
public ComplexNumber subtract(ComplexNumber other) {
double diffReal = this.real - other.real;
double diffImaginary = this.imaginary - other.imaginary;
return new ComplexNumber(diffReal, diffImaginary);
}
// Display method
public void display() {
System.out.println("Complex Number: " + real + " + " + imaginary + "i");
}
}
ComplexDemo.java
// Display results
num1.display();
num2.display();
System.out.println("Sum:");
sumResult.display();
System.out.println("Difference:");
diffResult.display();
}
}
Output
METHOD OVERRIDING
Aim: Write a Java program to override method greatest () for finding the
greatest of 2 numbers and 3 numbers
Source Code
class NumberOperations {
public int greatest(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
Source Code
import javax.swing.*;
import java.awt.*;
public MovingBall() {
x = 0; // initial x-coordinate of the ball
y = 100; // initial y-coordinate of the ball (center)
dx = 5; // speed of the ball
setBackground(Color.white);
setPreferredSize(new Dimension(400, 200)); // set preferred size of the
panel
startAnimation(); // start the animation
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawBall(g);
}
Source Code
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputText = textField.getText();
String resultText = changeCase(inputText);
resultLabel.setText("Result: " + resultText);
}
});
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
Aim: a) Write a menu driven applet program to draw circle and triangle. The
radius of circle and sides of triangle are passed as parameters through HTML.
b) Develop an applet that receives an integer in one text field and compute the
factorial of that integer which returns in another text field when the button
“compute” clicks
Source Code
import java.util.Arrays;
import java.util.Scanner;
Aim: Write a java program that displays the number of characters, lines and
words in a text file.
Source Code
FileStatistics.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
try {
BufferedReader reader = new BufferedReader(new
FileReader(fileName));
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
String line;
while ((line = reader.readLine()) != null) {
charCount += line.length();
wordCount += countWords(line);
lineCount++;
}
reader.close();
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
}
}
hello.txt
hello sangeetha
how are you?
hope you are doing well.
bye.
Output
THREAD
Aim: Write a program to create three threads in java. First thread displays
“GoodMorning” in every single second, the second thread displays “Hello” in
every 2 seconds and the third thread displays “Welcome” in every 3 seconds.
Source Code
class DisplayMessage extends Thread {
private String message;
private int interval;
thread1.start();
thread2.start();
thread3.start();
}
}
Output
MOUSE EVENTS
Source Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public MouseEventExample() {
setTitle("Mouse Events Example");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseMotionListener());
}
@Override
public void mousePressed(MouseEvent e) {
statusLabel.setText("Mouse Pressed at (" + e.getX() + ", " + e.getY() +
")");
}
@Override
public void mouseReleased(MouseEvent e) {
statusLabel.setText("Mouse Released at (" + e.getX() + ", " + e.getY() +
")");
}
@Override
public void mouseEntered(MouseEvent e) {
statusLabel.setText("Mouse Entered");
}
@Override
public void mouseExited(MouseEvent e) {
statusLabel.setText("Mouse Exited");
}
}
@Override
public void mouseDragged(MouseEvent e) {
statusLabel.setText("Mouse Dragged at (" + e.getX() + ", " + e.getY() +
")");
}
}
Aim: Create a table student which contains roll_no, name, and marks of n
subjects with 5 records of data in Minimum. Write a java program to read data
from the table and displaying roll_no, name, total_marks and percentage of
each student
Source Code
import java.sql.*;
import java.util.*;
import java.io.*;
import java.lang.*;