Evennumber

Ads

 
 
 

Share on Google+Share on Google+

Guily Gwo
Evennumber
1 Answer(s)      6 years and 7 months 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


Ads









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
Tutorials   
Java Spring Hibernate Struts Training java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile?

Ads

 
Advertisement null

Ads