Question

Please help me. package a1; public class Methods {    /*    * Instructions for students:...

Please help me.

package a1;

public class Methods {

   /*
   * Instructions for students: Use the main method only to make calls to the
   * other methods and to print some testing results. The correct operation of
   * your methods should not depend in any way on the code in main.
   *
   * Do not do any printing within the method bodies, except the main method.
   *
   * Leave your testing code in main -- you will be graded on this. You can remove
   * this comment from your submission.
   */

   /**
   * In main, write code that exercises all the methods you write. This code
   * should call the methods with different arguments and print out results. You
   * should think about different arguments to try that tests different cases. For
   * example, if the method returns a true or false, write code that creates a
   * true result and other code that produces a false result. Use print statements
   * to explain what each test is checking and the result that was obtained. The
   * assignment gives a small example of this. Running this program should output
   * a small report on examples of using the different methods.
   *
   * Replace this comment with your own Javadoc comment
   */
   public static void main(String[] args) {
       // Add tests here
      
   }

/**

   * Produces a String starting and ending with the edge character and having the
   * inner char repeated in-between. The total number of characters in the string
   * is width. As an example makeLine('+', '-', 8) would return the string
   * "+------+".
   *
   * NOTE: This method is already completely implemented and must not be modified
   * for the assignment.
   *
   * @param edge The character used at the start and end of the returned string.
   * @param inner The character repeated in-between the edge char.
   * @param width The total number of characters in the returned string. Width
   * must be greater or equal to 2.
   * @return A string with width characters.
   */
   public static String makeLine(char edge, char inner, int width) {
       String line = "";
       int currentLocation = 0;
       // Make the middle part of the line first.
       while (currentLocation < width - 2) {
           line = line + inner;
           currentLocation = currentLocation + 1;
       }
       // Add in the start and end character to the line.
       return edge + line + edge;
   }

   /**
   * Returns a string which, when printed out, will be a square shaped like this,
   * but of varying size (note - even though there are the same number of
   * characters side-to-side as up-and-down, the way text is drawn makes this look
   * not like a square. We will consider it a square.):
   *
   * <pre>
   * +-----+
   * | |
   * | |
   * | |
   * | |
   * | |
   * +-----+
   * </pre>
   *
   * The returned string should consist of width lines, each ending with a
   * newline. In addition to the newline, the first and last lines should begin
   * and end with '+' and should contain width-2 '-' symbols. In addition to the
   * newline, the other lines should begin and end with '|' and should contain
   * width-2 spaces.
   *
   * A newline is a special character use to force one string to be on multiple
   * lines. System.out.println("Hi\nThere"); will produce output like Hi There
   *
   * The '\n' character is a newline.
   *
   * The method does not print anything.
   *
   * The width parameter must be greater than or equal to 2.
   *
   * You should use while, if, and else statement.
   *
   * IMPLEMENTATION NOTE: For full credit (and for easier implementation), make
   * use of the makeLine method provided above in your implementation of
   * makeRectangle. You'll need to use a loop to call makeLine the correct number
   * of times.
   *
   * Replace this comment with your own Javadoc comment
   */
   public static String makeSquare(int width) {
       return ""; // Replace this line with your own code.
   }
}

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please try not to lose any formatting/spacing while copy-pasting the code, it might mess with the tests.

// Methods.java

public class Methods {

    /*

    * Instructions for students: Use the main method only to make calls to the

    * other methods and to print some testing results. The correct operation of

    * your methods should not depend in any way on the code in main.

    *

    * Do not do any printing within the method bodies, except the main method.

    *

    * Leave your testing code in main -- you will be graded on this. You can

    * remove this comment from your submission.

    */

    /**

    * In main, write code that exercises all the methods you write. This code

    * should call the methods with different arguments and print out results.

    * You should think about different arguments to try that tests different

    * cases. For example, if the method returns a true or false, write code

    * that creates a true result and other code that produces a false result.

    * Use print statements to explain what each test is checking and the result

    * that was obtained. The assignment gives a small example of this. Running

    * this program should output a small report on examples of using the

    * different methods.

    *

    * Replace this comment with your own Javadoc comment

    */

