11 Replies - 987 Views - Last Post: 22 April 2010 - 07:12 PM Rate Topic: -----

#1 JavaBunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-February 10

getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 04:57 PM

My code compiles but the initial grades & final grades outputs are backwards, the highest grade is not correct and the difference between the highest grade and 100 is not being added to the original array values and returned as the final grades.
Here is what I want to get:

Initial Grades:
Student Initial Grade
1 95
2 79
3 89
4 78
5 82

Highest Grade Is 95

Student Final Grade
1 100
2 84
3 94
4 83
5 87

And here is what I get:
Final Grades:

Student Final Grade
Student 1 95
Student 2 79
Student 3 89
Student 4 78
Student 5 82

Initial Grades:
Student Initial Grade
Student 1 95
Student 2 79
Student 3 89
Student 4 78
Student 5 82

Highest Grade is 100

Here is my code. Why are my getMaximum & modifyArray methods not working? Please help!
//GradeCurveArray.java

import java.util.*;

public class GradeCurveArray
{
    
    public static void main (String[] args)
    { // Output Grade Array
        int[]gradesArray = {95, 79, 89, 78, 82};
        outputGrades(gradesArray);
        System.out.println( "\n\nInitial Grades:\n");
        System.out.printf("%s%15s", "Student", "Initial Grade");

        for (int student  = 0; student < gradesArray.length; student++)
            System.out.printf("\nStudent %2d%10d", student+1, gradesArray[ student ]);

        modifyArray (gradesArray);//pass array reference
        
        System.out.printf("\n%s %d\n", "\n\nHighest Grade Is", getMaximum(gradesArray));
    }//end main

    public static int getMaximum(int[]grades) 
        { 
        int highGrade = grades[0]; 
        for (int iterate : grades) 
        { 
                if (iterate > highGrade) 
                { 
                    highGrade = iterate; 
                } 
        }//end outer for 
        return highGrade; //return the highest grade 
        }//end Method getMaximum

    //Add Grade Curve amount to array elements
    public static void modifyArray(int[]gradesArray)
    {
        int amountToAdd = 100-getMaximum(gradesArray);

        for (int i=0; i < gradesArray.length; i++)
            gradesArray[i] += amountToAdd;

    }//end method modifyArray


    public static void outputGrades(int[]gradesArray)
    {
        //output final grade array
        System.out.println("Final Grades:\n");
        System.out.printf("%s%15s", "Student", "Final Grade");

        for (int student  = 0; student < gradesArray.length; student++)
            System.out.printf("\nStudent %2d%10d", student+1, gradesArray[ student ]);

    }//end method outputGrades

}//end class GradeCurveArray


Is This A Good Question/Topic? 0
  • +

Replies To: getMaximum & modifyArray Methods Not Working

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 05:22 PM

your getMaximum should be ok, i dont see anything wrong with it, check out your methods.. what is modifyArray supposed to do?

This post has been edited by mostyfriedman: 22 April 2010 - 05:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 JavaBunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-February 10

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 05:27 PM

If getMaximum is OK then why is it returning 100 rather than 95 which is the highest grade in the array? The modifyArray is supposed to take the highest grade in the array and subtracting it from 100, add the difference to the grades in the array so that the highest grade ends up being 100; for example 95 is the highest grade in the array so 5 should be added to all grades in the arrray as the final grades after the curve and then re-displayed. Does that make sense?
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 05:36 PM

getMaximum looks ok to me, it will return the maximum value in the array, i also tested it to make sure, and it is ok...it is retuning 100 rather than 95 because you are displaying the max value of the array AFTER you modify the array, so now the max value will be for sure 100

actually all the methods you got look correct to me, just make sure that you are calling them right from the main method, check the order in which you are calling them

This post has been edited by mostyfriedman: 22 April 2010 - 05:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 JavaBunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-February 10

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 05:41 PM

I had figured out the location of the modify array pass reference was causing the issue so I fixed that but the final grades are not showing with the 5 added points on them and the order is still wrong. I will take another look at it and see if I can figure anything out. Any more suggestions or hints? Thanks SO much for your help
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 05:44 PM

can you post the updated code?
Was This Post Helpful? 0
  • +
  • -

#7 JavaBunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-February 10

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 05:55 PM

Here it is. The only thing I changed was I swapped the order of the System.out.prinf and the pass array reference so at least now it is returning 95 like I want but the location is still wrong. It is at the bottom instead of in between the Initial Grades and the Final Grades.
//GradeCurveArray.java

import java.util.*;

public class GradeCurveArray
{
    
    public static void main (String[] args)
    { // Output Grade Array
        int[]gradesArray = {95, 79, 89, 78, 82};
        outputGrades(gradesArray);
        System.out.println( "\n\nInitial Grades:\n");
        System.out.printf("%s%15s", "Student", "Initial Grade");

        for (int student  = 0; student < gradesArray.length; student++)
            System.out.printf("\nStudent %2d%10d", student+1, gradesArray[ student ]);

        System.out.printf("\n%s %d\n", "\n\nHighest Grade Is", getMaximum(gradesArray));

	modifyArray (gradesArray);//pass array reference

    }//end main

    public static int getMaximum(int[]grades) 
        { 
        int highGrade = grades[0]; 
        for (int iterate : grades) 
        { 
                if (iterate > highGrade) 
                { 
                    highGrade = iterate; 
                } 
        }//end outer for 
        return highGrade; //return the highest grade 
        }//end Method getMaximum

    //Add Grade Curve amount to array elements
    public static void modifyArray(int[]gradesArray)
    {
        int amountToAdd = 100-getMaximum(gradesArray);

        for (int i=0; i < gradesArray.length; i++)
            gradesArray[i] += amountToAdd;

    }//end method modifyArray


    public static void outputGrades(int[]gradesArray)
    {
        //output final grade array
        System.out.println("Final Grades:\n");
        System.out.printf("%s%15s", "Student", "Final Grade");

        for (int student  = 0; student < gradesArray.length; student++)
            System.out.printf("\nStudent %2d%10d", student+1, gradesArray[ student ]);

    }//end method outputGrades

}//end class GradeCurveArray

Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 06:18 PM

i dont quite understand how you want the output to look like, can you give me a sample of how you want the output to look like
Was This Post Helpful? 0
  • +
  • -

#9 JavaBunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-February 10

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 06:21 PM

Sure. It is in the first post. I want it to show the initial grades array first, then the highest grade and then the final grade array which is modified by the "curve" amount.

Initial Grades:
Student Initial Grade
1 95
2 79
3 89
4 78
5 82

Highest Grade Is 95

Student Final Grade
1 100
2 84
3 94
4 83
5 87

Instead, I get the final grade array first, then the intial grade array (non-modified) and then the highest grade.

Final Grades:

Student Final Grade
Student 1 95
Student 2 79
Student 3 89
Student 4 78
Student 5 82

Initial Grades:
Student Initial Grade
Student 1 95
Student 2 79
Student 3 89
Student 4 78
Student 5 82

Highest Grade is 95

I don't know how to fix the code to get the order right and then I have to find out how to fix the fact that 5 is not being added to the grades array and redisplayed.

This post has been edited by JavaBunny: 22 April 2010 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 06:55 PM

i think this should do it
  public static void main (String[] args)
    { // Output Grade Array
        int[]gradesArray = {95, 79, 89, 78, 82};
        System.out.println( "\n\nInitial Grades:\n");
	outputGrades(gradesArray);
	System.out.printf("\n%s %d\n", "\n\nHighest Grade Is", getMaximum(gradesArray));
	System.out.println();
	modifyArray (gradesArray);//pass array reference
	outputGrades(gradesArray);        
    }//end main

    public static int getMaximum(int[]grades) 
        { 
        int highGrade = grades[0]; 
        for (int iterate : grades) 
        { 
                if (iterate > highGrade) 
                { 
                    highGrade = iterate; 
                } 
        }//end outer for 
        return highGrade; //return the highest grade 
        }//end Method getMaximum

    //Add Grade Curve amount to array elements
    public static void modifyArray(int[]gradesArray)
    {
        int amountToAdd = 100-getMaximum(gradesArray);

        for (int i=0; i < gradesArray.length; i++)
            gradesArray[i] += amountToAdd;

    }//end method modifyArray


    public static void outputGrades(int[]gradesArray)
    {
        System.out.printf("%s%15s", "Student", "Grade");

        for (int student  = 0; student < gradesArray.length; student++)
            System.out.printf("\nStudent %2d%10d\n", student+1, gradesArray[ student ]);

    }//end method outputGrades



all i did is changed the order in which you are calling the methods and changes did some very small tweaks
Was This Post Helpful? 1
  • +
  • -

#11 JavaBunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-February 10

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 07:06 PM

That Worked Great!! Thank you SO much; you are awesome! Now I have to go back and see what changes you made to see why it fixed it. Hope you will be on again tomorrow. I have to work on my class project and I'm sure I will run into issues. :tup: Thanks again!
Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: getMaximum & modifyArray Methods Not Working

Posted 22 April 2010 - 07:12 PM

no problem, glad i could help. I am probably going to be on tomorrow, but even if i am not, there are a lot of smart people around here that would love to help you out :).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1