Question

I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

I am unsure how to add the following methods onto this code?? please help

- rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false.

- columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false.

- isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n} where n is the number of cells, otherwise false. So, for example, if there are 10 cells the numbers would be 1, 2, . . . , 10 with no missing or duplicate numbers.

The code is;


public class Table {

/**
* The main method is just used for testing.
* @param args command line arguments are not used.
*/
public static void main(String[] args) {
final int[][] valid = {{1, 4, 5, 10, 11}, {2, 6, 8}, {3, 9, 12}, {7}};
System.out.println(Table.toString(valid));
System.out.println(Table.rowsLengthsDecrease(valid));
}

/**
* Determines whether the array passed to it is a valid table or not.
* @param t a two-dimensional array to test for table.
* @return true if the parameter is a valid table, otherwise false
*/
public static boolean isTable(int[][] t){
return false;
}

/**
* Returns a string representation of an array based table.
* @param t a two-dimensional array which represents a table.
* @return a string representation of an array based table.
*/
public static String toString(int[][] t) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < t.length; i++) {
for (int j = 0; j < t[i].length; j++) {
result.append(String.format("%-4s", t[i][j]));
}
if (i < t.length-1) {
result.append("\n");
}
}
return result.toString();
}
/**
* returns true if no row is longer than row preceding, otherwise
* returns false.
* @param t a 2-D array which represents a table
* @return a boolean true or false value
*/
public static boolean rowsLengthsDecrease (int [][] t){
int shortest = t[0].length;
for(int i=0; i<t.length; i++){
if(t[i].length <= shortest){
shortest = t[i].length;
}else{
return false;
}
}
return true;
  
}

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

PLEASE GIVE IT A THUMBS UP :-)

class Table {

/**
* The main method is just used for testing.
* @param args command line arguments are not used.
*/
public static void main(String[] args) {
final int[][] valid = {
{
1, 4, 5, 10, 11
}, {
2, 6, 8, 9
}, {
3, 9, 12
}, {
7
}
};
System.out.println(Table.toString(valid));
System.out.println(Table.rowsLengthsDecrease(valid));
System.out.println(Table.rowValuesIncrease(valid));
System.out.println(Table.columnValuesIncrease(valid));
System.out.println(Table.isSetOf1toN(valid));

}

static boolean rowValuesIncrease(int[][] t) {
   for(int i=0;i<t.length;i++){
       int flag=0;
       for(int j=1;j<t[i].length;j++){
           if(t[i][j]<t[i][j-1])
               flag=1;
       }
       if(flag==0)
           return true;
   }
   return false;
}

static boolean columnValuesIncrease(int[][] t){
   for(int i=0;i<t.length;i++){
       int flag=0;
       for(int j=0;j<t[i].length;j++){
           try{
               if(t[j][i]>t[j][i+1])
                   flag=1;
           }
           catch(Exception e){}
       }
       if(flag==0)
           return true;
   }
   return false;
}

static boolean isSetOf1toN(int[][] t){
   int n=0;
   for(int i=0;i<t.length;i++){
       n+=t[i].length;
   }
   boolean f[] = new boolean[n];
   for(int i=0;i<n;i++)
       f[i]=false;
   for(int i=1;i<=n;i++){
       for(int j=0;j<t.length;j++){
           for(int k=0;k<t[j].length;k++){
               if(t[j][k]==i){
                   if(f[i-1]==false)
                       return false;
                   f[i-1]=true;
               }
           }
       }
   }
   for(int i=0;i<n;i++)
   {
       if(f[i]==false)
           return false;
   }
   return true;
}

/**
* Determines whether the array passed to it is a valid table or not.
* @param t a two-dimensional array to test for table.
* @return true if the parameter is a valid table, otherwise false
*/
public static boolean isTable(int[][] t) {
return false;
}

/**
* Returns a string representation of an array based table.
* @param t a two-dimensional array which represents a table.
* @return a string representation of an array based table.
*/
public static String toString(int[][] t) {
StringBuilder result = new StringBuilder();
for (int i = 0; i<t.length; i++) {
for (int j = 0; j<t[i].length; j++) {
result.append(String.format("%-4s", t[i][j]));
}
if (i<t.length - 1) {
result.append("\n");
}
}
return result.toString();
}
/**
* returns true if no row is longer than row preceding, otherwise
* returns false.
* @param t a 2-D array which represents a table
* @return a boolean true or false value
*/
public static boolean rowsLengthsDecrease(int[][] t) {
int shortest = t[0].length;
for (int i = 0; i<t.length; i++) {
if (t[i].length<= shortest) {
shortest = t[i].length;
} else {
return false;
}
}
return true;

}
}

Add a comment
Know the answer?
Add Answer to:
I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...
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
  • Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...

    Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {            if(arr == null || arr.length == 0) {                return -1;            }            for (int i = 0; i < arr.length; i++) {                if(arr[i] == ch) {                    return i;                }            }        return -1;       ...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • Hey, I was wondering if there’s another way to make these methods not reliable to imports...

    Hey, I was wondering if there’s another way to make these methods not reliable to imports such as “import java.util.Arrays” and “import java.util.Hash” I am still new in coding and would like to know if there’s another way to do it! Here is the code! (Thank you in advance and I will really appreciate it :) ) /** This exercise involves implementing several methods. Stubs for each method with documentation are given here. It is your task to fill out...

  • \\ this is the code i need help to get this part working the rest works...

    \\ this is the code i need help to get this part working the rest works but this and im not sure what is missing or how to make it work. this is what they asking for to do and there is two errors the ones shown in the pictures this is all the information I have from zybooks\\ Q3. (54 Points) Complete a public class to represent a Movie as described below. (a-e 4 pts each) (f-h 8 pts...

  • /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /**...

    /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; }    /** * Create a String that...

  • cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

    cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

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
ADVERTISEMENT