Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java problem. Write a program that manipulates two strings. The program inputs t

ID: 3842480 • Letter: J

Question

Java problem. Write a program that manipulates two strings. The program inputs two strings (string1 and string2) and a character (char1) and an integer (position) to represent a character position within a string. The program will display the following:
1. Whether string1 is less, greater or equal to string2
2. string1 in upper case
3. string2 in lower case
4. The number of characters in string1
5. The first character in string2
6. The number of occurrences that char1 is contained in string1.
Hint: use a for loop and charAt
7. The character that is in the position of string1.
Turn in a run of the program that uses your first and last names as the strings.
Use at least two methods with the following headers:
int countOccurrences(String s, char c) // to answer #6
char showChar(String s, int pos)// to answer #7

Do not use any methods such as StringBuilder and array of Strings.

Explanation / Answer

import java.util.Scanner;

public class Hwk5A {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter your First Name: ");

        String string1 = keyboard.nextLine();

        System.out.print("Enter your Last Name: ");

        String string2 = keyboard.nextLine();

        System.out.print("Enter a character: ");

        char char1 = keyboard.next().charAt(0);

        System.out.print("Enter an number: ");

        int position = keyboard.nextInt();

        if(string1.compareTo(string2)>0){

            System.out.println("First name is greater than Last name");

        }

        else if (string1.compareTo(string2)==0) {

            System.out.println("First name is equal to Last Name");

        }

        else {

            System.out.println("First name is less than Last name");

        }

        System.out.println(" string1 uppercase:" + string1.toUpperCase());

        System.out.println("string2 lowercase:" + string2.toLowerCase());

        System.out.println("number of characters in string1: " + string1.length());

        System.out.println("first character in string2: " + string2.charAt(0));

        System.out.println("# of occurrences that char1 is contained for string1: "

                + countOccurrences(string1, char1));

        System.out.println("the character in string 1 from inputted position # is: "

                + showChar(string1, position));

    }  

    public static int countOccurrences(String s, char c) {

        int countOccurrences = 0;

        int totalOccurrences = 0;

        for(int i = 0; i <= s.length()-1; i++){

            if(s.charAt(i) == c) {

                countOccurrences = 1;

                totalOccurrences += countOccurrences;

            }  

        }

        return totalOccurrences;

    }

    public static char showChar(String s, int pos) {

        return s.charAt(pos);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote