0% found this document useful (0 votes)
44 views8 pages

DSA Lab 01 SPRING 2022 SE

The document provides an introduction to Java elementary programming including an overview of the Java language specification, API, JDK, IDE, and how to create, compile and execute a simple Java program. It also demonstrates how to write a Java program that performs basic math calculations and outputs the result.

Uploaded by

Mehmood Sheikh
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)
44 views8 pages

DSA Lab 01 SPRING 2022 SE

The document provides an introduction to Java elementary programming including an overview of the Java language specification, API, JDK, IDE, and how to create, compile and execute a simple Java program. It also demonstrates how to write a Java program that performs basic math calculations and outputs the result.

Uploaded by

Mehmood Sheikh
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/ 8

USMAN INSTITUTE OF TECHNOLOGY

CS211 DATA STRUCTURES & ALGORITHMS


LAB 01
SPRING 2022

Name : _________________________________________
Roll No. : _________________ Section : ________________
Date : _________________________________________
Remarks : _________________________________________
Signature : _________________________________________
March 30, 2022 [LAB 01 – INTRODUCTION TO JAVA ELEMENTARY PROGRAMMING]
Name: [Roll No]: [Section]

Introduction to Java Elementary Programming


To l e a r n a bo u t J A V A E l e m e n t a r y p r o gr a mm in g, Un de r st a n d in g t he I D E , AP K J L S
a n d JD K w it h t he e xe cu t ion cy cle of Ja va a s hi gh le vel la n gu a ge .

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,

The Java Language Specification, API, JDK, and IDE


Computer languages have strict rules of usage. If you do not follow the rules when writing a program, the computer
will be unable to understand it. The Java language specification and Java API define the Java standard.

Java language specification


The Java language specification is a technical definition of the language that includes the syntax and semantics of the
Java programming language. The complete Java language specification can be found at Google .

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 SE, EE, and ME & JDK


Java is a full-fledged and powerful language that can be used in many ways. It comes in three editions: Java Standard
Edition (Java SE), Java Enterprise Edition (Java EE), and Java Micro Edition (Java ME). Java SE can be used to
develop client-side standalone applications or applets. Java EE can be used to develop server-side applications, such
as Java servlets and JavaServer Pages. Java ME can be used to develop applications for mobile devices, such as cell
phones. This course uses Java SE to introduce Java programming.
There are many versions of Java SE. The latest, Java SE 6, will be used for this course. It releases each version with
a Java Development Toolkit (JDK). For Java SE 6, the Java Development Toolkit is called JDK 1.6 (also known as
Java 6 or JDK 6). JDK consists of a set of separate programs, each invoked from a command line, for developing and
testing Java programs.

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.

A Simple Java Program


Let us begin with a simple Java program that displays the message “Welcome to Java!” on the console. Console refers
to text entry and display device of a computer. The program is shown in example 1.1.

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

Example 1.2 ComputeExpression.java

1 public class ComputeExpression {


2 public static void main(String[] args) {
3 System.out.println((10.5 + 2 * 3) / (45 – 3.5));
4 }
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]

Creating, Compiling, and Executing a Java Program


You have to create your program and compile it before it can be executed. This process is repetitive, as shown in
Figure 1.1. If your program has compilation errors, you have to modify the program to fix them, then recompile it. If
your program has runtime errors or does not produce the correct result, you have to modify the program, recompile it,
and execute it again.

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

.class bytecode file


If there are no syntax errors, the compiler generates a bytecode file with a .class extension. So the preceding command
generates a file named Welcome. class, as shown in Figure 1.3(a). The Java language is a high-level language while
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & Algorithm 3
March 30, 2022 [LAB 01 – INTRODUCTION TO JAVA ELEMENTARY PROGRAMMING]
Name: [Roll No]: [Section]
Java bytecode is a low-level language. The bytecode is similar to machine instructions but is architecture neutral and
can run on any platform that has a Java Virtual Machine (JVM), as shown in Figure 1.3 (b). Rather than a physical
machine, the virtual machine is a program that interprets Java bytecode. This is one of Java’s primary advantages:
Java bytecode can run on a variety of hardware platforms and operating systems.

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]

Java Escape Sequences


Escape
Character
Sequence

\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)

Reading Input from the Console


You can use the Scanner class for console input. Java uses System.outto refer to the standard output device and
System.in to the standard input device. By default the output device is the display monitor, and the input device is the
keyboard. To perform console output, you simply use the println method to display a primitive value or a string to
the console. Console input is not directly supported in Java, but you can use the Scanner class to create an object to
read input from System.in, as follows:

Scanner input = new Scanner(System.in);

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

TABLE 1.1 Methods for Scanner Objects


Method Description
nextByte() reads an integer of the byte type.
nextShort() reads an integer of the short type.
nextInt() reads an integer of the inttype.
nextLong() reads an integer of the long type.
nextFloat() reads a number of the float type.
nextDouble() reads a number of the double type.
next() reads a string that ends before a whitespace character.
nextLine() reads a line of text (i.e., a string ending with the Enter key pressed).

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]

Answer the following questions:


1. What is a source program? What is a compiler?
2. What is the JVM?
3. What are the input and output of a Java compiler?
4. List some Java development tools. Are tools like NetBeans and Eclipse different languages
from Java, or are they dialects or extensions of Java?
5. Is Java case sensitive? What is the case for Java keywords?
6. What is the Java source filename extension, and what is the Java bytecode filename extension?
7. What is the statement to display a string on the console?
8. The following program is wrong. Reorder the lines so that the program displays morning followed
by
afternoon.
public static void main(String[] args) {
}
public class Welcome {
System.out.println("aftern
oon");
System.out.println("morning");
}
9. Identify and fix the errors in the following code:
public class Welcome {
public void Main(String[] args) {
System.out.println('Welcome to
Java!);
}
)
10. Show the output of the following
code:
public class Test
{
public static void main(String[] args) {
System.out.println("3.5 * 4 / 2 –
2.5 is ");
System.out.println(3.5 * 4 / 2 – 2.5);
}
}

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

You might also like