0% found this document useful (0 votes)
15 views

Java Practical File

All java programs for Bca

Uploaded by

Sameer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java Practical File

All java programs for Bca

Uploaded by

Sameer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

ARNI SCHOOL OF TECHNOLOGY

1|Page
ARNI SCHOOL OF TECHNOLOGY

TABLE OF CONTENTS

S.No. Practical Date Signature


1 How to set up Java & print Hello World. 20-07-2024
2 How to use Java variables to print your 26-07-2024
information.
3 How to take input from user by using java 27-07-2024
scanner.
a) Integer
b) String
4 WAP in Java to demonstrate the Java 02-08-2024
operator.
5 WAP to print Even & Odd numbers on the 03-08-2024
basis of given input.
6 WAP in Java to print Fibonacci series up to 10-08-2024
nth number.
7 WAP in java to print star pattern in triangle 16-08-2024
and reverse triangle.
8 WAP in Java to use nested loop. 17-08-2024
9 WAP in Java to create object in class. 23-08-2024
10 WAP in Java to print numbers in 24-08-2024
triangle & reverse triangle pattern.
11 WAP in Java to use Java String 30-08-024
methods.
12 WAP in Java to use Single & Multilevel 31-08-2024
inheritance.
13 WAP in Java to print Prime numbers. 06-09-2024

14 WAP in Java to Swap two numbers 13-09-2024


using Bitwise operator.
15 WAP in Java of Palindrome. 14-09-2024
16 WAP in Java to find Factorial of user 20-09-2024
input.
17 WAP in Java to initialize object-using 21-09-2024
constructor.

2|Page
ARNI SCHOOL OF TECHNOLOGY

18 WAP in Java to print Multiplication 27-09-2024


table of any number while using the
exception handling in order to take
input.
19 WAP in Java to display the networking 28-09-2024
programming using Sockets.
20 WAP in Java to make simple chat 18-10-2024
application using TCP/IP.

21 WAP in Java to demonstrate 19-10-2024


overloading method names.
22 WAP in java of overloading a 08-11-2024
constructor.
23 WAP in Java of overriding method. 09-11-2024

3|Page
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-1

Aim: How to set up Java & print Hello World.

Solution:
Steps to setup java:- Here are the simple steps for Java JDK download and
how you can install Java JDK in your system.
Step 1: Verify that it is already installed or not
Before you can install Java in your system, you need to check whether it is
already in the system. To do so, type ‘cmd’ in the search bar and tap ‘enter’.
This opens up the command prompt.

Run the command ‘java –version’, and it will return an output message stating
if Java is already installed.

Step 2: Download JDK


For the latest Java JDK download, visit the Oracle Java page, and navigate to
the Downloads section. Once you reach the Downloads page, look for the x64
Installer download link and click on it. You will find it under the Windows
category.
Step 3: Install JDK
Once you have downloaded the file, move to these next steps:

 Look for the ‘downloaded file’ option and click on it.

4|Page
ARNI SCHOOL OF TECHNOLOGY

 Select option ‘Next’.

 Choose the option ‘Development Tools’ and select the option ‘Next’.

5|Page
ARNI SCHOOL OF TECHNOLOGY

 Wait while the setup is finished.

 Choose your preferred destination folder to install Java Windows. Select


‘Next’.

 Wait while installation is completed.

6|Page
ARNI SCHOOL OF TECHNOLOGY

 Now the Java SE development kit is successfully installed in your system.


Click on the ‘Close’ option, and you’re ready to run Java.
Step 4: Set the Permanent Path
To install Java from the command line of Windows 10, you need to fix the Java
Path. To set it up, you should follow the given steps:

 Open the File Explorer by pressing Win + E.


 Right-click on "This PC" or "My Computer" and
select "Properties" from the context menu.

 Click "Advanced system settings" in the System window on the left


sidebar. This will open the System Properties window.

7|Page
ARNI SCHOOL OF TECHNOLOGY

 Click the "Environment Variables" button at the bottom in the System


Properties window.

 You'll see two sections in the Environment Variables window: User variables
and System variables.

8|Page
ARNI SCHOOL OF TECHNOLOGY

 Scroll down under the System variables section and find the "Path" variable.
Select it and click on the "Edit" button.

 Click the "New" button and add the path to your Java installation directory.
For example, if you installed Java in the default location, you can
add "C:\Program Files\Java\jdk1.8.0_161\bin" (replace the version
number with your installed version).

 Click "OK" to save the changes and close all the windows.

 With this step, you have successfully set up Java Path. Now you need to
reopen the Command Prompt and write the command ‘javac’.

 Another new window appears, and now Java download for Windows 10 and
its installation is completed. To run JavaFX applications, you must configure
the IDEs or Integrated Development Environments.

9|Page
ARNI SCHOOL OF TECHNOLOGY

Program to print Hello World:- The "Hello World!" program is a


simple and traditional program used to introduce basic programming
concepts, such as the structure of a programming language and the syntax
for printing output to the console. In Java, printing to the console is done
using the System.out.println() method.

Input:-

Output:-

10 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-2

Aim: How to use Java variables to print your information.

Solution: A variable is a storage location in the computer's memory


that is used to hold data that can be changed during the execution of a
program. Variables are essentially named containers for storing values,
and the values can be of different types, such as numbers, text, or more
complex data structures.

Input:-

11 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

12 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-3

Aim: How to take input from user by using java scanner.

Solution: Scanner class is a part of the java.util package and is used to


read input from various sources, such as the keyboard, files, or other
input streams. It is commonly used to get user input in command-line
programs. The Scanner class simplifies reading input from the user or
other sources. It can read data of various types, such as integers, strings,
and floating-point numbers. The Scanner is part of the java.util package,
so you need to import it to use it in your programs.

a) Integer: The int (short for "integer") data type is used to represent
whole numbers (i.e., numbers without a decimal point). It is one of
the primitive data types in Java and is used to store integer values.

Input:-

13 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

14 | P a g e
ARNI SCHOOL OF TECHNOLOGY

b) String: String data type is used to represent sequences of characters.


Unlike primitive data types (like int or float), a String is a reference
data type, meaning it refers to an object rather than storing a simple
value. Strings are widely used in Java to handle and manipulate text
data.

Input:-

Output:-

15 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-4

Aim: WAP in Java to demonstrate the Java operator.

Solution: An operator is a special symbol or keyword that is used to


perform operations on variables and values. These operations can
include arithmetic calculations, comparisons, logical operations, and
more. Operators are used to manipulate data and variables in Java
programs.
Different Types of Operators in Java
Unary Operators.
Arithmetic Operators.
Bitwise Operators.
Logical Operators.
Relational Operators.
Shift Operators.
Ternary Operators.
Assignment Operators.

Input:-

16 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

17 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-5

Aim: WAP to print Even & Odd numbers on the basis of given input.

Soluion: Even Numbers: An even number is any integer that is divisible


by 2 without a remainder. In other words, when an even number is
divided by 2, the result is always an integer, and the remainder is 0.
Example: 2, 4, 6, 8, 10, -4, -2 are even numbers.
Odd Numbers: An odd number is any integer that is not divisible by 2.
When an odd number is divided by 2, there is always a remainder of 1.
Example: 1, 3, 5, 7, 9, -3, -1 are odd numbers.

Input:-

18 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

19 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-6

Aim: WAP in Java to print Fibonacci series up to nth number.

Solution: The Fibonacci sequence is a type series where each number is the sum
of the two that precede it. It starts from 0 and 1 usually. The Fibonacci sequence is
given by 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on. The list of first 20 terms in
the Fibonacci Sequence is:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181.

Input:-

20 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

21 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-7

Aim: WAP in java to print star pattern in triangle and reverse triangle.

Solution: These are some common triangle patterns that you can print
using loops in Java. Each pattern involves simple logic with for loops
and understanding how to control the number of spaces and stars on each
line. You can adjust these patterns by changing the number of rows or
modifying the loops accordingly.

Input (Triangle):-

22 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output (Triangle):-

23 | P a g e
ARNI SCHOOL OF TECHNOLOGY

(Reverse Triangle):- Starts with the maximum number of stars at the


top, and decreases the stars in each subsequent row. You can modify this
pattern by adding spaces for centering the triangle or adjusting the
number of rows for larger or smaller patterns.

Input:-

Output (Reverse Triangle):-

24 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-8

Aim: WAP in Java to use nested loop.

Solution: A nested loop in Java is a loop inside another loop. It allows


you to iterate through multiple dimensions or levels of data, which is
useful when dealing with multi-dimensional structures such as matrices
or multi-level patterns. They are commonly used to generate patterns,
work with multi-dimensional arrays, or perform complex simulations that
require multiple levels of iteration. The time complexity of nested loops
increases based on the number of iterations in each loop, so they should
be used wisely, especially with large datasets.

Input:-

25 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

26 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-9

Aim: WAP in Java to create object in class.

Solution: An object is an instance of a class. It is a real-world entity that has


properties (attributes) and behaviors (methods). An object represents a specific
entity based on the blueprint provided by the class. Objects are fundamental in
object-oriented programming (OOP), and they allow us to model real-world
entities, manage data, and perform actions related to those entities

Input:-

Output:-

27 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-10

Aim: WAP in Java to print numbers in triangle & reverse triangle


pattern.

Solution: Creating a number pattern triangle in Java using loops involves


using nested loops to print a series of numbers arranged in a triangular
format. Each row of the triangle typically has an increasing count of
numbers, with the first row containing one number, the second row two
numbers, and so on. This structure can be achieved using loops, with the
outer loop handling the rows and the inner loop controlling the number of
elements printed on each row.

Input (Triangle):-

28 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output (Triangle):-

29 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Input (Reverse Triangle):-

Output (Reverse Triangle):-

30 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-11

Aim: WAP in Java to use Java String methods.

Solution: A String method is a predefined function available for String objects


that allows us to manipulate and perform various operations on strings. Java's
String class in the java.lang package provides numerous methods to work with
strings, such as finding lengths, comparing strings, extracting substrings, and
modifying or checking content.

