You are on page 1of 37

CS433P Programming Paradigm Lab

REG NO : 2262070

NAME : Harsheet Thakur

EXPERIMENT NO 1 : Arithmetic Operations


DATE : 21/11/2023

BASIC ARITHMETIC OPERATIONS

PROBLEM STATEMENT
Write a Java Program to implement basic arithmetic operations using basic symbols and
operators.

AIM
Implementation of arithmetic operation using basic symbols

PROGRAM
import java.util.Scanner;

public class ArithmeticOperations {


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

System.out.print("Enter the first number: ");


double num1 = scanner.nextDouble();

System.out.print("Enter the second number: ");


double num2 = scanner.nextDouble();

double sum = num1 + num2;


System.out.println("Sum: " + sum);
1
CS433P Programming Paradigm Lab

double difference = num1 - num2;


System.out.println("Difference: " + difference);

double product = num1 * num2;


System.out.println("Product: " + product);

if (num2 != 0) {
double quotient = num1 / num2;
System.out.println("Quotient: " + quotient);
} else {
System.out.println("Cannot divide by zero.");
}

if (num2 != 0) {
double remainder = num1 % num2;
System.out.println("Remainder: " + remainder);
} else {
System.out.println("Cannot calculate remainder when dividing by zero."); }

scanner.close();
}
}

2
CS433P Programming Paradigm Lab
OUTPUT
Enter the first number: 15
Enter the second number: 7

Sum: 22.0
Difference: 8.0
Product: 105.0
Quotient: 2.142857142857143
Remainder: 1.0

RESULT
Demonstration of arithmetic operators and if-else conditional statements has been successfully
executed.

3
CS433P Programming Paradigm Lab
REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 2 : Objects & Strings
DATE : 28/11/2023

OBJECTS & STRINGS

PROBLEM STATEMENT
Write a Java Program using objects of Student and Employee by using looping statements. Get
the details of 5 students and display it.

AIM
From main function, create objects of student and employee by using looping statement. Get 5
students details and display it.

PROGRAM
import java.util.Scanner;

class Student {
String name;
int age;
String course;

public Student(String name, int age, String course) {


this.name = name;
this.age = age;
this.course = course;
}

public String getRecord() {

4
CS433P Programming Paradigm Lab
return String.format("| %-15s | %-5d | %-15s |", name, age, course); }
}

class Employee {
String name;
int age;
String designation;

public Employee(String name, int age, String designation)


{ this.name = name;
this.age = age;
this.designation = designation;
}

public String getRecord() {


return String.format("| %-15s | %-5d | %-15s |", name, age, designation); }
}

public class RecordBook {


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

Student[] students = new Student[5];


Employee[] employees = new Employee[5];

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

5
CS433P Programming Paradigm Lab
System.out.println("Enter details for Student " + (i + 1) + ":");
System.out.print("Name: ");
String studentName = scanner.next();
System.out.print("Age: ");
int studentAge = scanner.nextInt();
System.out.print("Course: ");
String studentCourse = scanner.next();

students[i] = new Student(studentName, studentAge, studentCourse); }

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


System.out.println("Enter details for Employee " + (i + 1) + ":");
System.out.print("Name: ");
String employeeName = scanner.next();
System.out.print("Age: ");
int employeeAge = scanner.nextInt();
System.out.print("Designation: ");
String employeeDesignation = scanner.next();

employees[i] = new Employee(employeeName, employeeAge, employeeDesignation); }

System.out.println("\nRecord Book\n");

System.out.println("Details of 5 Students:");
System.out.printf("| %-15s | %-5s | %-15s |\n", "Name", "Age", "Course"); for
(int i = 0; i < 5; i++) {

6
CS433P Programming Paradigm Lab
System.out.println(students[i].getRecord());
}
System.out.println("\nDetails of 5 Employees:");
System.out.printf("| %-15s | %-5s | %-15s |\n", "Name", "Age", "Designation"); for
(int i = 0; i < 5; i++) {
System.out.println(employees[i].getRecord());
}

scanner.close();
}
}

OUTPUT
Enter details for Student 1:
Name: John
Age: 20
Course: Computer Science
Enter details for Student 2:
Name: Alice
Age: 22
Course: Mathematics
Enter details for Student 3:
Name: Bob
Age: 21

7
CS433P Programming Paradigm Lab
Course: Physics
Enter details for Student
4: Name: Carol
Age: 23
Course: Chemistry
Enter details for Student
5: Name: Dave
Age: 25
Course: Biology
Enter details for Employee
1: Name: ProfA
Age: 40
Designation: Professor
Enter details for Employee
2: Name: DrB
Age: 35
Designation: Associate
Professor Enter details for
Employee 3: Name: ProfC
Age: 45
Designation: Professor
Enter details for Employee
4: Name: DrD
Age: 38
Designation: Assistant
Professor Enter details for
Employee 5: Name: ProfE
Age: 42

8
CS433P Programming Paradigm Lab
Designation: Professor

Record Book
Details of 5 Students:
| Name | Age | Course |
| John | 20 | Computer Science |
| Alice | 22 | Mathematics |
| Bob | 21 | Physics |
| Carol | 23 | Chemistry |
| Dave | 25 | Biology |

Details of 5 Employees:
| Name | Age | Designation |
| ProfA | 40 | Professor |
| DrB | 35 | Associate Professor |
| ProfC | 45 | Professor |
| DrD | 38 | Assistant Professor |
| ProfE | 42 | Professor |

RESULT
Thus the program to implement the Student and Employee details by using Java has been
implemented successfully & verified.

9
CS433P Programming Paradigm Lab
REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 3a: Array
Implementation
DATE : 12/12/2023
ARRAY IMPLEMENTATION

PROBLEM STATEMENT
Write a Java program to get Student & Faculty details using arrays.

AIM
From main function, implement Student & Faculty information using arrays.

PROGRAM
import java.util.Scanner;

class Person {
String name;
int age;

public Person(String name, int age) {


this.name = name;
this.age = age;
}

public String getRecord() {


return String.format("| %-15s | %-5d |", name, age);
}
}

10
CS433P Programming Paradigm Lab

class Student extends Person {


String course;
public Student(String name, int age, String course)
{ super(name, age);
this.course = course;
}

@Override
public String getRecord() {
return super.getRecord() + String.format(" %-15s |", course); }
}

class Faculty extends Person {


String designation;

public Faculty(String name, int age, String designation)


{ super(name, age);
this.designation = designation;
}

@Override
public String getRecord() {
return super.getRecord() + String.format(" %-15s |", designation); }
}

11
CS433P Programming Paradigm Lab

public class Record {


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

Student[] students = new Student[5];


Faculty[] faculties = new Faculty[5];

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


System.out.print("Enter Student Name: ");
String studentName = scanner.next();
System.out.print("Enter Student Age: ");
int studentAge = scanner.nextInt();
System.out.print("Enter Student Course: ");
String studentCourse = scanner.next();

students[i] = new Student(studentName, studentAge, studentCourse); }

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


System.out.print("Enter Faculty Name: ");
String facultyName = scanner.next();
System.out.print("Enter Faculty Age: ");
int facultyAge = scanner.nextInt();
System.out.print("Enter Faculty Designation: ");
String facultyDesignation = scanner.next();

faculties[i] = new Faculty(facultyName, facultyAge, facultyDesignation);

12
CS433P Programming Paradigm Lab
}

System.out.printf("| %-15s | %-5s | %-15s | %-15s |\n", "Name", "Age", "Course",


"Designation");

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


System.out.print(students[i].getRecord());
System.out.println(faculties[i].getRecord());
}

scanner.close();
}
}

OUTPUT
Enter Student Name: John
Enter Student Age: 20
Enter Student Course: ComputerScience
Enter Student Name: Alice
Enter Student Age: 22
Enter Student Course: Mathematics
Enter Student Name: Bob
Enter Student Age: 21
Enter Student Course: Physics
Enter Student Name: Carol
Enter Student Age: 23
Enter Student Course: Chemistry

13
CS433P Programming Paradigm Lab
Enter Student Name: Dave
Enter Student Age: 25
Enter Student Course: Biology
Enter Faculty Name: ProfA
Enter Faculty Age: 40
Enter Faculty Designation: Professor
Enter Faculty Name: DrB
Enter Faculty Age: 35
Enter Faculty Designation: AssociateProfessor
Enter Faculty Name: ProfC
Enter Faculty Age: 45
Enter Faculty Designation: Professor
Enter Faculty Name: DrD
Enter Faculty Age: 38
Enter Faculty Designation: AssistantProfessor
Enter Faculty Name: ProfE
Enter Faculty Age: 42
Enter Faculty Designation: Professor
| Name | Age | Course | Designation |
| John | 20 | ComputerScience | Professor | | Alice | 22 |
Mathematics | AssociateProfessor | | Bob | 21 | Physics |
Professor |
| Carol | 23 | Chemistry | AssistantProfessor | | Dave | 25
| Biology | Professor |

RESULT
Implementation of arrays has been done successfully and without errors.

14
CS433P Programming Paradigm Lab
REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 3b: Constructor
Overloading
DATE : 12/12/2023

CONSTRUCTOR OVERLOADING

PROBLEM STATEMENT
Write a Java program to find the area of a shape with 4 sides where the input of each side may or
may not be given.

AIM
To demonstrate the working of Java constructor overloading.

PROGRAM
import java.util.Scanner;

class Shape {
double side1, side2, side3, side4;

public Shape() {
// Default constructor
}

public Shape(double side1) {


this.side1 = side1;
}

public Shape(double side1, double side2) {

15
CS433P Programming Paradigm Lab
this.side1 = side1;
this.side2 = side2;
}

public Shape(double side1, double side2, double side3)


{ this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public Shape(double side1, double side2, double side3, double side4)
{ this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
this.side4 = side4;
}
public double calculateArea() {
return side1 * side2;
}
}
public class AreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the length of side 1 (or 0 if not provided): ");
double side1 = scanner.nextDouble();

System.out.print("Enter the length of side 2 (or 0 if not provided): ");


double side2 = scanner.nextDouble();

16
CS433P Programming Paradigm Lab
System.out.print("Enter the length of side 3 (or 0 if not provided): ");
double side3 = scanner.nextDouble();

System.out.print("Enter the length of side 4 (or 0 if not provided): ");


double side4 = scanner.nextDouble();
Shape shape = new Shape(side1, side2, side3, side4);

double area = shape.calculateArea();

System.out.println("Area of the shape: " + area);

scanner.close();
}
}
OUTPUT
Enter the length of side 1 (or 0 if not provided): 5
Enter the length of side 2 (or 0 if not provided): 8
Enter the length of side 3 (or 0 if not provided): 0
Enter the length of side 4 (or 0 if not provided): 0
Area of the shape: 40.0

RESULT
Implementation of the given program using constructor overloading has been successfully
implemented without errors.

17
CS433P Programming Paradigm Lab
REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 4a: Code Debugging
DATE : 19/12/2023

CODE DEBUGGING

PROBLEM STATEMENT
Add appropriate code in the subclass and the base class to print the
following. class A {
int i;
void display()
{
}
}
class B
{
void display()
{
System.out.println(j);
}
}
class inheritance_demo
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;

18
CS433P Programming Paradigm Lab
obj.display();
}
}

AIM
To debug the problem statement and to get the output
PROGRAM
class A {
int i;
void display() {
System.out.print(i + " ");
}
}

class B extends A {
int j;
void display() {
super.display(); // Call display() of class A
System.out.println(j);
}
}

public class InheritanceDemo {


public static void main(String args[])
{ B obj = new B();
obj.i = 1;
obj.j = 2;

19
CS433P Programming Paradigm Lab
obj.display();
}
}

OUTPUT
1
2
RESULT
Inheritance concept has been successfully implemented without any errors.

20
CS433P Programming Paradigm Lab
REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 4b: Code Debugging
DATE : 19/12/2023

CODE DEBUGGING

PROBLEM STATEMENT
Add appropriate code to print the following output. Find Out the
appropriate variable names and its data types.
class A
{
}
class B extends A
{
}
class Mainclass
{
public static void main(String args[])
{
B p=new B();
System.out.println("B salary is:"+p.sal);
System.out.println("Bonus of B is:"+p.bonus);
}
}

AIM
To debug the given code and to get the expected output

21
CS433P Programming Paradigm Lab

PROGRAM
class A {
}

class B extends A {
double sal = 4000.0;
double bonus = 10000;
}
public class Mainclass {
public static void main(String args[])
{ B p = new B();
System.out.println("B salary is: " + p.sal);
System.out.println("Bonus of B is: " + p.bonus); }
}

OUTPUT
B salary is: 4000.0
Bonus of B is: 10000

RESULT
Single-level inheritance implemented successfully.

22
CS433P Programming Paradigm Lab
REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 4c: Code Debugging
DATE : 19/12/2023

CODE DEBUGGING

PROBLEM STATEMENT
Add the appropriate code to print the following output. Use only the child class
object. class Parent
{
void display(){
System.out.println(“Parent”);
}
class Child
{
void display(){
System.out.println(“Child”);
}
}
class MainClass
{
public static void main(String args[]){
}
}

AIM
To debug the given program and to get the expected output.

23
CS433P Programming Paradigm Lab
PROGRAM
class Parent {
void display() {
System.out.println("Parent");
}
}

class Child extends Parent {


void display() {
System.out.println("Child");
}
}
public class MainClass {
public static void main(String args[]) {
Child childObject = new Child();
((Parent) childObject).display(); // Calls Parent class display method
childObject.display(); // Calls Child class display method }
}

OUTPUT
Parent
Child

RESULT
Multi-Level inheritance has been successfully implemented without errors.

24
CS433P Programming Paradigm Lab
REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 4d: Code Debugging
DATE : 19/12/2023

CODE DEBUGGING

PROBLEM STATEMENT
Complete the code to get the following output. Don't change anything in the main class.
In all the classes, you need to show the methods for displaying at least any one of the
output. Demonstrate multilevel inheritance
public class Side
{
}
public class Square extends Side
{
}
public class Cuboid extends Square
{
}
public class MainClass
{
public static void main(String argos[])
{
//Dont Change anything here.
Cuboid C1=new Cuboid();
C1.getValueOfSide();
C1.Display();
}

25
CS433P Programming Paradigm Lab
}

AIM
To debug the given program and to get the desired output

PROGRAM
class Side {
double side;

void getValueOfSide() {
System.out.print("Enter the value of a side: ");
side = 5
}
}
class Square extends Side {
void Display() {
System.out.println("The value of the side is: " + side);
System.out.println("The area of the square is: " + side * side); }
}

class Cuboid extends Square {


void Display() {
super.Display(); // Call Display method of Square class
System.out.println("The volume of the cuboid is: " + side * side * side); }
}

26
CS433P Programming Paradigm Lab

public class MainClass {


public static void main(String args[]) {
Cuboid C1 = new Cuboid();
C1.getValueOfSide();
C1.Display();
}
}

OUTPUT
Enter the value of a side : 5
The value of the side is : 5
The area of the square is : 25
The volume of the cuboid is : 125.

RESULT
The given program has been successfully implemented without any errors.
27
CS433P Programming Paradigm Lab
REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 5 : Access Specifiers
DATE : 5/01/2024

ACCESS SPECIFIERS

PROBLEM STATEMENT
Write a Java program to implement all types of access specifiers.

AIM
To demonstrate access specifiers
-public -private -default -protected

PROGRAM
class AccessSpecifiersDemo {
private int privateVariable = 10;
int defaultVariable = 20;
protected int protectedVariable = 30;
public int publicVariable = 40;

private void privateMethod() {


System.out.println("Private Method");
}

void defaultMethod() {
System.out.println("Default Method");
}

28
CS433P Programming Paradigm Lab
protected void protectedMethod() {
System.out.println("Protected Method");
}

public void publicMethod() {


System.out.println("Public Method");
}
}

public class MainClass {


public static void main(String[] args) {
AccessSpecifiersDemo obj = new AccessSpecifiersDemo();

// System.out.println(obj.privateVariable); // Error: privateVariable has private access


System.out.println(obj.defaultVariable);
System.out.println(obj.protectedVariable);
System.out.println(obj.publicVariable);

/
// obj.privateMethod(); // Error: privateMethod() has private access
obj.defaultMethod();
obj.protectedMethod();
obj.publicMethod();
}
}

29
CS433P Programming Paradigm Lab

OUTPUT
20
30
40
Default Method
Protected Method
Public Method

RESULT
The implementation of access specifiers has been successfully implemented without any errors.

30
CS433P Programming Paradigm Lab
REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 6 : Interfaces
DATE : 9/01/2024

INTERFACES

PROBLEM STATEMENT
Write a Java program to implement the area of square, rectangle & circle by using interfaces
with the base class as Shape class.

AIM
To implement interfaces in Java

PROGRAM
interface Shape {
double calculateArea();
}

class Square implements Shape {


private double side;

public Square(double side) {


this.side = side;
}

@Override
public double calculateArea() {
return side * side;

31
CS433P Programming Paradigm Lab
}
}

class Rectangle implements Shape {


private double length;
private double width;

public Rectangle(double length, double width)


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

@Override
public double calculateArea() {
return length * width;
}
}

class Circle implements Shape {


private double radius;

public Circle(double radius) {


this.radius = radius;
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;

32
CS433P Programming Paradigm Lab
}
}

public class MainClass {


public static void main(String[] args) {
Square square = new Square(5);
Rectangle rectangle = new Rectangle(4, 6);
Circle circle = new Circle(3);

System.out.println("Area of Square: " + square.calculateArea());


System.out.println("Area of Rectangle: " + rectangle.calculateArea());
System.out.println("Area of Circle: " + circle.calculateArea()); }
}

OUTPUT
Area of Square: 25.0
Area of Rectangle: 24.0
Area of Circle: 28.274333882308138

RESULT
The implementation of interfaces in Java has been done successfully without any errors.

33
CS433P Programming Paradigm Lab
REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 7 : Inner Class
DATE : 16/01/2024

INNER CLASS

PROBLEM STATEMENT
Write a Java program to demonstrate the usage of static, nested and inner class methods.

AIM
To implement static, nested and inner class methods.

PROGRAM
class OuterClass {
private static String staticVariable = "Static Variable in OuterClass";

static void staticMethod() {


System.out.println("Static Method in OuterClass");
}

static class NestedStaticClass {


void nestedStaticMethod() {
System.out.println("Nested Static Method in NestedStaticClass");
System.out.println(staticVariable); // Accessing static variable from outer class }
}

34
CS433P Programming Paradigm Lab
class InnerClass {
void innerMethod() {
System.out.println("Inner Method in InnerClass");
System.out.println(staticVariable); // Accessing static variable from outer class }
}
}

public class MainClass {


public static void main(String[] args) {
OuterClass.staticMethod(); // Calling static method of OuterClass

OuterClass.NestedStaticClass nestedStaticObj = new OuterClass.NestedStaticClass();


nestedStaticObj.nestedStaticMethod(); // Calling method of NestedStaticClass

OuterClass outerObj = new OuterClass();


OuterClass.InnerClass innerObj = outerObj.new InnerClass();
innerObj.innerMethod(); // Calling method of InnerClass
}
}

OUTPUT
outer_x = 10
outer_private = 30
outer_y = 20

RESULT
The implementation of static, inner & nested class has been done successfully without any errors.

35
CS433P Programming Paradigm Lab

REG NO : 2262054 NAME : Denil Jos EXPERIMENT NO 8 : Login Page GUI


DATE : 23/01/2024
LOGIN PAGE GUI

PROBLEM STATEMENT
Write a Java program to construct a Login Page UI.

AIM
To implement a Login Page UI in Java.

PROGRAM
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginPageUI {


public static void main(String[] args) {
JFrame frame = new JFrame("Login Page");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());

JPanel panel = new JPanel();

36
CS433P Programming Paradigm Lab
panel.setLayout(new GridLayout(3, 2));

JLabel userLabel = new JLabel("Username:");


JTextField userTextField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
JPasswordField passwordField = new JPasswordField();

JButton loginButton = new JButton("Login");


loginButton.addActionListener(new ActionListener()
{ @Override
public void actionPerformed(ActionEvent e)
{ String username = userTextField.getText();
char[] password = passwordField.getPassword(); // Add
your login logic here
JOptionPane.showMessageDialog(null, "Login Successful!"); }
});

panel.add(userLabel);
panel.add(userTextField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(new JLabel()); // Empty label for spacing
panel.add(loginButton);

frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);

37
CS433P Programming Paradigm Lab
}
}

OUTPUT
RESULT
The implementation of Login Page UI has been done successfully without any errors.

38

You might also like