Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Homework help using max method

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Homework help using max method

    Okay I've got a homework assignment to write a program that reads integers, finds the TWO (2) largest of them, and counts their occurrences. I've managed to get the largest number but can I get any tips on how to find the second largest?
    Here is my code so far and thanks in advance!

    import java.util.Scanner;
     
    public class Unit4PT1 {
    	public static void main (String [] args){
    	Scanner in = new Scanner(System.in);
    	System.out.print("Enter numbers: ");
    int max = -1;
    int count = 0;
    int number;
    while((number = in.nextInt()) != 0){
    if(number > max){
    max = number;
    count = 1;
    } else if (number == max){
    count++;
    }
    }
    System.out.println("The largest number is " + max);
    System.out.println("The occurence count of the largest number is " + count);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    Define two variables: largest and nextLargest and save the appropriate values in each as the values are read in and compared to what was found previously.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    hhman1 (May 1st, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Homework help using max method

    How would I compare the largest variable to the old largest? Here is what I now have
    import java.util.Scanner;
     
    public class Unit4PT1 {
    	public static void main (String [] args){
    	Scanner in = new Scanner(System.in);
    	System.out.print("Enter numbers: ");
     
     
    int max = -1;
    int count = 0;
    int largest;
    int nextLargest;
    while((number = in.nextInt()) != 0){
    if(number > max){
    max = largest;
    count = 1;
    } else if (number == max){
    count++;
    }
    }
     
    System.out.println("The largest number is " + max);
    System.out.println("The occurence count of the largest number is " + count);
    System.out.println("The second largest number is " + max);
    System.out.println("The occurence count of the second largest number is " + count);
    }
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    I compare the largest variable to the old largest?
    Use if statements to compare the various variables' values.

    The posted code needs proper formatting. Nested statements should be indented 3-4 spaces
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Homework help using max method

    What I don't understand is how to assign a value to nextLargest. if(number > max) gives me largest but...

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    There needs to be more complicated logic used that uses multiple if statements and nested if statements.
    Pseudo code:
    initialize
    begin loop
    get number
    is number > max
    ... more logic here
    else is number == max
    count
    else is number > nextmax
    ... more logic here
    else is number == nextmax
    count
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Homework help using max method

    Haha I'm just getting more and more confused. Really wish I had a real life teacher. Here is what I now have but I don't think it makes sense.
    import java.util.Scanner;
     
    public class Unit4PT1 {
    	public static void main (String [] args){
    	Scanner in = new Scanner(System.in);
    	System.out.print("Enter numbers: ");
     
     
    int max = -1;
    int count = 0;
    int largest;
    int nextLargest;
    while((number = in.nextInt()) != 0){
    if(number > max){
    max = largest;
    count = 1;
    	} else if (number == max){
    	count++;
    	}else if (number > nextmax){
    	nextmax = nextLargest;
    	count = 1;
    	}else if (number == nextmax){
    	count++;
    }
    }
     
    System.out.println("The largest number is " + max);
    System.out.println("The occurence count of the largest number is " + count);
    System.out.println("The second largest number is " + nextLargest);
    System.out.println("The occurence count of the second largest number is " + count);
    }
    }

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    Do you need separate counters, one each for max and nextMax?

    Look at what variables are needed for the task. You seem to have a few extra that are not used or are mis-used.


    What about the case:
    max = 5, count=2
    nextMax = 4, nxMcount=3
    and the current number that was read is 6?
    What should be the new values of max, nextMax, count and nxMcount?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Homework help using max method

    The new values would be:
    max =6, count=1
    nextMax = 5 count=2

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    OK except for count being used twice (vs nxMcount).
    Now write down the logic needed to do that
    and then write the code for it.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Homework help using max method

    So I've changed quite a bit over the past few days and I'm on the verge getting it right. I still have a few problems though. My output is 0 regardless of the integers I put in. I changed the while loop to an if and now my code is:
    import java.util.Scanner;
    public class idk {
     public static void main (String [] args)
     
    {
     
            int max = 0;
            int maxcount = 0;
     
            int nextmax=0;
     
            int nextmaxcount=0;
     
            int number=0;
     
     
     
            Scanner input = new Scanner(System.in);
     
         System.out.print("Enter numbers: ");
     
        number = input.nextInt();
     
     
     
    if(number != 0){
     
        if(number > max){
     
     
     
                maxcount = 1;
     
                        }
     
        else if (number == max){
     
            maxcount++;
     
                                }
     
        else if (number > max){
     
            number = nextmax;
     
            nextmaxcount = 1;
     
                                }
     
        else if (number == nextmax){
     
                nextmaxcount++;
     
                                    }
     
                       }
     
        System.out.println("The largest number is " + max);
     
        System.out.println("The occurence count of the largest number is " + maxcount);
     
        System.out.println("The second largest number is " + nextmax);
     
        System.out.println("The occurence count of the second largest number is " + nextmaxcount);
     
        }
       }

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    If the variable definitions are inside the loop, then all previous saved data is lost every time the loop goes around. There isn't enough code posted to see what it does.

    Does the code compile without errors?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Homework help using max method

    Oops forgot a part, which I just edited in. And yes it compiles without errors.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    My output is 0 regardless of
    Is a value ever assigned to the variable that is printed as 0?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Homework help using max method

    The if loops should assign values to the four variables all of which print as 0. For some reason they do not.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    What is the name of the variable you are talking about that has the value of 0?
    Where is that variable assigned a value (other than 0)?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Homework help using max method

    Max and nextmax both have a value of 0. The variables should be assigned a value in
    if(number != 0){
     
        if(number > max){
     
     
     
                maxcount = 1;
     
                        }
     
        else if (number == max){
     
            maxcount++;
     
                                }
     
        else if (number > max){
     
            number = nextmax;
     
            nextmaxcount = 1;
     
                                }
     
        else if (number == nextmax){
     
                nextmaxcount++;

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    variables should be assigned a value
    Where are they assigned a value? Where is the statement that starts with: max =
    or with: nextmax =
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    hhman1 (May 8th, 2013)

  21. #19
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Homework help using max method

    So I tried to assign values and now it prints out that max is the first number I type and that the occurrence is one.
    if(number != 0){
     
        if(number > max){
     
     	max = number;
     
                maxcount = 1;
     
                        }
     
        else if (number == max){
     
            maxcount++;
     
                                }
     
        else if (number > max){
    	max = nextmax;
    	number = max;
     
            nextmaxcount = 1;
     
                                }
     
        else if (number == nextmax){
     
                nextmaxcount++;
     
                                    }
     
                       }

  22. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    Does the program work correctly now? If not please explain what the problems are.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #21
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Homework help using max method

    It is not working correctly. Say I enter the following numbers: 4 4 3 3 3 5 6 6 the program says:
    The largest number is 4
    The occurrence count of the largest number is 1
    The second largest number is 0
    The occurrence count of the second largest number is 1
    It should say:
    The largest number is 6
    The occurrence count of the largest number is 2
    The second largest number is 5
    The occurrence count of the second largest number is 1

  24. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Homework help using max method

    You need to look at the code posted in post#19 and make sure it does what is needed to solve the problem.

    The second largest number is 0
    This line says that the variable that holds the second largest number is never assigned a value.
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    hhman1 (May 8th, 2013)

Similar Threads

  1. [SOLVED] String max out
    By lorider93p in forum Java Theory & Questions
    Replies: 7
    Last Post: March 2nd, 2012, 10:43 AM
  2. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 08:50 PM
  3. Need help with C++ min and max heaps.
    By javapenguin in forum Other Programming Languages
    Replies: 6
    Last Post: October 19th, 2011, 12:08 PM
  4. SwingWorker - max memory?
    By fractalorbit in forum AWT / Java Swing
    Replies: 1
    Last Post: September 15th, 2011, 03:58 PM
  5. max length of an array?
    By qsbladey in forum Collections and Generics
    Replies: 4
    Last Post: April 3rd, 2011, 12:17 PM