Computer Science, asked by teck20idea, 3 months ago

Rewrite the following statement using if else statement and write a Java program to find the value of z. int z=(a-b)>(b-a)?(a-b):(a-b)<(b-a)?(b-a):(a+b); Data Members: a, b and z Member functions/methods: void getab() - to get the values a and b. void calculate() - to find the value of z. void display() - to display the value of z. 1 SEE ANSWER

Answers

Answered by Anonymous
3

The statement rewritten with if...else will be:

if((a-b)>(b-a))

z= (a-b);

else if((a-b)<(b-a))

z=(b-a);

else

z=(a+b);

The program will be:

import java.util.Scanner;

public class GetX{

int a, b, z;

GetX() { //Default constructor

a=b=z=0;

}

Scanner sc=new Scanner(System.in);

void getab(){

System.out.println("Enter the value of a:");

a=sc.nextInt();

System.out.println("Enter the value of b:");

b=sc.nextInt();

}

void calculate(){

if((a-b)>(b-a))

z= (a-b);

else if((a-b)<(b-a))

z=(b-a);

else

z=(a+b);

}

void display(){

GetX obj =new GetX();

obj.getab();

obj.calculate();

System.out.println("The value of z is: " + z);

}

}

Similar questions