Question

In: Computer Science

Write a JAVA program that contains the following 4 void methods whic will print all subtrings...

Write a JAVA program that contains the following 4 void methods whic will print all subtrings of s in specific orders: printSub1(String s), printSub2(String s), printSub3(String s), and printSub4(String s)

For example, if S = "abcd":

printSub1 will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d"

printSub2 will print "a", "ab", "b", "abc", "bc", "c", "abcd", "bcd", "cd", "d"

printSub3 will print "d", "c", "b", "a", "cd", "bc", "ab", "bcd", "abc", "abcd"

printSub4 will print "abcd", "bcd", "abc", "cd", "bc", "ab", "d", "c", "b", "a"

(Note: the actual output will not have quotation marks around the substrings.)

Create a menu in your main method using a switch statement inside of a do…while loop so the user can repeatedly choose which method to execute from the menu. The user should be asked to input the string in your main method before the menu is presented to them.

Additional Assignment Requirements

1. You must use NESTED FOR LOOPS to do the work in each method in this lab.

2. YOU MAY NOT USE any concepts not yet taught in this course like ARRAYS.

3. The only built-in String methods you can use are length() and substring().

4. You must work with everything as Strings.

5. If appropriate for this algorithm, you should validate any input from the user to make sure the data input is an appropriate value to work in your program’s logic. You don’t have to worry about validating that it is the correct data type. For now, assume the user is only giving you the correct data type and just worry about validating the value given is usable in your program.

Solutions

Expert Solution

import java.util.*;
public class Main
{
    public static void printSub1(String s){
        int n=s.length();
        for(int i=0;i<n;i++){
            for(int j=i+1;j<=n;j++){
                System.out.print(s.substring(i,j)+" ");
            }
        }
        System.out.println();
    }
    public static void printSub2(String s){
        int n=s.length();
        for(int i=1;i<=n;i++){
            for(int j=0;j<i;j++){
                System.out.print(s.substring(j,i)+" ");
            }
        }
        System.out.println();
    }
    public static void printSub3(String s){
        int n=s.length();
        for(int i=1;i<=n;i++){
            for(int j=n-i;j>=0;j--){
                System.out.print(s.substring(j,j+i)+" ");
            }
        }
        System.out.println();
    }
    public static void printSub4(String s){
        int n=s.length();
        for(int i=n;i>0;i--){
            for(int j=n-i;j>=0;j--){
                System.out.print(s.substring(j,j+i)+" ");
            }
        }
        System.out.println();
    }
        public static void main(String[] args) {
                Scanner scanner=new Scanner(System.in);
                String s=scanner.next();
                boolean flag=true;
                while(flag){
                    System.out.println("Enter option to be executed in range 1-4");
                    int option=scanner.nextInt();
                    switch(option){
                        case 1: printSub1(s);break;
                        case 2: printSub2(s);break;
                        case 3: printSub3(s);break;
                        case 4: printSub4(s);break;
                        default: flag=false;
                    }
                }
        }
}

if we get any option other than 1-4, then we will exit the menu.


Related Solutions

Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
Please write a java program that has the following methods in it: (preferably in order)   a...
Please write a java program that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled, and the total tuition Design Notes: The...
Write a java program that will take a line of input and go through and print...
Write a java program that will take a line of input and go through and print out that line again with all the word numbers swapped with their corresponding numeric representations (only deal with numbers from one to nine). Sample runs might look like this: Please enter a line of input to process: My four Grandparents had five grandchildren My 4 grandparents had 5 grandchildren without array and methods.
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 program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT