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

OOP LabManual 1(Week 1)

This laboratory manual for Object Oriented Programming (COSC-1202) outlines the objectives, software requirements, and basic syntax of Java. It includes instructions for installing necessary tools, compiling and running Java programs, and various tasks and questions to reinforce learning. Key concepts covered include the roles of JDK, JRE, and JVM, as well as Java's object-oriented features and syntax rules.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

OOP LabManual 1(Week 1)

This laboratory manual for Object Oriented Programming (COSC-1202) outlines the objectives, software requirements, and basic syntax of Java. It includes instructions for installing necessary tools, compiling and running Java programs, and various tasks and questions to reinforce learning. Key concepts covered include the roles of JDK, JRE, and JVM, as well as Java's object-oriented features and syntax rules.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Laboratory Manual

Object Oriented Programming-Lab


COSC-1202
BS-INFT 2A,2B,2C,2D
Spring Semester 2025

Roll No
Name
Class
Lab Instructor
JAVA INTRODUCTION
Lab-01

Object Oriented Programming-Lab [COSC-1202] 1


Lab Objectives:
Understand principles of object oriented paradigm
Contents / Sub Contents
▪ Installation required software: JDK / TextPad / Note Pad++ or any IDE
▪ Java Basic Syntax Practice
▪ First Program develop and run “Hello World”
Software Required:
• JDK(Java Development Kit)
Installation link
https://www.oracle.com/pk/java/technologies/downloads/
To check if java has been successfully installed in your system or not
type and run this command on cmd.
java --version
• Notepad++
Installation link
https://notepad-plus-plus.org/downloads/

Java is guaranteed to be Write Once, Run Anywhere.

Features of Java

• Object Oriented − In Java, everything is an Object. Java can be easily extended since
it is based on the Object model.
• Platform Independent − Unlike many other programming languages including C
and C++, when Java is compiled, it is not compiled into platform specific machine,
rather into platform independent byte code. This byte code is distributed over the web
and interpreted by the Java Virtual Machine (JVM) on whichever platform it is being
run on.

Object Oriented Programming-Lab [COSC-1202] 2


JDK, JRE, and JVM
JDK is an abbreviation for Java Development Kit which includes all the tools, executables,
and binaries required to compile, debug, and execute a Java Program.The Java Development
Kit (JDK) is a complete package used for developing Java applications. It includes the Java
Runtime Environment (JRE), which allows Java programs to run, and additional tools like
the Java compiler, debugger, and other development utilities. Developers need the JDK to
write, compile, and execute Java programs.

The Java Runtime Environment (JRE) is required to run Java applications but does not
include development tools. It consists of the Java Virtual Machine (JVM) and essential
libraries that Java programs need to function. The JRE is mainly for users who only want to
run Java applications without writing or compiling code.

The JVM (Java Virtual Machine) is a virtual machine, an abstract computer that has its
own ISA, memory, stack, heap, etc. It runs on the host OS and places its demands for
resources on it.The Java Virtual Machine (JVM) is the core component that executes Java
programs. It takes Java bytecode (compiled Java code) and converts it into machine code that
the computer understands. JVM is the core engine that enables Java's "Write Once, Run
Anywhere" feature The JVM is what makes Java platform-independent, allowing the same
Java program to run on different operating systems like Windows, macOS, and Linux. Every
Java application runs inside a JVM, ensuring security, memory management, and efficient
execution.

Basic Syntax:
Java is an object-oriented programming language which is known for its
simplicity, portability, and robustness. The syntax of Java programming
language is very closely aligned with C and C++ which makes it easier to
understand.
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.

Object Oriented Programming-Lab [COSC-1202] 3


• 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
• 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()

• Source File Name − Name of the source file should exactly match the class name.
When saving the file, you should save it using the class name (Remember Java is case
sensitive) and append '.java' to the end of the name (if the file name and the class name
do not match, your program will not compile).
But please make a note that in case you do not have a public class present in the file then
file name can be different than class name. It is also not mandatory to have a public
class in the file.
Example − Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved
as 'MyFirstJavaProgram.java'
• public static void main(String args[]) − Java program processing starts from the
main() method which is a mandatory part of every Java program.
• Comments in Java

There are three types of comments in Java.

i. Single line Comment

// System.out.println("This is an comment.");

ii. Multi-line Comment

/*
System.out.println("This is the first line comment.");
System.out.println("This is the second line comment.");
*/

iii. Documentation Comment. Also called a doc comment.

Object Oriented Programming-Lab [COSC-1202] 4


/** documentation */

TASK 1: Compile and Run the Following Program

STEPS FOR COMPILING AND EXECUTING THE JAVA PROGRAM


Let's look at how to save the file, compile, and run the program. Please follow the subsequent
steps −
• Open notepad and add the code as above.
• Save the file as: MyFirstJavaProgram.java.
• Open a command prompt window and go to the directory where you saved the class.
Assume it's C:\.
• Type javac MyFirstJavaProgram.java press enter to compile your code. If there are
no errors in your code, the command prompt will take you to the next line
(Assumption : The path variable is set).

• To actually run the program, you must use the Java application launcher called java.
To do so, pass the class name Example as a command-line argument. Now, type java
Example to run your program.

• You will be able to see This is a simple Java program printed on the window.

TASK 2: Compile and run the Following program.

Object Oriented Programming-Lab [COSC-1202] 5


/*
Here is another short example.
Call this file "Example2.java".
*/

Task 3: Write a Java program to print 'Hello' on screen and then print
your name on a separate line.(paste screenshot of code & output)
Task 4: Write a Java program to print the sum of two numbers. .(paste
screenshot of code & output)
Task 5: Write a Java program to display the following pattern. .(paste
screenshot of code & output)
Sample Pattern :
J a v v a
J aa v v aa
J J aaaaa V V aaaaa
J J a a V a a

Object Oriented Programming-Lab [COSC-1202] 6


Task 6: Write a Java program to check whether Java is installed on your
computer.

QUESTIONS
Question 1:
What is byte code and which command is responsible for its creation?
Question 2:
Which command is responsible for the compilation of the java code?
Question 3:
What will be the name and the type of the file which will be created if the
following command is executed javac MyClass.java?
Question 4:
What is the difference between print() and println() functions?
Question 5:
Difference between JDK, JRE, and JVM

The End..

Object Oriented Programming-Lab [COSC-1202] 7

You might also like