answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Kryger
3 months ago
10

java methods Write a program whose input is a character and a string, and whose output indicates the number of times the charact

er appears in the string.

Computers and Technology
1 answer:
oksian1 [950]3 months ago
5 0

Answer:

This is the JAVA code:

import java.util.Scanner; // to obtain user input

//class designed to count occurrences of a character within a string

public class CharacterCounter

//method that calculates how many times a character appears within a string

{ public static int CountCharacter(String userString, char character)

   {  

       int counter = 0;   //keeps track of the number of times the character appears in the string

for (int i=0; i<userString.length(); i++) //iterates over every character in the string

       { // if the character aligns with the one sought in the string

           if (userString.charAt(i) == character)

           counter++;  // increments the character count variable

       }  

       return counter;     } // returns the count of the character found in the string

public static void main(String[] args) {

    Scanner scanner = new Scanner(System. in);// accepts user input

    String string;

    char character;

       System.out.println("Enter a string"); // prompts the user for a string

       string = scanner.nextLine(); //captures the input string

       System.out.println("Enter a character");

       // requests the character input from the user

       character = scanner.next().charAt(0); //captures and reads the character

       System.out.println("number of times character "+character + " appears in the string " + string +": " + CountCharacter(string, character));  }  }

// calls CountCharacter method to display the frequency of a character in the input string

Explanation:

The CountCharacter() method contains a loop where index variable i traverses through each character of the string, incrementing the counter variable by 1 each time the sought character matches. This indicates that the counter maintains the tally of occurrences of the character in the string. The loop terminates when i exceeds the string length, which means the full string has been scanned for the specified character. Finally, the count variable yields the number of times the character was found in the string. The main() function receives user inputs for a string and a character to search for within that string. It subsequently invokes the CountCharacter() function to determine how many times the designated character appears in the specified string, displaying the result on the screen.

The program's output is provided in the attached screenshot.

You might be interested in
Your computer uses 4 bits to represent decimal numbers (0, 1, 2, 3 and so on) in binary. What is the SMALLEST number for which a
amid [951]

Response: C

Clarification:

The reason is that four binary bits are insufficient to represent the number sixteen. The maximum is 15.

3 0
3 months ago
Use Excel to develop a regression model for the Consumer Food Database (using the "Excel Databases.xls" file on Blackboard) to p
oksian1 [950]
Step 1: Use the provided formula to create an Indicator Variable for cities with metro areas. Step 2: Apply a filter to isolate data specific to metro cities, selecting only those marked with Metro Indicator 1. Step 3: Transfer the filtered data to a new worksheet. Step 4: Navigate to Data - Data Analysis - Regression. Step 5: Input the specified Y-variable and X-variable ranges as indicated. Choose the output range and ensure residuals are checked, which will produce the Output Summary and the Predicted Values alongside Residuals. Please see the accompanying attachment.
5 0
1 month ago
Write a function called simulate_several_key_strikes. It should take one argument: an integer specifying the number of key strik
oksian1 [950]

Answer:

The code for the solution is implemented using Python 3.

  1. import random
  2. import string
  3. def simulate_several_key_strikes(l):
  4.    char_set = string.ascii_lowercase
  5.    return ''.join(random.choice(char_set) for i in range(l))
  6. print (simulate_several_key_strikes(10))

Explanation:

This program is designed to create random characters, and the quantity of characters produced is determined by the user's input. Therefore, it is necessary to import the random library (Line 1). Additionally, we incorporate the string module to access its associated method for generating English letters (Line 2).

Then, we define the function simulate_several_key_strikes that accepts one parameter, l. Inside this function, the ascii_lowercase method is utilized to establish the character set of lowercase letters, which is stored in the variable char_set (Line 5). The random.choice method randomly selects a character from char_set, concatenating it with an empty string (Line 6). A for-loop is utilized to produce a total of l characters, generating the final output.

To test the function, we input 10 as an argument, producing a sample output such as:

xuiczuskoj

7 0
2 months ago
Olivia needs to get permission to use a graph of data gathered by a trade association she plans to include the graph in a report
Natasha_Volkova [1026]

She can locate the copyright notice on the webpage that features the graph.

Explanation: ~Apex~

3 0
2 months ago
Read 2 more answers
Given num_rows and num_cols, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print
zubka84 [1067]

Answer:

The Python program is outlined below.

Explanation:

4 0
3 months ago
Other questions:
  • Which of the following type of online advertising intermediaries decide the placement and pricing of online display ads by using
    9·1 answer
  • A technician with a PC is using multiple applications while connected to the Internet. How is the PC able to keep track of the d
    8·2 answers
  • 3. Megan and her brother Marco have a side business where they shop at flea markets, garage sales, and estate
    9·1 answer
  • Which generation of programming languages provides programmers with a visual environment for coding programs?
    12·1 answer
  • 7.7 LAB: Using a while loop countdown Write a program that takes in an integer in the range 10 to 100 as input. Your program sho
    11·1 answer
  • Team A found 342 errors during the software engineering process prior to release. Team B found 184 errors. What additional measu
    12·1 answer
  • The Coins class was created to hold all your loose change, kind of like a piggy bank! For this exercise, you are going to simula
    15·1 answer
  • A ____ partition contains the data necessary to restore a hard drive back to its state at the time the computer was purchased an
    8·2 answers
  • There are two algorithms called Alg1 and Alg2 for a problem of size n. Alg1 runs in n2 microseconds and Alg2 runs in 100n log n
    13·1 answer
  • A competitor goes to your public website and discovers that they can get into a directory that you did not know could be reached
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!