[go: up one dir, main page]

0% found this document useful (0 votes)
22 views11 pages

OOP (LAB1) Solv

Uploaded by

muhammad shahbaz
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)
22 views11 pages

OOP (LAB1) Solv

Uploaded by

muhammad shahbaz
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/ 11

JAVA INTRODUCTION

Lab-01

LAB 01 Java Introduction


Lab Objectives:
1. Compile and Run Java Program
2. Understand Java syntax
Software Required:
JDK / Text Pad/ Note Pad
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
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()
Program File Name − Name of the program 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).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.

TASK 1: Compile and Run the Following Program

/*
This is a simple Java program.
Call this file "Example.java".
*/
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java 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 Example.java and 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.
/*
Here is another short example.
Call this file "Example2.java".
*/
class Example2 {

public static void main(String args []) {


int num; // this declares a variable called num
num = 100; // this assigns num the value 100
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.println(num);
}
}
Task 3: Write a Java program to print 'Hello' on screen and then print your
name on a separate line.

Task 4: Write a Java program to print the sum of two numbers.


Task 5: Write a Java program to display the following pattern.
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
Task 6: Write a Java program to check whether Java is installed on your
computer.
QUESTIONS
Please Fill the blank space with respective answers to following questions:
Question 1: What is byte code and which command is responsible for its
creation?
The Java compiler translates Java source code into bytecode, which is then
executed by the Java Virtual Machine (JVM).
Code:

javac YourJavaFile.java
Question 2: Which command is responsible for the compilation of the java
code?
The command responsible for compiling Java code is javac. The javac
command is part of the Java Development Kit (JDK) and is used to
translate Java source code.
Code:
javac YourJavaFile.java

Replace "YourJavaFile" with the actual name of your Java source


code file. This command will compile the Java source code and
generate a corresponding bytecode file (YourJavaFile.class).

After compilation, you can run the Java program using the java
command:

Code:

java YourJavaFile

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
If you execute the command:
Code:

javac MyClass.java

and the compilation is successful, it will create a bytecode file


named MyClass.class. The .class file is the compiled form of your Java
source code (MyClass.java). This file contains the bytecode that can
be executed by the Java Virtual Machine (JVM) when you run your
Java program.

After compilation, you can run the program using the java command:

Code:

java MyClass
Question 4: What will the following line of java code output?
System.out.print(“Pakistan is our country and we are are responsible for it”);
The provided Java code contains a string within double quotes and
is using System.out.print() to output that string. However, there is a
syntax error in the code due to the incorrect use of double quotes.
The quotes in the code snippet are formatted using curly double
quotes (“ ”) instead of straight double quotes (").

Here is the corrected version of the code:

Code:

System.out.print("Pakistan is our country and we are responsible


for it");

Now, when you run this corrected code, it will output the specified
string:

Code:

Pakistan is our country and we are responsible for it

Question 5: What is the difference between print() and println() functions?


print():
 The print() method is used to display text on the same line
without moving the cursor to the next line.
 It does not append a newline character at the end of the
output, so the next output will appear on the same line.
Example:
System.out.print("Hello, ");
System.out.print("world!");
Output:

Hello, world!
println():
 The println() method is used to display text and move the
cursor to the next line.
 It appends a newline character ( \n) at the end of the output, so
the next output will start on a new line.
Example:
System.out.println("Hello,");
System.out.println("world!");
Output:

Hello,
world!
THE END

You might also like