diff --git a/README.md b/README.md index 064f0ae..200dd7d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/ty2f_0nx) + # Java-Task1 — Introductory Java Exercises This repository contains three beginner-friendly Java exercises built around the following topics: @@ -81,4 +83,4 @@ java q3 - Basic understanding of command line/terminal usage - Text editor (VSCode Recommended) or IDE (IntelliJ IDEA recommended) -Proceed to the `q1/`, `q2/`, and `q3/` folders and complete the exercises in order. \ No newline at end of file +Proceed to the `q1/`, `q2/`, and `q3/` folders and complete the exercises in order. diff --git a/q1/README.md b/q1/README.md index f4b81b2..a84c6e8 100644 --- a/q1/README.md +++ b/q1/README.md @@ -3,6 +3,7 @@ ## Objective Complete the fill-in-the-blanks exercise in `q1.java` to demonstrate understanding of: + - Basic Java program structure - Variable declarations and initialization - Data types (int, double, char, boolean, String) @@ -74,4 +75,4 @@ java q1 - [ ] All variables are properly declared and initialized - [ ] Output matches the expected format exactly - [ ] Proper use of Java naming conventions -- [ ] All "FILL HERE" comments are replaced with appropriate code \ No newline at end of file +- [ ] All "FILL HERE" comments are replaced with appropriate code diff --git a/q1/q1.class b/q1/q1.class new file mode 100644 index 0000000..0998d24 Binary files /dev/null and b/q1/q1.class differ diff --git a/q1/q1.java b/q1/q1.java index 17e9650..af69290 100644 --- a/q1/q1.java +++ b/q1/q1.java @@ -1,47 +1,31 @@ // FILL HERE: Import the necessary package for input operations - // FILL HERE: Write the class declaration with proper naming convention -{ +public class q1 { + // FILL HERE: Write the main method signature - { + public static void main(String[] args) { // Variable declarations and initialization - // FILL HERE: Declare an integer variable named 'age' and initialize it to 25 - - // FILL HERE: Declare a double variable named 'height' and initialize it to 5.8 - - // FILL HERE: Declare a char variable named 'grade' and initialize it to 'A' - - // FILL HERE: Declare a boolean variable named 'isStudent' and initialize it to true - - // FILL HERE: Declare a String variable named 'name' and initialize it to "John Doe" - - + int age = 25; + double height = 5.8; + char grade = 'A'; + boolean isStudent = true; + String name = "John Doe"; + // Output statements System.out.println("=== Student Information ==="); - - // FILL HERE: Print the name using System.out.println - - // FILL HERE: Print the age using System.out.println (format: "Age: 25") - - // FILL HERE: Print the height using System.out.println (format: "Height: 5.8 feet") - - // FILL HERE: Print the grade using System.out.println (format: "Grade: A") - - // FILL HERE: Print the student status using System.out.println (format: "Is Student: true") - - + System.out.println("Name: " + name); + System.out.println("Age: " + age); + System.out.println("Height: " + height); + System.out.println("Grade: " + grade); + System.out.println("Is Student: " + isStudent); + // Data type demonstration System.out.println("\n=== Data Type Information ==="); - - // FILL HERE: Print the data type of age variable (hint: use "int") - - // FILL HERE: Print the data type of height variable (hint: use "double") - - // FILL HERE: Print the data type of grade variable (hint: use "char") - - // FILL HERE: Print the data type of isStudent variable (hint: use "boolean") - - // FILL HERE: Print the data type of name variable (hint: use "String") + System.out.println("age is of type: int"); + System.out.println("height is of type: double"); + System.out.println("grade is of type: char"); + System.out.println("isStudent is of type: boolean"); + System.out.println("name is of type: String"); } } \ No newline at end of file diff --git a/q2/README.md b/q2/README.md index d62df78..164fbc9 100644 --- a/q2/README.md +++ b/q2/README.md @@ -3,8 +3,9 @@ ## Objective Write a complete Java program that demonstrates: + - Getting user input using Scanner -- Arithmetic operators (+, -, *, /, %) +- Arithmetic operators (+, -, \*, /, %) - Relational operators (>, <, >=, <=, ==, !=) - Logical operators (&&, ||, !) - Basic expressions and calculations @@ -28,7 +29,7 @@ Your program should: 6. **Perform** and display the following calculations: - Addition (num1 + num2) - Subtraction (num1 - num2) - - Multiplication (num1 * num2) + - Multiplication (num1 \* num2) - Division (num1 / num2) - Modulus (num1 % num2) - only if both numbers are integers 7. **Demonstrate** relational operators by comparing the two numbers @@ -37,12 +38,14 @@ Your program should: ## Sample Input/Output **Input:** + ``` Enter first number: 15 Enter second number: 4 ``` **Expected Output:** + ``` === Calculator Results === First Number: 15.0 @@ -72,20 +75,24 @@ Modulus: 15 % 4 = 3 ## Implementation Hints 1. **Scanner Setup:** + ```java Scanner scanner = new Scanner(System.in); ``` 2. **Reading Input:** + ```java System.out.print("Enter first number: "); double num1 = scanner.nextDouble(); ``` 3. **Modulus Operation:** + - Convert doubles to integers for modulus: `(int)num1 % (int)num2` 4. **Formatting Output:** + - Use `System.out.println()` for formatted output - Show operations in the format: `operand1 operator operand2 = result` @@ -117,6 +124,7 @@ java q2 ## Additional Challenge (Optional) If you finish early, try adding: + - Input validation (handle invalid input) - Division by zero checking -- More complex expressions combining multiple operators \ No newline at end of file +- More complex expressions combining multiple operators diff --git a/q2/q2.class b/q2/q2.class new file mode 100644 index 0000000..bb57c7a Binary files /dev/null and b/q2/q2.class differ diff --git a/q2/q2.java b/q2/q2.java index 5f6c53b..508fde1 100644 --- a/q2/q2.java +++ b/q2/q2.java @@ -9,4 +9,51 @@ // 5. Perform arithmetic operations and display results // 6. Demonstrate different types of operators (arithmetic, relational, logical) -// Write your complete solution below: \ No newline at end of file +// Write your complete solution below: + +import java.util.Scanner; + +public class q2 { + + public static void main(String[] args) { + + // initialized scanner variable from scanner class + Scanner input = new Scanner(System.in); + // Taking input using scanner class + System.out.print("Enter first number: "); + double num1 = input.nextDouble(); + System.out.print("Enter second number: "); + double num2 = input.nextDouble(); + + System.out.println("\n~~~~~~~~~~~~ Arithmetic Operations ~~~~~~~~~~~~"); + System.out.println("Addition: " + num1 + " + " + num2 + " = " + (num1 + num2)); + System.out.println("Subtraction: " + num1 + " - " + num2 + " = " + (num1 - num2)); + System.out.println("Multiplication: " + num1 + " * " + num2 + " = " + (num1 * num2)); + System.out.println("Division: " + num1 + " / " + num2 + " = " + (num1 / num2)); + System.out.println("Modulus: " + (int) num1 + " % " + (int) num2 + " = " + ((int) num1 % (int) num2)); + + boolean greater = num1 > num2; + boolean less = num1 < num2; + boolean greaterEqual = num1 >= num2; + boolean lessEqual = num1 <= num2; + boolean equal = num1 == num2; + boolean notEqual = num1 != num2; + + System.out.println("\n~~~~~~~~~~~~ Relational Operations ~~~~~~~~~~~~"); + System.out.println(num1 + " > " + num2 + ": " + greater); + System.out.println(num1 + " < " + num2 + ": " + less); + System.out.println(num1 + " >= " + num2 + ": " + greaterEqual); + System.out.println(num1 + " <= " + num2 + ": " + lessEqual); + System.out.println(num1 + " == " + num2 + ": " + equal); + System.out.println(num1 + " != " + num2 + ": " + notEqual); + + // Logical Operations + System.out.println("\n=== Logical Operations ==="); + System.out.println("(" + num1 + " > " + num2 + ") && (" + num1 + " != " + num2 + "): " + (greater && notEqual)); + System.out.println("(" + num1 + " < " + num2 + ") || (" + num1 + " == " + num2 + "): " + (less || equal)); + System.out.println("!(" + num1 + " == " + num2 + "): " + (!equal)); + + input.close(); + } + +} \ No newline at end of file diff --git a/q3/q3.class b/q3/q3.class new file mode 100644 index 0000000..8b49740 Binary files /dev/null and b/q3/q3.class differ diff --git a/q3/q3.java b/q3/q3.java index b77541b..c99a4ed 100644 --- a/q3/q3.java +++ b/q3/q3.java @@ -1,126 +1,87 @@ // q3.java - Literals, Increment/Decrement Operators, and Expressions Demo // Complete the fill-in-the-blanks to demonstrate various Java concepts - public class q3 { public static void main(String[] args) { System.out.println("=== Java Literals and Operators Demo ===\n"); - + // Part 1: Different types of literals System.out.println("=== Part 1: Literals Demo ==="); - - // FILL HERE: Create an integer literal variable 'decimal' with value 42 - int decimal = 0; // Replace 0 with correct value - - // FILL HERE: Create a binary literal variable 'binary' with value 0b101010 (which is 42 in decimal) - int binary = 0; // Replace 0 with correct binary literal - - // FILL HERE: Create an octal literal variable 'octal' with value 052 (which is 42 in decimal) - int octal = 0; // Replace 0 with correct octal literal - - // FILL HERE: Create a hexadecimal literal variable 'hex' with value 0x2A (which is 42 in decimal) - int hex = 0; // Replace 0 with correct hexadecimal literal - + + int decimal = 42; + int binary = 0b101010; + int octal = 052; + int hex = 0x2A; + System.out.println("Decimal literal: " + decimal); System.out.println("Binary literal: " + binary); System.out.println("Octal literal: " + octal); System.out.println("Hexadecimal literal: " + hex); - - // FILL HERE: Create a float literal variable 'floatNum' with value 3.14f - float floatNum = 0.0f; // Replace with correct value - - // FILL HERE: Create a double literal variable 'doubleNum' with value 2.718281828 - double doubleNum = 0.0; // Replace with correct value - + + float floatNum = 3.14f; + double doubleNum = 2.718281828; + System.out.println("Float literal: " + floatNum); System.out.println("Double literal: " + doubleNum); - - // FILL HERE: Create a char literal variable 'letter' with value 'A' - char letter = ' '; // Replace with correct character - - // FILL HERE: Create a char literal variable 'unicodeChar' with value '\u0041' (which is 'A') - char unicodeChar = ' '; // Replace with correct unicode character - + + char letter = 'A'; + char unicodeChar = '\u0041'; + System.out.println("Character literal: " + letter); System.out.println("Unicode character: " + unicodeChar); - - // FILL HERE: Create a boolean literal variable 'isTrue' with value true - boolean isTrue = false; // Replace with correct value - - // FILL HERE: Create a String literal variable 'message' with value "Hello, Java!" - String message = ""; // Replace with correct string - + + boolean isTrue = true; + String message = "Hello, Java!"; + System.out.println("Boolean literal: " + isTrue); System.out.println("String literal: " + message); - + System.out.println("\n=== Part 2: Increment/Decrement Operators ==="); - - // FILL HERE: Create an integer variable 'counter' with initial value 10 - int counter = 0; // Replace with correct initial value - + + int counter = 10; System.out.println("Initial counter value: " + counter); - - // FILL HERE: Use post-increment (counter++) in the println statement - System.out.println("Post-increment (counter++): " + counter); // Add post-increment operation + + System.out.println("Post-increment (counter++): " + counter++); System.out.println("Counter after post-increment: " + counter); - - // FILL HERE: Use pre-increment (++counter) in the println statement - System.out.println("Pre-increment (++counter): " + counter); // Add pre-increment operation + + System.out.println("Pre-increment (++counter): " + ++counter); System.out.println("Counter after pre-increment: " + counter); - - // FILL HERE: Use post-decrement (counter--) in the println statement - System.out.println("Post-decrement (counter--): " + counter); // Add post-decrement operation + + System.out.println("Post-decrement (counter--): " + counter--); System.out.println("Counter after post-decrement: " + counter); - - // FILL HERE: Use pre-decrement (--counter) in the println statement - System.out.println("Pre-decrement (--counter): " + counter); // Add pre-decrement operation + + System.out.println("Pre-decrement (--counter): " + --counter); System.out.println("Counter after pre-decrement: " + counter); - + System.out.println("\n=== Part 3: Data Type of Expressions ==="); - - // Variables for expression demonstrations + int intVar = 5; double doubleVar = 2.5; float floatVar = 1.5f; char charVar = 'B'; - - // Expression 1: int + int - // FILL HERE: Create a variable 'result1' that stores intVar + 3 (determine the correct data type) - int result1 = 0; // Replace with correct expression and fix data type if needed - + + int result1 = intVar + 3; + double result2 = intVar + doubleVar; + double result3 = floatVar + doubleVar; + int result4 = charVar + intVar; + System.out.println("int + int = " + result1 + " (Type: int)"); - - // Expression 2: int + double - // FILL HERE: Create a variable 'result2' that stores intVar + doubleVar (determine the correct data type) - double result2 = 0.0; // Replace with correct expression and fix data type if needed - System.out.println("int + double = " + result2 + " (Type: double)"); - - // Expression 3: float + double - // FILL HERE: Create a variable 'result3' that stores floatVar + doubleVar (determine the correct data type) - double result3 = 0.0; // Replace with correct expression and fix data type if needed - System.out.println("float + double = " + result3 + " (Type: double)"); - - // Expression 4: char + int - // FILL HERE: Create a variable 'result4' that stores charVar + intVar (determine the correct data type) - int result4 = 0; // Replace with correct expression and fix data type if needed - System.out.println("char + int = " + result4 + " (Type: int, 'B' has ASCII value 66)"); - + System.out.println("\n=== Part 4: Operator Associativity ==="); - - // Demonstrate left-to-right associativity with subtraction + int a = 20, b = 10, c = 5; - // FILL HERE: Calculate result5 = a - b - c (should be evaluated as (a - b) - c) - int result5 = 0; // Replace with correct expression - + int result5 = a - b - c; + System.out.println("Left-to-right: " + a + " - " + b + " - " + c + " = " + result5); - System.out.println("Evaluation: (" + a + " - " + b + ") - " + c + " = " + (a - b) + " - " + c + " = " + result5); - - // FILL HERE: Demonstrate right-to-left associativity with assignment - int x = 0, y = 0, z = 0; // Replace this line with chained assignment x = y = z = 15 - + System.out + .println("Evaluation: (" + a + " - " + b + ") - " + c + " = " + (a - b) + " - " + c + " = " + result5); + + int x, y, z; + x = y = z = 15; + System.out.println("Right-to-left assignment: x = y = z = 15"); System.out.println("x = " + x + ", y = " + y + ", z = " + z); } -} \ No newline at end of file +}