Question: JAVA Programming Program Description: This method will insert spaces in front of a string and return the new string. The number of spaces to insert

JAVA Programming

  1. Program Description: This method will insert spaces in front of a string and return the new string. The number of spaces to insert and the string itself will be passed in as parameters.

BEGIN setLeft (number of spaces, string)

FOR (each space to be inserted)

add a space to be beginning of the string

END FOR

return the new string with the leading spaces

END setLeft

public static String setLeft(int num, String word)
 {
 //local constants
 
 //local variables
 
 /*******************************************************/
 
 for (int i = 0;i < num;i++)
 
 word = " " + word;
 
 return word;
 
 }
 
 
 
  1. Program Description: This method will insert spaces in front of a string and return the new string. The number of spaces will be determined by the field width and the length of the string (Width - Length).If the string is wider than the field width, no spaces will be added to the front of the string
 
 BEGIN setRight (field width, string)
 Find the length of the string
 Calc the number of spaces to be added
 IF (there is room to add spaces)
 FOR (each space to be added)
 add a space to the front of the string
 END FOR
 END IF
 return the string
 
public static String setRight(int width, String word)
 {
 //local constants
 
 //local variables
 int len = word.length(); //get the string length
 int pad = width - len; //how many spaces to add to front of string
 
 /*******************/
 
 //if there is room to add sapces
 if (len < width)
 
 //add the spaces to the front of the string
 for (int i = 0; i < pad; i++)
 word = " " + word;
 
 //return the string
 return word;
 
 }
 
 
  1. Program Description: This method clears the screen.
 BEGIN cls
 try to create a new processbuilder
 END cls
 ******************************************/
 public static void cls()
 {
 try
 {
 new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
 }
 catch (Exception E)
 {
 System.out.println(E);
 }
 
 }
}

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!