Answer to Question #208092 in Java | JSP | JSF for Talha

Question #208092

Consider the following program segment:

public class Ch7_PrExercise4

{

public static void main(String[] args)

{

int num;

double dec;

.

.

.

}

public static int one(int x, int y)

{

.

.

.

}

public static double two(int x, double a)

{

int first;

double z;

.

.

.

}

}


a. Write the definition of method one so that it returns the sum of

x and y if x is greater than y; otherwise, it should return x minus 2

times y.

b. Write the definition of method two as follows:

i. Read a number and store it in z.

ii. Update the value of z by adding the value of a to its previous value.

iii. Assign the variable first the value returned by method one with

the parameters 6 and 8.

iv. Update the value of first by adding the value of x to its previous

value.

v. If the value of z is more than twice the value of first, return z;

otherwise, return 2 times first minus z.

c. Write a Java program that tests parts a and b. (Declare additional variables

in the method main, if necessary.)


1
Expert's answer
2021-06-18T02:54:03-0400
import java.util.Scanner;

public class Ch7_PrExercise4 {
    public static void main(String[] args) {
        System.out.println(two(5,3));
    }

    public static int one(int x, int y) {
        return x > y ? x + y : x - y * 2;
    }

    public static double two(int x, double a) {
        Scanner in = new Scanner(System.in);
        int first = one(6, 8);
        double z = in.nextDouble();
        z += a;
        first += x;
        return z > first * 2 ? z : 2 * first - z;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS