Core Java Module3 Concepts Solutions
Core Java Module3 Concepts Solutions
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;
Concepts:
- Modulus Operator (%): Checks divisibility.
- Conditional Statements: if-else for decision making.
Core Java Module 3 - Concepts and Solutions
Solution:
import java.util.Scanner;
Concepts:
- Leap year rules: divisible by 4 and not by 100 unless also divisible by 400.
- Nested if conditions.
Solution:
import java.util.Scanner;
5. Multiplication Table
Concepts:
- Looping with for loop to repeat actions.
- Multiplying with loop counter.
Solution:
import java.util.Scanner;
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);
}
}
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);
}
}