1. Write an interactive program that will prompt to the user for 3 integers and calculate the mean. Then, the program will print out the mean. [You need to include comments in the program.] public class MeanFunction
{
public static void main (String[] args) { int a; int b; int c; double x; System.out.println("Give me three integers"); a = In.getInt(); b = In.getInt(); c = In.getInt(); x = (a+b+c)/3; System.out.println("Mean is " + x); } // main method
} // MeanFunction class
2. Write a program that asks the user to input 3 integers and print out the largest number.
a) Method 1: Use if statement in your program. public class MaxFunction
{
public static void main (String[] args) { //Methode 1 int a; int b; int c; System.out.println("Give me three integers"); a = In.getInt(); b = In.getInt(); c = In.getInt(); if(a>b && a>c){ System.out.println(a + " is the largest"); }else if(b>a && b>c){ System.out.println(b + " is the largest"); }else{ System.out.println(c + " is the largest"); } } // main method
} // MaxFunction class
b) Method 2: Use Math.min() in your program.(Please see textbook page74 as reference) public class MaxFunction2
{
public static void main (String[] args) { int a; int b; int c; int x; int y; System.out.println("Give me three integers"); a = In.getInt(); b = In.getInt(); c = In.getInt(); x = Math.max(a, b); y = Math.max(c, x); System.out.println("largest number is " + y); } // main method
} // MaxFunction2 class