    public static void main(String[] args) {

        // code for testing makeLine method

        System.out.println("Testing makeLine() method");

        String expected = "+---+"; // expected result

        String actual = makeLine('+', '-', 5); // actual result

        System.out.println("makeLine('+', '-', 5)");

        // printing expected, actual and the result of the test

        System.out.println("Expected:\n" + expected);

        System.out.println("Actual:\n" + actual);

        if (expected.equals(actual)) {

             //success

             System.out.println("Test PASSED\n");

        } else {

             //failure

             System.out.println("Test FAILED\n");

        }

        expected = "XoX";

        actual = makeLine('X', 'o', 3);

        System.out.println("makeLine('X', 'o', 3)");

        System.out.println("Expected:\n" + expected);

        System.out.println("Actual:\n" + actual);

        if (expected.equals(actual)) {

             System.out.println("Test PASSED\n");

        } else {

             System.out.println("Test FAILED\n");

        }

        expected = "----------";

        actual = makeLine('-', '-', 10);

        System.out.println("makeLine('-', '-', 10)");

        System.out.println("Expected:\n" + expected);

        System.out.println("Actual:\n" + actual);

        if (expected.equals(actual)) {

             System.out.println("Test PASSED\n");

        } else {

             System.out.println("Test FAILED\n");

        }

        // code for testing makeSquare method

        System.out.println("Testing makeSquare() method");

        expected = "+-+\n| |\n+-+";

        actual = makeSquare(3);

        System.out.println("makeSquare(3)");

        System.out.println("Expected:\n" + expected);

        System.out.println("Actual:\n" + actual);

        if (expected.equals(actual)) {

             System.out.println("Test PASSED\n");

        } else {

             System.out.println("Test FAILED\n");

        }

        expected = "++\n++";

        actual = makeSquare(2);

        System.out.println("makeSquare(2)");

        System.out.println("Expected:\n" + expected);

        System.out.println("Actual:\n" + actual);

        if (expected.equals(actual)) {

             System.out.println("Test PASSED\n");

        } else {

             System.out.println("Test FAILED\n");

        }

        expected = "+----+\n|    |\n|    |\n|    |\n|    |\n+----+";

        actual = makeSquare(6);

        System.out.println("makeSquare(6)");

        System.out.println("Expected:\n" + expected);

        System.out.println("Actual:\n" + actual);

        if (expected.equals(actual)) {

             System.out.println("Test PASSED\n");

        } else {

             System.out.println("Test FAILED\n");

        }

    }

    /**

    *

    * Produces a String starting and ending with the edge character and having

    * the inner char repeated in-between. The total number of characters in the

    * string is width. As an example makeLine('+', '-', 8) would return the

    * string "+------+".

    *

    * NOTE: This method is already completely implemented and must not be

    * modified for the assignment.

    *

    * @param edge

    *            The character used at the start and end of the returned

    *            string.

    * @param inner

    *            The character repeated in-between the edge char.

    * @param width

    *            The total number of characters in the returned string. Width

    *            must be greater or equal to 2.

    * @return A string with width characters.

    */

    public static String makeLine(char edge, char inner, int width) {

        String line = "";

        int currentLocation = 0;

        // Make the middle part of the line first.

        while (currentLocation < width - 2) {

             line = line + inner;

             currentLocation = currentLocation + 1;

        }

        // Add in the start and end character to the line.

        return edge + line + edge;

    }

