Question

In: Computer Science

. Write a function in JAVA called shorten that is defined in the class P8 and...

. Write a function in JAVA called shorten that is defined in the class P8 and shortens each element of an array of strings. Every string with more than two characters is cut down to its first two characters. For example, a program that uses the function shorten follows.

public class P8 {

public static void main(String args[]) {

String x[] = {"CSCI", "1", "11", "Queens", "College", "CUNY"};

shorten(x);

for (int i = 0; i < 6; i++) System.out.print(x[i] + " "); // Output: CS 1 11 Qu Co CU System.out.println();

Solutions

Expert Solution

SOLUTION:

  1. Loop through the passed array of strings
  2. if the length of the string is greater than 2 characters, shorten the length using substring function. start position is 0 and end position is 2 as we need first characters only.
  3. if length of the string is less than 2 characters, do not do anything with it.

CODE:

public class P8
{
/* Method: shorten */
/* shorten the strings which are more than two characters to first two characters */
public static void shorten(String []x)
{
/* loop through all the strings */
for (int i = 0; i < 6; i++)
{
/* if the length of the string greater than 2 character,
then shorter the string to first 2 characters*/
if(x[i].length() > 2)
{
/* use substring function. start position is 0, end position is 2 */
x[i] = x[i].substring(0,2);
}
}
}
  
public static void main (String args[])
{
String x[] = {"CSCI", "1", "11", "Queens", "College", "CUNY"};

shorten (x);

for (int i = 0; i < 6; i++)
System.out.print (x[i] + " ");   // Output: CS 1 11 Qu Co CU
System.out.println ();
}
}

OUTPUT:


Related Solutions

Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Python Create a move function that is only defined in the base class called Objects. The...
Python Create a move function that is only defined in the base class called Objects. The move function will take two parameters x,y and will also return the updated x,y parameters.
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the...
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the user to think of a number between 0 and n−1, then makes guesses as to what the number is. After each guess, the program must ask the user if the number is lower, higher, or correct. You must implement the divide-and-conquer algorithm from class. In particular, you should round up when the middle of your range is in between two integers. (For example, if...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
1. Write a public Java class called WriteFile which opens an empty file called letters.dat. This...
1. Write a public Java class called WriteFile which opens an empty file called letters.dat. This program will read a String array called letters and write each word onto a new line in the file. The method should include an appropriate throws clause and should be defined within a class called TFEditor. The string should contain the following words: {“how”, “now”, “brown”, “cow”}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT