# Homework help using max method

• May 1st, 2013, 08:41 PM
hhman1
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!

Code java:

```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); } }```
• May 1st, 2013, 08:51 PM
Norm
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.
• May 1st, 2013, 09:50 PM
hhman1
Re: Homework help using max method
How would I compare the largest variable to the old largest? Here is what I now have
Code java:

```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); } }```
• May 2nd, 2013, 05:39 AM
Norm
Re: Homework help using max method
Quote:

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
• May 2nd, 2013, 09:13 AM
hhman1
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...
• May 2nd, 2013, 09:19 AM
Norm
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
• May 2nd, 2013, 07:44 PM
hhman1
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.
Code java:

```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); } }```
• May 2nd, 2013, 07:49 PM
Norm
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.

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?
• May 2nd, 2013, 08:04 PM
hhman1
Re: Homework help using max method
The new values would be:
max =6, count=1
nextMax = 5 count=2
• May 2nd, 2013, 08:11 PM
Norm
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.
• May 6th, 2013, 07:54 PM
hhman1
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:
Code java:

```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);   } }```
• May 6th, 2013, 08:04 PM
Norm
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?
• May 6th, 2013, 08:11 PM
hhman1
Re: Homework help using max method
Oops forgot a part, which I just edited in. And yes it compiles without errors.
• May 6th, 2013, 08:32 PM
Norm
Re: Homework help using max method
Quote:

My output is 0 regardless of
Is a value ever assigned to the variable that is printed as 0?
• May 6th, 2013, 08:42 PM
hhman1
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.
• May 6th, 2013, 08:50 PM
Norm
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)?
• May 6th, 2013, 09:24 PM
hhman1
Re: Homework help using max method
Max and nextmax both have a value of 0. The variables should be assigned a value in
Code java:

```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++;```
• May 7th, 2013, 05:52 AM
Norm
Re: Homework help using max method
Quote:

variables should be assigned a value
Where are they assigned a value? Where is the statement that starts with: max =
or with: nextmax =
• May 7th, 2013, 09:09 PM
hhman1
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.
Code java:

```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++;   }   }```
• May 8th, 2013, 05:59 AM
Norm
Re: Homework help using max method
Does the program work correctly now? If not please explain what the problems are.
• May 8th, 2013, 09:32 AM
hhman1
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:
Code java:

```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:
Code java:

```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```
• May 8th, 2013, 09:37 AM
Norm
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.

Quote:

The second largest number is 0
This line says that the variable that holds the second largest number is never assigned a value.