Oops Lab 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Osama Shabbir 22SE20

Lab Session 01
Basic Programming
Objective: Understanding basic programming concept, program execution and
compilation process in java.
Theory: Program Compilation and Execution Process in Java
• You have to create your program and compile it before it can be executed.
• Any text editor or IDE that can be used to create and edit a Java source-code file,
source file is officially called a compilation unit. This process is repetitive, as
shown in figure below.

• If the program has compile errors, it needs to be modify to fix them, then it needs
to recompiled.
• If program has runtime errors or does not produce the correct result, we have to
modify the program, recompile it, and execute it again.
• The Java language is a high-level language, but 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).
• The Virtual Machine is a program that interprets Java bytecode.
• Java bytecode can run on a variety of hardware platforms and operating systems.
• Java source code is compiled into Java bytecode and Java bytecode is
interpreted by the JVM.

SOFTWARE ENGINIEERING
Osama Shabbir 22SE20

• To execute a Java program is to run the program’s bytecode. We can execute


the bytecode on any platform with a JVM, which is an interpreter.
• It translates the individual instructions in the bytecode into the target machine
language code one at a time rather than the whole program as a single unit.
• Each step is executed immediately after it is translated.
Implémentions of a simple java program
public class Class22 {

public static void main(String[] args) {


System.out.println("Hello World!");
}
}

After execution the program produces the following output :

Exercise

Lab task. 01
Write a Simple java program that displays your name, age, class roll number,
technology and email address on the screen.

SOFTWARE ENGINIEERING
Osama Shabbir 22SE20

Program:
public class Main {
public static void main(String[] args) {
System.out.println("Name: Osama Shabbir");
System.out.println("Age : 20");
System.out.println("Roll NO : 22SE20");
System.out.print("Department : Software Engineering");
System.out.print("Email Adress : [email protected]");
}
}
Output:

Lab Task. 02
Write a java program that can add, subtract, multiply and divide 2 numbers and
display the output on the screen.
Program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter first Number:");
int NUM1 = in.nextInt();
System.out.println("Enter 2nd Number:");
int NUM2= in.nextInt();
System.out.println(NUM1 +"+"+NUM2+"="+ (NUM1 + NUM2));
System.out.println(NUM1 +"-"+NUM2+"="+ (NUM1 - NUM2));
System.out.println(NUM1 +"x"+NUM2+"="+ (NUM1 * NUM2));
System.out.println(NUM1 +"/"+NUM2+"="+ (NUM1 % NUM2));

SOFTWARE ENGINIEERING
Osama Shabbir 22SE20

}
}
Output:

Lab Task. 03
Write a java program in which use increment and decrement operators. Initialize an
integer type variable with a positive value. Using increment and decrement operator
your program should increase the value of integer by 5 and display it on the screen
and then decrease the integer value by 3 and display the final answer on the screen
using a standard output statement.
Program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a = 10;
System.out.println("After incrementing by 5: "+(a + 5));
System.out.println("After decrementing by 3: " +(a - 3));
}
}

Output:

Lab Task. 04
Write a java program that can determines the value of the following expressions
relational expressions, assuming a = 5, b = 2, c = 4, d = 6, and e = 3. Your
program should display the output on the screen.
• a>b
• a != b
• d % b == c % b
• a * c != d * b

SOFTWARE ENGINIEERING
Osama Shabbir 22SE20

• d * b == c * e
Program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a = 5, b = 2, c = 4, d = 6, e = 3;
if (a > b) {
System.out.println(" a>b ");
}
if (a != b) {
System.out.println("a!=b");
}
if (d % b == c % d) {
System.out.println("d % b == c % d");
}
if(a * c != d * b){
System.out.println("a * C != d * b");
}
if( d * b == c * e){
System.out.println("d * b == c * e");
}
}
}
Output

SOFTWARE ENGINIEERING

You might also like