Guily Gwo
Evennumber
1 Answer(s)      6 years ago
Posted in : Java Beginners

Write an application program that prompts the user to key in 5 whole numbers. The program will then print out the following information (based on the 10 numbers keyed in). Do not use an array. Provide a tester class to test the implementation of your solution. Writing the entire program in a single Class is not allowed.

a) All even numbers b) The average c) All numbers smaller than the average value calculated in (b) d) All numbers larger than the average value calculated in (b)

Ads

View Answers

October 26, 2010 at 5:22 PM


Hello Friend,

Try this:

import java.util.*;
class EvenNumber{
 public boolean findEven(int num){
     if(num%2==0){
         return true;
     }
     return false;
 }
 public int average(int sum){
     int avg=sum/10;
     return avg;
 }
 public int smallerThanAverage(int num,int sum){
     int avg=average(sum);
     if(num<avg){
         return num;
     }
    return 0;
 }

 public int greaterThanAverage(int num,int sum){
     int avg=average(sum);
     if(num>avg){
         return num;
     }
    return 0;
 }
}
public class Tester{
    public static void main(String[] args){
        EvenNumber even=new EvenNumber();
        int sum=0;
        ArrayList<Integer> list=new ArrayList<Integer>();
        Scanner input = new Scanner(System.in);
        System.out.println("Enter 10 numbers:");
        for (int i=1;i<=10;i++){
            int number = input.nextInt();
            list.add(number);
        }
        System.out.println("Even Numbers are: ");
        for(int i=0;i<list.size();i++){
            sum+=list.get(i);
            if(even.findEven(list.get(i))){
            System.out.println(list.get(i));
            }
        }
        int avg=even.average(sum);
        System.out.println("Average of numbers: "+avg);
        System.out.println("Numbers smaller than Average: ");
        for(int i=0;i<list.size();i++){
        int num=even.smallerThanAverage(list.get(i),sum);
        if(num!=0){
        System.out.println(num);
        }
        }

        System.out.println("Numbers greater than Average: ");
        for(int i=0;i<list.size();i++){
        int num=even.greaterThanAverage(list.get(i),sum);
        if(num!=0){
        System.out.println(num);
        }
        }
            }
}

Thanks










Related Tutorials/Questions & Answers:
Evennumber
java.util.*; class EvenNumber{ public boolean findEven(int num){ if(num%2==0...){ EvenNumber even=new EvenNumber(); int sum=0; ArrayList<
Check even or odd number.
java.io.InputStreamReader; public class EvenNumber{ public static void main(String
Advertisements

Ads

Advertisement