0

Ok so I been working on this assignment all day for the past 3 days but I haven't had any luck. I wasn't going to ask for help but I finally gave up. But there is also one more thing I need to implement to the code. This is what I gotta implement "Find the length of the longest continuous series of positive numbers in the array data. If the contents were: 4 5 0 2 . . . -1 88 78 66 -6. The length would be 3. For this problem, 0 is considered non-negative but not positive". Plus I have an issue where I can't print the largest int in the array of 20.

import java.util.Random;
import java.util.ArrayList;
public class arrayops {

public static int findLargest(ArrayList<Integer> nums) {
      int greatestnum = nums.get(0);
      for (Integer item : nums) {
          if (item > greatestnum) {
              greatestnum = item;
          }
      }
      return greatestnum; 
  }
  public static int randomData(ArrayList<Integer> nums) {
      int[] array = new int [20];
      Random random = new Random();
      for (int i = 0; i < array.length; i++) {
          array[i] = -100 + random.nextInt(201);
      }
      return -100 + random.nextInt(201);
  }

  public static void main(String[] args) {
      ArrayList<Integer> nums = new ArrayList<Integer>();
      nums.add(1);
      nums.add(4);
      nums.add(13);
      nums.add(43);
      nums.add(-25);
      nums.add(17);
      nums.add(22);
      nums.add(-37);
      nums.add(29);
      System.out.println("The Greatest Number from the hardcoded numbers " + findLargest(nums));
      System.out.println("The Greatest number from the random numbers " + randomData(nums));
     }
    } 
azro
  • 53,056
  • 7
  • 34
  • 70
  • 1
    The randomData method purpose is not given, you have a List as parameter that is not used, you fill an array that is not used and just return a Random number between -100 and 100 – azro Aug 19 '18 at 12:44
  • Possible duplicate of [finding longest sequence of consecutive numbers](https://stackoverflow.com/questions/27283305/finding-longest-sequence-of-consecutive-numbers) – maha Aug 19 '18 at 12:48

3 Answers3

1

The findLargest method:

public static int findLargest(ArrayList<Integer> nums) {
  int greatestnum = 0;
  int greatestLen = 0;
  for (Integer item : nums) {
      if (item > 0) {
          greatestLen++ ;
          if(greatestLen > greatestnum)
              greatestnum = greatestLen;
      }
      else
          greatestLen = 0;
  }
  return greatestnum; 
}

Logic used:

  1. Keep the length of the longest chain encountered, and the length of current chain, in two separate variables (greatestnum and greatestLen respectively)

  2. Increment greatestLen every time a positive number is encountered. If the number if less than or equal to zero, reset this count.

  3. If the length of current chain is greater than the previous longest chain, sent the longest chain size to current chain size.

Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29
  • Why would you name a variable `greatestnum` when it represents the "length of the longest chain encountered"? Why not something more descriptive like `longestChainLength`? Similarly why use the name `greatestLen` when it represents "length of current chain"? How about `currentChainLength`? – D.B. Aug 19 '18 at 14:56
  • I tried to preserve the original code as much as possible. – Susmit Agrawal Aug 19 '18 at 15:39
1

The problem is you created a list with random numbers but never put that list into the findLargest method. You also never created a method to find the consecutive positive numbers. If you didn't know how to go about coding it, I recommend drawing out an algorithm on paper.

Largest value in ArrayList...

public static int findL(ArrayList<Integer> nums)
{
    int top = nums.get(0);
    for(int i = 0; i<nums.size(); i++)
    {
        if(nums.get(i)>top)
        {
            top = nums.get(i);
        }
    }
    return top;
}

Largest number of consecutive positives...

public static int positiveString(ArrayList<Integer> nums)
{
    int longest = 0;
    int count = 0;
    for(int i = 0; i<nums.size(); i++)
    {
        if(nums.get(i) > 0)
        {
            count++;
        }
        else
        {
            if(longest<count)
            {
                longest = count;
            }
            count = 0;
        }
    }
    return longest;
}
Ben
  • 31
  • 8
0

If you want to arrange the numbers into order you can simply use java.util.TreeSet. Then use the method last() to get the largest number.

public static int findLargest(ArrayList<Integer> nums) {
    return new TreeSet<Integer>(nums).last();
}
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80