    /**

    * Returns a string which, when printed out, will be a square shaped like

    * this, but of varying size (note - even though there are the same number

    * of characters side-to-side as up-and-down, the way text is drawn makes

    * this look not like a square. We will consider it a square.):

    *

    * <pre>

    * +-----+

    * |     |

    * |     |

    * |     |

    * |     |

    * |     |

    * +-----+

    * </pre>

    *

    * The returned string should consist of width lines, each ending with a

    * newline. In addition to the newline, the first and last lines should

    * begin and end with '+' and should contain width-2 '-' symbols. In

    * addition to the newline, the other lines should begin and end with '|'

    * and should contain width-2 spaces.

    *

    * A newline is a special character use to force one string to be on

    * multiple lines. System.out.println("Hi\nThere"); will produce output like

    * Hi There

    *

    * The '\n' character is a newline.

    *

    * The method does not print anything.

    *

    * The width parameter must be greater than or equal to 2.

    *

    * You should use while, if, and else statement.

    *

    * IMPLEMENTATION NOTE: For full credit (and for easier implementation),

    * make use of the makeLine method provided above in your implementation of

    * makeRectangle. You'll need to use a loop to call makeLine the correct

    * number of times.

    *

    * Replace this comment with your own Javadoc comment

    */

    public static String makeSquare(int width) {

        // assuming width >= 2 always

        // making a line with given width and has '+' on edges and '-' on inner,

        // assigning to a String

        String str = makeLine('+', '-', width);

        // looping for width-2 number of times

        for (int i = 0; i < width - 2; i++) {

             // making a line with given width and has '|' on edges and ' ' on

             // inner, appending to str preceeded by a newline

             str += "\n" + makeLine('|', ' ', width);

        }

        // making a line with given width and has '+' on edges and '-' on inner,

        // appending to str preceeded by a newline and returning it

        str += "\n" + makeLine('+', '-', width);

        return str;

    }

}

/*OUTPUT*/


Add a comment
Know the answer?
Add Answer to:
Please help me. package a1; public class Methods {    /*    * Instructions for students:...
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
  • Question 1:    * - Print out the welcome message: "Welcome to this choose your own...

    Question 1:    * - Print out the welcome message: "Welcome to this choose your own adventure system!"    * - Begin the play again loop:    * - Prompt for a filename using the promptString method with the prompt:    * "Please enter the story filename: "    * - Prompt for a char using the promptChar method with the prompt:    * "Do you want to try again? "    * - Repeat until the character returned by...

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

  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /**  * Takes in a string containing a first, middle and last name in that order.  * For example Amith Mamidi Reddy to A. M. Reddy.  * If there are not three words in the string then the method will return null  * @param name in...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = '...

    This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = ' '; private static final char UPPER_BOUND = '_'; private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1; /** * This method determines if a string is within the allowable bounds of ASCII codes * according to the LOWER_BOUND and UPPER_BOUND characters * @param plainText a string to be encrypted, if it is within the allowable bounds * @return true if all characters...

  • I need help filling in the the code in the methods add, multiply, and evaluate package...

    I need help filling in the the code in the methods add, multiply, and evaluate package poly; import java.io.IOException; import java.util.Scanner; /** * This class implements evaluate, add and multiply for polynomials. * * * */ public class Polynomial {       /**    * Reads a polynomial from an input stream (file or keyboard). The storage format    * of the polynomial is:    * <pre>    * <coeff> <degree>    * <coeff> <degree>    * ...    *...

  • Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed...

    Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed from block-letters of size 7. Each block-letter is composed of 7 horizontal line-segments of width 7 (spaces included): SOLID                        as in block-letters F, I, U, M, A, S and the blurb RThere are six distinct line-segment types: TRIPLE                      as in block-letter M DOUBLE                   as in block-letters U, M, A LEFT_DOT                as in block-letters F, S CENTER_DOT          as in block-letter...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

  • How can i print a diamond implementing these methods public static void printNChars(int n, char c))....

    How can i print a diamond implementing these methods public static void printNChars(int n, char c)). This method will print n times the character c in a row, public static void printDiamond(int size, char edgeChar, char fillChar). This method will call printNChars() method to print the shape. It will use the edgeChar to print the sides and the fillChar to fill the interior of the shape. The shape will have a height and a width of the given size. public...

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