Question

Simplify the following piece of code: (java) if (a>b) x = a + b; if (a>0)...

Simplify the following piece of code: (java)

if (a>b)
x = a + b;
if (a>0)
x = a + b;

Homework Answers

Answer #1

We can simplify these statements as :

if a>b OR a>0 then x=a+b

import java.util.Scanner;
public class simplify {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
       System.out.println("Enter value of a");
       float a=scanner.nextFloat();
        float x=0;
        System.out.println("Enter value of b");
        float b=scanner.nextFloat();

        if((a>b)||(a>0)){
            x=a+b;
        }
     System.out.println("Value of x= "+x);
    }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The following code should print X is greater than 0. However, the code has errors. Fix...
The following code should print X is greater than 0. However, the code has errors. Fix the code so that it compiles and runs correctly. (Rewrite the program with all the fixes) public class Test1 { public static void main(String[] args) { int x = 3; if (x > 0 System.out.println("x is greater than 0") else System.out.println(x is less than or equal 0"); } } Must be in java and easy to understand grade 11 course
Java programming. 1) What, if anything, is wrong with the following Java code? void test(String x)...
Java programming. 1) What, if anything, is wrong with the following Java code? void test(String x) { switch (x) { case 1: break; case 2: break; case 0: break; default: break; case 4: break; } } Select one: a)Each case section must end with a break statement. b)The case label 0 must precede case label 1. c)There is nothing wrong with the Java code. d)The default label must be the last label in the switch statement. e)The variable x does...
In Java 1. What will be the value of x after the following section of code...
In Java 1. What will be the value of x after the following section of code executes: int x = 3; if (x > 3)     x = x – 2; else     x = x + 2; A. 1 B.3    C.5              D.7 2. What will be the value of y after the following section of code executes: int y = 5, z = 3; if (y > 4){      z = 2;      y = y – 1;...
For the following piece of Java code: a) State which object creation mechanism is being used...
For the following piece of Java code: a) State which object creation mechanism is being used b) Explain how you determined which object creation mechanism is being used c) Complete the missing portions of the code (indicated by underscores) ____________________ part1 { public void function1(String someInput); public double function2(); public int function3(int thisVal, int thatVal); } public ____________________part2 ____________________ { double aNumber; public void function1(String someInput) { System.out.println(someInput); } public double function2() { aNumber = 6.0; return aNumber; } public...
Machine Language - 1. Which of the following assembly code represents the high-level Java code below?...
Machine Language - 1. Which of the following assembly code represents the high-level Java code below? int x; int i = 5; if (i < 0) x = -1; else x = 1; @5 D=M @i M=D @BRANCH M;JLT @x M=1 @END 0;JMP (BRANCH) @x M=-1 (END) @END 0;JMP @5 D=M @i M=D @BRANCH M;JGE @x M=1 @END 0;JMP (BRANCH) @x M=-1 (END) @END 0;JMP @5 D=M @i M=D @BRANCH M;JLT @x M=1 @END 0;JMP (BRANCH) @x M=-1 (END) @END...
(4) 1. Simplify the following assignments/calculations if possible: a. a = 0; b = 0; c...
(4) 1. Simplify the following assignments/calculations if possible: a. a = 0; b = 0; c = 0; d = 0; _____________________________ b. n1 = n1 % n2; _____________________________ c. n3 += 1; _____________________________ d. n4 = n5 / n4; _____________________________ 2. Suppose that the statement x = 12; has already been done. What values will the following printf statements output assuming that the statements are done in the following order: a. printf("%d\n", ++x); __________ b. printf("%d\n", x); __________ c....
(java) a. The following code has compilation AND logic errors. Rewrite the code (using the while...
(java) a. The following code has compilation AND logic errors. Rewrite the code (using the while loop) so it will compile correctly and print all the even numbers from 0 to 10 on one line separated by a space: int j = 0; while j > 10 ; j + 2; System.println(j, “ “); b.You want to print the values of integers a and b to an external file “test123.txt”, assigned to the PrintWriter variable output. Correct the following statements:...
1. a. Write the Java code to fill a Java array of ints of size 100...
1. a. Write the Java code to fill a Java array of ints of size 100 with the value -1. b. Write the Java code to create an ArrayList of Strings, and add the Strings "Just", "Do", "It!" to it.
(c programming) Examine the following code: int x = 5; while(x = 0){ x += x;...
(c programming) Examine the following code: int x = 5; while(x = 0){ x += x; } while(x-- > 5){} What is the final value of x? Does the assignment expression in the first loop occur and if so, what value does it evaluate to?
Java programming 1) Which among the choices is the following Java expression equivalent to? !((b !=...
Java programming 1) Which among the choices is the following Java expression equivalent to? !((b != 0) || (c <= 5)) Select one: a)       (b != 0) && (c <= 5) b)       (b == 0) && (c > 5) c)       !((b <> 0) && (c <= 5)) d)       (! (b = 0)) && (! (c > 5)) e)       (b == 0) && (c <= 5) 2) The scope of a variable is an important feature in Java, the following variable...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT