-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssignment_3.java
More file actions
438 lines (368 loc) · 14.6 KB
/
Copy pathAssignment_3.java
File metadata and controls
438 lines (368 loc) · 14.6 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
Question 1. Find the error in the following code and explain in few lines why it is wrong. (Score 1) Here is the code.
public class Book{
int size;
int price;
String name;
/*
This constructor should set the default price and name of the book as well. Would be better if we just call the more general constructor here.
If the default price = 0 and name = "unnamed"
this(size, 0, "unnamed");
Another way would be to replace this with a setter
public void setSize(int size)
{
this.size = size
}
*/
public Book(int size){
this.size = size;
}
public Book(int size, int price, String name){
super(); //this is use to inherit from parent class constuctor but this does not seem to inherit anything
this.size = size;
this.price = price;
this.name = name;
}
/*
This function would cause a compiler error as the signature Book (int) is already defined in the first constructor. This should be replaced with a setter
public void setPrice(int price) {
this.price = price;
}
*/
public Book(int price){
this.price = price;
}
/*
No return type for this function, would result in a compiler error. In current form this function should have return type String.
public String setName(String name)
Although if this function is a setter the correct signature would be
public void setName(String name) {
this.name = name
}
*/
public setName(String name){
return name; //function is named as a setter, so instead of returning name, it should be setting the value of name e.g. this.name = name.
// Although this wouldn't result in a compile error
}
}
........................................................................Question 2 -----------------------------------------------------
Find the error in the following code and explain in few lines why it is wrong. (Score 1) Here is the code.
class Clock{
String time;
/*
Function returns value of time, but return type is provided as void. This would result in a compiler error.
The correct signature of the method should be
String getTime()
*/
void getTime(){
return time;
}
void setTime(String t){
time = t ;
}
}
.....................................................................question 3.........................................................
Write a Java function to remove vowels in a string. (Score 2)
i. The function should take a string as input.
ii. Should return the input string after omitting the vowels.
import java.util.Scanner;
public class Remove_Vowels{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string");
String input = sc.nextLine();
String output =removeVowelsFromString(input) ;
System.out.println(output);
}
public static String removeVowelsFromString(String input){
String noVowels = "";
for (int i=0; i<input.length(); i++)
{
if (!isVowel(Character.toLowerCase(input.charAt(i))))
{
noVowels += input.charAt(i);
}
}
return noVowels;
}
public static boolean isVowel(char x) {
String vowels = "aeiou";
for (int i=0; i<5; i++)
{
if (x == vowels.charAt(i))
return true;
}
return false;
}
}
.....................................................................Question 4...............................................................
Write a java function to check if two strings are Anagrams or not. (Score 2)
i. The function should take two input strings.
ii. Should return a boolean ‘true’ if the inputs are Anagrams else return ‘false’. Here is the prototype you can work with
import java.util.Arrays;
import java.util.Scanner;
public class Anagrams{
public static void main(String args[]){
String s1="cinema";
String s2="iceman";
boolean result = checkIfTwoStringsAreAnagrams(s1 , s2);
if(result==true){
System.out.println("given strings are Anagrams");
}
else{
System.out.println("given strings are not Anagrams");
}
}
// for checking two strings anagrams here i am doing (checking)-(here String s1=cinema , String s2 = iceman;)
// 1)check length,if length is not equal it will return false and if equal it will process further (lenghth is equal)
//2) sort strings (aceimn ; aceimn)
//3) compare sorted Strings (aceimn = aceimn), if they are same then return true(anagram),else flase(not anagram)
public static boolean checkIfTwoStringsAreAnagrams(String s1, String s2){
// isAnagrams is not used
boolean isAnagrams = true;
//checking length of strings
if(s1.length()!=s2.length()){
return false;
}
//sort string by calling sortString function
String a = sortString(s1);
String b = sortString(s2);
// comparing
if(a.equals(b)){
return true;
}
else{
return false;
}
}
// sort both string for comparison
// Method to sort a string alphabetically
public static String sortString(String input){
// convert input string to char array
char tempArray[] = input.toCharArray();
// sort tempArray
Arrays.sort(tempArray);
// return new sorted string
return new String(tempArray);
}
}
........................................................................Question 5...........................................................
Create a calculator that can perform the following features. (Total Score 4)
i. The calculator should be able to perform Addition, subtraction, multiplication, division. (Score 2)
ii. Should be able to perform squareRoot, square, cube. (Score 1) iii. Should be able to convert ‘Fahrenheit-Celsius’ , ‘Feet-Inches’. (Score 1)
Extra Credit(Score 2)
The calculator should be able to solve a quadratic equation and return the solution as array. i. This function should take three arguments. ii. For example, if quadratic equation is Ax2 + Bx + C. The function should take A,B,C as arguments and return a solution as array.
import java.util.Scanner;
// Main calculator class, its just an entry point, actual work happens in class CalcEngine.
public class Calculator {
public static void main(String[] args) {
Ui calcUI = new Ui(new CalcEngine());
calcUI.start();
}
}
// The actual calculator operations are performed by this class
class CalcEngine {
double add (double a, double b) {
return a+b;
}
double subtract (double a, double b) {
return a-b;
}
double multiply (double a, double b) {
return a*b;
}
double divide (double a, double b) {
// We are assuming the inputs are sanitized, so there wouldn't be any divide by zero.
return a / b;
}
double squareRoot(double a) {
return Math.sqrt(a);
}
double square (double a) {
return a * a;
}
double cube (double a) {
return a * a * a;
}
double fahrenheitTocelsius (double fahrenheit) {
double celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
double celsiusToFahrenheit (double celsius) {
double fahrenheit = (celsius * 9 / 5) + 32;
return fahrenheit;
}
double feetToInches (double feet) {
return feet * 12;
}
double inchestoFeet (double inches) {
return inches / 12;
}
// Solves a quadratic equation of form ax^2+bx+c. return the roots in an array.
// number of roots depends on the value of discriminant = b^2 - 4ac
double[] quadraticEquation (double a, double b, double c) {
double [] roots;
double discriminant = (b * b) - (4 * a * c);
if (discriminant > 0){
// The equation has two roots
roots = new double[2];
// What if a == 0? Wrong form, should be (-b + sqrt(discriminant)) / (2 * a)
roots[0] = ((-b + discriminant) / (2 * a));
roots[1] = ((-b - discriminant) / (2 * a));
}
else if (discriminant == 0) {
//The equation has one root
roots = new double[1];
roots[0] = -b/(2*a);
}
else {
// Discriminant < 0 means there are no real roots of the equation
roots = new double[0];
}
return roots;
}
}
// This class handle the UI of the calculator, providing menu choices and recording responses.
class Ui {
private CalcEngine engine;
// Maximum three operands are possible
double operand1;
double operand2;
double operand3;
Scanner sc;
public Ui(CalcEngine engine) {
this.engine = engine;
sc = new Scanner(System.in);
}
int getInput() {
System.out.println("Please select an operation: ");
System.out.println("Press 1 for Addition");
System.out.println("Press 2 for Subtraction");
System.out.println("Press 3 for Multiplication");
System.out.println("Press 4 for Division");
System.out.println("Press 5 for Square root");
System.out.println("Press 6 for Square");
System.out.println("Press 7 for Cube");
System.out.println("Press 8 for Fahrenheit to Celsius conversion");
System.out.println("Press 9 for Celsius to Fahrenheit conversion");
System.out.println("Press 10 for Feet to Inches conversion");
System.out.println("Press 11 for Inches to Feet conversion");
System.out.println("Press 12 for Solving a quadratic equation");
System.out.println("Press 0 to exit");
return sc.nextInt();
}
void start(){
boolean done = false;
while (done == false) {
int input = getInput();
switch (input) {
case 0:
// Exit
done = true;
break;
case 1:
// Addition
inputOperand(2);
System.out.println("Result = " + engine.add(operand1, operand2));
break;
case 2:
// Subtraction
inputOperand(2);
System.out.println("Result = " + engine.subtract(operand1, operand2));
break;
case 3:
// Multiplication
inputOperand(2);
System.out.println("Result = " + engine.multiply(operand1, operand2));
break;
case 4:
//Division
inputOperand(2);
// Make sure that second operand (divisor) is not zero
if (operand2 != 0.0){
System.out.println("Result = " + engine.divide(operand1, operand2));
} else {
System.out.println("Error: Can not divide by zero !");
}
break;
case 5:
// SquareRoot
inputOperand(1);
// Make sure input is not negative
if (operand1 < 0.0) {
System.out.println("Error: Can not calculate sqrt of a negative number");
} else {
System.out.println("Result = " + engine.squareRoot(operand1));
}
break;
case 6:
// Square
inputOperand(1);
System.out.println("Result = " + engine.square(operand1));
break;
case 7:
// Cube
inputOperand(1);
System.out.println("Result = " + engine.cube(operand1));
break;
case 8:
// Fahrenheit To Celsius
inputOperand(1);
System.out.println("Result = " + engine.fahrenheitTocelsius(operand1));
break;
case 9:
// Celsius to Fahrenheit
inputOperand(1);
System.out.println("Result = " + engine.celsiusToFahrenheit(operand1));
break;
case 10:
// Feet to Inches
inputOperand(1);
System.out.println("Result = " + engine.feetToInches(operand1));
break;
case 11:
// Inches to Feet
inputOperand(1);
System.out.println("Result = " + engine.inchestoFeet(operand1));
break;
case 12:
// Quadratic equation
inputOperand(3);
double[] result = engine.quadraticEquation(operand1, operand2, operand3);
if (result.length == 0) {
System.out.println("No roots for this equation !!");
} else if (result.length == 1) {
System.out.println("root = " + result[0]);
} else {
System.out.println("root 1 = " + result[0] + " root 2 = " + result[1]);
}
break;
default:
// Should not reach here, wrong selection
System.out.println("Invalid input, please try again range (0-12)");
}
}
}
void inputOperand(int numberOfOperands) {
// Initialize all operands to zero
operand1 = 0;
operand2 = 0;
operand3 = 0;
if (numberOfOperands >= 1) {
if (numberOfOperands == 1) {
// only one operand
System.out.println("Enter value");
} else {
System.out.println("Enter first value");
}
operand1 = sc.nextDouble();
}
if (numberOfOperands >= 2) {
System.out.println("Enter second value");
operand2 = sc.nextDouble();
}
if (numberOfOperands >= 3) {
System.out.println("Enter third value");
operand3 = sc.nextDouble();
}
}
}