Results 1 to 2 of 2
  1. #1
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Java Odd and Even Average

    In the class, you will have three instance variables, 1) array to hold the numbers, 2) the odd average, and 3) the even average. The constructor should accept as input the array from the tester class and assign it to the instance variable. You should then have a method, findAverage, that will read the array and then calculate the average of the even input values and the average of the odd input values. (Hint: You can use % (mod) to determine if a number is even or odd). Your program will need a getOddAverage and a getEvenAverage method.

    ---------------------------------------...

    public class oddEvenAverage
    {

    int [] myArray = new int[10];
    public double oddAverage;
    public double evenAverage;

    public oddEvenAverage(int number){
    int myArray = number;
    }

    int evenSum = 0;
    int oddSum = 0;
    int evenCount = 0;
    int oddCount = 0;
    public double getEvenAverage(){
    for (int m=0;m<9;m++){
    if ((myArray[m]) / (2) == 0){
    evenSum = evenSum + myArray[m];
    evenCount = evenCount+1;
    }

    }
    evenAverage = evenSum/evenCount;
    return evenAverage;
    }

    public double getOddAverage(){
    for (int n=0;n<9;n++){
    if ((myArray[n]) / (2) != 0){
    oddSum = oddSum+myArray[n];
    oddCount = oddCount+1;
    }

    }
    oddAverage = oddSum/oddCount;
    return oddAverage;
    }
    }
    -----------------------------------------------------------------
    public class oddEvenAverageTester
    {
    public static void main( String [] args){
    System.out.println("Enter a odd or even number: ");
    Scanner in = new Scanner(System.in);
    int number = in.nextInt();
    oddEvenAverage average = new oddEvenAverage(number);
    System.out.println("even average: " +average.getEvenAverage());
    System.out.println("odd average: " +average.getOddAverage());
    ---------------------------------------------------------------------------------
    What am i doing wrong here? I can't get it working!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Java Odd and Even Average

    I can't get it working!
    Please explain.
    Post the output and say what is wrong with it.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. java Calculating the mean average
    By danielki in forum New To Java
    Replies: 5
    Last Post: 02-22-2014, 03:39 AM
  2. How to make a java program that gets average test scores?
    By johnsonparkar224 in forum New To Java
    Replies: 1
    Last Post: 04-02-2013, 12:40 PM
  3. need some help finding average on java
    By jtw0812 in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 05:26 AM
  4. I need help with my java servlet homework(average)
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-23-2007, 10:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •