Professional Documents
Culture Documents
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;
OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT
PRACTICAL 1D:
import java.util.*;
OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT
PRACTICAL 2A:
import java.util.*;
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.*;
scanner.close();
}
}
OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT
PRACTICAL 2C:
import java.util.*;
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";
}
OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT
PRACTICAL 2D:
import java.util.*;
scanner.close();
}
}
OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT
PRACTICAL 2E:
import java.util.*;
int sum = 0;
int digit;
while (number != 0) {
digit = number % 10;
sum += digit;
number /= 10;
}
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;
OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT
PRACTICAL 3B:
import java.util.*;
OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT
PRACTICAL 3C:
import java.util.*;
OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT
PRACTICAL 4A:
public class Box {
private double length;
private double width;
private double height;
OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT
PRACTICAL 4B:
public class Box2 {
private double length;
private double width;
private double height;
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;
}
}
}
OUTPUT:
PRACTICAL 5A:
class Shape {
SAHIL ANSARI IT21001 SYBSCIT
shape.draw();
circle.draw();
}
}
OUTPUT:
PRACTICAL 5B:
class Animal {
SAHIL ANSARI IT21001 SYBSCIT
dog.eat();
animal.eat();
dog.bark();
labrador.eat();
labrador.bark();
labrador.playFetch();
}
}
OUTPUT:
PRACTICAL 5C:
interface InterfaceA {
SAHIL ANSARI IT21001 SYBSCIT
interface InterfaceB {
public void methodB();
}
OUTPUT:
PRACTICAL 6A:
SAHIL ANSARI IT21001 SYBSCIT
Main code:
SAHIL ANSARI IT21001 SYBSCIT
int a = 10, b = 5;
double areaCircle =
geometry.areaOfCircle(radius);
double areaRectangle =
geometry.areaOfRectangle(length, width);
double areaTriangle =
geometry.areaOfTriangle(base, height);
OUTPUT:
SAHIL ANSARI IT21001 SYBSCIT
PRACTICAL 6B:
SAHIL ANSARI IT21001 SYBSCIT
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
if (columns1 != rows2) {
System.out.println("Multiplication not
possible");
return;
}
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.*;
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.");
}
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.*;
OUTPUT:
PRACTICAL 9B:
SAHIL ANSARI IT21001 SYBSCIT
import java.util.*;
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();
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 InterestCalculator() {
setTitle("Interest Calculator");
setSize(500, 200);
setLayout(null); // 5 rows, 2 columns, with
10 pixel gaps
setVisible(true);
btnCalculateInterest.addActionListener(this);
btnFinalAmount.addActionListener(this);
btnClear.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
}
SAHIL ANSARI IT21001 SYBSCIT
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));
}
}
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");
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: