Core Java Module 3 - Concepts and Solutions
1. Hello World Program
Concepts:
- Class: Blueprint for objects.
- Main Method: Entry point of Java programs.
- System.out.println(): Prints output to the console.
Solution:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Simple Calculator
Concepts:
- Arithmetic operations (+, -, *, /).
- User Input: Using Scanner.
- Control Flow: Using switch or if-else to determine operation.
Solution:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
System.out.print("Choose operation (+, -, *, /): ");
char op = sc.next().charAt(0);
double result;
switch (op) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num2 != 0 ? num1 / num2 : Double.NaN; break;
default: System.out.println("Invalid operator."); return;
}
System.out.println("Result: " + result);
}
}
3. Even or Odd Checker
Concepts:
- Modulus Operator (%): Checks divisibility.
- Conditional Statements: if-else for decision making.
Core Java Module 3 - Concepts and Solutions
Solution:
import java.util.Scanner;
public class EvenOddChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = sc.nextInt();
if (num % 2 == 0) {
System.out.println(num + " is Even");
} else {
System.out.println(num + " is Odd");
}
}
}
4. Leap Year Checker
Concepts:
- Leap year rules: divisible by 4 and not by 100 unless also divisible by 400.
- Nested if conditions.
Solution:
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(year + " is a Leap Year");
} else {
System.out.println(year + " is not a Leap Year");
}
}
}
5. Multiplication Table
Concepts:
- Looping with for loop to repeat actions.
- Multiplying with loop counter.
Solution:
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
Core Java Module 3 - Concepts and Solutions
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
for (int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}
}
}
6. Data Type Demonstration
Concepts:
- Primitive data types: int, float, double, char, boolean.
- Display values using println.
Solution:
public class DataTypesDemo {
public static void main(String[] args) {
int age = 25;
float height = 5.8f;
double weight = 70.5;
char grade = 'A';
boolean passed = true;
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Weight: " + weight);
System.out.println("Grade: " + grade);
System.out.println("Passed: " + passed);
}
}
7. Type Casting Example
Concepts:
- Widening (automatic) and narrowing (manual) conversions.
- Syntax for casting: (int) value.
Solution:
public class TypeCastingDemo {
public static void main(String[] args) {
double d = 9.78;
int i = (int) d;
int j = 10;
double newDouble = j;
System.out.println("Double to Int: " + i);
System.out.println("Int to Double: " + newDouble);
}
}
Core Java Module 3 - Concepts and Solutions
8. Operator Precedence
Concepts:
- Precedence of operators: *, / before +, -.
- Use of parentheses to control order.
Solution:
public class OperatorPrecedence {
public static void main(String[] args) {
int result1 = 10 + 5 * 2;
int result2 = (10 + 5) * 2;
System.out.println("10 + 5 * 2 = " + result1);
System.out.println("(10 + 5) * 2 = " + result2);
}
}