Results 1 to 4 of 4

Thread: Generics

  1. #1
    bschmitt78 is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Generics

    I am writing a program using generics that is supposed to calculate the miles jogged each day, calculate the total calories burned each day, and calculate the sum of the calories for the week. It is supposed to print the days of the week on one line, the miles jogged on the second line, and the calories burned for each day on the third line.



    I highlighted a method in red to show where I am getting a syntax error - it doesn't like the fact I am trying to multiply a static final Number n variable to a Number element.



    Any help is appreciated!



    Here is my code:


    Java Code:
    package resolutiongenerics;
    
    import java.util.Scanner;
    import java.util.ArrayList;
    import java.lang.Number;
    
    public class Main
    {
    
        public static final Number n = 112;
        public static void main( String args[] )
        {
            Integer [] milesJogged = new Integer[7];
            String [] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
     Double [] calories = new Double[7];
            Scanner scan = new Scanner(System.in);
            for(String day:days)
                System.out.println("Enter miles jogged on " + day);
    
                for(int i = 0; i<=milesJogged.length-1; i++)
                {
                    milesJogged[i] = scan.nextInt();
                }
    
                printArray(days);
                System.out.println("\nMiles jogged:");
                printArray(milesJogged);
                System.out.println("\nCalories burned each day:");
                printArray(calories);
                System.out.println("\nTotal burned for the week");
                sum(calories);
    
                } // end main
    
    
    
               [COLOR="Red"]  public static void caloriesBurned(ArrayList<? extends Number > milesPerDay) //generic method to calculate calories burned each day
                {
                    for(Number element:milesPerDay)
                     System.out.printf("$s", element * n);
    
                    System.out.println();
                }[/COLOR]            public static double sum(ArrayList<? extends Number> burnedEachDay) // generic method to add each days' calories burned together
                {
                    double total = 0;
                    for(Number element:burnedEachDay)
                        total += element.doubleValue();
    
                    return total;
                }
    
                public static < T > void printArray( T[] inputArray ) // generic method to print out each array, regardless of data type
                {
                    // display array elements
                    for ( T element : inputArray )
                        System.out.printf( "%s ", element );
    
                    System.out.println();
                } // end method printArray
    
    
    
    } //end class Main
    Moderator Edit: Code tags added to aid readability
    Last edited by Fubarable; 03-16-2010 at 03:15 AM. Reason: Moderator Edit: Code tags added to aid readability

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    What you're effectively trying to do is this:

    Object a1;
    Object a2;

    double x = a1 * a2; // multiplication not defined for Objects!!!

    why don't you instead use

    element.doubleValue * n.doubleValue ()

    ?

  3. #3
    bschmitt78 is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    Thank you! I changed the method as follows:

    public static double caloriesBurned(ArrayList<? extends Number > milesPerDay) //generic method to calculate calories burned each day
    {
    double burned = 0;
    for(Number element:milesPerDay)
    burned = element.doubleValue() * n.doubleValue();
    return burned;
    }

    However, I am getting the following result now from the printArray() method:

    Calories burned each day:
    null null null null null null null

    It seems to me my array in main() is not being populated with the 7 values calculated in the caloriesBurned() method. CaloriesBurned() is supposed to take the miles entered and multiply each element by 112 (n) and return those values to that array.

    Any ideas as to where I am off?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Generics Question
    By jdgallag in forum New To Java
    Replies: 8
    Last Post: 10-28-2008, 07:15 PM
  2. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 08:58 PM
  3. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 04:08 AM
  4. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-11-2008, 12:08 AM
  5. Generics
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 10:10 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
  •