Question

In: Computer Science

in java write a program that initializes a String variable with "Welcome to Jac444!". Then, use...

in java

write a program that initializes a String variable with "Welcome to Jac444!". Then, use methods of the String class to examine the string and output a few properties of the string, namely The string by itself The length of the string The first character in the string The last character in the string The first word of the string The last word of the string Make sure that your output also contains how many words exists in the string.

Solutions

Expert Solution

You should save file as main.java in your system.

public class Main { 
    public int lengthOfString(String a) 
    { 
        //for printing length of string
        int len = 0; 
        len=a.length();
        return len; 
    } 
    public char firstcharOfString(String a) 
    { 
        //for printing first charcter in a string
        return a.charAt(0);
    }
    public char lastcharOfString(String a) 
    { 
        //for printing last charcter in a string
        int i=0;
        i=a.length()-1;
        return a.charAt(i);
    }
    public String firstwordOfString(String a) 
    { 
        //for printing first word in a string
         a= a.substring(0, a.indexOf(" ")); 
         return a;
    }
    public String lastwordOfString(String a) 
    { 
        //for printing last word in a string
         a= a.substring(a.lastIndexOf(" ")+1); 
         return a;
    }
     public int noofwordOfString(String a) 
    { 
        //for printing number of words in a string
         int c = 0;
        if (!(" ".equals(a.substring(0, 1))) || !(" ".equals(a.substring(a.length() - 1))))
        {
            for (int i = 0; i < a.length(); i++)
            {
                if (a.charAt(i) == ' ')
                {
                    c++;
                }
            }
            c = c+ 1; 
        }
        return c; // returns 0 if string sta
    }
    public static void main(String[] args) 
    { 
        String input = "Welcome to Jac444!"; 
        Main m = new Main(); 
        System.out.println("The length of word is " + m.lengthOfString(input));
        System.out.println("The first character in the string is "+ m.firstcharOfString(input));
        System.out.println("The last character in the string is "+ m.lastcharOfString(input));
        System.out.println("The first word in the string is "+ m.firstwordOfString(input));
        System.out.println("The last word in the string is "+ m.lastwordOfString(input));
        System.out.println("The number of words in the string is "+ m.noofwordOfString(input));
    } 
}

Related Solutions

Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and produces as output the sum of integers in an array, the largest and the smallest element in an array. Your program should contain the following methods: public static int sum(int[]) public static int findLargest(int[]) public static int findSmallest(int[])
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant float variable NJSALES_TAX; declares and initializes to 1000 an integer variable total; declares and initializes to zero a float variable grand_total; prompt and input on new line total; calculate grand_total to equal total plus (total*NJSALES_TAX); if grand_total <= 1000 print on new line “Grand total is less than or equal to 1000 it is $” and the grand_total to two decimal places; else if...
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a program in Java which will search a space delimited string of integers for the...
Write a program in Java which will search a space delimited string of integers for the two values which add up to a provided amount. The program must output th following text to the command prompt/console: Indexes: { Index of first number} { Index of second number} Example One: NumSerach( 100, " 5 75 25"); output: Indexes: 1 2
Write a java program with 3 recursive methods that reverse the order of a string. The...
Write a java program with 3 recursive methods that reverse the order of a string. The first recursive method should reverse the order starting from the left selecting the leftmost character as the head and the remaining part of the string as its tail., the second starting from the right selecting the rightmost character as the head and the remaining part of the string on its left as the tail, and the last a recursive method starting from the middle...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT