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

Skill Lab Manual

Manual

Uploaded by

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

Skill Lab Manual

Manual

Uploaded by

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

Aldel Education Trust’s

St. John College of Engineering and Management, Palghar


NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 01

Experiment Title: - Write C++ Program to Add Two Numbers.

Date of Performance: Date of Submission:

********************************************************************
Theory: -

C++ is a compiled language. For a program to run, its source text has to be processed by
a compiler, producing object files, which are combined by a linker yielding an executable
program. A C++ program typically consists of many source code files (usually simply called
source files).

An executable program is created for a specific hardware/system combination; it is not


portable, say, from a Mac to a Windows PC. When we talk about portability of C++ programs,
we usually mean portability of source code; that is, the source code can be successfully
compiled and run on a variety of systems.
The ISO C++ standard defines two kinds of entities:
• Core language features, such as built-in types (e.g., char and int) and loops (e.g., for-
statements and while-statements)
• Standard-library components, such as containers (e.g., vector and map) and I/O
operations (e.g., << and getline())
The standard-library components are perfectly ordinary C++ code provided by every C++
implementation. That is, the C++ standard library can be implemented in C++ itself (and is with
very minor uses of machine code for things such as thread context switching). This implies that
C++ is sufficiently expressive and efficient for the most demanding systems programming tasks.
C++ is a statically typed language. That is, the type of every entity (e.g., object, value,
name, and expression) must be known to the compiler at its point of use. The type of an object
determines the set of operations applicable to it.
The minimal C++ program is
int main() { } // the minimal C++ program
This defines a function called main, which takes no arguments and does nothing.
Curly braces, { }, express grouping in C++. Here, they indicate the start and end of the
function body. The double slash, //, begins a comment that extends to the end of the line. A
comment is for the human reader; the compiler ignores comments.
Every C++ program must have exactly one global function named main(). The program
starts by executing that function. The int value returned by main(), if any, is the program’s return
value to ‘‘the system.’’ If no value is returned, the system will receive a value indicating
successful completion. A nonzero value from main() indicates failure. Not every operating
system and execution environment make use of that return value: Linux/Unix-based
environments often do, but Windows-based environments rarely do.

Procedure: -
1. In this program, the user is asked to enter two integers.
2. These two integers are stored in
variables first_number and second_number respectively.
3. The variables are added using the + operator and stored in the sum variable.
4. Finally, sum is displayed on the screen.

Conclusion: -
In this program, user is asked to enter two integers. Then, the sum of those two
integers is stored in a variable and displayed on the screen.

Subject In-charge
Program: -

#include <iostream.h>
#include<conio.h>
using namespace std;

void main()
{

int first_number, second_number, sum;


clrscr();

cout << "Enter two integers: \n";


cin >> first_number >> second_number;

// sum of two numbers in stored in variable sumOfTwoNumbers

sum = first_number + second_number;

// prints sum

cout << first_number << " + " << second_number << " = " << sum;

getch();
}

Output: -
Enter two integers:
4
5
4 + 5 = 9
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 02

Experiment Title: - Write C++ Program to Print Number Entered by User.

Date of Performance: Date of Submission:

********************************************************************
Theory: -
The given task is to take an integer as input from the user and print that
integer in C++ language.

In below program, the syntax and procedures to take the integer as input
from the user is shown in C++ language.
Steps:

• The user enters an integer value when asked.


• This value is taken from the user with the help of cin method. The cin method, in
C++, reads the value from the console into the specified variable.

Syntax:

cin >> variableOfXType;


where >> is the extraction operator and is used along with the object
cin for reading inputs.
The extraction operator extracts the data from the object cin which
is entered using the keyboard.

• For an integer value, the X is replaced with type int. The syntax of cin method
becomes as follows then:

Syntax:
cin >> variableOfIntType;

• This entered value is now stored in the variableOfIntType.


• Now to print this value, cout method is used. The cout method, in C++, prints the
value passed as the parameter to it, on the console screen.

Syntax:
cout << variableOfXType;
where << is the insertion operator.
The data needed to be displayed on the screen is inserted in the
standard output stream (cout) using the insertion operator (<<).

• For an integer value, the X is replaced with type int. The syntax of cout() method
becomes as follows then:

Syntax:
cout << variableOfIntType;

Procedure: -
1. In This program asks the user to enter a number.
2. When the user enters an integer, it is stored in variable number using cin.
3. Then it is displayed on the screen using cout.

Conclusion: -
Hence, the integer value is successfully read and printed.

Subject In-charge
Program: -

#include <iostream.h>
#include<conio.h>
using namespace std;

void main()
{

// Declare the variables


int num;
clrscr();

// Input the integer


cout << "Enter the integer: ";
cin >> num;

// Display the integer


cout << "\nEntered integer is: " << num;

getch();
}

Output: -
Enter the integer: 23

Entered integer is: 23


Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 03

Experiment Title: - Write C++ Program to Swap Two Numbers.

Date of Performance: Date of Submission:

********************************************************************
Theory: -

Method 1:
To perform swapping in Method 1, three variables are used.

The contents of the first variable is copied into the temp variable. Then, the contents of
second variable is copied to the first variable.

Finally, the contents of the temp variable is copied back to the second variable which
completes the swapping process.

Method 2:
The output of this method is the same as the first program above.

Let us see how this method works:

1. Initially, a = 5 and b = 10.


2. Then, we add a and b and store it in a with the code a = a + b. This means a = 5 + 10.
So, a = 15 now.
3. Then we use the code b = a - b. This means b = 15 - 10. So, b = 5 now.
4. Again, we use the code a = a - b. This means a = 15 - 5. So finally, a = 10.
Hence, the numbers have been swapped.
Procedure: -
*Method 1:

1. Start.
2. Read a number in num1.
3. Read a number in num2.
4. Declare a variable temp.
5. Assign temp with num1.
6. Assign num1 with num2.
7. Assign num2 with temp.
8. Print num1 and num2.
9. Stop.

*Method 2:

1. Start.
2. Read a number in num1.
3. Read a number in num2.
4. Assign num1 with num2+num1.
5. Assign num2 with num1-num2.
6. Assign num1 with num1-num2.
7. Print num1 and num2.
8. Stop.

Conclusion: -
Hence, program contains two different techniques to swap numbers in C
programming. The first program uses temporary variable to swap numbers, whereas
the second program does not use temporary variables.

Subject In-charge
Program: -

#include <iostream.h>
#include<conio.h>
using namespace std;

void main()
{

int a = 45 , b = 54, temp;


clrscr();

cout << "Method - 1: \n";


cout << "\nBefore swapping " << a <<" and "<< b ;

// METHOD - 1
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping " << a <<" and "<< b ;

// METHOD - 2
a = 44;
b = 55;
cout << "\n\nMethod - 2: \n";
cout << "\nBefore swapping " << a <<" and "<< b ;
a = a + b;
b = a - b;
a = a - b;
cout << "\nAfter swapping " << a <<" and "<< b ;

getch();
}

Output: -
Method - 1:
Before swapping 45 and 54
After swapping 54 and 45

Method - 2:
Before swapping 44 and 55
After swapping 55 and 44
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 04

Experiment Title: - Write C++ Program to Check Whether Number is Even or Odd.

Date of Performance: Date of Submission:

********************************************************************
Theory: -
In computer programming, we use the if...else statement to run one block of code under
certain conditions and another block of code under different conditions.

For example, assigning grades (A, B, C) based on marks obtained by a student.

• if the percentage is above 90, assign grade A


• if the percentage is above 75, assign grade B
• if the percentage is above 65, assign grade C
There are three forms of if...else statements in C++.
1. if statement
2. if...else statement
3. if...else if...else statement

C++ if Statement

The syntax of the if statement is:

if (condition) {
// body of if statement
}

The if statement evaluates the condition inside the parentheses ( ).


• If the condition evaluates to true, the code inside the body of if is executed.
• If the condition evaluates to false, the code inside the body of if is skipped.

Note: The code inside { } is the body of the if statement.

C++ if...else

The if statement can have an optional else clause. Its syntax is:

if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}

The if..else statement evaluates the condition inside the parenthesis.

How if...else Statement Works


If the condition evaluates true,
• the code inside the body of if is executed
• the code inside the body of else is skipped from execution
If the condition evaluates false,
• the code inside the body of else is executed
• the code inside the body of if is skipped from execution

C++ if...else...else if statement

The if...else statement is used to execute a block of code among two alternatives. However, if
we need to make a choice between more than two alternatives, we use the if...else
if...else statement.
The syntax of the if...else if...else statement is:

if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else {
// code block 3
}

Here,

• If condition1 evaluates to true, the code block 1 is executed.


• If condition1 evaluates to false, then condition2 is evaluated.
• If condition2 is true, the code block 2 is executed.
• If condition2 is false, the code block 3 is executed.

How if...else if...else Statement Works


Note: There can be more than one else if statement but only one if and else statements.
C++ Nested if...else

Sometimes, we need to use an if statement inside another if statement. This is known as


nested if statement.
Think of it as multiple layers of if statements. There is a first, outer if statement, and inside it is
another, inner if statement. Its syntax is:

// outer if statement
if (condition1) {

// statements

// inner if statement
if (condition2) {
// statements
}
}

Notes:
• We can add else and else if statements to the inner if statement as required.
• The inner if statement can also be inserted inside the outer else or else if statements (if
they exist).
• We can nest multiple layers of if statements.

Procedure: -
1. An if..else statement is used to check whether n % 2 == 0 is true or not.
2. If this expression is true, n is even.
3. Else, n is odd.

Conclusion: -
Here, if...else statement is used to check whether a number entered by the user is even or
odd.

Subject In-charge
Program: -

#include <iostream.h>
#include<conio.h>
using namespace std;

void main()
{

int n;
clrscr();

cout << "Enter an integer: ";


cin >> n;

if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";

getch();
}

Output: -
Enter an integer: 23
23 is odd.

Enter an integer: 58
58 is even.
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 05

Experiment Title: - Write C++ Program to Find Largest Number Among Three

Numbers.

Date of Performance: Date of Submission:

********************************************************************
Theory: -
To find the largest of the three number
• if
• if else
• nested if else statements
are used.
Here we write program using nested if else statement.

Flowchart: -
Procedure: -
1. Start
2. Read the three numbers to be compared, as A, B and C.
3. Check if A is greater than B.

3.1 If true, then check if A is greater than C.


3.1.1 If true, print 'A' as the greatest number.
3.1.2 If false, print 'C' as the greatest number.
3.2 If false, then check if B is greater than C.
3.1.1 If true, print 'B' as the greatest number.
3.1.2 If false, print 'C' as the greatest number.
4. End

Conclusion: -
Here, we learn to find the largest number among three numbers using nested if else
statements.

Subject In-charge
Program: -

#include <iostream.h>
#include<conio.h>
using namespace std;

void main()
{

int a, b, c;
clrscr();
cout << "Enter the three numbers a, b & c" <<endl;
cin >> a >> b >> c;

if(a >= b)
{
if(a >= c)
{
cout << "The Largest Among Three Numbers is : " << a << endl;
}
else
{
cout << "The Largest Among Three Numbers is : " <<c << endl;
}
}
else
{
if(b >= c)
{
cout << "The Largest Among Three Numbers is : " <<b << endl;
}
else
{
cout << "The Largest Among Three Numbers is : " <<c << endl;
}
}

getch();
}

Output: -
Enter the three numbers a, b & c
75 200 134
The Largest Among Three Numbers is : 200
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 06

Experiment Title: - Write C++ Program to Create a simple class and object.

Date of Performance: Date of Submission:

********************************************************************
Theory: -
The main purpose of C++ programming is to add object orientation to the C programming
language and classes are the central feature of C++ that supports object-oriented programming
and are often called user-defined types.
A class is used to specify the form of an object and it combines data representation and methods
for manipulating that data into one neat package. The data and functions within a class are called
members of the class.

C++ Class Definitions


When you define a class, you define a blueprint for a data type. This doesn't actually define any
data, but it does define what the class name means, that is, what an object of the class will consist
of and what operations can be performed on such an object.
A class definition starts with the keyword class followed by the class name; and the class body,
enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a
list of declarations. For example, we defined the Box data type using the keyword class as follows

class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
The keyword public determines the access attributes of the members of the class that follows it.
A public member can be accessed from outside the class anywhere within the scope of the class
object. You can also specify the members of a class as private or protected which we will
discuss in a sub-section.

Define C++ Objects


A class provides the blueprints for objects, so basically an object is created from a class. We
declare objects of a class with exactly the same sort of declaration that we declare variables of
basic types. Following statements declare two objects of class Box −
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Both of the objects Box1 and Box2 will have their own copy of data members.

Accessing the Data Members


The public data members of objects of a class can be accessed using the direct member access
operator (.).
It is important to note that private and protected members can not be accessed directly using
direct member access operator (.). We will learn how private and protected members can be
accessed.

Classes and Objects in Detail


So far, you have got very basic idea about C++ Classes and Objects. There are further interesting
concepts related to C++ Classes and Objects which we will discuss in various sub-sections listed
below −

Sr.No Concept & Description

1 Class Member Functions


A member function of a class is a function that has its definition or its prototype within
the class definition like any other variable.

2 Class Access Modifiers


A class member can be defined as public, private or protected. By default members
would be assumed as private.

3 Constructor & Destructor


A class constructor is a special function in a class that is called when a new object of
the class is created. A destructor is also a special function which is called when created
object is deleted.

4 Copy Constructor
The copy constructor is a constructor which creates an object by initializing it with an
object of the same class, which has been created previously.

5 Friend Functions
A friend function is permitted full access to private and protected members of a class.

6 Inline Functions
With an inline function, the compiler tries to expand the code in the body of the function
in place of a call to the function.

7 this Pointer
Every object has a special pointer this which points to the object itself.

8 Pointer to C++ Classes


A pointer to a class is done exactly the same way a pointer to a structure is. In fact a
class is really just a structure with functions in it.

9 Static Members of a Class


Both data members and function members of a class can be declared as static.

Conclusion: -
Here, we learn how to create a class, create object of that class and calling the
member function in main function.

Subject In-charge
Program: -

#include <iostream.h>
#include<conio.h>
using namespace std;

class Hello
{
public:
void sayHellow( )
{
cout<< “Hello World” << endl;
}
};

void main()
{

Hello h;

h.sayHello( );

getch();
}

Output: -
Hello World
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 07

Experiment Title: - Write Java Program to Display addition of number.

Date of Performance: Date of Submission:

********************************************************************
Theory: -
Adding or finding the sum of two numbers in Java is one of the fundamental aspects in
Java. The given two numbers will be added and the sum will be displayed. The size of the data
type must be kept in mind while adding the two numbers. If the size of the answer exceeds the
size of the data type, overflow occurs.

Addition uses the "+" operator for adding two numbers.

Procedure: -
1. Declare and initialize two variables to be added.
2. Declare another variable to store the sum of numbers.
3. Apply mathematical operator (+) between the declared variable and store the
result.
Conclusion: -
This is the easiest way to find the sum of two numbers in Java. We will initialise and
declare the value in the program itself. Here the input is not taken from the user.

Subject In-charge
Program: -

Output: -

C:\Users\visha\OneDrive\Desktop\Java Prog>javac javaexp1.java

C:\Users\visha\OneDrive\Desktop\Java Prog>java SumOfNumbers


The sum of numbers is: 340
The sum of 225 and 115 is: 340
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 08

Experiment Title: - Write Java Program to Accept marks from user, if Marks

greater than 40, declare the student as “Pass” else “Fail””.

Date of Performance: Date of Submission:

********************************************************************
Theory: -
Java Scanner

Scanner class in Java is found in the java.util package. Java provides various ways to read
input from the keyboard, the java.util.Scanner class is one of them.

The Java Scanner class breaks the input into tokens using a delimiter which is whitespace
by default. It provides many methods to read and parse various primitive values.

The Java Scanner class is widely used to parse text for strings and primitive types using a
regular expression. It is the simplest way to get input in Java. By the help of Scanner in Java, we
can get input from the user in primitive types such as int, long, double, byte, float, short, etc.

The Java Scanner class extends Object class and implements Iterator and Closeable
interfaces.

4 The Java Scanner class provides nextXXX() methods to return the type of value such as
nextInt(), nextByte(), nextShort(), next(), nextLine(), nextDouble(), nextFloat(), nextBoolean(),
etc. To get a single character from the scanner, you can call next().charAt(0) method which
returns a single character.

Java Scanner Class Declaration


1. public final class Scanner
2. extends Object
3. implements Iterator<String>
How to get Java Scanner

To get the instance of Java Scanner which reads input from the user, we need to pass the input
stream (System.in) in the constructor of Scanner class. For Example:

1. Scanner in = new Scanner(System.in);

To get the instance of Java Scanner which parses the strings, we need to pass the strings in the
constructor of Scanner class. For Example:

1. Scanner in = new Scanner("Hello Javatpoint");

Java Scanner Class Constructors

SN Constructor Description

1) Scanner(File source) It constructs a new Scanner that produces


values scanned from the specified file.

2) Scanner(File source, String charsetName) It constructs a new Scanner that produces


values scanned from the specified file.

3) Scanner(InputStream source) It constructs a new Scanner that produces


values scanned from the specified input
stream.

4) Scanner(InputStream source, String It constructs a new Scanner that produces


charsetName) values scanned from the specified input
stream.

5) Scanner(Readable source) It constructs a new Scanner that produces


values scanned from the specified source.

6) Scanner(String source) It constructs a new Scanner that produces


values scanned from the specified string.

7) Scanner(ReadableByteChannel source) It constructs a new Scanner that produces


values scanned from the specified channel.

8) Scanner(ReadableByteChannel source, It constructs a new Scanner that produces


String charsetName) values scanned from the specified channel.

9) Scanner(Path source) It constructs a new Scanner that produces


values scanned from the specified file.

10) Scanner(Path source, String charsetName) It constructs a new Scanner that produces
values scanned from the specified file.
Java Scanner Class Methods

The following are the list of Scanner methods:

SN Modifier & Type Method Description

1) void close() It is used to close this scanner.

2) pattern delimiter() It is used to get the Pattern which the


Scanner class is currently using to match
delimiters.

3) Stream<MatchResult> findAll() It is used to find a stream of match results


that match the provided pattern string.

4) String findInLine() It is used to find the next occurrence of a


pattern constructed from the specified
string, ignoring delimiters.

5) string findWithinHorizon() It is used to find the next occurrence of a


pattern constructed from the specified
string, ignoring delimiters.

6) boolean hasNext() It returns true if this scanner has another


token in its input.

7) boolean hasNextBigDecimal() It is used to check if the next token in this


scanner's input can be interpreted as a
BigDecimal using the nextBigDecimal()
method or not.

8) boolean hasNextBigInteger() It is used to check if the next token in this


scanner's input can be interpreted as a
BigDecimal using the nextBigDecimal()
method or not.

9) boolean hasNextBoolean() It is used to check if the next token in this


scanner's input can be interpreted as a
Boolean using the nextBoolean() method
or not.

10) boolean hasNextByte() It is used to check if the next token in this


scanner's input can be interpreted as a
Byte using the nextBigDecimal() method
or not.

11) boolean hasNextDouble() It is used to check if the next token in this


scanner's input can be interpreted as a
BigDecimal using the nextByte() method
or not.
12) boolean hasNextFloat() It is used to check if the next token in this
scanner's input can be interpreted as a
Float using the nextFloat() method or not.

13) boolean hasNextInt() It is used to check if the next token in this


scanner's input can be interpreted as an
int using the nextInt() method or not.

14) boolean hasNextLine() It is used to check if there is another line


in the input of this scanner or not.

15) boolean hasNextLong() It is used to check if the next token in this


scanner's input can be interpreted as a
Long using the nextLong() method or not.

16) boolean hasNextShort() It is used to check if the next token in this


scanner's input can be interpreted as a
Short using the nextShort() method or
not.

17) IOException ioException() It is used to get the IOException last


thrown by this Scanner's readable.

18) Locale locale() It is used to get a Locale of the Scanner


class.

19) MatchResult match() It is used to get the match result of the


last scanning operation performed by this
scanner.

20) String next() It is used to get the next complete token


from the scanner which is in use.

21) BigDecimal nextBigDecimal() It scans the next token of the input as a


BigDecimal.

22) BigInteger nextBigInteger() It scans the next token of the input as a


BigInteger.

23) boolean nextBoolean() It scans the next token of the input into a
boolean value and returns that value.

24) byte nextByte() It scans the next token of the input as a


byte.

25) double nextDouble() It scans the next token of the input as a


double.

26) float nextFloat() It scans the next token of the input as a


float.

27) int nextInt() It scans the next token of the input as an


Int.
28) String nextLine() It is used to get the input string that was
skipped of the Scanner object.

29) long nextLong() It scans the next token of the input as a


long.

30) short nextShort() It scans the next token of the input as a


short.

31) int radix() It is used to get the default radix of the


Scanner use.

32) void remove() It is used when remove operation is not


supported by this implementation of
Iterator.

33) Scanner reset() It is used to reset the Scanner which is in


use.

34) Scanner skip() It skips input that matches the specified


pattern, ignoring delimiters

35) Stream<String> tokens() It is used to get a stream of delimiter-


separated tokens from the Scanner
object which is in use.

36) String toString() It is used to get the string representation


of Scanner using.

37) Scanner useDelimiter() It is used to set the delimiting pattern of


the Scanner which is in use to the
specified pattern.

38) Scanner useLocale() It is used to sets this scanner's locale


object to the specified locale.

39) Scanner useRadix() It is used to set the default radix of the


Scanner which is in use to the specified
radix.

Conclusion: -
Here we learn Scanner class to read marks entered from user. Also we declare
result about “Pass” or “Fail”.

Subject In-charge
Program: -

Output: -

C:\Users\visha\OneDrive\Desktop\Java Prog>javac javaexp2.java

C:\Users\visha\OneDrive\Desktop\Java Prog>java PassFail


Enter score:
73
Pass!

C:\Users\visha\OneDrive\Desktop\Java Prog>java PassFail


Enter score:
35
Fail!
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 09

Experiment Title: - Write Java Program to Accept 3 numbers from user.

Compare them and declare the largest number

(Using if-else statement).

Date of Performance: Date of Submission:

********************************************************************
Theory: -
Java If-else Statement
In Java if statement is used to test the condition. It checks boolean condition: true or false.
There are various types of if statement in Java.

o if statement
o if-else statement
o if-else-if ladder
o nested if statement

Java if Statement

The Java if statement tests the condition. It executes the if block if condition is true.

Syntax:

1. if(condition){
2. //code to be executed
3. }
Java if-else Statement

The Java if-else statement also tests the condition. It executes the if block if condition is
true otherwise else block is executed.

Syntax:

1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
Java if-else-if ladder Statement

The if-else-if ladder statement executes one condition from multiple statements.

Syntax:

1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }

Java Nested if statement

The nested if statement represents the if block within another if block. Here, the inner if
block condition executes only when outer if block condition is true.

Syntax:

1. if(condition){
2. //code to be executed
3. if(condition){
4. //code to be executed
5. }
6. }

Procedure: -
1. Three numbers stored in variables a, b and c respectively.
2. Then, to find the largest, the following conditions are checked using if else
statements

3. If a is greater or equals to b,
a. and if a is greater or equals to c, a is the greatest.
b. else, c is the greatest.
4. Else,

a. if b is greater or equals to both c, b is the greatest.


b. else, c is the greatest.

Conclusion: -
Here we learn to find the largest among three numbers using nested if..else
statement in Java.

Subject In-charge
Program: -

Output: -

C:\Users\visha\OneDrive\Desktop\Java Prog>javac javaexp3.java

C:\Users\visha\OneDrive\Desktop\Java Prog>java LargestNumberExample1


Enter the first number:
25
Enter the second number:
86
Enter the third number:
49
The largest number is: 86
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 10

Experiment Title: - Write Java Program to Display sum of first 10 even

numbers using do-while loop.

Date of Performance: Date of Submission:

********************************************************************
Theory: -
Java do-while Loop
The Java do-while loop is used to iterate a part of the program repeatedly, until the specified
condition is true. If the number of iteration is not fixed and you must have to execute the loop at
least once, it is recommended to use a do-while loop.

Java do-while loop is called an exit control loop. Therefore, unlike while loop and for loop, the
do-while check the condition at the end of loop body. The Java do-while loop is executed at least
once because condition is checked after loop body.

Syntax:

1. do{
2. //code to be executed / loop body
3. //update statement
4. }while (condition);

The different parts of do-while loop:tory of Java

1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed
and control goes to update expression. As soon as the condition becomes false, loop breaks
automatically.
Example:

i <=100

2. Update expression: Every time the loop body is executed, the this expression increments or
decrements loop variable.

Example:

i++;

Note: The do block is executed at least once, even if the condition is false.

Flowchart of do-while loop:

Conclusion: -
Here we learn to find the sum of first 10 even numbers using do-while loop in
Java.

Subject In-charge
Program: -

Output: -
C:\Users\visha\OneDrive\Desktop\Java Prog>javac javaexp4.java

C:\Users\visha\OneDrive\Desktop\Java Prog>java EvenSum


Sum of first 10 even numbers = 110
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 11

Experiment Title: - Write Java Program to Display Multiplication table of 15

using while loop.

Date of Performance: Date of Submission:

********************************************************************
Theory: -
Java while Loop
Java while loop is a control flow statement that allows code to be executed
repeatedly based on a given Boolean condition. The while loop can be thought of as a
repeating if statement. While loop in Java comes into use when we need to repeatedly
execute a block of statements. The while loop is considered as a repeating if
statement. If the number of iterations is not fixed, it is recommended to use the while
loop.
Syntax:
while (test_expression)

// statements

update_expression;

The various parts of the While loop are:


1. Test Expression: In this expression, we have to test the condition. If the condition
evaluates to true then we will execute the body of the loop and go to update
expression. Otherwise, we will exit from the while loop.

Example:
i <= 10

2. Update Expression: After executing the loop body, this expression


increments/decrements the loop variable by some value.

Example:
i++;

How Does a While loop execute?


1. Control falls into the while loop.
2. The flow jumps to Condition
3. Condition is tested.
• If Condition yields true, the flow goes into the Body.
• If Condition yields false, the flow goes outside the loop
4. The statements inside the body of the loop get executed.
5. Updation takes place.
6. Control flows back to Step 2.
7. The while loop has ended and the flow has gone outside.
Flowchart For while loop (Control Flow):

Conclusion: -
Here we learn to display Multiplication table of 15 using while loop in Java.

Subject In-charge
Program: -

Output: -
C:\Users\visha\OneDrive\Desktop\Java Prog>javac javaexp5.java

C:\Users\visha\OneDrive\Desktop\Java Prog>java MultiplicationTable

Multiplication table of 15 using while loop:


15 * 1 = 15
15 * 2 = 30
15 * 3 = 45
15 * 4 = 60
15 * 5 = 75
15 * 6 = 90
15 * 7 = 105
15 * 8 = 120
15 * 9 = 135
15 * 10 = 150
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
NAAC Accredited with Grade A
Department of Electronics and Telecommunication
A.Y. 2022-2023
Subject: Skill Lab: C++ and Java Programming [ECL304]

Class: S.E-EXTC Div: A

Student Name: ___________________________________________ Roll No:

Experiment No.: - 12

Experiment Title: - Write Java Program to Display the sum of elements of arrays.

Date of Performance: Date of Submission:

********************************************************************
Theory: -
Java Arrays

Java array is an object which contains elements of a similar data type. Additionally, the
elements of an array are stored in a contiguous memory location. It is a data structure where we
store similar elements. We can store only a fixed set of elements in a Java array.

Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.

Unlike C/C++, we can get the length of the array using the length member. In C/C++, we
need to use the sizeof operator.

In Java, array is an object of a dynamically generated class. Java array inherits the Object
class, and implements the Serializable as well as Cloneable interfaces. We can store primitive
values or objects in an array in Java. Like C/C++, we can also create single dimensional or
multidimensional arrays in Java.

Moreover, Java provides the feature of anonymous arrays which is not available in C/C++.
Advantages
o Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
o Random access: We can get any data located at an index position.

Disadvantages
o Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its
size at runtime. To solve this problem, collection framework is used in Java which grows
automatically.

Types of Array in java

There are two types of array.

o Single Dimensional Array


o Multidimensional Array

Single Dimensional Array in Java

Syntax to Declare an Array in Java

1. dataType[] arr; (or)


2. dataType []arr; (or)
3. dataType arr[];

Instantiation of an Array in Java

1. arrayRefVar=new datatype[size];

Declaration, Instantiation and Initialization of Java Array

We can declare, instantiate and initialize the java array together by:dempt

1. int a[]={33,3,4,5};//declaration, instantiation and initialization


For-each Loop for Java Array

We can also print the Java array using for-each loop. The Java for-each loop prints the
array elements one by one. It holds an array element in a variable, then executes the body of
the loop.

The syntax of the for-each loop is given below:

1. for(data_type variable:array){
2. //body of the loop
3. }

Conclusion: -
Here we learn to calculate sum of elements of array in Java.

Subject In-charge
Program: -

Output: -
C:\Users\visha\OneDrive\Desktop\Java Prog>javac javaexp6.java

C:\Users\visha\OneDrive\Desktop\Java Prog>java ArraySum


Enter 5 array element :
23 51 12 36 9
Sum of given array is = 131

You might also like