Title: String Methods
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

String Methods Problem

Create a package named string_methods for this program.

The Starting Template for this program is shown below. Fill in code where you see highlighted comments with instructions in the methods readString(), countOccurrences() ... etc. Write code in these highlighted sections that do what the comments request.

Don't modify main() or the printCounts() routine.

When you run your program, you will need to enter in a string for the "readString" method. Use the string:

Aardvarks get big and bigger as You feed them big and bigger ants who Feed on apples.


package string_methods;

// Starting Template
import java.util.Scanner;


public class MyStringMethods {

    private String myStr="";

    public void readString()
    {
        // Prompt the user and read in a String from a Scanner class from the keyboard
        // with the nextLine() method and store it in "myStr"
    }
    public void setString(String s)
    {
        myStr =s;
    }
    public int countOccurrences(String s)
    {
        // use indexOf and return the number of occurrences of the string "s" in "myStr"
    }
    public int countOccurrences(char c)
    {
        // use indexOf and return the number of occurrences of the character "c" in "myStr"
    }
    int countUpperCaseLetters()
    {
        // return the number of upper case letters in "myStr"
    }

    int countLowerCaseLetters()
    {
        // return the number of lower case letters in "myStr"
    }
    public void printCounts(String s, char c)
    {
        System.out.println("***************************************");
        System.out.println("Analyzing: myStr="+myStr);
        System.out.println("Number of Upper case letters="+ countUpperCaseLetters());
        System.out.println("Number of Lower case letters="+ countLowerCaseLetters());
        System.out.println("Number of "+s + " is "+ countOccurrences(s));
        System.out.println("Number of "+c + " is "+ countOccurrences(c));
    }

    public static void main(String[] args) {
        MyStringMethods msm = new MyStringMethods();
        msm.readString();
        msm.printCounts("big", 'a');

        msm.setString("Parked in a van down by the river bank .... The van evan  vanished  near a lot of other vans");
        msm.printCounts("van", 'a');

        MyStringMethods msm2 = new MyStringMethods();
        msm2.setString("the elephant in the room wouldn't budge");
        msm2.printCounts("the", 'i');
    }
}



Some of the Output from your program will look like the following


Enter in the desired String:
Aardvarks get big and bigger as you feed them big and bigger ants who feed on apples.
***************************************
Analyzing: myStr=Aardvarks get big and bigger as You feed them big and bigger ants who Feed on apples.
Number of Upper case letters=3
Number of Lower case letters=65
Number of big is 4
Number of a is 7
***************************************
Analyzing: myStr=Parked in a van down by the river bank .... The van had was near a lot of other vans

..... More output follows