DSA Lab 01 SPRING 2022 SE
DSA Lab 01 SPRING 2022 SE
Name : _________________________________________
Roll No. : _________________ Section : ________________
Date : _________________________________________
Remarks : _________________________________________
Signature : _________________________________________
March 30, 2022 [LAB 01 – INTRODUCTION TO JAVA ELEMENTARY PROGRAMMING]
Name: [Roll No]: [Section]
Overview
There are many programming languages—so why Java?
The answer is that Java enables users to develop and deploy applications on the Internet for servers, desktop
computers, and small hand-held devices. The future of computing is being profoundly influenced by the Internet, and
Java promises to remain a big part of that future. Java is the Internet programming language.
You are about to begin an exciting journey, learning a powerful programming language. At the outset, it is helpful to
review computer basics, programs, and operating systems and to become familiar with number systems. If you are
already familiar with such terms as CPU, memory, disks, operating systems, and programming languages,
API
The application program interface (API) contains predefined classes and interfaces for developing Java programs.
The Java language specification is stable, but the API is still expanding. At Google, you can view and download the
latest version of the Java API.
Java IDE
Besides JDK, you can use a Java development tool (e.g., Net-Beans, Eclipse, and Text Pad)—software that provides
an integrated development environment (IDE) for rapidly developing Java programs. Editing, compiling, building,
debugging, and online help are integrated in one graphical user interface. Just enter source code in one window or
open an existing file in a window, then click a button, menu item, or function key to compile and run the program.
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & Algorithm 1
March 30, 2022 [LAB 01 – INTRODUCTION TO JAVA ELEMENTARY PROGRAMMING]
Name: [Roll No]: [Section]
Example 1.1 Welcome.java
1 public class Welcome {
2 public static void main(String[] args) {
3 // Display message Welcome to Java! to the console
4 System.out.println("Welcome to Java!");
5 }
6 }
Output:
The line numbers are displayed for reference purposes but are not part of the program. So, don’t type line numbers in
your program.
Line 1 defines a class. Every Java program must have at least one class. Each class has a name. By convention, class
names start with an uppercase letter. In this example, the class name is Welcome.
Line 2 defines the main method. In order to run a class, the class must contain a method named main. The program is
executed from the main method. A method is a construct that contains statements. The main method in this program
contains the System.out.println statement. This statement prints a message "Welcome to Java!" to the console (line 4).
Every statement in Java ends with a semicolon (;), known as
The statement terminator. Reserved words, or keywords, have a specific meaning to the compiler and cannot be used
for other purposes in the program. For example, when the compiler sees the word class, it understands that the word
after class is the name for the class. Other reserved words in this
program are public, static, and void.
Line 3 is a comment that documents what the program is and how it is constructed. Comments help programmers to
communicate and understand the program. They are not programming statements and thus are ignored by the compiler.
In Java, comments are preceded by two slashes ( //) on a line, called a line comment, or enclosed between /* and */ on
one or several lines, called a block comment. When the compiler sees //, it ignores all text after // on the same line.
When it sees /*, it scans for the next */ and ignores any text between /* and */. Here are examples of comments:
// This application program prints Welcome to Java!
/* This application program
prints Welcome to Java! */
A pair of braces in a program forms a block that groups the program’s components. In Java, each block begins with
an opening brace ({) and ends with a closing brace (}). Every class has a class block that groups the data and methods
of the class. Every method has a method block that groups the statements in the method. Blocks can be nested, meaning
that one block can be placed within another, as shown in the following code.
Further, you can perform mathematical computations and display the result to the console. Example 1.2 gives an
10.5 + 2 × 3
example of evaluating .
45−3.5
Output:
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & Algorithm 2
March 30, 2022 [LAB 01 – INTRODUCTION TO JAVA ELEMENTARY PROGRAMMING]
Name: [Roll No]: [Section]
FIGURE 1.1The Java program-development process consists of repeatedly creating/modifying source code,
compiling, and executing programs.
Editor
You can use any text editor or IDE to create and edit a Java source-code file. This section demonstrates how to create,
compile, and run Java programs from a command window. If you wish to use an IDE such as Eclipse, NetBeans, or
TextPad, please refer to Supplement II for tutorials. From the command window, you can use the NotePad to create
the Java source code file, as shown in Figure 1.2.
FIGURE 1.2You can create the Java source file using Windows Note Pad.
Compile
javac Welcome.java
FIGURE 1.3(a) Java source code is translated into bytecode. (b) Java bytecode can be executed on any computer with
a Java Virtual Machine.
Interpreting bytecode
To execute a Java program is to run the program’s bytecode. You can execute the bytecode on any platform with a
JVM. Java bytecode is interpreted. Interpreting translates the individual steps in the bytecode into the target machine-
language code one at a time rather than translating the whole program as a single unit. Each step is executed
immediately after it is translated.
The following command runs the bytecode:
java Welcome
Figure 1.4 shows the javac command for compiling Welcome.java. The compiler generated the Welcome.class file.
This file is executed using the java command.
FIGURE 1.4 The output of Example 1.1 displays the message “Welcome to Java!”
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & Algorithm 4
March 30, 2022 [LAB 01 – INTRODUCTION TO JAVA ELEMENTARY PROGRAMMING]
Name: [Roll No]: [Section]
\n Newline
\t Tab
\b Backspace
\f form feed
\r Return
\" " (double quote)
\' ' (single quote)
\\ \ (back slash)
\uDDDD character from the Unicode character set (DDDD is four hex digits)
The syntax new Scanner(System.in) creates an object of the Scanner type. The syntax Scanner input declares that
input is a variable whose type is Scanner. The whole line Scanner input = new Scanner(System.in) creates a
Scanner object and assigns its reference to the variable input. An object may invoke its methods. To invoke a method
on an object is to ask the object to perform a task. You can invoke the methods in Table 2.1 to read various types of
input.
import class
create a Scanner
read a double
For now, we will see how to read a number that includes a decimal point by invoking the nextDouble() method.
Other methods will be covered when they are used.
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & Algorithm 5
March 30, 2022 [LAB 01 – INTRODUCTION TO JAVA ELEMENTARY PROGRAMMING]
Name: [Roll No]: [Section]
Example1.3 ComputeAreaWithConsoleInput.java
Output:
The Scanner class is in the java.util package. It is imported in line 1. Line 6 creates a Scanner object. The statement
in line 9 displays a message to prompt the user for input.
System.out. ("Enter a number for radius: ");
The print method is identical to the println method except that println moves the cursor to the next line after
displaying the string, but print does not advance the cursor to the next line when completed. The statement in line 10
reads an input from the keyboard.
double radius = input.nextDouble();
After the user enters a number and presses the Enter key, the number is read and assigned to radius.
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & Algorithm 6
March 30, 2022 [LAB 01 – INTRODUCTION TO JAVA ELEMENTARY PROGRAMMING]
Name: [Roll No]: [Section]
Programming Exercise
1. Write an application that asks the user to enter two integers, obtains them from the user and prints
their sum, product, difference and quotient (division).
2. Write a program to calculate your daily fuel consumption. You can add parameters
accordingly.
3. Write a program that can collect the system time from your computer. Use this program and
execute above two programs again to find the total execution time, by difference two time
tags
Submission Details:
Section A
Submission mode: Online
Due Date: March 31, 2022
Via MSTEAM Lab Manual 01
Lab 1 CS211 [Rollno] [Name]
Softcopy, java file and MS word file.
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & Algorithm 7