-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiled-assignment.txt
More file actions
365 lines (313 loc) · 11.9 KB
/
compiled-assignment.txt
File metadata and controls
365 lines (313 loc) · 11.9 KB
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
#
# JAVA PROGRAMMING ASSIGNMENT
# BASIC APPROACHES
---------------------------------------------------------------------------------------
# QUESTION 1 ~
-----------------------------------------------------------------------
/**
* question 1
* @author benkimz
*/
import java.util.Scanner;
public class IncomeDebtRatio{
//DECLARE VARIABLES
private static double debt = 0, income;
private static double mortgages, automobile_loan, credit_loan;
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
//IncomeDebtRatio compute = new IncomeDebtRatio();
int number_of_users = 2;
// Loop 20 times requesting for details about several users
for(int i = 0; i < number_of_users; i++){
System.out.print("\n Enter User Details:-\n");
/*
* Take details about:
* income, mortgages, automobile loans, credit loans
*/
try{
System.out.print("\n Income: ");
income = keyboard.nextDouble();
System.out.print("\n Mortgages: ");
mortgages = keyboard.nextDouble();
System.out.print("\n Automobile Loan: ");
automobile_loan = keyboard.nextDouble();
System.out.print("\n Credit Card Loan: ");
credit_loan = keyboard.nextDouble();
debt = mortgages + automobile_loan + credit_loan;
// Print the debt income ratio
// Debt / Income
System.out.print("\n \t Debt / Income Ratio: " +
(debt / income) + "\n");
}catch(Exception e){
System.out.println("...........invalid input............");
}
}
}
}
# QUESTION 2 ~
-----------------------------------------------------------------
/**
*
* @author benkimz
*/
import java.util.Scanner;
public class DistanceLearning {
public static void dashes(int count){
for(int i=0;i<count;i++){System.out.print("-");}
}
public static void main(String args[]) {
Scanner keypad = new Scanner(System.in);
while(true){
dashes(30);
try{
System.out.print("\n Enter Hours: ");
int hours = keypad.nextInt();
if(hours <= 0){break;}
double cost = 0;
if(hours <= 15){
cost += hours * 500;
}else{
cost += hours * 445;
}
dashes(30);
System.out.print("\n Total cost: " + cost + "\n");
}catch(Exception e){
break;
}
}
}
}
# QUESTION 3 ~
--------------------------------------------------------------
/**
*
* @author benkimz
*/
import java.util.Scanner;
public class BodyMassIndex {
public static void dashes(int count){
for(int i=0;i<count;i++){System.out.print("-");}
}
public static void main(String args[]) {
Scanner keypad = new Scanner(System.in);
double weight, height;
while(true){
try{
dashes(30);
System.out.print("\n Enter Weight: ");
weight = keypad.nextDouble();
System.out.print("\n Enter Height: ");
height = keypad.nextDouble();
dashes(30);
System.out.print("\n Your BMI is: " +
(weight / (height * height)) + "\n");
}catch(Exception e){
break;
}
System.out.print("\n BMI VALUES \n");
dashes(30);
System.out.print("\n UNDER WEIGHT" + "\t\t\t" + "LESS THAN 18.5");
System.out.print("\n NORMAL" + "\t\t\t" + "BETWEEN 18.5 AND 24.9");
System.out.print("\n OVERWEIGHT" + "\t\t\t" + "BETWEEN 25 AND 29.9");
System.out.print("\n OBESE" + "\t\t\t" + "30 OR GREATER");
System.out.print("\n\n");
}
}
}
# QUESTION 4 ~
-------------------------------------------------------------------
/**
*
* @author benkimz
*/
import java.util.Scanner;
public class QuadraticEquations {
public static void main(String args[]) {
Scanner keypad = new Scanner(System.in);
double discriminant;
while(true){
try{
double a, b, c;
System.out.print("\n Enter coefficient a: "); a = keypad.nextDouble();
System.out.print("\n Enter coefficient b: "); b = keypad.nextDouble();
System.out.print("\n Enter coefficient c: "); c = keypad.nextDouble();
discriminant = Math.pow(b, 2) - (4 * a * c);
// PRINT SOME DASHES FOR A NICE LOOK
System.out.print('\n');for(int i=0;i<60;i++){System.out.print('-');}
if(discriminant == 0){
//ROOT1 == ROOT2
System.out.print("\n The roots are real and identical: [" +
(-b / 2 * a) + ", " + (-b / 2 * a) + "]\n\n");
}else if(discriminant > 0){
//ROOTS ARE REAL BUT UNIDENTICAL
System.out.print("\n The roots are real and unidentical: [" +
((-b + Math.sqrt(discriminant)) / 2 * a) + ", " + ((-b - Math.sqrt(discriminant)) / 2 * a) + "]\n\n");
}else{
//THE ROOTS ARE COMPLEX
System.out.print("\n The roots are complex: [" +
String.format("%.2f " + "+" + " %.2fi", (-b / 2 * a), (Math.sqrt(-discriminant) / 2* a)) +
", " + String.format("%.2f " + "+" + " %.2fi", (-b / 2 * a), (Math.sqrt(-discriminant) / 2* a)) + "]\n\n");
}
}catch(Exception e){
break;
}
}
}
}
---------------------------------------------------------------------------------------
#
# JAVA PROGRAMMING ASSIGNMENT
# AN ADVANCED APPROACH
---------------------------------------------------------------------------------------
# QUESTION 1 ~
-----------------------------------------------------------------
/**
* question 1
* @author benkimz
*/
import java.util.Scanner;
@FunctionalInterface
interface OutputFormatting{
public abstract void dashes(int count);
}
public class IncomeDebtRatio{
//DECLARE VARIABLES
private static double debt, income;
public static void main(String args[]){
OutputFormatting format = (int count)->{
for(int i=0;i<count;i++){System.out.print("-");}
};
Scanner keyboard = new Scanner(System.in);
//IncomeDebtRatio compute = new IncomeDebtRatio();
int users_no = 2;
// Loop 20 times requesting for details about several users
for(int i = 0; i < users_no; i++){
format.dashes(30); debt = 0.0;
System.out.print("\n Enter User Details:-\n");
/*
* Take details about:
* income, mortgages, automobile loans, credit loans
*/
try{
System.out.print("\n Income: ");
income = keyboard.nextDouble();
System.out.print("\n Mortgages: ");
debt += keyboard.nextDouble();
System.out.print("\n Automobile Loan: ");
debt += keyboard.nextDouble();
System.out.print("\n Credit Card Loan: ");
debt += keyboard.nextDouble();
format.dashes(30);
// Print the debt income ratio
System.out.print("\n \t Debt / Income Ratio: " +
(debt / income) + "\n");
}catch(Exception e){
System.out.println("...........invalid input............");
}
}
}
}
# QUESTION 3 ~
----------------------------------------------------------------
/**
*
* @author benkimz
*/
import java.util.Scanner;
public class BodyMassIndex {
public static void dashes(int count){
for(int i=0;i<count;i++){System.out.print("-");}
}
public static void main(String args[]) {
Scanner keypad = new Scanner(System.in);
double weight, height;
while(true){
try{
dashes(30);
System.out.print("\n Enter Weight: ");
weight = keypad.nextDouble();
System.out.print("\n Enter Height: ");
height = keypad.nextDouble();
dashes(30);
System.out.print("\n Your BMI is: " +
(weight / (height * height)) + "\n");
}catch(Exception e){
break;
}
System.out.print("\n BMI VALUES \n");
dashes(30);
printRow(2, "UNDER WEIGHT", "LESS THAN 18.5",
"NORMAL", "BETWEEN 18.5 AND 24.9",
"OVERWEIGHT", "BETWEEN 25 AND 29.9",
"OBESE", "30 OR GREATER");
System.out.print("\n\n");
}
}
public static void printRow(int cols, String... args){
int k = cols;
for(String element: args){
if(k > 0){
System.out.print(element + "\t\t\t");
}else{
System.out.print("\n" + element + "\t\t\t");
k = cols;
}
k -= 1;
}
}
}
# QUESTION 4 ~
----------------------------------------------------------------------
/**
*
* @author benkimz
*/
import java.util.Scanner;
@FunctionalInterface
interface RootsFinder{
abstract void getRoots(double a, double b, double c);
}
class Complex{
public String value(double real, double imaginary, String symb){
return String.format("%.2f " + symb + " %.2fi", real, imaginary);
}
protected String symb;
Complex(String... args){this.symb = args[0];}
}
public class QuadraticEquations {
public static void main(String args[]) {
//CUSTOM TYPE COMPLEX
Complex complex = new Complex("j");
RootsFinder roots = (double a, double b, double c) -> {
double discriminant = Math.pow(b, 2) - (4 * a * c);
System.out.print('\n');for(int i=0;i<60;i++){System.out.print('-');}
if(discriminant == 0){
//ROOT1 == ROOT2
System.out.print("\n The roots are real and identical: [" +
(-b / 2 * a) + ", " + (-b / 2 * a) + "]\n\n");
}else if(discriminant > 0){
//ROOTS ARE REAL BUT UNIDENTICAL
System.out.print("\n The roots are real and unidentical: [" +
((-b + Math.sqrt(discriminant)) / 2 * a) + ", " + ((-b - Math.sqrt(discriminant)) / 2 * a) + "]\n\n");
}else{
//THE ROOTS ARE COMPLEX
System.out.print("\n The roots are complex: [" +
complex.value((-b / 2 * a), (Math.sqrt(-discriminant) / 2* a), "+") + ", " +
complex.value((-b / 2 * a), (Math.sqrt(-discriminant) / 2* a), "-") + "]\n\n");
}
};
Scanner keypad = new Scanner(System.in);
while(true){
try{
double a, b, c;
System.out.print("\n Enter coefficient a: "); a = keypad.nextDouble();
System.out.print("\n Enter coefficient b: "); b = keypad.nextDouble();
System.out.print("\n Enter coefficient c: "); c = keypad.nextDouble();
roots.getRoots(a, b, c);
}catch(Exception e){
break;
}
}
}
}
---------------------------------------------------------------------------------------