/* Remove all parentheses that do not change the output. Example: Replace int x
ID: 3744278 • Letter: #
Question
/* Remove all parentheses that do not change the output.
Example:
Replace
int x = (1 +4);
with
int x = 1 + 4;
*/
public class Math130HW2 {
public static void main(String[] args) {
int x = (1 + 9) * 2;
System.out.println("1: " + x);
x = (15 / 3) % 2 + 2 * 7;
System.out.println("2: " + x);
x = 4 + (7 * 8);
System.out.println("3: " + x);
x = (4 + 500);
System.out.println("4: " + x);
String s = (1 + 99) + " abc";
System.out.println("5: " + s);
s = 2 + (9 + 5) + " abc";
System.out.println("6: " + s);
s = (4 + 2) + " abc " + (100 + 5);
System.out.println("7: " + s);
x = (99 / 7) * 6;
System.out.println("8: " + x);
x = 22 / 7 * (3 + 1);
System.out.println("9: " + x);
s = 4 / 3 + " abc " + (34 / 7);
System.out.println("10: " + s);
s = "2" + (3 + "4");
System.out.println("11: " + s);
s = 2 + (3 + "4");
System.out.println("12: " + s);
s = (2 + 3) + "4";
System.out.println("13: " + s);
s = (6 + "7") + (1 + "100");
System.out.println("14: " + s);
s = "abc " + (7 / 2) + 4;
System.out.println("15: " + s);
s = "abc " + 7 / (2 + 4);
System.out.println("16: " + s);
s = "hello";
x = (4 + s.length());
System.out.println("17: " + x);
s = ("one " + " ") + " two" + (1 + 4);
System.out.println("18: " + s);
}
}
The correct output is
1: 20
2: 15
3: 60
4: 504
5: 100 abc
6: 16 abc
7: 6 abc 105
8: 84
9: 12
10: 1 abc 4
11: 234
12: 234
13: 54
14: 671100
15: abc 34
16: abc 1
17: 9
18: one two5
Explanation / Answer
public class Math130HW2 { public static void main(String[] args) { int x = (1 + 9) * 2; System.out.println("1: " + x); x = 15 / 3 % 2 + 2 * 7; System.out.println("2: " + x); x = 4 + 7 * 8; System.out.println("3: " + x); x = 4 + 500; System.out.println("4: " + x); String s = 1 + 99 + " abc"; System.out.println("5: " + s); s = 2 + 9 + 5 + " abc"; System.out.println("6: " + s); s = 4 + 2 + " abc " + (100 + 5); System.out.println("7: " + s); x = 99 / 7 * 6; System.out.println("8: " + x); x = 22 / 7 * (3 + 1); System.out.println("9: " + x); s = 4 / 3 + " abc " + 34 / 7; System.out.println("10: " + s); s = "2" + 3 + "4"; System.out.println("11: " + s); s = 2 + (3 + "4"); System.out.println("12: " + s); s = 2 + 3 + "4"; System.out.println("13: " + s); s = 6 + "7" + 1 + "100"; System.out.println("14: " + s); s = "abc " + 7 / 2 + 4; System.out.println("15: " + s); s = "abc " + 7 / (2 + 4); System.out.println("16: " + s); s = "hello"; x = 4 + s.length(); System.out.println("17: " + x); s = "one " + " " + " two" + (1 + 4); System.out.println("18: " + s); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.