Given int item1, item2, item3, avevalue; item1 = 90; item2 = 67; item3 = 72; ave
ID: 3733481 • Letter: G
Question
Given
int item1, item2, item3, avevalue;
item1 = 90;
item2 = 67;
item3 = 72;
avevalue = 0;
Write a program to perform the following tasks.
I. declare and initialize item1, item2, item3 and avevalue as above.
II. compute the average value – avevalue on item1, item2, and item3, and then display
avevalue on the screen.
III. define int max(int a, int b, int c) to return the maximum value among the three input arguments.
IV. using max() to find out the maximum value among item1, item2, and item3, return the maximum
value, and then display it on the screen.
Explanation / Answer
MaxOfThree.java
public class MaxOfThree {
public static void main(String[] args) {
int item1, item2, item3, avevalue;
item1 = 90;
item2 = 67;
item3 = 72;
avevalue = 0;
avevalue=(item1+item2+item3)/3;
System.out.println("Average: "+avevalue);
System.out.println("Max: "+max(item1,item2, item3));
}
public static int max(int a, int b, int c) {
if(Math.max(a, b) == a && Math.max(a, c) == a ) {
return a;
} else if(Math.max(c, b)==b) {
return b;
} else {
return c;
}
}
}
Output:
Average: 76
Max: 90
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.