0% found this document useful (0 votes)
14 views3 pages

Basic Java Programs for Beginners

The document contains multiple simple Java programs demonstrating basic programming concepts such as printing messages, handling command line arguments, converting temperature, swapping numbers, getting user input, performing arithmetic operations, and using conditional statements. Each program is self-contained and illustrates a specific functionality or concept in Java. These examples serve as a foundational guide for beginners learning Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Basic Java Programs for Beginners

The document contains multiple simple Java programs demonstrating basic programming concepts such as printing messages, handling command line arguments, converting temperature, swapping numbers, getting user input, performing arithmetic operations, and using conditional statements. Each program is self-contained and illustrates a specific functionality or concept in Java. These examples serve as a foundational guide for beginners learning Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Simple Java Program

public class SimpleProgram {


public static void main(String[] args) {
[Link]("Hello, Java!");
}
}

Print Integer in Java


public class PrintInteger {
public static void main(String[] args) {
int number = 25;
[Link]("The number is: " + number);
}
}

Command Line Arguments


public class CommandLine {
public static void main(String[] args) {
[Link]("Argument passed: " + args[0]);
}
}

Convert Fahrenheit to Celsius


import [Link];

public class FahrenheitToCelsius {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Fahrenheit: ");
float f = [Link]();
float c = (f - 32) * 5/9;
[Link]("Celsius: " + c);
}
}

Swap Two Numbers Using Variable


public class SwapWithTemp {
public static void main(String[] args) {
int a = 10, b = 20, temp;
temp = a;
a = b;
b = temp;
[Link]("a = " + a + ", b = " + b);
}
}

Swap Two Numbers Without Variable


public class SwapWithoutTemp {
public static void main(String[] args) {
int a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
[Link]("a = " + a + ", b = " + b);
}
}

Get Input Using Scanner


import [Link];

public class GetInput {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello, " + name);
}
}

Add Two Numbers


import [Link];

public class AddTwoNumbers {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
int sum = a + b;
[Link]("Sum: " + sum);
}
}

Find Largest of Two Numbers


import [Link];

public class LargestNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();

if (a > b) {
[Link]("Largest: " + a);
} else {
[Link]("Largest: " + b);
}
}
}

If-Else and Nested If


import [Link];

public class IfElseExample {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

if (num > 0) {
if (num % 2 == 0) {
[Link]("Positive and Even");
} else {
[Link]("Positive and Odd");
}
} else {
[Link]("Number is not positive");
}
}
}

Check Odd or Even


import [Link];

public class OddEvenCheck {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

if (num % 2 == 0) {
[Link]("Even number");
} else {
[Link]("Odd number");
}
}
}

Common questions

Powered by AI

The `Scanner` class in Java simplifies the process of reading input from various sources, such as user input via the command line. It provides flexibility for reading different data types, which enhances user interaction with the program, allowing dynamic data processing as demonstrated in examples like `GetInput`, `AddTwoNumbers`, and `FahrenheitToCelsius` .

The `OddEvenCheck` program effectively uses the modulus operator `%` within an `if` condition to distinguish odd and even numbers, showcasing a straightforward approach to control flow based on integer properties. This logic path minimizes complexity, though it requires an understanding of modular arithmetic .

Using a temporary variable simplifies the swapping process by providing an intermediate storage to hold the value of one variable while the values are exchanged, making it easy to understand and less prone to errors. In contrast, swapping without a temporary variable involves arithmetic operations that require understanding the sequence to ensure the variables are correctly swapped without data loss .

Reading different types of input consecutively can lead to input mismatches and buffering issues, such as not correctly consuming newline characters, which can disrupt the expected sequence. These can be resolved by correctly managing the input stream, explicitly consuming leftover newlines, and using specific `Scanner` methods for different data types as shown in several Java programs using `Scanner` .

Control flow structures like if-else and nested if allow programs to make decisions based on conditions, enabling dynamic response execution. For instance, the `IfElseExample` program uses nested if statements to differentiate between positive and negative numbers, and further classify even and odd for positive inputs, showcasing complex decision processes within a simple flow .

Swapping with a temporary variable is straightforward, easy to follow, and has a complexity of O(1). It is best suited for clear and concise code when readability is a priority. Swapping without a temporary variable, using arithmetic operations, achieves the same complexity but introduces risk of overflow and requires careful reasoning about the sequence of operations, making it more suited for memory-constrained environments where variable usage matters .

The conversion formula `(f - 32) * 5/9` accurately transforms Fahrenheit to Celsius, capturing the linear temperature relationship. Potential pitfalls include precision loss with floating-point division and ensuring the input is correctly parsed, as a misunderstanding here can lead to unexpected outputs or errors .

Command-line arguments enhance flexibility by allowing dynamic input at runtime, enabling easier program customization without source code modification. However, their limitations include manual setup in execution environments and potential errors in handling or interpreting arguments if not well-defined or documented .

Command-line arguments offer the flexibility to modify program behavior at runtime by accepting varied input without changing the code, making programs more adaptable and easier to test across different parameters. Static variables require explicit changes in the code for new values, making them less versatile and suitable for static environment setups .

Descriptive prompts guide users to provide the correct type of input, enhancing usability and reducing input errors. They improve the program's user interface by making interactions intuitive and predictable, as seen in the `GetInput` and `AddTwoNumbers` programs, where prompts clearly specify expected actions .

You might also like