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