|
1 | 1 | # Java Programming Exercises
|
2 | 2 |
|
3 | | -This repository contains a series of basic Java programming exercises designed to help you learn and practice fundamental Java concepts. |
| 3 | +This repository contains Java programming exercises divided into two levels: Level 1 and Level 2. The exercises cover various fundamental programming concepts. |
4 | 4 |
|
5 | 5 | ## Table of Contents
|
| 6 | +- [Level 1 Tasks](#level-1-tasks) |
| 7 | +- [Level 2 Tasks](#level-2-tasks) |
6 | 8 |
|
7 | | -1. [Task 01 - Setting Up Your First Java Program](#task-01---setting-up-your-first-java-program) |
8 | | -2. [Task 02 - Declaring a Class and Main Method](#task-02---declaring-a-class-and-main-method) |
9 | | -3. [Task 03 - Understanding Code Blocks and Statements](#task-03---understanding-code-blocks-and-statements) |
10 | | -4. [Task 04 - Declaring Variables and Printing Their Values](#task-04---declaring-variables-and-printing-their-values) |
11 | | -5. [Task 05 - Working with Primitive Data Types](#task-05---working-with-primitive-data-types) |
12 | | -6. [Task 06 - Wrapper Classes and MIN/MAX Values](#task-06---wrapper-classes-and-minmax-values) |
13 | | -7. [Task 07 - Demonstrating Overflow and Underflow](#task-07---demonstrating-overflow-and-underflow) |
14 | | -8. [Task 08 - Working with Long and Double](#task-08---working-with-long-and-double) |
15 | | -9. [Task 09 - Conversion between Types (Casting)](#task-09---conversion-between-types-casting) |
16 | | -10. [Task 10 - Converting Pounds to Kilograms](#task-10---converting-pounds-to-kilograms) |
17 | | -11. [Task 11 - Working with Char and Boolean](#task-11---working-with-char-and-boolean) |
18 | | -12. [Task 12 - Basic Conditional Logic (if-else)](#task-12---basic-conditional-logic-if-else) |
19 | | -13. [Task 13 - Using Ternary Operator](#task-13---using-ternary-operator) |
20 | | -14. [Task 14 - Exploring Strings](#task-14---exploring-strings) |
21 | | -15. [Task 15 - Conclusion and Cleanup](#task-15---conclusion-and-cleanup) |
22 | | -16. [Task 16 - Simple Calculator](#task-16---simple-calculator) |
23 | | -17. [Task 17 - Area and Perimeter of a Rectangle](#task-17---area-and-perimeter-of-a-rectangle) |
24 | | -18. [Task 18 - Temperature Converter (Celsius to Fahrenheit)](#task-18---temperature-converter-celsius-to-fahrenheit) |
25 | | -19. [Task 19 - Sum of Digits](#task-19---sum-of-digits) |
26 | | -20. [Task 20 - Grade Calculator](#task-20---grade-calculator) |
| 9 | +## Level 1 Tasks |
27 | 10 |
|
28 | | -## Task Descriptions |
| 11 | +### Exercise 1: Setting Up Your First Java Program |
| 12 | +- **Task**: Create a new project in IntelliJ, and write a Java program that prints "Hello, World!". |
| 13 | +- **Expected Output**: |
| 14 | + ``` |
| 15 | + Hello, World! |
| 16 | + ``` |
29 | 17 |
|
30 | | -### Task 01 - Setting Up Your First Java Program |
31 | | -Learn how to set up a basic Java program and run it successfully. |
| 18 | +### Exercise 2: Declaring a Class and Main Method |
| 19 | +- **Task**: Declare a new class named `FirstClass` and within that, declare the main method as discussed in the lecture. |
| 20 | +- **Expected Output**: No specific output required yet, but the code should compile without errors. |
32 | 21 |
|
33 | | -### Task 02 - Declaring a Class and Main Method |
34 | | -Understand how to declare a class in Java and define the `main` method. |
| 22 | +### Exercise 3: Understanding Code Blocks and Statements |
| 23 | +- **Task**: Within the main method, write a statement that outputs your name to the console using `System.out.println()`. |
| 24 | +- **Expected Output**: |
| 25 | + ``` |
| 26 | + My name is [Your Name] |
| 27 | + ``` |
35 | 28 |
|
36 | | -### Task 03 - Understanding Code Blocks and Statements |
37 | | -Explore how Java code is structured using code blocks and statements. |
| 29 | +### Exercise 4: Declaring Variables and Printing Their Values |
| 30 | +- **Task**: Declare an integer variable `age` and set its value to your age. Then, print it using `System.out.println()`. |
| 31 | +- **Expected Output**: |
| 32 | + ``` |
| 33 | + My age is: [Your Age] |
| 34 | + ``` |
38 | 35 |
|
39 | | -### Task 04 - Declaring Variables and Printing Their Values |
40 | | -Practice declaring variables of different types and printing their values to the console. |
| 36 | +### Exercise 5: Working with Primitive Data Types |
| 37 | +- **Task**: Declare variables for different data types: byte, short, int, long, float, double, char, and boolean. Assign values to each and print them. |
| 38 | +- **Expected Output**: |
| 39 | + ``` |
| 40 | + Byte value: [Your Byte Value] |
| 41 | + Short value: [Your Short Value] |
| 42 | + Int value: [Your Int Value] |
| 43 | + Long value: [Your Long Value] |
| 44 | + Float value: [Your Float Value] |
| 45 | + Double value: [Your Double Value] |
| 46 | + Char value: [Your Char Value] |
| 47 | + Boolean value: [Your Boolean Value] |
| 48 | + ``` |
41 | 49 |
|
42 | | -### Task 05 - Working with Primitive Data Types |
43 | | -Get familiar with Java's primitive data types, such as `int`, `double`, `char`, and `boolean`. |
| 50 | +### Exercise 6: Wrapper Classes and MIN/MAX Values |
| 51 | +- **Task**: Use the wrapper class for `int` (`Integer`) to print the minimum and maximum values. |
| 52 | +- **Expected Output**: |
| 53 | + ``` |
| 54 | + Min int value: -2147483648 |
| 55 | + Max int value: 2147483647 |
| 56 | + ``` |
44 | 57 |
|
45 | | -### Task 06 - Wrapper Classes and MIN/MAX Values |
46 | | -Learn about wrapper classes in Java and how to retrieve the minimum and maximum values of data types. |
| 58 | +### Exercise 7: Demonstrating Overflow and Underflow |
| 59 | +- **Task**: Assign the value `2147483647` to an `int` variable and add `1`. Print the result to observe overflow. Repeat for a byte variable. |
| 60 | +- **Expected Output**: |
| 61 | + ``` |
| 62 | + Maximum value of int: [originally assigned value] |
| 63 | + Maximum value of byte: [originally assigned value] |
| 64 | + Overflow example with int: [Overflowed Value] |
| 65 | + Overflow example with byte: [Overflowed Value] |
| 66 | + Underflow example with int: [Underflowed Value] |
| 67 | + Underflow example with byte: [Underflowed Value] |
| 68 | + ``` |
47 | 69 |
|
48 | | -### Task 07 - Demonstrating Overflow and Underflow |
49 | | -Experiment with overflow and underflow in numeric data types. |
| 70 | +### Exercise 8: Working with Long and Double |
| 71 | +- **Task**: Declare a long variable with the value `10000000000L` and a double variable with a value of your choice. Print both values. Demonstrate why adding `L` is important for long data types. |
| 72 | +- **Expected Output**: |
| 73 | + ``` |
| 74 | + Long value: 10000000000 |
| 75 | + Double value: [Your Double Value] |
| 76 | + ``` |
50 | 77 |
|
51 | | -### Task 08 - Working with Long and Double |
52 | | -Work with larger numbers using `long` and floating-point numbers with `double`. |
| 78 | +### Exercise 9: Conversion between Types (Casting) |
| 79 | +- **Task**: Convert a double value (e.g., `5.99`) to an int and print the result. |
| 80 | +- **Expected Output**: |
| 81 | + ``` |
| 82 | + Double value: 5.99 |
| 83 | + After casting to int: 5 |
| 84 | + ``` |
53 | 85 |
|
54 | | -### Task 09 - Conversion between Types (Casting) |
55 | | -Practice type conversion (casting) between different data types in Java. |
| 86 | +### Exercise 10: Converting Pounds to Kilograms |
| 87 | +- **Task**: Write a program to convert pounds to kilograms (1 pound = 0.45359237 kg). Declare a double variable for pounds and calculate the equivalent kilograms. Print both values. |
| 88 | +- **Expected Output**: |
| 89 | + ``` |
| 90 | + Pounds: [Your Pounds Value] |
| 91 | + Kilograms: [Calculated Kilograms] |
| 92 | + ``` |
56 | 93 |
|
57 | | -### Task 10 - Converting Pounds to Kilograms |
58 | | -Write a program to convert a given weight in pounds to kilograms. |
| 94 | +### Exercise 11: Working with Char and Boolean |
| 95 | +- **Task**: Declare a char variable and assign a Unicode character. Also, declare a boolean variable and assign it true. Print both values. |
| 96 | +- **Expected Output**: |
| 97 | + ``` |
| 98 | + Char value: [Your Unicode Character] |
| 99 | + Boolean value: true |
| 100 | + ``` |
59 | 101 |
|
60 | | -### Task 11 - Working with Char and Boolean |
61 | | -Work with `char` and `boolean` data types and explore their usage. |
| 102 | +### Exercise 12: Basic Conditional Logic (if-else) |
| 103 | +- **Task**: Write an if-else statement that checks whether a number is greater than 10. If it is, print "Number is greater than 10"; otherwise, print "Number is less than or equal to 10." |
| 104 | +- **Expected Output**: |
| 105 | + ``` |
| 106 | + Number is [Condition Result] |
| 107 | + ``` |
62 | 108 |
|
63 | | -### Task 12 - Basic Conditional Logic (if-else) |
64 | | -Implement basic conditional logic using `if-else` statements in Java. |
| 109 | +### Exercise 13: Using Ternary Operator |
| 110 | +- **Task**: Use a ternary operator to check whether a boolean value `isAvailable` is true or false, and print the result. |
| 111 | +- **Expected Output**: |
| 112 | + ``` |
| 113 | + Availability: [true/false] |
| 114 | + ``` |
65 | 115 |
|
66 | | -### Task 13 - Using Ternary Operator |
67 | | -Learn how to use the ternary operator for concise conditional expressions. |
| 116 | +### Exercise 14: Exploring Strings |
| 117 | +- **Task**: Declare a String variable and assign a value to it. Print the string, and then reassign a new value and print again to observe the immutability of strings. |
| 118 | +- **Expected Output**: |
| 119 | + ``` |
| 120 | + Initial String: [Your String] |
| 121 | + Modified String: [New String] |
| 122 | + ``` |
68 | 123 |
|
69 | | -### Task 14 - Exploring Strings |
70 | | -Dive into Java strings and practice various string manipulation techniques. |
| 124 | +### Exercise 15: Conclusion and Cleanup |
| 125 | +-**Task**: Go through all your code, ensure it compiles and runs correctly, and print any final messages or observations you have made during the lab session. |
71 | 126 |
|
72 | | -### Task 15 - Conclusion and Cleanup |
73 | | -Wrap up the basics and ensure your code is organized and clean. |
| 127 | +### Exercise 16: Simple Calculator |
| 128 | +- **Task**: Write a program that acts as a basic calculator. It should take two integers and perform addition, subtraction, multiplication, and division on them. |
| 129 | +- **Expected Output**: |
| 130 | + ``` |
| 131 | + Number 1: [Your First Number] |
| 132 | + Number 2: [Your Second Number] |
| 133 | + Addition: [Result of Addition] |
| 134 | + Subtraction: [Result of Subtraction] |
| 135 | + Multiplication: [Result of Multiplication] |
| 136 | + Division: [Result of Division] |
| 137 | + ``` |
74 | 138 |
|
75 | | -### Task 16 - Simple Calculator |
76 | | -Create a simple calculator that performs basic arithmetic operations (addition, subtraction, multiplication, division). |
| 139 | +### Exercise 17: Area and Perimeter of a Rectangle |
| 140 | +- **Task**: Write a program that calculates the area and perimeter of a rectangle. |
| 141 | +- **Expected Output**: |
| 142 | + ``` |
| 143 | + Length: [Your Length Value] |
| 144 | + Width: [Your Width Value] |
| 145 | + Area: [Calculated Area] |
| 146 | + Perimeter: [Calculated Perimeter] |
| 147 | + ``` |
77 | 148 |
|
78 | | -### Task 17 - Area and Perimeter of a Rectangle |
79 | | -Write a program to calculate the area and perimeter of a rectangle given its length and width. |
| 149 | +### Exercise 18: Temperature Converter (Celsius to Fahrenheit) |
| 150 | +- **Task**: Create a program that converts temperature from Celsius to Fahrenheit using the formula: `Fahrenheit = (Celsius * 9/5) + 32`. |
| 151 | +- **Expected Output**: |
| 152 | + ``` |
| 153 | + Celsius: [Your Celsius Value] |
| 154 | + Fahrenheit: [Calculated Fahrenheit] |
| 155 | + ``` |
80 | 156 |
|
81 | | -### Task 18 - Temperature Converter (Celsius to Fahrenheit) |
82 | | -Build a program to convert temperature from Celsius to Fahrenheit. |
| 157 | +### Exercise 19: Sum of Digits |
| 158 | +- **Task**: Write a program that takes a three-digit number and calculates the sum of its digits. |
| 159 | +- **Expected Output**: |
| 160 | + ``` |
| 161 | + Input number: [Your Input Number] |
| 162 | + Sum of digits: [Calculated Sum] |
| 163 | + ``` |
83 | 164 |
|
84 | | -### Task 19 - Sum of Digits |
85 | | -Create a program that calculates the sum of the digits of a given integer. |
| 165 | +### Exercise 20: Grade Calculator |
| 166 | +- **Task**: Write a program that takes a student's marks out of 100 and calculates the grade based on predefined conditions. |
| 167 | +- **Expected Output**: |
| 168 | + ``` |
| 169 | + Marks: [Your Marks] |
| 170 | + Grade: [Calculated Grade] |
| 171 | + ``` |
| 172 | + |
| 173 | +## Level 2 Tasks |
86 | 174 |
|
87 | | -### Task 20 - Grade Calculator |
88 | | -Write a program that calculates a student's grade based on input scores. |
| 175 | +### Exercise 1: Identifying Java Keywords |
| 176 | +- **Task**: Identify all the keywords used in the following Java program and explain the purpose of each keyword. |
| 177 | +- **Expected Output**: A list of keywords with explanations of their function. |
89 | 178 |
|
90 | | -## How to Run the Programs |
| 179 | +### Exercise 2: Writing and Evaluating Expressions |
| 180 | +- **Task**: Write a program that declares four integer variables, performs an arithmetic operation (addition or multiplication), and prints the result. |
| 181 | +- **Expected Output**: The result of the arithmetic operation. |
| 182 | + |
| 183 | +### Exercise 3: Understanding Code Blocks and Indentation |
| 184 | +- **Task**: Write a program where you include a simple if-else block to check whether a number is positive or negative. Ensure proper indentation for clarity. |
| 185 | +- **Expected Output**: The program will print whether the number is positive or negative. |
| 186 | + |
| 187 | +### Modified Exercise 4: Grading Multiple Students and Class Average |
| 188 | +- **Task**: Write a program that evaluates the scores of multiple students and assigns grades based on the following criteria: |
| 189 | + - 90 and above: Grade A |
| 190 | + - 80 and above: Grade B |
| 191 | + - 70 and above: Grade C |
| 192 | + - 60 and above: Grade D |
| 193 | + - Below 60: Fail |
| 194 | + - Additionally, after grading each student, the program should calculate the class average and display it. |
| 195 | +- **Expected Output**: |
| 196 | + ``` |
| 197 | + Student 1: Score = 95, Grade = A |
| 198 | + Student 2: Score = 82, Grade = B |
| 199 | + Student 3: Score = 74, Grade = C |
| 200 | + Student 4: Score = 61, Grade = D |
| 201 | + Student 5: Score = 58, Grade = Fail |
| 202 | + Class Average Score is = 74.0 |
| 203 | + ``` |
| 204 | + |
| 205 | +### Exercise 5: Writing a Simple Function |
| 206 | +- **Task**: Write a function that takes two integer inputs, multiplies them, and returns the result. Call the function from the main method and print the result. |
| 207 | +- **Expected Output**: The product of the two integers. |
| 208 | + |
| 209 | +### Exercise 6: Function with Conditional Logic |
| 210 | +- **Task**: Write a function that accepts an integer as an argument, checks if it's even or odd, and returns the corresponding message. |
| 211 | +- **Expected Output**: The function will return a message indicating whether the number is even or odd. |
| 212 | + |
| 213 | +### Exercise 7: Method Overloading – Adding Integers and Doubles |
| 214 | +- **Task**: Overload a method named `addValues` that adds two integers in one version and two doubles in another. Call both versions and print the results. |
| 215 | +- **Expected Output**: Two outputs: one with the sum of integers, and the other with the sum of doubles. |
| 216 | + |
| 217 | +### Modified Exercise 8: Calculating Total Inventory Value Using Method Overloading |
| 218 | +- **Task**: Write overloaded methods that calculate the total value of an inventory item, where: |
| 219 | + 1. The first method calculates the total value based on the product count (integer) and price per unit (double). |
| 220 | + 2. The second method calculates the total value based on the product count (integer) and price per unit (integer), assuming both quantities are whole numbers (e.g., for bulk pricing). |
| 221 | +- **Expected Output**: |
| 222 | + ``` |
| 223 | + Total value of Product A (50 units at 25ドル.5 per unit): 1275ドル.0 |
| 224 | + Total value of Product B (200 units at 20ドル per unit): 4000ドル |
| 225 | + ``` |
| 226 | + |
| 227 | +### Exercise 9: Accessing Variables Within Code Blocks |
| 228 | +- **Task**: Write a program that declares a variable inside an if block and attempts to access it outside the block. Analyze the result and explain the scope of the variable. |
| 229 | +- **Expected Output**: The program should explain why the variable is inaccessible outside the block. |
| 230 | + |
| 231 | +### Exercise 10: Returning Values from Functions |
| 232 | +- **Task**: Create a function named `calculateRectangleArea` that takes the length and width of a rectangle as parameters, computes the area, and returns the result. In the main method, call this function using predefined values for the length and width, and display the area in a user-friendly message. |
| 233 | +- **Expected Output**: |
| 234 | + ``` |
| 235 | + The area of the rectangle with length [length] and width [width] is [area]. |
| 236 | + ``` |
| 237 | + |
| 238 | +### Exercise 11: Passing Arguments to Functions |
| 239 | +- **Task**: Create a function named `displayEmployeeInfo` that accepts an employee's name (as a string) and their year of birth (as an integer) as input parameters. The function should calculate the employee's age based on the current year and print a formatted message that includes both the name and the calculated age. In the main method, call this function multiple times with different employee names and years of birth to demonstrate its functionality. |
| 240 | +- **Expected Output**: |
| 241 | + ``` |
| 242 | + Employee Name: [name], Age: [calculated age] |
| 243 | + ``` |
| 244 | +Sure! Here are the questions for **Exercises 12 and 13** without the code: |
| 245 | + |
| 246 | +### Exercise 12: Using Method Overloading for Input Types |
| 247 | + |
| 248 | +**Task**: Create a method named `printValue` that is overloaded to handle different input types: integer, float, and double. This method should be used to print the details of various financial transactions. |
| 249 | + |
| 250 | +#### Steps to Complete: |
| 251 | +1. Define the `printValue` method three times: once for integers, once for floats, and once for doubles. |
| 252 | +2. Each version of the method should print a formatted message describing what the value represents (e.g., "Quantity:", "Product Price:", "Account Balance:"). |
| 253 | +3. In the main method, call each overloaded version of the `printValue` method with appropriate hardcoded values. |
| 254 | +4. Ensure the output is clear and informative. |
| 255 | + |
| 256 | +--- |
| 257 | + |
| 258 | +### Exercise 13: Combining Method Overloading with Conditional Logic |
| 259 | + |
| 260 | +**Task**: Create a method named `calculateResult` that is overloaded to handle both integers and doubles. Each version of the method should perform different calculations based on the input type. |
| 261 | + |
| 262 | +#### Steps to Complete: |
| 263 | +1. Define the overloaded `calculateResult` method: |
| 264 | + - For the integer version, calculate and return both the sum of the integer with a predefined constant (e.g., 10) and the factorial of that integer. |
| 265 | + - For the double version, calculate and return both the total cost after applying a discount (e.g., a 10% discount on the original price) and the total tax (e.g., 5% tax) on that amount. |
| 266 | +2. In the main method, call the integer version of `calculateResult` with a sample integer (e.g., 5) and print the results. |
| 267 | +3. Call the double version of `calculateResult` with a sample double (e.g., 200.0) and print the results. |
| 268 | +4. Ensure that the output is clear and well-structured, providing context for each result. |
91 | 269 |
|
92 | | -1. Ensure you have Java Development Kit (JDK) installed on your machine. |
93 | | -2. Clone this repository: |
94 | | - ```bash |
95 | | - git clone https://github.com/faizan-ahmad5/Java-Programming-Exercises.git |
96 | | - ``` |
97 | | -3. Navigate to the project directory and open any Java file in your preferred IDE or text editor. |
98 | | -4. Compile and run the programs using the following commands: |
99 | | - ```bash |
100 | | - javac TaskXX.java |
101 | | - java TaskXX |
102 | | - ``` |
103 | 270 |
|
104 | 271 |
|
0 commit comments