Question: This JAVA program is getting the errors (below) and I cannot figure out how to fix: /** * Code to find the longest increasing sequence

This JAVA program is getting the errors (below) and I cannot figure out how to fix:

/** * Code to find the longest increasing sequence of numbers in a set * @author Me * */

import java.util.Stack;

/** * public class LongestIncreasingSequence * @author Me * */ public class LongestIncreasingSequence {

private static void getLongestIncreasingSequence(int[][] grid, int r, int c){ //declare grid int[][] g = new int[r][c]; //loop for rows for(int a = 0; a < r; a++){ //loop for columns for(int b = 0; b < c; b++){ //update g[a][b] = 1; } } //declare max length int maxLen = -1; //declare index1 int idx1 = -1; //decalre index2 int idx2 = -1; //loop to iterate rows in grid for(int a = 0; a < r; a++){ //loop to iterate cols in grid for(int b = 0; b < c; b++){ //check condition if(a>0 && Math.abs(grid[a][b]-grid[a-1][b])==1){ //update grid g[a][b] = Math.max(g[a][b], g[a-1][b]+1); } //check condition if(b>0 && Math.abs(grid[a][b]-grid[a][b-1])==1){ //update grid g[a][b] = Math.max(g[a][b],g[a][b-1]+1); } //check condition if(maxLen > 0){ //update max length maxLen = g[a][b]; //update index idx1 = a; //update index idx2 = b; } } } //call to display display(grid, g, maxLen, idx1, idx2); } /** * method function * @param grid * @param g * @param maxLen * @param idx1 * @param idx2 */ private static void display(int[][] grid, int[][] g, int maxLen, int idx1, int idx2){ //declare stack Stack s1 = new Stack<>(); //loop while(maxLen >= 1){ //add to stack s1.add(grid[idx1][idx2]); //check condition if(idx1>0 && Math.abs(g[idx1-1][idx2]-g[idx1][idx2])==1){ //decrement index idx1--; } else if(idx2>0 && Math.abs(g[idx1][idx2-1]-g[idx1][idx2])==1){ //decrement index idx2--; } maxLen--; } //declare stack Stack s2 = helper(s1); //loop for(Integer integer : s2){ //display System.out.print(integer+" "); } } public static Stack helper(Stack input){ //declare stack Stack tempStack = new Stack(); //loop while(!input.isEmpty()){ //declare temp int temp = input.pop(); //loop while(!tempStack.isEmpty() && tempStack.peek() > temp){ //update input.push(tempStack.pop()); } tempStack.push(temp); } //return return tempStack; } //driver method main public static void main(String[] args){ //declare and initialize grid int grid[][] = { {97, 47, 56, 36}, {35, 57, 41, 13}, {89, 36, 98, 75}, {25, 45, 26, 17} }; //display System.out.print("Longest Increasing Sequence: "); //call sequence getLongestIncreasingSequence(grid, 4, 4); } }

ERRORS:

LongestIncreasingSequence.java:130: error: incompatible types: Object cannot be converted to Integer for(Integer integer : s2){ ^ LongestIncreasingSequence.java:148: error: incompatible types: Object cannot be converted to int int temp = input.pop(); ^ LongestIncreasingSequence.java:151: error: bad operand types for binary operator '>' while(!tempStack.isEmpty() && tempStack.peek() > temp){ ^ first type: Object second type: int Note: LongestIncreasingSequence.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 3 errors

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!