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

LAB 01

Uploaded by

dr.sharafhussain
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)
16 views

LAB 01

Uploaded by

dr.sharafhussain
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/ 18

Computer Science, Iqra University, Gulshan Campus,

Karachi

LAB 01: Introducing Programming


Language, Introducing Java
Programming Language.
Computer Science, Iqra University, Gulshan Campus,
Karachi

Course Title: Programming Fundamentals (LAB)


Course Code: 032410104
Introducing Java Programming Language.
Computer Science, Iqra University, Gulshan Campus,
Karachi

Title: Programming Fundamentals Lab


Code: CSC-131
Lab 01 – Introducing Java Programming Language

Instructor: Abdullah Bin Tahir


E-mail: [email protected]

Objective
By the end of this lecture, students will be able to:

1. Understand the concept and importance of programming languages and their role in
problem-solving.
2. Define and create basic flowcharts and algorithms to represent logical problem-solving
steps.
3. Identify the key features of Java, including Java tokens, data types, and the structure of a
Java program.
4. Demonstrate basic input and output operations in Java using IO streams.
5. Write simple Java programs with a clear understanding of its structure and syntax

Instructions
You are required to complete the lab tasks outlined below and submit your work by the given
deadline. If you encounter any issues during the lab, please raise your hand for assistance. Use
of ChatGPT or any other AI tool is strictly prohibited. If any such usage is detected, marks
will be deducted.

How to Submit
• Submit lab work in a single .txt file on LMS. (No other format will be accepted)
• Lab work file name should be saved with your roll number (e.g. 22542_lab01.txt)
Computer Science, Iqra University, Gulshan Campus,
Karachi

1.1 What is Java?

Java is one of the most popular object-oriented programming (OOP) languages. It allows
developers to create a wide range of applications that can run on different types of hardware and
operating systems. Java is also a platform that provides an environment for running these
applications.

Initially, Java was designed to solve small-scale problems, but it quickly proved useful for
handling larger issues, especially over the internet. This led to its rapid rise in popularity, and
soon, millions of programmers worldwide started using it.

● Java Platform, Standard Edition (Java SE) - for building desktop applications.
● Java Platform, Enterprise Edition (Java EE) - for building server-side applications.
● Java Platform, Micro Edition (Java ME) - for building mobile or handheld
applications.

1.2 Advantages of Java:

 Platform Independence: Java programs can run on any device or operating


system that supports the Java Virtual Machine (JVM). This "write once, run
anywhere" capability makes Java highly versatile.
 Object-Oriented Programming (OOP): Java's OOP approach helps developers
organize code into reusable classes and objects, making it easier to develop,
maintain, and scale complex applications.
 Large Community and Libraries: Java has a huge global community and a
wide range of libraries and frameworks. This makes it easier to find solutions,
resources, and support while building applications.
Computer Science, Iqra University, Gulshan Campus,
Karachi

1.3 Downloading and Installing NetBeans


To download the NetBeans installer on the local system, perform the following steps:
1.3.1Download JDK

1. Go to the official Oracle JDK download page:


https://www.oracle.com/java/technologies/javase-jdk-downloads.html.
2. Select the latest JDK version for your operating system (Windows, macOS, Linux).
3. Download the installer by clicking the appropriate link for your OS.

1.3.2 Download Apache NetBeans


1. Access https://netbeans.apache.org/download/index.html and click download on latest
edition then you will be directed to
https://netbeans.apache.org/download/nb123/nb123.html select option according to your
platform this will results in downloading of exe file.
2. Click on exe file this will results in installing of NetBeans. Select next option on the
window that will appear once installation will start
Computer Science, Iqra University, Gulshan Campus,
Karachi

3. Accept terms in the license agreement and then click on next button

4. Then a window will appear that will ask you to set a location for NetBeans.
Computer Science, Iqra University, Gulshan Campus,
Karachi

5. Then select install on the window that appears right after destination selection.
Computer Science, Iqra University, Gulshan Campus,
Karachi
6. After that installation will start

7. Once it's completed a window will be displayed that asks you to select finish
Computer Science, Iqra University, Gulshan Campus,
Karachi
Computer Science, Iqra University, Gulshan Campus,
Karachi
1.4 Algorithm:
An algorithm is a finite set of well-defined, step-by-step instructions used to solve a specific
problem or perform a computation. Algorithms are essential in programming as they define the
logic for solving problems before translating them into code.
1.4.1 Characteristics of a Good Algorithm:

1. Clear and Unambiguous: Each step of the algorithm must be clear and lead to only one
meaning.
2. Well-defined Inputs and Outputs: The algorithm should clearly specify input and
produce output.
3. Finiteness: The algorithm must terminate after a finite number of steps.
4. Feasibility: The steps of the algorithm must be simple enough to be executed.
5. Independence: The algorithm should not depend on any specific programming language.

1.4.2 Example of an Algorithm:

Problem: Write an algorithm to find the largest of three numbers.

Algorithm:

1. Start.
2. Take three numbers as input, say A, B, and C.
3. Compare A with B.
o If A > B, go to step 4.
o Otherwise, go to step 5.
4. Compare A with C.
o If A > C, print A is the largest.
o Otherwise, print C is the largest.
5. Compare B with C.
o If B > C, print B is the largest.
o Otherwise, print C is the largest.
6. Stop.
Computer Science, Iqra University, Gulshan Campus,
Karachi

1.5 Flowchart:

A flowchart is a graphical representation of an algorithm or a process, using various symbols to


denote different types of actions or steps. Flowcharts are used to visualize the flow of control in a
system, making it easier to understand the process and its logical structure.

1.5.1 Common Flowchart Symbols:

 Oval: Represents the start or end of a process.


 Parallelogram: Denotes input or output operations.
 Rectangle: Represents a process or operation (e.g., calculations or tasks).
 Diamond: Used for decision-making steps where the flow can branch based on a condition.
 Arrows: Indicate the flow of the process from one step to another.

1.5.2 Example:
Computer Science, Iqra University, Gulshan Campus,
Karachi

1.6 Java First Program:


In the code below we are writing our first program that is printing “Hello World”
Code:

Output:

1.7 Basic Syntax about Java programs:


It is very important to keep in mind the following points:
● Case Sensitivity - Java is case sensitive, which means identifier Hello and hello would
have different meaning in Java.

● Class Names - For all class names the first letter should be in Upper Case. If several
words are used to form a name of the class, each inner word's first letter should be in
Upper Case.
Example: class MyFirstJavaClass
Computer Science, Iqra University, Gulshan Campus,
Karachi

● Method Names - All method names should start with a Lower Case letter. If several
words are used to form the name of the method, then each inner word's first letter should
be in Upper Case.
Example: public void myMethodName().
● public static void main(String args[]) - Java program processing starts from the main()
method which is a mandatory part of every Java program.

1.6 Java Comments:


Comments can be used to explain Java code, and to make it more readable. It can also be used to
prevent execution when testing alternative code.

1. Single-line comments start with two forward slashes (//).


2. Multi-line comments start with /* and ends with */.

Code:

Output:

1.8 Input-Output in Java:


1. System.in: This is the standard input stream that is used to read characters from the
keyboard or any other standard input device.
Computer Science, Iqra University, Gulshan Campus,
Karachi
2. System.out: This is the standard output stream that is used to produce the result of a
program on an output device like the computer screen.

1.9 Java Output:


In Java, you can simply use the below two statements for output.

System.out.println(); or System.out.print();

● print() - It prints string inside the quotes.


● println() - It prints string inside the quotes similar like print() method. Then the cursor
moves to the beginning of the next line.

Code:

Output:

1.10 Java Data Types:


Data types specify the different sizes and values that can be stored in the variable. There are two
types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
Computer Science, Iqra University, Gulshan Campus,
Karachi
2. Non-primitive data types: The non-primitive data types include Classes, String,
and Arrays. (Discuss this data type later when we will cover Class topic.)

There are 8 types of primitive data types:

● boolean
● byte
● char
● short
● int
● long
● float
● double
Computer Science, Iqra University, Gulshan Campus,
Karachi

Example Program:
Computer Science, Iqra University, Gulshan Campus,
Karachi

Output:

1.10 Escape Sequence in Java:

Escape Sequence Description Example


\\ Backslash System.out.println("A backslash: \\");
\' Single quote System.out.println("Single quote: \'");
\" Double quote System.out.println("Double quote: \"");
\n New line System.out.println("First line\nSecond line");
\t Tab System.out.println("Column1\tColumn2");
\b Backspace System.out.println("Hi\b World");
\r Carriage return System.out.println("Hello\rWorld");
\f Form feed System.out.println("Hello\fWorld");

Lab Task
Computer Science, Iqra University, Gulshan Campus,
Karachi
Question No 1: Write a Java Program to display “Hello World” and your Student ID.

Question No 2: Write a java program to declare two Integers, Doubles and Characters.

Question No 3: Write a Java program to initialize two Integers, Doubles and Characters and
display them.

Question No: 4 Write a Java program that demonstrates the use of the following escape sequences:

 \\ (Backslash)
 \' (Single quote)
 \" (Double quote)
 \n (New line)
 \t (Tab)
 \b (Backspace)
 \r (Carriage return)
 \f (Form feed)

Question No: 5 Write a Java language program to produce the following output.

You might also like