Answer :
Certainly! Let's tackle each Java programming task mentioned step by step:
1. Swap Two Variables in Java:
```java
public class SwapVariables {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swap: a = " + a + ", b = " + b);
// Swap logic
int temp = a;
a = b;
b = temp;
System.out.println("After swap: a = " + a + ", b = " + b);
}
}
```
2. Print a Face in Java:
```java
public class PrintFace {
public static void main(String[] args) {
System.out.println(" +\"\"\"\"\"+ ");
System.out.println("[| o o |]");
System.out.println(" | ^ | ");
System.out.println(" | '-' | ");
System.out.println(" +-----+ ");
}
}
```
3. Compare Three Numbers:
```java
import java.util.Scanner;
public class CompareNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter three numbers: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();
if (num1 >= num2 && num1 >= num3) {
System.out.println(num1 + " is the largest number.");
} else if (num2 >= num1 && num2 >= num3) {
System.out.println(num2 + " is the largest number.");
} else {
System.out.println(num3 + " is the largest number.");
}
}
}
```
4. Check Alphabet Using if-else:
```java
import java.util.Scanner;
public class CheckAlphabet {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char ch = scanner.next().charAt(0);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
System.out.println(ch + " is an alphabet.");
} else {
System.out.println(ch + " is not an alphabet.");
}
}
}
```
5. Check Even or Odd Number:
```java
import java.util.Scanner;
public class EvenOrOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
```
6. Find Factorial of a Number:
```java
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = scanner.nextInt();
long factorial = 1;
for (int i = 2; i <= number; i++) {
factorial = i;
}
System.out.println("Factorial of " + number + " is " + factorial);
}
}
```
7. Output for the PrintPattern Class:
Given a simple Java code snippet using loops to print a pattern of stars:
```
```
8. Check Vowel or Consonant Using if and switch:
```java
import java.util.Scanner;
public class VowelConsonant {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char ch = scanner.next().charAt(0);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
switch (Character.toLowerCase(ch)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println(ch + " is a vowel.");
break;
default:
System.out.println(ch + " is a consonant.");
}
} else {
System.out.println(ch + " is not an alphabet.");
}
}
}
```
9. Explanation of Java Program ommissions:
a. Omitting 'main': Without the `main` method, the program won’t run as it's the entry point for any Java application.
b. Omitting 'String': The `main` method parameter, `String[] args`, is essential for command-line arguments; removing `String` will lead to a compilation error.
c. Omitting 'HelloWorld': Removing the original class name will cause a compilation error since the file name and the class name must match.
d. Omitting 'System.out': `System.out` is the standard output stream in Java; omitting it will result in errors when trying to print to the console.
These tasks cover a range of basic Java programming skills. Let me know if you need help with anything else!
1. Swap Two Variables in Java:
```java
public class SwapVariables {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swap: a = " + a + ", b = " + b);
// Swap logic
int temp = a;
a = b;
b = temp;
System.out.println("After swap: a = " + a + ", b = " + b);
}
}
```
2. Print a Face in Java:
```java
public class PrintFace {
public static void main(String[] args) {
System.out.println(" +\"\"\"\"\"+ ");
System.out.println("[| o o |]");
System.out.println(" | ^ | ");
System.out.println(" | '-' | ");
System.out.println(" +-----+ ");
}
}
```
3. Compare Three Numbers:
```java
import java.util.Scanner;
public class CompareNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter three numbers: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();
if (num1 >= num2 && num1 >= num3) {
System.out.println(num1 + " is the largest number.");
} else if (num2 >= num1 && num2 >= num3) {
System.out.println(num2 + " is the largest number.");
} else {
System.out.println(num3 + " is the largest number.");
}
}
}
```
4. Check Alphabet Using if-else:
```java
import java.util.Scanner;
public class CheckAlphabet {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char ch = scanner.next().charAt(0);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
System.out.println(ch + " is an alphabet.");
} else {
System.out.println(ch + " is not an alphabet.");
}
}
}
```
5. Check Even or Odd Number:
```java
import java.util.Scanner;
public class EvenOrOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
```
6. Find Factorial of a Number:
```java
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = scanner.nextInt();
long factorial = 1;
for (int i = 2; i <= number; i++) {
factorial = i;
}
System.out.println("Factorial of " + number + " is " + factorial);
}
}
```
7. Output for the PrintPattern Class:
Given a simple Java code snippet using loops to print a pattern of stars:
```
```
8. Check Vowel or Consonant Using if and switch:
```java
import java.util.Scanner;
public class VowelConsonant {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char ch = scanner.next().charAt(0);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
switch (Character.toLowerCase(ch)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println(ch + " is a vowel.");
break;
default:
System.out.println(ch + " is a consonant.");
}
} else {
System.out.println(ch + " is not an alphabet.");
}
}
}
```
9. Explanation of Java Program ommissions:
a. Omitting 'main': Without the `main` method, the program won’t run as it's the entry point for any Java application.
b. Omitting 'String': The `main` method parameter, `String[] args`, is essential for command-line arguments; removing `String` will lead to a compilation error.
c. Omitting 'HelloWorld': Removing the original class name will cause a compilation error since the file name and the class name must match.
d. Omitting 'System.out': `System.out` is the standard output stream in Java; omitting it will result in errors when trying to print to the console.
These tasks cover a range of basic Java programming skills. Let me know if you need help with anything else!