Name :- Shweta Sadashiv Mali.
Roll No :-46
Class :- BCA III Sem V.
Lab Exercise 1
Java programs based on branching and looping statements.
1) if Statement :-
class If
{
public static void main(String args[])
{
int x = 10;
if( x < 20 )
{
System.out.print("This is if statement");
}
}
}
Output :-
2)if –else Statement :-
class Ifelse
{
public static void main(String args[])
{
int x = 30;
if( x < 20 )
{
System.out.print("This is if statement");
}
else
{
System.out.print("This is else statement");
}
}
}
Output :-
3) The if...else if...else Statement :-
class Ifelseif
{
public static void main(String args[])
{
int x = 30;
if( x == 10 )
{
System.out.print("Value of X is 10");
}
else if( x == 20 )
{
System.out.print("Value of X is 20");
}
else if( x == 30 )
{
System.out.print("Value of X is 30");
}
else
{
System.out.print("This is else statement");
}
}
}
Output :-
4) Nested if...else Statement :-
class Nestedif
{
public static void main(String args[])
{
int x = 30;
int y = 10;
if( x == 30 )
{
if( y == 10 )
{
System.out.print("X = 30 and Y = 10");
}
}
}
}
Output :-
5) The switch Statement :-
public class Switch1
{
public static void main(String[] args){
char grade='C';
switch(grade)
{
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Distinction");
break;
case 'C':
System.out.println("well done");
break;
case 'D':
System.out.println("you passed");
break;
case 'F':
System.out.println("Better try again");
break;
default:
System.out.println("Invalid grade");
break;
}
System.out.println("your grade is "+grade);
}
}
Output :-
b) Looping Statement :-
1)while Statement :-
class While
{
public static void main(String args[])
{
int x = 10;
while( x < 20 )
{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
Output :-
2) do-while :-
class Dowhile
{
public static void main(String args[])
{
int x = 10;
do
{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
while( x < 20 );
}
}
Output :-
3) For :-
class For
{
public static void main(String args[])
{
for(int x = 10; x < 20; x = x+1)
{
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 2
Java programs based on Type Casting.
1) Implicit Type Casting :-
public class ImplicitCasting {
public static void main(String[] args) {
int intValue = 42;
long longValue = intValue;
float floatValue = longValue;
System.out.println("Int value: " + intValue);
System.out.println("Long value: " + longValue);
System.out.println("Float value: " + floatValue);
}
}
Output :-
2)Explicit Type Casting :-
public class ExplicitCasting {
public static void main(String[] args)
{
double doubleValue = 9.78;
int intValue = (int) doubleValue;
System.out.println("Double value: " + doubleValue);
System.out.println("Int value after casting: " + intValue);
long longValue = 1000L;
short shortValue = (short) longValue;
System.out.println("Long value: " + longValue);
System.out.println("Short value after casting: " + shortValue);
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Class :- BCA III Sem V.
Subject :- Java Programming.
Lab Exercise 3
Java program based on command line arguments.
public class CommandLine
{
public static void main(String[] args)
{
int a,b,c,d;
a=Integer.parseInt(args [0]);
b=Integer.parseInt(args [1]);
c=Integer.parseInt(args [2]);
d=a+b+c;
System.out.println("1st argument is:"+args[0]);
System.out.println("2nd argument is:"+args[1]);
System.out.println("3rd argument is:"+args[2]);
System.out.println("sum is:"+d);
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 4
Java programs based on constructors.
1) Default Constructor :-
class Bike
{
Bike()
{
System.out.println("Bike is created");
}
public static void main(String args[])
{
Bike b=new Bike();
}
}
Output :-
2)Parameterized Constructor :-
class Student4
{
int id;
String name;
Student4(int i,String n)
{
id=i;
name=n;
}
void display()
{
System.out.println(id+" "+name);
}
}
public class ParameterizedCon
{
public static void main(String args[])
{
Student4 s1=new Student4(17,"Ashwini");
Student4 s2=new Student4(12,"Sneha");
s1.display();
s2.display();
}
}
Output :-
3)Copy Constructor :-
class CopyConstructor
{
int id;
String name;
CopyConstructor(int i, String nm)
{
id=i;
name=nm;
}
CopyConstructor(CopyConstructor s)
{
id=s.id;
name=s.name;
}
void display()
{
System.out.println(id+""+name);
}
public static void main(String[] args) {
CopyConstructor s1= new CopyConstructor(46,”Shweta);
CopyConstructor s2= new CopyConstructor(s1);
System.out.println("Object s1= ");
s1.display();
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 5
Java programs based on inheritance.
1) Single Inheritance :-
class Student{
int rollno;
String name;
}
class Marks1 extends Student{
int sub1,sub2;
void dispStud(){
System.out.println("-------------------------------------");
System.out.println(" New English School");
System.out.println("----------------------------------------");
System.out.println();
System.out.println("Roll No :- " + rollno);
System.out.println("Name of Student :- " + name);
System.out.println("Marks in Marathi :- " + sub1);
System.out.println("Marks in English :- " + sub2);
System.out.println("Marks(total) :- " + (sub1+sub2));
}
}
public class SingleInheritance{
public static void main(String args[]){
Marks1 obj=new Marks1();
obj.rollno=1;
obj.name="Shweta Sadashiv Mali";
obj.sub1=80;
obj.sub2=90;
obj.dispStud();
}
}
Output :-
2) Multilevel Inheritance :-
class A
{
int a,b;
void display(){
System.out.println(a+" "+b);
}
}
class B extends A{
int c;
void show(){
System.out.println(a+" "+b+" "+c);
}
}
class c extends B{
int d;
void shows()
{
System.out.println(a+" "+b+" "+c+" "+d);
}
}
public class MultilevelInherit{
public static void main(String args[]){
c obj=new c();
obj.a=10;
obj.b=20;
obj.c=30;
obj.d=40;
obj.display();
obj.show();
obj.show();
}
}
Output :-
3) Hierechial Inheritance:-
class Vehicle {
public void start() {
System.out.println("The vehicle starts.");
}
}
class Car extends Vehicle {
public void drive() {
System.out.println("The car drives.");
}
}
class Bike extends Vehicle {
public void ride() {
System.out.println("The bike rides.");
}
}
public class Hierechial{
public static void main(String[] args) {
Car car = new Car();
car.start();
car.drive();
Bike bike = new Bike();
bike.start();
bike.ride();
}
}
Output :-
4) Hybrid Inheritance :-
interface CanFly {
void fly();
}
interface CanSwim {
void swim();
}
class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
class Duck extends Animal implements CanFly, CanSwim {
public void fly() {
System.out.println("The duck can fly.");
}
public void swim() {
System.out.println("The duck can swim.");
}
}
public class Hybrid {
public static void main(String[] args) {
Duck duck = new Duck();
duck.eat();
duck.fly();
duck.swim();
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 6
Java programs based on method overloading.
class Room
{
float length, breadth;
Room(float a, float b)
{
length=a;
breadth=b;
}
Room(float x)
{
length= breadth=x;
}
float area()
{
return(length*breadth);
}
}
public class MethodOverLoading
{
public static void main (String[]args)
{
Room room=new Room(25.0F,12.0F);
Room room1=new Room(20.0F);
float area1=room.area();
System.out.println("Area of Room 1: "+area1);
float area2=room1.area();
System.out.println("Area of Room2: "+area2);
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 7
Java programs based on method overriding.
class A
{
public int x=10;
}
class B extends
{
public int x=20;
public void showvalues()
{
System.out.println("Value of x = "+x);
System.out.println("Value of x = "+x);
}
}
class MethodOverRide
{
public static void main (String[]args)
{
B bobj = new B();
bobj.showvalues();
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 8
Java programs based on interfaces.
import java.util.Scanner;
interface Shap
{
public double area();
}
class Circle implements Shap
{
double radius;
public Circle (double r)
{
radius=r;
}
public double area()
{
return Math.PI*radius*radius;
}
}
class Rectangle implements Shap
{
double length;
double breadth;
public Rectangle (double l, double b)
{
length=l;
breadth=b;
}
public double area()
{
return length*breadth;
}
}
class Interface
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter Radius of Circle: ");
double r= sc.nextDouble();
Circle c= new Circle(r);
double a1= c.area();
System.out.println("Area of Circle :" +a1);
System.out.println("Enter Length of Rectangle: ");
double l1 = sc.nextDouble();
System.out.println("Enter Breadth of Rectangle: ");
double b1 = sc.nextDouble();
Rectangle b= new Rectangle (l1, b1);
double a2= b.area();
System.out.println("Area of rectangle :"+ a2);
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 9
Java programs based on packages.
public class Package {
private final String prn;
private final String name;
private final int[] semesterMarks;
private int totalMarks;
public Student(String prn, String name, int[] semesterMarks) {
if (prn == null || prn.isEmpty()) {
throw new IllegalArgumentException("PRN cannot be empty");
}
if (name == null) {
throw new NullPointerException("Name cannot be null");
}
if (semesterMarks == null || semesterMarks.length != 6) {
throw new IllegalArgumentException("Semester marks must be an array of 6
integers");
}
this.prn = prn;
this.name = name;
this.semesterMarks = semesterMarks.clone(); // Create a defensive copy
this.totalMarks = calculateTotalMarks();
}
private int calculateTotalMarks() {
int sum = 0;
for (int marks : semesterMarks) {
if (marks < 0 || marks > 100) {
throw new IllegalArgumentException("Invalid marks: " + marks);
}
sum += marks;
}
return sum;
}
public String getDetails() {
return "PRN: " + prn + "\nName: " + name + "\nTotal Marks: " + totalMarks;
}
public static void main(String[] args) {
int[] marks = {85, 90, 78, 88, 92, 76}; // Example marks
Student student = new Student("123456", "John Doe", marks);
System.out.println(student.getDetails());
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 10
Java programs based on multithreading.
class MyThread extends Thread
{
String name;
MyThread(String n)]{
name=n;
}
public void run(){
for(int i=1;i<=5;i++)
System.out.println(name+":"+i);
try{
this.sleep(1000);
}catch(Exception e){ }
System.out.println(name+" finished");
}
public static void main(String[]args){
MyThread a=new MyThread("Thread 1");
MyThread b=new MyThread("Thread 2");
a.start();
b.start();
try{
a.join();
b.join();
}catch(Exception e){ }
System.out.println("Both Threads are finished");
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 11
Java programs based on exception handling.
public class ExceptionHandling {
public static void main(String[] args) {
try {
int num1 = 10;
int num2 = 0;
int result = num1 / num2;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
} finally {
System.out.println("End of the try-catch block.");
}
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 12
import java.awt.*;
import java.awt.event.*;
public class AwtApp extends Frame
{
private Label firstNameLabel, lastNameLabel, dobLabel;
private TextField firstNameField, lastNameField, dobField;
private Button submitButton, resetButton;
public AwtApp()
{
System.out.println();
setTitle("User Information Form");
setSize(400, 200);
setLayout(null);
firstNameLabel = new Label("First Name:");
firstNameLabel.setBounds(20, 50, 80, 20);
add(firstNameLabel);
firstNameField = new TextField();
firstNameField.setBounds(120, 50, 200, 20);
add(firstNameField);
lastNameLabel = new Label("Last Name:");
lastNameLabel.setBounds(20, 80, 80, 20);
add(lastNameLabel);
lastNameField = new TextField();
lastNameField.setBounds(120, 80, 200, 20);
add(lastNameField);
dobLabel = new Label("Date of Birth:");
dobLabel.setBounds(20, 110, 80, 20);
add(dobLabel);
dobField = new TextField();
dobField.setBounds(120, 110, 200, 20);
add(dobField);
submitButton = new Button("Submit");
submitButton.setBounds(120, 140, 80, 30);
add(submitButton);
resetButton = new Button("Reset");
resetButton.setBounds(490, 140, 80, 30);
add(resetButton);
submitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String firstName = firstNameField.getText();
String lastName = lastNameField.getText();
String dob = dobField.getText();
System.out.println("First Name: " + firstName);
System.out.println("Last Name: " + lastName);
System.out.println("Date of Birth: " + dob);
}
});
resetButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
firstNameField.setText("");
lastNameField.setText("");
dobField.setText("");
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
AwtApp form = new AwtApp();
form.setVisible(true);
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 13
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SwingApp extends JFrame
{
private JTextField firstNameField, lastNameField, dobField;
private JButton submitButton, resetButton;
public SwingApp()
{
System.out.println();
setTitle("User Information Form");
setSize(400, 250); // Adjusted width and height
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
JLabel firstNameLabel = new JLabel("First Name:");
firstNameLabel.setBounds(20, 30, 80, 20);
add(firstNameLabel);
firstNameField = new JTextField();
firstNameField.setBounds(120, 30, 200, 25); // Adjusted width and height
add(firstNameField);
JLabel lastNameLabel = new JLabel("Last Name:");
lastNameLabel.setBounds(20, 70, 80, 20);
add(lastNameLabel);
lastNameField = new JTextField();
lastNameField.setBounds(120, 70, 200, 25); // Adjusted width and height
add(lastNameField);
JLabel dobLabel = new JLabel("Date of Birth:");
dobLabel.setBounds(20, 110, 80, 20);
add(dobLabel);
dobField = new JTextField();
dobField.setBounds(120, 110, 200, 25); // Adjusted width and height add(dobField);
submitButton = new JButton("Submit");
submitButton.setBounds(120, 150, 80, 30);
add(submitButton);
submitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String firstName = firstNameField.getText();
String lastName = lastNameField.getText();
String dob = dobField.getText();
JOptionPane.showMessageDialog(null, "First Name: " + firstName + "\nLastName: " +
lastName +"\nDate of Birth: " + dob);
}
});
resetButton = new JButton("Reset");
resetButton.setBounds(490, 150, 80, 30);
add(resetButton);
resetButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
firstNameField.setText("");
lastNameField.setText("");
dobField.setText("");
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
SwingApp form = new SwingApp();
form.setVisible(true);
}
});
}
}
Output :-