Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Lab Report 1 - Understanding Java Code Execution and User Interaction 1

Lab Report 1 _ Understanding Java Code Execution and User Interaction 1

Uploaded by

samirgolder2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab Report 1 - Understanding Java Code Execution and User Interaction 1

Lab Report 1 _ Understanding Java Code Execution and User Interaction 1

Uploaded by

samirgolder2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Dhaka International

University
Dept of CSE

Lab Report: Understanding Java Code Execution and


User Interaction (01)

Course Name : Object-Oriented Programming Language


Course Number : 0613-201

Submitted By :
Sudip Mandal
Batch D-85
Roll 05
HackerRank Profile : https://www.hackerrank.com/profile/sudip7981

Submitted To :
Majharul Hasan Lecturer,
Dept of Computer Science & Engineering
Dhaka International University
Introduction
In this lab, we explored the fundamental concepts of Java programming, including how Java
code is compiled and executed. We also practiced basic input and output operations in Java,
which are crucial for developing interactive programs.

Objectives
• Understand Java Code Execution: Learn how Java code is compiled and run.
• Practice Basic Input/Output Operations: Implement code to read from and write to the
console.

Lab Exercises
1. Java Code Execution

Objective: Understand the compilation and execution process of


Java code.

Discussion:

Java code undergoes a specific process to be executed on a computer. Here's a detailed look at
how Java code is compiled and run:

• Writing Java Code:


• Java code is written in a .java file using a text editor or an Integrated
Development Environment (IDE). This code is human-readable and includes
classes and methods.
• Compilation:
• Java Compiler (javac): The Java compiler (javac) is used to translate the
source code into bytecode. Bytecode is an intermediate form of code that the
Java Virtual Machine (JVM) can understand.
• Command: To compile a Java program, the javac command is
used. For example code

javac HelloWorld.java

• Output: This command produces a .class file, which contains the bytecode. For
instance, compiling HelloWorld.java results in HelloWorld.class.
• Bytecode:
• Intermediate Code: Bytecode is not tied to any specific hardware. It is designed
to be executed by the JVM, making Java code platform-independent.
• Execution:
• Java Virtual Machine (JVM): The JVM executes the bytecode. It interprets or
compiles the bytecode into machine code specific to the host system's hardware.
• Command: To run the compiled program, use the java command followed by the
class name (without the .class extension). For example:
code
java HelloWorld

• Output: The JVM runs the bytecode, producing the output as defined by the
program. For example, it prints "Hello, World!" to the console.

Results: The process demonstrated how Java code is transformed from


source code to executable form, showing the role of the JVM in making
Java programs platform-independent.

2. Basic Input/Output Operations

Objective: Learn how to handle user input and output in Java.

Procedure:

• Reading User Input:


• We used the Scanner class to read input from the user.
• java
Copy code
import java.util.Scanner;

• public class UserInput {
• public static void main(String[] args) {
• Scanner scanner = new Scanner(System.in);
• System.out.print("Enter your name: ");
• String name = scanner.nextLine();
• System.out.println("Hello, " + name + "!");
• scanner.close();
• }
• }


• Explanation: The Scanner class provides methods to read different types of
input. In this case, nextLine() reads a full line of text entered by the user.
• Outputting Data:
• We used System.out.println() to print messages to the console.

Results: The program successfully read user input and displayed a


personalized message.

Conclusion
This lab provided a foundational understanding of how Java code is compiled and executed, as
well as basic input/output operations. We learned to write simple Java programs, compile them,
and run them to interact with the user through the console.
Future Work
• Explore more complex data types and structures in Java.
• Implement error handling for user input.
• Develop more interactive programs using advanced Java features.

You might also like