Question

Below is my code and the instructions for the code. I can't figure out what's wrong....

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:

  1. Writing a searching function for 1D arrays.
  2. 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.

  1. Start Eclipse.

  1. 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).

  1. Go to the File menu and select New Java Project. Call it YOURNAME_Lab1BArray.

  1. 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 (Scanner’s nextInt).

  1. 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.

  1. Call that method in your main, for the array data.

  1. 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;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I saw your code and found that you were creating a new array ( whose name was array) inside the LongestPositiveSeries function. So this was wrong, you don't need to create a new array inside the LongestPositiveSeries function.
Instead, you need to use the myArray (which has come as a parameter to the LongestPositiveSeries function) inside the for loop. You were creating and then using array inside the for loop, but in the corrected code I have used myArray inside for loop and commented on the statement where you were creating the array.

myArray contains the data entered by the user, you passed data array to the LongestPositiveSeries function and myArray is the receiving parameter, so it is obvious that you need to use myArray inside for loop and not array.

Corrected code:

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) {

       // no need to use the below statement
       // int[][] array = new int[5][5];

       int positiveStreak = 0;

       int longestPosStreak = 0;

       // use myArray inside the for loop
       for (int row = 0; row < myArray.length; row++) {

           for (int column = 0; column < myArray[0].length; column++) {

               if (myArray[row][column] > 0) {

                   positiveStreak++;

                   // longestPosStreak++;

               }

               else {

                   if (longestPosStreak < positiveStreak) {

                       longestPosStreak = positiveStreak;

                       positiveStreak = 0;

                   }

                   positiveStreak = 0;

               }

           }

       }

       return longestPosStreak;

   }

}

Note: Please refer to the screenshot of the corrected code to understand the indentation of the code.

Screenshot of the corrected code:

Output and Input:

Add a comment
Know the answer?
Add Answer to:
Below is my code and the instructions for the code. I can't figure out what's wrong....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

  • I can't figure out how to change my code to write info in from a text...

    I can't figure out how to change my code to write info in from a text file for the input. I'll post the context of the problem I am working on and the code I have finished so far. The program will create a Catapult object that will create a searchable matrix of trajectories for the given speeds and angles. The object should store these values in a 2D array. Given a target range of minimum and maximum distances, representing...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • The last element in each array in a 2D array is incorrect. It’s your job to...

    The last element in each array in a 2D array is incorrect. It’s your job to fix each array so that the value 0 is changed to include the correct value. In the first array, the final value should be the length of the first array. In the second array, the final value should be the sum of the first value, and the second to last value in the array. In the third array, the final value should be the...

  • C# Code please with picture of code.Thanks! 1. Create a Main method, and create a 2D...

    C# Code please with picture of code.Thanks! 1. 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 (Scanner’s nextInt) 2.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...

  • Can someone explain this code with comments I am supposed to dispay an array an add...

    Can someone explain this code with comments I am supposed to dispay an array an add each columns and add each row An application uses a two-dimensional array declared as follows: int[][] days = new int[29][5]; a. Write code that sums each row in the array and displays the results. b. Write code that sums each column in the array and displays the results. class TwoDimensionalArrayDemo { public static void main( String[] arg ) { int[][] days = new int[29][5];...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT