Core Java Module 3 - Concepts and Solutions
1. Hello World Program
Concepts:
- Class: Blueprint for objects.
- Main Method: Entry point of Java programs.
- [Link](): Prints output to the console.
Solution:
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
2. Simple Calculator
Concepts:
- Arithmetic operations (+, -, *, /).
- User Input: Using Scanner.
- Control Flow: Using switch or if-else to determine operation.
Solution:
import [Link];
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
double num1 = [Link]();
[Link]("Enter second number: ");
double num2 = [Link]();
[Link]("Choose operation (+, -, *, /): ");
char op = [Link]().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 : [Link]; break;
default: [Link]("Invalid operator."); return;
}
[Link]("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 [Link];
public class EvenOddChecker {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter an integer: ");
int num = [Link]();
if (num % 2 == 0) {
[Link](num + " is Even");
} else {
[Link](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 [Link];
public class LeapYearChecker {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
[Link](year + " is a Leap Year");
} else {
[Link](year + " is not a Leap Year");
}
}
}
5. Multiplication Table
Concepts:
- Looping with for loop to repeat actions.
- Multiplying with loop counter.
Solution:
import [Link];
public class MultiplicationTable {
public static void main(String[] args) {
Core Java Module 3 - Concepts and Solutions
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
for (int i = 1; i <= 10; i++) {
[Link](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;
[Link]("Age: " + age);
[Link]("Height: " + height);
[Link]("Weight: " + weight);
[Link]("Grade: " + grade);
[Link]("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;
[Link]("Double to Int: " + i);
[Link]("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;
[Link]("10 + 5 * 2 = " + result1);
[Link]("(10 + 5) * 2 = " + result2);
}
}