Question

Do anyone knows how to write this in java?    You are allowed to use the...

Do anyone knows how to write this in java?   

You are allowed to use the following methods from the Java API:

  • class String
    • length
    • charAt
  • class StringBuilder
    • length
    • charAt
    • append
    • toString
  • class Character
    • any method
  1. moveXDownLeft takes a char and a two dimensional array of char as input and returns nothing:
    The method should find the first occurrence of the char in the array (searching from "top" to "bottom" and "left" to "right"), and it should slide that character in the array down and to the left as far as it can go. Any characters on that diagonal are slide up and to the right to fill. Do not use Strings to solve this problem.

    > char[][] board = {{'a','b','c','X'},{'d'},{'e','f','g','h'},{'i','j','k'},{'l','m','n','o'}};
    > HW2.moveXDownLeft('X', board)
    > board
    { { a, b, c, f }, { d }, { e, i, g, h }, { X, j, k }, { l, m, n, o } }
    
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Steps Followed:
-> Searched Location of X
-> Moved X to left down
-> Printed resulting Array
Code: (Text File Also Included in End)
MyProgram.java (Main Class)


HW2.java


Result:


Code:
MyProgram.java (Main Class)

public class MyProgram {

    public static void main(String[] args) {
        char[][] board = {{'a', 'b', 'c', 'X'},
                           {'d'},
                           {'e', 'f', 'g', 'h'},
                           {'i', 'j', 'k'},
                           {'l', 'm', 'n', 'o'}};
      
        HW2.moveXDownLeft('X', board);
    
    }

}
HW2.java

public class HW2 {

    // Move Down Left Function
    public static void moveXDownLeft(char X, char[][] board) {
        // Variable Initilization For Store location of X
        int valueColumn = -1;
        int valueRow = -1;
        Boolean found = false;

        // Searching X array by array
        int i = 0;
        for (char[] semiArray : board) {
            // If Find X break Loop
            if (found) {
                break;
            }

            int j = 0;
            for (char arrayValue : semiArray) {
                if (!found) {
                    // Checking if arrayValue is equal To X
                    if (arrayValue == X) {
                        valueRow = i;
                        valueColumn = j;
                        found = true;
                        break;
                    }
                }
                j++;
            }
            i++;
        }

        // Moving Down X
        int col = valueColumn;
        for (int row = (valueRow + 1); row < board.length; row++) {

            // Checking if we reached left end
            if (col > 0) {
                char[] semiArray = board[row];
                // Checking if row has space for move X
                if (semiArray.length >= col) {
                    // Swapping X
                    char temp = board[row][col - 1];
                    board[row][col - 1] = X;
                    board[valueRow][valueColumn] = temp;

                    // Saving new Location of X
                    valueColumn = col - 1;
                    valueRow = row;

                }
                col = col - 1;
            } else {
                break;
            }
        }

        //Printing Resulting Array
        System.out.println("{");
        for (char[] semiArray : board) {
            System.out.print("{");
            int v = 0;
            for (char arrayValue : semiArray) {
                System.out.print(arrayValue);
                if (v < semiArray.length - 1) {
                    System.out.print(",");
                }
            }
            System.out.println("}");
        }
        System.out.println("}");
    }

}

Add a comment
Know the answer?
Add Answer to:
Do anyone knows how to write this in java?    You are allowed to use the...
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
  • Programming project in Java: You are allowed to use the following methods from the Java API:...

    Programming project in Java: You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method Create a class called HW2 that contains the following methods: 1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...

  • In Java Only can use class String length charAt class StringBuilder length charAt append toString class...

    In Java Only can use class String length charAt class StringBuilder length charAt append toString class Character any method Create the following methods nthWord takes an int and a String as input and returns a String: The input int represents a number n that is assumed to be positive, and the output string contains every nth word of the input string, starting with the first word, separated by a single space. {\em For this method, a word is defined to...

  • In Java All methods listed below must be public and static. If your code is using...

    In Java All methods listed below must be public and static. If your code is using a loop to modify or create a string, you need to use the StringBuilder class from the API. Keep the loops simple but also efficient. Remember that you want each loop to do only one "task" while also avoiding unnecessary traversals of the data. No additional methods are needed. However, you may write additional private helper methods, but you still need to have efficient...

  • Write a Java method that searches a 2-d array for a specific character. Return the index...

    Write a Java method that searches a 2-d array for a specific character. Return the index of the first location of the character. Method header: public static void play(char c, char[][]wordSearch){ Example: a e v s l g r e d k h k q s e z j c p o a t s o v a n m n l q p f o x b The character 's' appears at index 0,3.

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However,...

    I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However, when I run the driver class, it just outputs two sets of dashed lines. What am I getting wrong? Is it the deleteRepeats() method?: public class CharArrayProject_3 { private char[] array; privateint length; privateintnumberOfRepeats; public CharArrayProject_3( char[] arr ) { length = arr.length; array = new char[ length ]; numberOfRepeats = 0; for( int k = 0; k < arr.length; k++ ) { array[k]...

  • Complete code and answer question in comments: Package hw4; import java.util.ArrayList; public c...

    Complete code and answer question in comments: Package hw4; import java.util.ArrayList; public class WordGame { /* * Returns all strings that appear * as a consecutive horizontal or vertical sequence of letters * (left-right, right-left, up-down, or down-up) * in the array board and also appear in dict. * Note that the same word may appear multiple times * on the board, and will then be multiple times in * the returned array. * * dict is assumed to be...

  • Programming Language : JAVA Write a class named Ship. Its purpose is to model a ship...

    Programming Language : JAVA Write a class named Ship. Its purpose is to model a ship in the BattleShip game and its placement on the Battleship board. Ships exist on a 10 x 10 battleship board with ten rows labeled A through J and columns labeled 1 through 9 and the final column labeled 0 to indicate the tenth column. We will refer to the Ship placed on the board at the origin which the pair (r,c) where in the...

  • Write a Java program for all items in Question Use System.out.println() for each resulting string. Let...

    Write a Java program for all items in Question Use System.out.println() for each resulting string. Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following statements: a. Check whether s1 is equal to s2 and assign the result to a Boolean variable isEqual. b. Check whether s1 is equal to s2, ignoring case, and assign the result to a Boolean variable isEqual. c. Compare s1 with s2 and assign the result to...

  • Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable...

    Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable string. This class represents a character array and provide functionalities need to do basic string processing. It contains an array of characters, representing a textual string. Overview In Java, Strings are a great device for storing arrays of characters. One limitation of Strings in Java, however (there is always at least one), is that they are immutable (ie. they cannot be changed once created)....

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