You are on page 1of 43

SAHIL ANSARI IT21001 SYBSCIT

SR. AIM FOR PROGRAMS SIGN


1A Write a Java program to swap two variables.
1B Write a Java program to print the area of a circle.
1C Write a Java program to print the sum (addition), multiply, subtract, divide and
remainder of two numbers.
1D Write a Java program that takes three numbers as input to calculate and print the
average of the numbers.
2A Write a Java program to check number is divisible by 7 or not.
2B Write a Java program to find maximum from 3 numbers.
2C Write a Java program to read day code and display day name using switch case.
2D Write a Java program that takes a number as input and prints its multiplication table
up to 10.
2E Write a Java program and compute the sum of the digits of an integer.
3A Write a Java program to find sum of array elements of size 10.
3B Designed a class SortData that contains the method asc() and desc() to sort array
elements.
3C Find the smallest and largest element from the array
4A Designed a Box class to find volume of a Box using class method.
4B Designed a Box class, initialize object using constructor and find volume of a box.
4C Designed a Box class with multiple constructors to initialize class object and find
volume of a box.
5A Write a java program to implement single level inheritance.
5B Write a java program to implement multilevel inheritance
5C Write a java program to implement multiple inheritance
6A Create a package MyMath, Add the necessary classes and import the package in java
class.
6B Write a java program to add two matrices and print the resultant matrix.
6C Write a java program for multiplying two matrices and print the product for the
same.
7A Write a java program to implement the vectors.
7B Write a java program to implement thread life cycle.
7C Write a java program to implement multithreading.
8A Write a java program to open a file and display the contents in the console window.
8B Write a java program to copy the contents from one file to another file.
9A Write a java program to implement exception handling.
9B Write a java program to implement exception handling with multiple catch.
10 Construct a simple calculator using the JAVA awt with 3 text fields (the 3rd text field
A should be read-only) and 4 buttons to add, subtract, multiply, divide with minimum
functionality.
10B Create an application using Java awt that accepts Principal Amount, No. of Years &
Rate of Interest from 3 text fields, when you click “Calculate Interest” button, the
calculated interest should be displayed in a read-only text field. When you click on
“Final Amount” button, the final amount by adding principal amount and interest
should also be displayed in a read-only text field.
10C Design a AWT program to print the factorial for an input value.
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 1A:
public class SwapVariables {
public static void main(String[] args) {
int x = 5;
int y = 10;

System.out.println("Before swapping:");
System.out.println("x = " + x);
System.out.println("y = " + y);

int temp = x;
x = y;
y = temp;

System.out.println("After swapping:");
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 1B:
public class CircleArea {
public static void main(String[] args) {
final double PI = 3.14159;
double radius=5;
double area;
area = PI * radius * radius
System.out.println("The area of the circle
is: " + area
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 1C:
public class ArithmeticOperations {
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
int quotient = num1 / num2;
int remainder = num1 % num2;

System.out.println("Sum(Addition) of " + num1


+ " and " + num2 + " is " + sum);
System.out.println("Difference(Subtraction)
of " + num1 + " and " + num2 + " is " + difference);
System.out.println("Product(Multiplication)
of " + num1 + " and " + num2 + " is " + product);
System.out.println("Quotient(Division) of " +
num1 + " and " + num2 + " is " + quotient);
System.out.println("Remainder(Modulus) of " +
num1 + " and " + num2 + " is " + remainder);
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 1D:
import java.util.*;

public class AverageCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of values


to calculate average: ");
int n = scanner.nextInt();

double[] numbers = new double[n];


double sum = 0;

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


System.out.println("Enter value " + (i +
1) + ": ");
numbers[i] = scanner.nextDouble();
sum += numbers[i];
}

double average = sum / n;


System.out.println("The average of the " + n
+ " numbers is: " + average);
scanner.close();
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 2A:
import java.util.*;

public class DivisibleBy7Checker {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


int number = scanner.nextInt();

if (number % 7 == 0) {
System.out.println(number + " is
divisible by 7");
} else {
System.out.println(number + " is not
divisible by 7");
}

scanner.close();
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 2B:
import java.util.*;

public class MaximumFinder {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter third number: ");
double num3 = scanner.nextDouble();
double max = num1;

if (num2 > max) {


max = num2;
}
if (num3 > max) {
max = num3;
}
System.out.println("The maximum of the three
numbers is: " + max);

scanner.close();
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 2C:
import java.util.*;

public class DayNameFinder {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter day code (1-7): ");


int dayCode = scanner.nextInt();

String dayName;

switch (dayCode) {
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 code";
}

System.out.println("The day name is: " +


dayName);
scanner.close();
}
}
SAHIL ANSARI IT21001 SYBSCIT

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 2D:
import java.util.*;

public class MultiplicationTable {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


int number = scanner.nextInt();

System.out.println("Multiplication table for


" + number + ":");

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


System.out.println(number + " x " + i + "
= " + (number * i));
}

scanner.close();
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 2E:
import java.util.*;

public class SumOfDigits {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer: ");


int number = scanner.nextInt();

int sum = 0;
int digit;

while (number != 0) {
digit = number % 10;
sum += digit;
number /= 10;
}

System.out.println("The sum of the digits is:


" + sum);

scanner.close();
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 3A:
public class ArraySum {
public static void main(String[] args) {
int[] array = {10, 10, 10, 10, 10, 10, 10,
10, 10, 10};
int sum = 0;

for (int i = 0; i < array.length; i++) {


sum += array[i];
}

System.out.println("The sum of the array


elements is: " + sum);
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 3B:
import java.util.*;

public class SortData {

public void asc(int[] arr) {


Arrays.sort(arr);
}

public void desc(int[] arr) {


Arrays.sort(arr);
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
}
public static void main(String args[]){
int[] arr = {5, 2, 8, 1, 9, 4};
SortData sorter = new SortData();
sorter.asc(arr);
System.out.println("Ascending order: " +
Arrays.toString(arr));
sorter.desc(arr);
System.out.println("Descending order: " +
Arrays.toString(arr));
}}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 3C:
import java.util.*;

public class MinMax {


public static void main(String[] args) {
int[] arr = {4, 1, 6, 2, 8, 5};
int min = arr[0];
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
} else if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("Array: " +
Arrays.toString(arr));
System.out.println("Smallest element: " +
min);
System.out.println("Largest element: " +
max);
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 4A:
public class Box {
private double length;
private double width;
private double height;

public Box(double length, double width, double


height) {
this.length = length;
this.width = width;
this.height = height;
}

public static double calculateVolume(Box box) {


return box.length * box.width * box.height;
}

public static void main(String[] args) {


Box box = new Box(2.0, 3.0, 4.0);
double volume = Box.calculateVolume(box);
System.out.println("Volume of box: " +
volume);
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 4B:
public class Box2 {
private double length;
private double width;
private double height;

public Box2(double length, double width, double


height) {
this.length = length;
this.width = width;
this.height = height;
}

public double calculateVolume() {


return length * width * height;
}

public static void main(String[] args) {


Box2 box = new Box2(3.0, 4.0, 5.0);
double volume = box.calculateVolume();
System.out.println("Volume of box: " +
volume);
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 4C:
public class Box3 {
private double length;
private double width;
private double height;

public Box3() {
this.length = 0;
this.width = 0;
this.height = 0;
}

public Box3(double length, double width, double


height) {
this.length = length;
this.width = width;
this.height = height;
}

public Box3(double side) {


this.length = side;
this.width = side;
this.height = side;
}

public double calculateVolume() {


return length * width * height;
}

public static void main(String[] args) {


Box3 box1 = new Box3();
double volume1 = box1.calculateVolume();
System.out.println("Volume of empty box: " +
volume1);

Box3 box2 = new Box3(2.0, 3.0, 4.0);


double volume2 = box2.calculateVolume();
System.out.println("Volume of box with
dimensions 2.0, 3.0, 4.0: " + volume2);

Box3 box3 = new Box3(5.0);


double volume3 = box3.calculateVolume();
System.out.println("Volume of box with side
length 5.0: " + volume3);
SAHIL ANSARI IT21001 SYBSCIT

}
}

OUTPUT:

PRACTICAL 5A:
class Shape {
SAHIL ANSARI IT21001 SYBSCIT

public void draw() {


System.out.println("Drawing shape...");
}
}

class Circle extends Shape {


public void draw() {
System.out.println("Drawing circle...");
}
}

public class SingleInheritance {


public static void main(String[] args) {
Shape shape = new Shape();
Circle circle = new Circle();

shape.draw();
circle.draw();
}
}

OUTPUT:

PRACTICAL 5B:
class Animal {
SAHIL ANSARI IT21001 SYBSCIT

public void eat() {


System.out.println("Animal is eating...");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking...");
}
}
class Labrador extends Dog {
public void playFetch() {
System.out.println("Labrador is playing
fetch...");
}
}

public class MultilevelInheritance {


public static void main(String[] args) {
Animal animal = new Animal();
Dog dog = new Dog();
Labrador labrador = new Labrador();

dog.eat();
animal.eat();
dog.bark();
labrador.eat();
labrador.bark();
labrador.playFetch();
}
}

OUTPUT:

PRACTICAL 5C:
interface InterfaceA {
SAHIL ANSARI IT21001 SYBSCIT

public void methodA();


}

interface InterfaceB {
public void methodB();
}

class ClassAB implements InterfaceA, InterfaceB {


public void methodA() {
System.out.println("Calling methodA from
InterfaceA...");
}

public void methodB() {


System.out.println("Calling methodB from
InterfaceB...");
}
}

public class MultipleInheritance {


public static void main(String[] args) {
ClassAB classAB = new ClassAB();

classAB.methodA(); // output: Calling methodA


from InterfaceA...
classAB.methodB(); // output: Calling methodB
from InterfaceB...
}
}

OUTPUT:

PRACTICAL 6A:
SAHIL ANSARI IT21001 SYBSCIT

Package programs(Different folder must be created with the package


name):
a)
public class Arithmetic {
public int add(int a, int b) {
return a + b;
}

public int subtract(int a, int b) {


return a - b;
}

public int multiply(int a, int b) {


return a * b;
}

public int divide(int a, int b) {


return a / b;
}
}
b)
package MyMath;

public class Geometry {


public double areaOfCircle(double radius) {
return Math.PI * radius * radius;
}

public double areaOfRectangle(double length,


double width) {
return length * width;
}

public double areaOfTriangle(double base, double


height) {
return 0.5 * base * height;
}
}

Main code:
SAHIL ANSARI IT21001 SYBSCIT

public class MyMathApp {


public static void main(String[] args) {
Arithmetic math = new Arithmetic();
Geometry geometry = new Geometry();

int a = 10, b = 5;

int sum = math.add(a, b);


int difference = math.subtract(a, b);
int product = math.multiply(a, b);
int quotient = math.divide(a, b);

double radius = 5.0;


double length = 10.0, width = 5.0;
double base = 10.0, height = 8.0;

double areaCircle =
geometry.areaOfCircle(radius);
double areaRectangle =
geometry.areaOfRectangle(length, width);
double areaTriangle =
geometry.areaOfTriangle(base, height);

System.out.println("Sum: " + sum);


System.out.println("Difference: " +
difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);

System.out.println("Area of circle: " +


areaCircle);
System.out.println("Area of rectangle: " +
areaRectangle);
System.out.println("Area of triangle: " +
areaTriangle);
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 6B:
SAHIL ANSARI IT21001 SYBSCIT

public class MatrixAddition {


public static void main(String[] args) {
int[][] matrix1 = { {1, 2, 3}, {4, 5, 6}, {7,
8, 9} };
int[][] matrix2 = { {9, 8, 7}, {6, 5, 4}, {3,
2, 1} };

int rows = matrix1.length;


int columns = matrix1[0].length;

int[][] result = new int[rows][columns];

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


for (int j = 0; j < columns; j++) {
result[i][j] = matrix1[i][j] +
matrix2[i][j];
}
}

System.out.println("Resultant matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}

OUTPUT:

PRACTICAL 6C:
public class MatrixMultiplication {
SAHIL ANSARI IT21001 SYBSCIT

public static void main(String[] args) {


int[][] matrix1 = { {1, 2, 3}, {4, 5, 6} };
int[][] matrix2 = { {7, 8}, {9, 10}, {11, 12}
};

int rows1 = matrix1.length;


int columns1 = matrix1[0].length;
int rows2 = matrix2.length;
int columns2 = matrix2[0].length;

if (columns1 != rows2) {
System.out.println("Multiplication not
possible");
return;
}

int[][] result = new int[rows1][columns2];


for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns2; j++) {
for (int k = 0; k < columns1; k++) {
result[i][j] += matrix1[i][k] *
matrix2[k][j];
}
}
}

System.out.println("Product matrix:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns2; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}
SAHIL ANSARI IT21001 SYBSCIT

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 7A:
import java.util.*;

public class VectorDemo {


public static void main(String[] args) {
Vector<Integer> vector = new
Vector<Integer>();
vector.add(1);
vector.add(2);
vector.add(3);
vector.add(4);
vector.add(5);
System.out.println("Vector: " + vector);
int size = vector.size();
System.out.println("Size of the vector: " +
size);
int element = vector.get(2);
System.out.println("Element at index 2: " +
element);
vector.set(3, 10);
System.out.println("Vector after setting
element at index 3 to 10: " + vector);
vector.remove(1);
System.out.println("Vector after removing
element at index 1: " + vector);
vector.clear();
System.out.println("Vector after clearing: "
+ vector);
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 7B:
public class ThreadLifeCycle implements Runnable {
public void run() {

System.out.println(Thread.currentThread().getName() +
" is running.");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println(Thread.currentThread().getName() +
" is done.");
}

public static void main(String[] args) {


ThreadLifeCycle obj = new ThreadLifeCycle();

Thread t1 = new Thread(obj, "Thread 1");

t1.start();

try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println(Thread.currentThread().getName() +
" is done.");
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 8A:
public class MultiThread implements Runnable {
private String name;
public MultiThread(String name) {
this.name = name;
}
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(name + ": " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new
MultiThread("Thread 1"));
Thread t2 = new Thread(new
MultiThread("Thread 2"));
Thread t3 = new Thread(new
MultiThread("Thread 3"));
t1.start();
t2.start();
t3.start();
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 8A:
File to be read:
Welcome to Java Programming, This is practical 8A.

Code:
import java.io.*;
public class ReadFile {
public static void main(String[] args) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new
FileReader("Practical 8a.txt"));
String line;
while ((line = reader.readLine()) !=
null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the file
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 8B:
Content to be copied:
Welcome to Core Java, This the content to be copied in practical 8b

Code:
import java.io.*;
public class CopyFile {
public static void main(String[] args) {
String sourceFilePath = "8b content to
copy.txt";
String destinationFilePath = "practical
8b.txt";
try (BufferedReader reader = new
BufferedReader(new FileReader(sourceFilePath));
BufferedWriter writer = new
BufferedWriter(new FileWriter(destinationFilePath)))
{
String line;
while ((line = reader.readLine()) !=
null) {
writer.write(line);
writer.newLine();
}
System.out.println("Contents of file
copied successfully.");
} catch (IOException e) {
System.out.println("Error occurred while
copying file contents.");
e.printStackTrace();
}
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 9A:
import java.util.*;

public class ExceptionHandling {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
try {
double number =
Double.parseDouble(scanner.nextLine());
double result = 10 / number;
System.out.println("Result: " + result);
} catch (NumberFormatException e) {
System.out.println("Invalid number
entered.");
} finally {
System.out.println("Program completed.");
}
scanner.close();
}
}

OUTPUT:

PRACTICAL 9B:
SAHIL ANSARI IT21001 SYBSCIT

import java.util.*;

public class ExceptionHandling1 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
try {
double number =
Double.parseDouble(scanner.nextLine());
double result = 10 / number;
System.out.println("Result: " + result);
} catch (NumberFormatException e) {
System.out.println("Invalid number
entered: " + e.getMessage());
} catch (Exception e) {
System.out.println("An error occurred: "
+ e.getMessage());
} finally {
System.out.println("Program completed.");
}
scanner.close();
}
}

OUTPUT:

PRACTICAL 10A:
SAHIL ANSARI IT21001 SYBSCIT

import java.awt.*;
import java.awt.event.*;
class AwtMaths extends Frame implements
ActionListener{
TextField tf1,tf2;
Button btnAdd,btnSub,btnMul,btnDiv,btnClear;
Label n,l1,l2,r;
AwtMaths()
{
n = new Label("AWT Basic Maths Calculator
Program");
l1 = new Label("Enter first number: ");
l2 = new Label("Enter second number: ");
r = new Label();

tf1 = new TextField();


tf2 = new TextField();

btnAdd = new Button("Add");


btnSub = new Button("Sub");
btnMul = new Button("Mul");
btnDiv = new Button("Div");
btnClear = new Button("Clear");

n.setBounds(50, 40, 250, 20);


l1.setBounds(50, 70, 150, 20);
tf1.setBounds(50, 90, 190, 30);
l2.setBounds(50, 120, 150, 20);
tf2.setBounds(50, 140, 190, 30);
btnAdd.setBounds(50, 180, 50, 30);
btnSub.setBounds(100, 180, 50, 30);
btnMul.setBounds(150, 180, 50, 30);
btnDiv.setBounds(200, 180, 50, 30);
btnClear.setBounds(250, 180, 50, 30);
r.setBounds(50, 220, 200, 20);

add(n);
add(l1);
add(tf1);
add(l2);
add(tf2);
add(btnAdd);
add(btnSub);
add(btnMul);
SAHIL ANSARI IT21001 SYBSCIT

add(btnDiv);
add(btnClear);
add(r);

setSize(350, 280);
setLayout(null);
setVisible(true);

btnAdd.addActionListener(this);
btnSub.addActionListener(this);
btnMul.addActionListener(this);
btnDiv.addActionListener(this);
btnClear.addActionListener(this);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
}
public void actionPerformed(ActionEvent e){
int num1 = Integer.parseInt(tf1.getText());
int num2 = Integer.parseInt(tf2.getText());
int result;
if(e.getSource()==btnAdd){
result = num1 + num2;
r.setText("Addition is " + result);
}
if(e.getSource()==btnSub){
result = num1 - num2;
r.setText("Subtraction is " + result);
}
if(e.getSource()==btnMul){
result = num1 * num2;
r.setText("Multiplication is " + result);
}
if(e.getSource()==btnDiv){
result = num1 / num2;
r.setText("Division is " + result);
}
if(e.getSource()==btnClear){
r.setText("");
tf1.setText("");
tf2.setText("");
SAHIL ANSARI IT21001 SYBSCIT

}
}
public static void main(String[] args) {
new AwtMaths();
}
}

OUTPUT:

PRACTICAL 10B:
import java.awt.*;
SAHIL ANSARI IT21001 SYBSCIT

import java.awt.event.*;

public class InterestCalculator extends Frame


implements ActionListener {
TextField tfPrincipal, tfYears, tfRate,
tfInterest, tfFinalAmount;
Button btnCalculateInterest, btnFinalAmount,
btnClear;

public InterestCalculator() {
setTitle("Interest Calculator");
setSize(500, 200);
setLayout(null); // 5 rows, 2 columns, with
10 pixel gaps
setVisible(true);

Label lblPrincipal = new Label("Principal


Amount:");
add(lblPrincipal);
lblPrincipal.setBounds(40 , 40, 100, 20);

tfPrincipal = new TextField();


add(tfPrincipal);
tfPrincipal.setBounds(140 , 40, 100, 20);

Label lblYears = new Label("No. of Years:");


add(lblYears);
lblYears.setBounds(260 , 40, 80, 20);

tfYears = new TextField();


add(tfYears);
tfYears.setBounds(340 , 40, 100, 20);

Label lblRate = new Label("Rate of


Interest:");
add(lblRate);
lblRate.setBounds(40 , 70, 100, 20);

tfRate = new TextField();


add(tfRate);
tfRate.setBounds(140 , 70, 100, 20);

Label lblInterest = new Label("Interest:");


add(lblInterest);
SAHIL ANSARI IT21001 SYBSCIT

lblInterest.setBounds(140 , 100, 100, 20);

tfInterest = new TextField();


tfInterest.setEditable(false); // read-only
add(tfInterest);
tfInterest.setBounds(240 , 100, 100, 20);

btnCalculateInterest = new Button("Calculate


Interest");
add(btnCalculateInterest);
btnCalculateInterest.setBounds(40 , 100, 95,
20);

Label lblFinalAmount = new Label("Final


Amount:");
add(lblFinalAmount);
lblFinalAmount.setBounds(140 , 130, 100, 20);

tfFinalAmount = new TextField();


tfFinalAmount.setEditable(false); // read-
only
add(tfFinalAmount);
tfFinalAmount.setBounds(240 , 130, 100, 20);

btnFinalAmount = new Button("Final Amount");


add(btnFinalAmount);
btnFinalAmount.setBounds(40 , 130, 95, 20);

btnClear = new Button("Clear");


add(btnClear);
btnClear.setBounds(350 , 130, 100, 20);

btnCalculateInterest.addActionListener(this);
btnFinalAmount.addActionListener(this);
btnClear.addActionListener(this);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
}
SAHIL ANSARI IT21001 SYBSCIT

public void actionPerformed(ActionEvent evt) {


double principal =
Double.parseDouble(tfPrincipal.getText());
double years =
Double.parseDouble(tfYears.getText());
double rate =
Double.parseDouble(tfRate.getText());

if(evt.getSource()==btnClear){
tfPrincipal.setText("");
tfYears.setText("");
tfRate.setText("");
tfInterest.setText("");
tfFinalAmount.setText("");
}
if (evt.getSource() == btnCalculateInterest)
{
double interest = (principal * years *
rate) / 100;

tfInterest.setText(Double.toString(interest));
} else if (evt.getSource() == btnFinalAmount)
{
double interest = (principal * years *
rate) / 100;
double finalAmount = principal +
interest;

tfInterest.setText(Double.toString(interest));

tfFinalAmount.setText(Double.toString(finalAmount));
}
}

public static void main(String[] args) {


new InterestCalculator();
}
}

OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT

PRACTICAL 10C:
SAHIL ANSARI IT21001 SYBSCIT

import java.awt.*;
import java.awt.event.*;
public class AwtFactorial extends Frame implements
ActionListener {
TextField tf1; Button btnFact; Label n,l1,r;

AwtFactorial() {
n = new Label("AWT Factorial Program");
l1 = new Label("Enter the number");
r = new Label();
tf1 = new TextField();
btnFact = new Button("Factorial");

n.setBounds(50, 40, 250, 20);


l1.setBounds(50, 70, 150, 20);
tf1.setBounds(50, 90, 190, 20);
btnFact.setBounds(50, 120, 80, 20);
r.setBounds(50, 150, 200, 20);

add(n);
add(l1);
add(btnFact);
add(tf1);
add(r);

setSize(300, 250);
setLayout(null);
setVisible(true);

btnFact.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
dispose();
}
});
}
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(tf1.getText());
int fact = 1;
while (num1>0) {
fact = fact * num1;
num1--;
}
r.setText("Factorial is "+fact);
SAHIL ANSARI IT21001 SYBSCIT

}
public static void main(String[] args) {
new AwtFactorial();
}
}

OUTPUT:

You might also like