0% found this document useful (0 votes)
17 views2 pages

Java Programming Basics Lab Guide

This document provides instructions for completing several tasks involving basic Java programs. It begins by explaining how to download and install Java, then describes creating a simple "Hello World" program. It also explains some key Java concepts like classes, methods, and System.out.println. Finally, it lists several tasks to write additional programs that output name/details, sum of integers, difference of integers, product of integers, and division of integers.

Uploaded by

Ghulam Ali
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)
17 views2 pages

Java Programming Basics Lab Guide

This document provides instructions for completing several tasks involving basic Java programs. It begins by explaining how to download and install Java, then describes creating a simple "Hello World" program. It also explains some key Java concepts like classes, methods, and System.out.println. Finally, it lists several tasks to write additional programs that output name/details, sum of integers, difference of integers, product of integers, and division of integers.

Uploaded by

Ghulam Ali
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

Advanced Object Oriented Programming (Lab 1)

Download and Install the Java:

1. [Link]

Windows x64 150.56 MB jdk-16.0.1_windows-x64_bin.exe


Installer

Task 1: Create, compile and run the first java program to print a greeting message on
screen:

1. Open any simple editor like Notepad, Notepad++.


2. Write the following code (to print a simple welcome message on the screen) and save the file
with the name “[Link]”.

3. Open command prompt to compile and run the program using following commands:
a. Press Windows+R key cmd (press enter key)
b. Go to the bin directory inside java (cd C:\Program Files\Java\jdk-16.0.1\bin)
c. You may need to set path to java’s bin directory for example by writing (set
path=C:/Program Files/Java/jdk-16.0.1/bin)
d. Type (javac [Link]) to compile the program
e. Type (java welcome) to run the program 

Understanding the first java program

Let's see what is the meaning of class, public, static, void, main, String[],
[Link]().
class keyword is used to declare a class in java.
public keyword is an access modifier which represents visibility. It means it is visible to
all.
static is a keyword. If we declare any method as static, it is known as the static method.
The core advantage of the static method is that there is no need to create an object to
invoke the static method. The main method is executed by the JVM, so it doesn't require
to create an object to invoke the main method. So it saves memory.
void is the return type of the method. It means it doesn't return any value.
main represents the starting point of the program.
String[] args is used for command line argument. We will learn it later.
[Link]() is used to print statement. Here, System is a class, out is the object
of PrintStream class, println() is the method of PrintStream class. We will learn about
the internal working of [Link] statement later.

Task 2: Write a java program to display your name, roll number, class and department
on the screen.

Task 3: Write a java program to create 2 integers and display their sum on the screen.

Task 4: Write a java program to create 2 integers and display their difference on the
screen.

Task 5: Write a java program to create 2 integers and display their product on the
screen.

Task 6: Write a java program to create 2 integers and display their division on the
screen.

Common questions

Powered by AI

In Java, the 'void' keyword is used in method signatures to indicate that the method does not return a value. This is often used for methods whose primary purpose is to perform an action rather than calculate a value. The use of 'void' impacts method design by simplifying scenarios where a return value is not applicable or necessary, focusing on tasks like printing or modifying the state of an object .

In Java, integer arithmetic operations such as addition, subtraction, multiplication, and division can be performed using the respective operators '+', '-', '*', and '/'. Data type compatibility is crucial because operations between incompatible types can lead to compile-time errors or unintended behavior due to type conversion. Ensuring operands are of compatible types prevents such issues and maintains data integrity .

The 'static' keyword indicates that a method belongs to the class, not instances of the class. Static methods can be called without creating an object of the class, which is beneficial for memory management. This is because static methods are loaded once when the class is loaded, and they don't require the instantiation of objects, reducing overhead and memory usage, particularly relevant for the 'main' method since it is the entry point executed directly by the JVM .

To structure a Java class for encapsulating addition and subtraction logic, define a class, e.g., 'Calculator'. Inside, write methods for 'add(int a, int b)' and 'subtract(int a, int b)'. These methods will perform operations and return results. By encapsulating in a class, logic is modularized, promoting reusability across different applications. Additionally, ensure good design practices such as parameter validation and clear method documentation .

To write, compile, and execute a basic Java program: 1) Write the Java code in a simple text editor and save it with a '.java' extension. 2) Open the command prompt and navigate to the directory containing the Java compiler (javac). 3) Compile the program with 'javac filename.java'. 4) Run it with 'java filename'. Setting the PATH variable is significant as it allows the operating system to locate executable programs, like the javac compiler, without needing to navigate to their directory every time .

The 'public' keyword in a Java class declaration is an access modifier that specifies visibility. It means the class is accessible by any other class. Compared to other access modifiers, 'private' restricts access to within the class, 'protected' allows access to subclasses and classes in the same package, and the default (package-private) access allows access within the same package only .

'String[] args' in the main method is used to pass command-line arguments to the application. It provides flexibility in applications by allowing users to input data at runtime, making programs more dynamic and adaptable to different scenarios without requiring changes to the source code. This enables the execution of the same program with different inputs without modifying the code .

Writing a Java program to perform division involves handling edge cases such as division by zero, which throws an 'ArithmeticException'. Solutions include checking the divisor before performing the operation and providing specific instructions or results if it is zero. This can be done using an if-statement to validate the divisor, ensuring program stability and user-friendly error messages. Exception handling with try-catch blocks is another solution to manage runtime errors gracefully .

Internally, 'System.out.println()' in Java works through several components. 'System' is a class that provides access to system resources. 'out' is a static member of the 'System' class, representing the standard output, which is an instance of 'PrintStream'. The 'println()' method of 'PrintStream' is then invoked to print output to the console. This combination allows text to be sent to the standard output device, usually the console .

The 'main' method is considered the entry point of a Java program because the Java Virtual Machine (JVM) requires it to start execution. It follows a predefined signature: 'public static void main(String[] args)'. When a Java program is executed, the JVM looks for this method to begin running the program. The sequence following its invocation typically involves setting up the environment, initializing any static elements, and executing instructions sequentially or as dictated by program logic .

You might also like