Java Programming Lab Assignment
Question 1: To understand the basic structure of a Java program and develop simple programs to
display output on the screen.
Answer:
The basic structure of a Java program consists of a class definition, the main method, and
statements inside the main method. The main method is the entry point of the program. A simple
Java program uses the [Link]() statement to display output on the screen.
Example Program:
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Question 2: To learn different methods of taking input in Java, including command-line arguments,
Scanner class, and keyboard-based I/O.
Answer:
Java provides multiple ways to take input from the user:
1. Command-Line Arguments: Input is passed while running the program using the command
line.
Example:
class CommandLineInput {
public static void main(String[] args) {
[Link](args[0]);
}
}
2. Scanner Class: The Scanner class is used to take input from the keyboard at runtime.
Example:
import [Link];
class ScannerInput {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num = [Link]();
[Link](num);
}
}
3. BufferedReader (Keyboard-based I/O): BufferedReader reads text from input efficiently.
Example:
import [Link].*;
class BufferedInput {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
String input = [Link]();
[Link](input);
}
}