Question: Below is my code and the instructions for the code. I can't figure out what's wrong. Please help. Tasks This lab has two parts: Writing
Below is my code and the instructions for the code. I can't figure out what's wrong. Please help.
Tasks
This lab has two parts:
- Writing a searching function for 1D arrays.
- Writing a simple class.
Part 1 Searching
Continuing with arrays, we will now expect you to find information in an array and derive useful properties from the information stored in an array.
- Start Eclipse.
- Change the workspace directory location to something "safe". You may want to temporarily choose the Desktop and then save the file to something permanent once you're done (e.g. a network drive or flash drive).
- Go to the File menu and select New Java Project. Call it YOURNAME_Lab1BArray.
- Create a main method, and create a 2D integer array called data, with a size of five (5) by five (5). Ask the user to input the values for this array (Scanners nextInt).
- Create a method called LongestPositiveSeries, that takes in a 2D array and finds the length of the longest continuous series of positive numbers in it.
- For example, if we had an array like this:
| 0 | 1 | 2 | 3 | 4 |
| 5 | -1 | -5 | -5 | 5 |
| 3 | -2 | 56 | 25 | -15 |
| 0 | 5 | 5 | 0 | 324 |
| 46 | 25 | 0 | 0 | 0 |
- The length would be 5. For this problem, 0 is considered non-negative and not positive, so we start counting from index [0, 1] (which is 1) to [1, 0] (which is 5) and end at index [1, 1] (which is 1).
- Hint: Consider using a basic linear search and modifying it to keep track of the current positive streak of numbers.
- Call that method in your main, for the array data.
- Print out the returned result from your method in your main, along with the content of data.
CODE BELOW:
import java.util.Scanner;
public class AssignmentOne {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[][] data = new int[5][5];
//setting values into the array;
System.out.println("Enter values for the array: ");
for (int row = 0; row < data.length; row++) {
for (int column = 0; column < data[0].length; column++) {
data[row][column] = scan.nextInt();
}
}
//printing array to screen
for (int row = 0; row < data.length; row++) {
for (int column = 0; column < data[row].length; column++) {
System.out.print(data[row][column] + " ");
}
System.out.println();
}
//calling the array;
int longestStreak = LongestPositiveSeries(data);
System.out.println("Longest positive streak: " + longestStreak);
//closing scanner;
scan.close();
}
public static int LongestPositiveSeries(int[][]myArray) {
int[][] array = new int[5][5];
int positiveStreak = 0;
int longestPosStreak = 0;
for (int row = 0; row < array.length; row++) {
for (int column = 0; column < array[0].length; column++) {
if (array[row][column] > 0) {
positiveStreak++;
//longestPosStreak++;
}
else {
if (longestPosStreak < positiveStreak) {
longestPosStreak = positiveStreak;
positiveStreak = 0;
}
positiveStreak = 0;
}
}
}
return longestPosStreak;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
