For this lab you will write a program with a main method and two other methods.
ID: 3684645 • Letter: F
Question
For this lab you will write a program with a main method and two other methods. The main method handles all the user input and all the println statements. It asks the user to enter 3 numbers. The main method should call the two other methods and pass the three numbers to both of them. The main method should print out all the results. The first method is called hasDuplicates and it returns true if any of the numbers are the same, and false otherwise. The second method is called hasAnA and it returns true if the average of the numbers is 90 or more and false otherwise. The main method will also need if statements so that the output looks similar to this…. Enter a number: 80 Enter another number: 85 Enter another number: 80 Some of your numbers are duplicates! Sorry, you do not have an A.
Explanation / Answer
import java.util.Scanner;
public class Duplicate {
public static void main(String args[])
{
int a[]=new int[3];
int i;
System.out.println("enter the three numbers");
Scanner p=new Scanner(System.in);
for(i=0;i<3;i++)
{
a[i]=p.nextInt();
}
boolean f=hasDuplicates(a);
System.out.println(f);
f=hasAnA(a);
System.out.println(f);
if((a[0]==a[1])||(a[0]==a[2])||(a[1]==a[2]))
System.out.println("sorry some of your numbers have duplicates you do not have an A");
else
System.out.println("no duplication");
}
public static boolean hasDuplicates(int[] a) {
if((a[0]==a[1])||(a[0]==a[2])||(a[1]==a[2]))
return true;
else
return false;
}
private static boolean hasAnA(int[] a) {
int sum=0;
sum=sum+a[0]+a[1]+a[2];
float avg=0.0f;
avg=sum/3;
if(avg>90)
return true;
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.