0% found this document useful (0 votes)
11 views25 pages

Practical No:1 AIM: Java Basics and Introduction: Name: Enrollment Number

The document outlines practical exercises in Java programming, covering fundamental concepts such as string handling, user input, arrays, classes, method overloading, inheritance, and mathematical operations. Each practical includes objectives, concepts used, code snippets, outputs, and conclusions that highlight the learning outcomes. The exercises aim to provide a comprehensive introduction to Java programming principles and Object-Oriented Programming (OOP).

Uploaded by

Ayan parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

Practical No:1 AIM: Java Basics and Introduction: Name: Enrollment Number

The document outlines practical exercises in Java programming, covering fundamental concepts such as string handling, user input, arrays, classes, method overloading, inheritance, and mathematical operations. Each practical includes objectives, concepts used, code snippets, outputs, and conclusions that highlight the learning outcomes. The exercises aim to provide a comprehensive introduction to Java programming principles and Object-Oriented Programming (OOP).

Uploaded by

Ayan parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Name: Sankesara Siddhant Vishalkumar

Enrollment number: 230149


Practical No :1

AIM: Java Basics and Introduction


A) Java program displaying: - "Welcome to Java, Learning is Fun”.
CONCEPT USED:
A Java program displaying "Welcome to Java, Learning is Fun" involves:

1. String Literals: The message is a string enclosed in double quotes.

2. Printing Output: The program uses System.out.println() to display the string.

3. Basic Syntax: It includes a class and a main method (public static void main(String[]
args)) as the entry point.

CODE:

OUTPUT:
CONCLUSION:
This simple Java program demonstrates the fundamental concepts of Java programming,
including defining a class, using the main method as the entry point, and outputting a message to
the console using System.out.println(). It serves as a basic introduction to syntax, string handling,
and printing output in Java.

B) Java program to print the sum of two numbers.


CONCEPT USED:
This Java program uses:

1. Scanner to take user input.

2. add() method to compute the sum of two integers.

3. System.out.println() to display the result.

4. Basic Java structure with a class and main method.

CODE:
OUTPUT:

CONCLUSION:
This Java program demonstrates how to take user input, perform a simple addition, and display
the result. It highlights key concepts like using the Scanner class for input, defining methods for
functionality, and following basic Java syntax.

C) Basic Calculator using switch case and functions.


CONCEPT USED:
The concepts in a Basic Calculator program using switch-case and functions include:

1. Switch-Case: Controls branching based on the operation selected by the user.

2. Functions: Defines methods like add(), subtract(), multiply(), and divide() to perform
tasks.

3. Input Handling: Takes user input for numbers and the operation using Scanner.

4. Arithmetic Operations: Performs basic operations (+, -, *, /) based on user input.

5. Control Flow: The switch statement handles different operations, with break to exit after
execution.
CODE:
OUTPUT:

CONCLUSION:
This program demonstrates a basic calculator using Java's switch-case and functions, allowing
users to perform simple arithmetic operations. The use of functions makes the code modular and
easy to maintain, while the switch-case structure handles user input efficiently.
Name: Sankesara Siddhant Vishalkumar
Enrollment number: 230149
Practical No :2
AIM: Arrays, Classes, and Overloading
A) Program demonstrating arrays in Java.

CONCEPT USED:
The concepts used in this Java program are:

1. ArrayList: An ArrayList<Integer> is used to store a dynamic list of integers, allowing


flexible resizing.

2. Basic Operations: The program performs operations like sum, average, minimum, and
maximum on the elements of the ArrayList.

3. Enhanced For Loop: An enhanced for loop (for (int i : a)) is used to iterate over the
elements of the ArrayList.

4. Math Operations: It calculates sum, average, minimum, and maximum values using basic
arithmetic and conditional checks.

5. Output: The program prints the results of the calculations using System.out.println().
CODE:
OUTPUT:

CONCLUSION:
This Java program demonstrates how to use an ArrayList to store and manipulate a collection of
integers. It performs basic calculations like sum, average, minimum, and maximum, showcasing
the use of loops, conditional statements, and arithmetic operations to process data efficiently.

B) Program demonstrating classes and objects.


CONCEPT USED:
The concepts used in this program are:

1. Class: Student is a class that defines a blueprint for student objects, with fields like name,
rollNo, and marks.

2. Object: An object student is created using the Student class to represent a specific student
with particular details.

3. Constructor: The constructor Student(String name, int rollNo, int[] marks) initializes the
object's fields when it is created.

4. Instance Variables: name, rollNo, and marks are instance variables that store
information specific to each object.

5. Method: The method displayDetails() performs operations on the instance variables and
displays the student's information.

6. Array: An array marks stores multiple values (marks for 5 subjects in this case).

7. Looping: A for-each loop is used to iterate through the marks array to calculate the total
marks.
8. Object Method Invocation: The student.displayDetails() call demonstrates invoking a
method on an object to perform an action.

CODE:
OUTPUT:

CONCLUSION:
This program demonstrates key Object-Oriented Programming concepts in Java, such as defining
a class, creating objects, using constructors for initialization, and encapsulating behavior within
methods. It shows how to store and manipulate data (like student marks) in an object-oriented
way.

C) Method overloading and constructor overloading.


CONCEPT USED:
1. Inner Classes: Rectangle and Addition are non-static inner classes, requiring an instance
of the outer class (Overloadings) to instantiate.

2. Constructor Overloading: Rectangle has multiple constructors with different parameter


lists to handle default, square, and rectangle initialization.

3. Method Overloading: Addition overloads the add method to handle different numbers
and types of parameters (int, double).

4. OOP Principles: Demonstrates encapsulation, abstraction, and instance-specific


methods.
CODE:
OUTPUT:

CONCLUSION:
The code highlights method and constructor overloading, showcasing flexibility in handling
different inputs. Using inner classes keeps related functionality organized, demonstrating
modularity and key OOP principles like encapsulation and reusability.
Name: Sankesara Siddhant Vishalkumar
Enrollment number: 230149
Practical No :3
AIM: Inheritance and Mathematical Operations
A) BMI Calculator

CONCEPT USED:
=>Object-Oriented Programming (OOP): It defines a BMI class with attributes (weight,
height), a constructor, and a method (calculateBMI).

=>Encapsulation: The class encapsulates data using instance variables and provides a
constructor for initialization.

=>User Input Handling: The Scanner class is used to take input from the user for weight and
height.
CODE:
OUTPUT:

CONCLUSION:
It effectively calculates BMI using Object-Oriented Programming (OOP) principles. It
demonstrates encapsulation by storing weight and height as instance variables and using a
constructor for initialization.

B) Solve equations using Cramer's Rule:


Eg. - 3.4x + 50.2y = 44.5
- 2.1x + 0.55y = 5.9
CONCEPT USED:
Methods and Modularity:

 The program defines reusable methods (determinant and solveEquations) to compute the
determinant and solve equations using Cramer’s Rule.

Conditional Statements:

 It checks if the determinant is zero (det == 0) to determine whether a unique solution


exists.

User Input Handling:

 The Scanner class is used to take coefficients as input from the user, demonstrating input
handling in Java.

Mathematical Computation:

 Uses Cramer’s Rule to solve a system of two linear equations in two variables (x and y).
CODE:
OUTPUT:

CONCLUSION:
It demonstrates modularity with separate methods for determinant calculation and equation
solving. The program also ensures user interaction using the Scanner class for input handling and
applies conditional checks to handle cases where no unique solution exists.

C) Sorting three numbers


CONCEPT USED:
1. Conditional Statements (if conditions): Used for comparing and swapping values to
arrange them in descending order.

2. Methods and Modularity: The sorting logic is encapsulated in the sortDescending method
for reusability.

3. User Input Handling: The Scanner class is used to take three integer inputs from the user.
CODE:
OUTPUT:

CONCLUSION:
This program efficiently sorts three numbers in descending order using a basic swapping
algorithm. It demonstrates modularity, conditional logic, and user input handling in Java.
Name: Sankesara Siddhant Vishalkumar
Enrollment number: 230149
Practical No :4
AIM: Inheritance and its concepts
CONCEPT USED:
1. Class & Object: vehicle, landvehicle, and Car are classes, and an object of Car is created
in main.

2. Constructor & Inheritance: landvehicle extends vehicle, and Car extends landvehicle to
reuse properties.

3. Method Overriding & Final Method: showVehicleInfo() is final, preventing overriding.

4. Interface & Implementation: Car implements airvehicle, defining msg().

5. Super Keyword: super(brand) calls the parent constructor to initialize properties.

6. User Input & Scanner Class: Scanner takes user input for brand, wheels, and model.

Flow of the Code:

1. User inputs the brand, number of wheels, and car model.

2. Car object is created using the constructor, calling super to initialize parent class
attributes.

3. Methods showVehicleInfo(), showLandVehicleInfo(), and showCarInfo() are called to


display details.

4. The msg() method from the airvehicle interface is executed.


CODE:
OUTPUT:

CONCLUSION:
This program demonstrates OOP concepts such as inheritance, interfaces, constructors, and
method overriding in Java. It efficiently models a vehicle hierarchy, ensuring code reusability
and structured design.

You might also like