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
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...
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;...
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...
(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....
Computer archieture 1. Given this piece of code fragment: for (int x = 0; x <...
Computer archieture 1. Given this piece of code fragment: for (int x = 0; x < 10; x++) {     if (x >= 5)         cout << "OK" << endl; } Assuming the branch prediction by default is “TAKEN”, What is the accuracy of branch prediction of the cout statement when we use a 1-bit branch history? What is the accuracy of branch prediction of the cout statement when we use a 2-bit branch history?
Complete the Java code. The code should print “x is in numbers” if the integer x...
Complete the Java code. The code should print “x is in numbers” if the integer x is one of the values stored in numbers. If x is not in numbers, your code should print “x is not in numbers” public static void main(String[] args){                         int[] numbers = <some array values>;                         int x = <some value>;             }
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...
(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.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT