|
| 1 | +Lab02 - CSE110_JAVA |
| 2 | +Spring-2024 |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +ClassWork:####################################################################################################### |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +task:03--------------------------------------------------------- |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +i) |
| 15 | + |
| 16 | + |
| 17 | +public class SwapUsingThirdVariable { |
| 18 | + public static void main(String[] args) { |
| 19 | + int a = 10; // Initialize first variable |
| 20 | + int b = 5; // Initialize second variable |
| 21 | + |
| 22 | + System.out.println("Before swapping: a = " + a + ", b = " + b); |
| 23 | + |
| 24 | + // Swap using a third variable |
| 25 | + int temp = a; |
| 26 | + a = b; |
| 27 | + b = temp; |
| 28 | + |
| 29 | + System.out.println("After swapping: a = " + a + ", b = " + b); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +ii) |
| 38 | + |
| 39 | + |
| 40 | +public class SwapWithoutThirdVariable { |
| 41 | + public static void main(String[] args) { |
| 42 | + int a = 10; // Initialize first variable |
| 43 | + int b = 5; // Initialize second variable |
| 44 | + |
| 45 | + System.out.println("Before swapping: a = " + a + ", b = " + b); |
| 46 | + |
| 47 | + // Swap without using a third variable |
| 48 | + a = a + b; // a now becomes 15 (10 + 5) |
| 49 | + b = a - b; // b now becomes 10 (15 - 5) |
| 50 | + a = a - b; // a now becomes 5 (15 - 10) |
| 51 | + |
| 52 | + System.out.println("After swapping: a = " + a + ", b = " + b); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +task:05--------------------------------------------------------- |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +/** |
| 65 | + * Auto Generated Java Class. |
| 66 | + */ |
| 67 | +import java.util.Scanner; |
| 68 | +public class Lab1 { |
| 69 | + public static void main(String[] args) { |
| 70 | + double a = 4.5; |
| 71 | + double b = 9.5; |
| 72 | + // Calculate the length of the third side (c) using the Pythagorean theorem |
| 73 | + double c = Math.sqrt((a * a) + (b * b)); |
| 74 | + System.out.println("c: "+c); |
| 75 | + // Calculate the sine and cosine values of angle A |
| 76 | + double sinA = a / c; |
| 77 | + double cosA = b / c; |
| 78 | + // Calculate the sine and cosine values of angle B |
| 79 | + double sinB = b / c; |
| 80 | + double cosB = a / c; |
| 81 | + |
| 82 | + // Print the results |
| 83 | + System.out.println("Sine of angle A (SinA): " + sinA); |
| 84 | + System.out.println("Cosine of angle A (CosA): " + cosA); |
| 85 | + System.out.println("Sine of angle B (SinB): " + sinB); |
| 86 | + System.out.println("Cosine of angle B (CosB): " + cosB); |
| 87 | + } |
| 88 | + /* ADD YOUR CODE HERE */ |
| 89 | +} |
| 90 | + |
| 91 | +Class Evaluation:####################################################################################################### |
| 92 | + |
| 93 | + |
| 94 | +task:01--------------------------------------------------------- |
| 95 | + |
| 96 | +/** |
| 97 | + * Auto Generated Java Class. |
| 98 | + */ |
| 99 | +import java.util.Scanner; |
| 100 | +public class Lab1 { |
| 101 | + |
| 102 | + |
| 103 | + public static void main(String[] args) { |
| 104 | + int a = 2; |
| 105 | + int b = 5; |
| 106 | + int c = 8; |
| 107 | + int d = ( (2*b) * ((c-a)/3) ) + 7; |
| 108 | + System.out.println("Answer: "+d); |
| 109 | + } |
| 110 | + |
| 111 | + /* ADD YOUR CODE HERE */ |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +task:02--------------------------------------------------------- |
| 117 | + |
| 118 | + |
| 119 | +/** |
| 120 | + * Auto Generated Java Class. |
| 121 | + */ |
| 122 | +import java.util.Scanner; |
| 123 | +public class Lab1 { |
| 124 | + |
| 125 | + |
| 126 | + public static void main(String[] args) { |
| 127 | + int id = 20301238; |
| 128 | + int last_two = id % 100; |
| 129 | + System.out.println(last_two%10+"\n"+last_two/10); |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + /* ADD YOUR CODE HERE */ |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +Homework:####################################################################################################### |
| 141 | + |
| 142 | +task:01--------------------------------------------------------- |
| 143 | + |
| 144 | +/** |
| 145 | + * Auto Generated Java Class. |
| 146 | + */ |
| 147 | +import java.util.Scanner; |
| 148 | +public class Lab1 { |
| 149 | + |
| 150 | + |
| 151 | + public static void main(String[] args) { |
| 152 | + System.out.println("Hello World!"); |
| 153 | + |
| 154 | + Scanner sc = new Scanner (System.in); |
| 155 | + System.out.print("Enter an integer: "); |
| 156 | + int num = sc.nextInt(); |
| 157 | + |
| 158 | + num=num%100; |
| 159 | + System.out.print("Output: "); |
| 160 | + if (num==0) { |
| 161 | + System.out.println("00"); |
| 162 | + } |
| 163 | + else { |
| 164 | + if (num<10){ |
| 165 | + System.out.print("0"); |
| 166 | + System.out.println(num); |
| 167 | + } |
| 168 | + else{ |
| 169 | + System.out.println(num); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | + /* ADD YOUR CODE HERE */ |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +task:02--------------------------------------------------------- |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | +/** |
| 187 | + * Auto Generated Java Class. |
| 188 | + */ |
| 189 | +import java.util.Scanner; |
| 190 | +public class Lab1 { |
| 191 | + |
| 192 | + |
| 193 | + public static void main(String[] args) { |
| 194 | + System.out.println("Hello World!"); |
| 195 | + |
| 196 | + Scanner sc = new Scanner (System.in); |
| 197 | + System.out.print("Given a value for inch: "); |
| 198 | + double num = sc.nextInt(); |
| 199 | + num= num* 0.0254; |
| 200 | + String value= String.valueOf(num); // Converting double to string |
| 201 | + System.out.println("1000 inch is "+ value+ " meters"); |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + /* ADD YOUR CODE HERE */ |
| 206 | +} |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +task:03--------------------------------------------------------- |
| 214 | + |
| 215 | + |
| 216 | +/** |
| 217 | + * Auto Generated Java Class. |
| 218 | + */ |
| 219 | +import java.util.Scanner; |
| 220 | +import java.lang.Math; |
| 221 | +public class Lab1 { |
| 222 | + |
| 223 | + |
| 224 | + public static void main(String[] args) { |
| 225 | + System.out.println("Hello World!"); |
| 226 | + int radius = 4; |
| 227 | + double circumference = 2 * Math.PI * radius; |
| 228 | + double area = Math.PI * radius * radius; |
| 229 | + |
| 230 | + System.out.println("For a circle with radius " + radius + " units:"); |
| 231 | + System.out.println("Circumference: " + circumference + " units"); |
| 232 | + System.out.println("Area: " + area + " square units"); |
| 233 | + |
| 234 | + |
| 235 | + } |
| 236 | + |
| 237 | + /* ADD YOUR CODE HERE */ |
| 238 | + /* We can convert float to double but cannot do vice-versa */ |
| 239 | +} |
| 240 | + |
| 241 | + |
| 242 | +task3 ALTERNATIVE using float |
| 243 | + |
| 244 | + |
| 245 | +/** |
| 246 | + * Auto Generated Java Class. |
| 247 | + */ |
| 248 | + |
| 249 | +import java.util.Scanner; |
| 250 | +public class test3 { |
| 251 | + |
| 252 | + |
| 253 | + public static void main(String[] args) { |
| 254 | + float radius = 4f; |
| 255 | + float circumference = 2 * (float) Math.PI * radius; |
| 256 | + float area = (float) Math.PI * radius * radius; |
| 257 | + System.out.println("Radius: " + radius); |
| 258 | + System.out.println("Circumference: " + circumference); |
| 259 | + System.out.println("Area: " + area); |
| 260 | + |
| 261 | + } |
| 262 | + |
| 263 | + } |
| 264 | + |
| 265 | + |
| 266 | +task:04--------------------------------------------------------- |
| 267 | + |
| 268 | + |
| 269 | + |
| 270 | +/** |
| 271 | + * Auto Generated Java Class. |
| 272 | + */ |
| 273 | +import java.util.Scanner; |
| 274 | +public class Lab1 { |
| 275 | + |
| 276 | + |
| 277 | + public static void main(String[] args) { |
| 278 | + System.out.println("Hello World!"); |
| 279 | + int n=5; |
| 280 | + System.out.println(n+" X 1 = "+n); |
| 281 | + System.out.println(n+" X 1 = "+(n*2)); |
| 282 | + System.out.println(n+" X 1 = "+(n*3)); |
| 283 | + System.out.println(n+" X 1 = "+(n*4)); |
| 284 | + System.out.println(n+" X 1 = "+(n*5)); |
| 285 | + System.out.println(n+" X 1 = "+(n*6)); |
| 286 | + System.out.println(n+" X 1 = "+(n*7)); |
| 287 | + System.out.println(n+" X 1 = "+(n*8)); |
| 288 | + System.out.println(n+" X 1 = "+(n*9)); |
| 289 | + System.out.println(n+" X 1 = "+(n*10)); |
| 290 | + |
| 291 | + |
| 292 | + } |
| 293 | + |
| 294 | + /* ADD YOUR CODE HERE */ |
| 295 | +} |
| 296 | + |
| 297 | + |
| 298 | + |
| 299 | + |
| 300 | + |
| 301 | +task:06--------------------------------------------------------- |
| 302 | + |
| 303 | + |
| 304 | +/** |
| 305 | + * Auto Generated Java Class. |
| 306 | + */ |
| 307 | +import java.util.Scanner; |
| 308 | +public class Lab1 { |
| 309 | + |
| 310 | + |
| 311 | + public static void main(String[] args) { |
| 312 | + System.out.println("Hello World!"); |
| 313 | + Scanner sc = new Scanner (System.in); |
| 314 | + System.out.print("Enter the number of terms 'n': "); |
| 315 | + int n = sc.nextInt(); |
| 316 | + System.out.print("Enter the first term 'a': "); |
| 317 | + int a= sc.nextInt(); |
| 318 | + System.out.print("Enter the last term 'L': "); |
| 319 | + int L = sc.nextInt(); |
| 320 | + float S = (n/2) *(a + L); |
| 321 | + System.out.println("Output: "+S); |
| 322 | + } |
| 323 | + |
| 324 | + /* ADD YOUR CODE HERE */ |
| 325 | +} |
| 326 | + |
| 327 | + |
| 328 | + |
| 329 | + |
| 330 | + |
| 331 | +task:07--------------------------------------------------------- |
| 332 | + |
| 333 | + |
| 334 | +/** |
| 335 | + * Auto Generated Java Class. |
| 336 | + */ |
| 337 | +import java.util.Scanner; |
| 338 | +public class Lab1 { |
| 339 | + |
| 340 | + |
| 341 | + public static void main(String[] args) { |
| 342 | + double hour = 5d; |
| 343 | + double minute = 56d; |
| 344 | + double seconds = 23d; |
| 345 | + double distance_meters = 1230d; |
| 346 | + double distance_km = distance_meters/1000; |
| 347 | + double distance_mile = distance_meters * (1.0/1609.0); |
| 348 | + hour = hour + (minute / 60) + (seconds /(60 * 60)); |
| 349 | + |
| 350 | + double velocity_km_hour = distance_km / hour; |
| 351 | + double velocity_mile_hour = distance_mile / hour; |
| 352 | + System.out.println("Velocity KMPH: "+velocity_km_hour+"\nVelocity MPH: "+velocity_mile_hour); |
| 353 | + |
| 354 | + |
| 355 | + } |
| 356 | + |
| 357 | + /* ADD YOUR CODE HERE */ |
| 358 | +} |
| 359 | + |
| 360 | + |
| 361 | + |
| 362 | +task:08--------------------------------------------------------- |
| 363 | + |
| 364 | + |
| 365 | +/** |
| 366 | + * Auto Generated Java Class. |
| 367 | + */ |
| 368 | +import java.util.Scanner; |
| 369 | +public class Lab1 { |
| 370 | + |
| 371 | + |
| 372 | + public static void main(String[] args) { |
| 373 | + double a = 8d; |
| 374 | + double b = 3d; |
| 375 | + //We know, c= root(a^2 * b^2) |
| 376 | + double c = Math.sqrt(Math.pow(a/2,2) + Math.pow(b,2)); // c is the length of a side |
| 377 | + System.out.println(c); |
| 378 | + double area = (3*Math.sqrt(3)*(c*c))/2; |
| 379 | + double circumference = 6 * c; |
| 380 | + System.out.println("Area: "+area+" and Circumference: "+circumference); |
| 381 | + } |
| 382 | + |
| 383 | + /* ADD YOUR CODE HERE */ |
| 384 | +} |
0 commit comments