Input:-

31 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

32 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-12

Aim: WAP in Java to use Single & Multilevel inheritance.

Solution: Inheritance in Java is a core concept in object-oriented


programming that allows a class to inherit properties and behaviors
(fields and methods) from another class. It promotes code reuse, logical
hierarchy, and readability by enabling new classes to be created based on
existing ones.

a) Single Inheritance: Single inheritance in Java is a type of inheritance


where a class (subclass) inherits properties and behaviors (fields and
methods) from only one superclass. In other words, each class has only
one direct superclass, and a subclass cannot inherit from more than one
class.

Input:-

33 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

34 | P a g e
ARNI SCHOOL OF TECHNOLOGY

b) Multilevel Inheritance: Multilevel inheritance in Java is a type of


inheritance where a class is derived from another class, which is also
derived from another class, creating a chain of inheritance. In this type
of inheritance, there is a hierarchy of classes where one class inherits
from another class, which in turn inherits from a third class, and so on.

Input:-

Output:-

35 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-13

Aim: WAP in Java to print Prime numbers.

Solution: A prime number is a natural number greater than 1 that has no


divisors other than 1 and itself. In other words, a prime number has
exactly two distinct positive divisors: 1 and itself. For example, 2, 3, 5, 7,
11, and 13 are prime numbers.

Input:-

36 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

37 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-14

Aim: WAP in Java to swap two numbers using Bitwise operator.

Solution: Bitwise operators are operators that operate on the binary


representation of integers. They perform operations at the bit level and
are typically used in low-level programming, such as bit manipulation,
encryption, compression, and working with binary data.

Input:-

38 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

39 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-15

Aim: WAP in Java of Palindrome.

Solution: A palindrome is a word, phrase, number, or any sequence of


characters that reads the same backward as forward. For example, the
words "madam", "level", and the number "121" are palindromes because
their reverse is identical to the original sequence..

Input:-

Output:-

40 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-16

Aim: WAP in Java to find Factorial of user input.

Solution: The factorial of a number is the product of all positive integers


from 1 up to that number.
For example:
 5!= 5×4×3×2×1=120
 3!= 3×2×1=6

Input:-

41 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

42 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-17

Aim: WAP in Java to initialize object-using constructor.

Solution: A constructor is a special method that is used to initialize


objects. It is called when an object of a class is created, and it helps to set
up the initial state of that object. Constructors are defined in the class and
have the same name as the class. They do not have a return type (not
even void), which distinguishes them from regular methods.

Input:-

43 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

44 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-18

Aim: WAP in Java to print Multiplication table of any number while


using the exception handling in order to take input.

Solution: Exception handling is used to manage errors and unusual


situations gracefully without crashing the program. When creating a
multiplication table, common exceptions could include:
1. InputMismatchException: If the user provides a non-integer input.
2. ArithmeticException: This can happen in specific mathematical
operations, though it’s uncommon in simple multiplication tables.

Input:-

45 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

46 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-19

Aim: WAP in Java to display the networking programming using


Sockets.

Solution: Network programming often uses sockets to enable communication


between devices over a network. A socket is an endpoint for sending or receiving
data across a network. Sockets are primarily used in client-server applications
where a server listens for requests from clients.

Input(Client):-

Output(Client):-

47 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Input(Server):-

Output(Server):-

48 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-20

Aim: WAP in Java to make simple chat application using TCP/IP.

Solution: In Java programming, TCP/IP (Transmission Control


Protocol/Internet Protocol) is used to establish reliable, connection-
oriented communication between a client and a server over a network.
Java provides a simple API to implement TCP/IP communication,
primarily using the Socket and ServerSocket classes in the java.net
package.
a)

Input:-

49 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

50 | P a g e
ARNI SCHOOL OF TECHNOLOGY

b)

Input:-

Output:-

51 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-21

Aim: WAP In Java to demonstrate overloading methods name.

Solution: Method Overloading in Java is a feature that allows a class to


have more than one method with the same name, but with different
parameter lists. It is a way to define multiple methods with the same
name but with different method signatures. The compiler differentiates
overloaded methods by their parameter type, number of parameters, or
order of parameters.

Input:-

52 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

53 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-22

Aim: WAP in Java of overloading a constructor.

Solution: Constructor overloading in Java is the concept where a class can have
more than one constructor, each with a different parameter list. The constructor is
invoked when an object of the class is created, and overloading allows the creation
of objects with different initializations by using different constructors.

Input:-

54 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

55 | P a g e
ARNI SCHOOL OF TECHNOLOGY

PRACTICAL-23

Aim: WAP in Java of overrinding method.

Solution: Method Overriding in Java is a concept that allows a subclass to provide


a specific implementation for a method that is already defined in its superclass.
The overridden method in the subclass must have the same signature (method
name, return type, and parameter list) as the method in the superclass. The primary
purpose of method overriding is to enable dynamic polymorphism, allowing the
behavior of an object to be determined at runtime based on its actual type.

Input:-

56 | P a g e
ARNI SCHOOL OF TECHNOLOGY

Output:-

57 | P a g e

You might also like