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
1 month 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 [797]1 month 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
Charlie has a large book collection. He was keeping track of it in a spreadsheet, but it has grown big enough that he wants to c
Harlamova29_29 [932]

Answer:

Review the explanation

Explanation:

Response 1) Keeping all data in a single row presents challenges; if one record in the sheet is deleted, all related records may also vanish unexpectedly. For instance, should the entry for author "James Taylor" be removed, all the books associated with him would get eliminated as well.

Response 2) In the first normal form (1NF), each row is supposed to have just one tuple, but in the case of the author "James Taylor," the title field contains two books, necessitating separation into distinct rows.

Response 3) The table shown in the image does not exhibit deletion anomalies; for the author "May Norton," if you erase this record, it will result only in the deletion of her sole book.

Thus, it’s essential to transition this table into the second normal form (2NF).

Create distinct tables for Authors and Books

Authors(Author Name, Author Country)

Books(Author Name, Book Title, Publisher, Publisher Location, Year, Price)

This cannot undergo further normalization.

3 0
1 month ago
Explain working principle of computer?​
maria [879]

Answer:

The principle behind how computer systems operate involves a primary machine-based function that remains invisible to us, serving as a control center that changes the input data into output. This central element known as the central processing unit (CPU) illustrates that the operation of computers is quite intricate.

Explanation:

5 0
1 month ago
Here is a super challenge for you if you feel up for it. (You will need to work this out in Excel.) In 2017 the Islamic month of
8_murik_8 [892]

Response:

7/73

Clarification:

May consists of 31 days. By calculating 31 - 26, we find 5 days remaining until June. Therefore, Muslims fasted for a total of 35 days. In 2017, there were 365 days, leading to 365 - 35 = 330 days without fasting. Consequently, the fraction representing the fasting period is 35/365, which simplifies to 7/73.

4 0
29 days ago
You are given a string of n characters s[1 : : : n], which you believe to be a corrupted text document in which all punctuation
ivann1987 [930]

Response: explained in the explanation section

Explanation:

Given that:

Assume D(k) =║ true if [1::: k] is a valid sequence of words, or false otherwise

  • In determining D(n)

the sub problem s[1::: k] is a valid sequence of words IFF s[1::: 1] is valid and s[ 1 + 1::: k] is a valid word.

Thus, we derive that D(k) is defined by the following recurrence relation:

D(k) = ║ false max(d[l] ∧ DICT(s[1 + 1::: k]) otherwise

Algorithm:

Valid sentence (s,k)

D [1::: k]             ∦ array of boolean variables.

for a ← 1 to m

do;

d(0) ← false

for b ← 0 to a - j

for b ← 0 to a - j

do;

if D[b] ∧ DICT s([b + 1::: a])

d (a) ← True

(b). Algorithm Output

      if D[k] == True

stack = temp stack            ∦stack assists in displaying the strings in order

c = k

while C > 0

stack push (s [w(c)]::: C] // w(p) denotes the index in s[1::: k] of the valid word // at position c

P = W (p) - 1

output stack

= 0 =

cheers, I hope this aids you!!!

8 0
16 days ago
During normal Windows operations, you receive a BSOD, and your computer shuts down. You restart the computer and receive the err
Amiraneli [921]
The correct answer to this query is "Hard drive." Explanation: Given the context of the inquiry, it's suggested that the most likely cause of this issue is the improper connection of the video cable. An incorrectly connected video cable can lead to display disruptions, such as the screen flashing black. While overheating of the GPU could also be a potential cause, it typically results in visual artifacts rather than shifting displays. Therefore, "Hard drive" is indeed the right answer.
4 0
16 days ago
Other questions:
  • To reduce costs and the environmental impact of commuting, your company decides to close a number of offices and to provide supp
    14·1 answer
  • In the middle of the iteration, how should a team handle requirement changes from the customer? (1 correct answer)
    7·2 answers
  • Remember for a moment a recent trip you have made to the grocery store to pick up a few items. What pieces of data did the Point
    10·1 answer
  • Allan needs to ensure that an object is in a very precise location on a slide. He decides to use the Ruler option to achieve thi
    5·2 answers
  • Suppose that a scheduling algorithm (at the level of short-term CPU scheduling) favors those processes that have used the least
    10·1 answer
  • 1. Write the Python code needed to perform the following:2. Calculate state withholding tax (stateTax) at 6.5 percent3. Calculat
    9·1 answer
  • RADIAC instruments that operate on the ionization principle are broken down into three main categories based on what?
    15·1 answer
  • Consider a one-way authentication technique based on asymmetric encryption: A --&gt; B: IDA B --&gt; A: R1 A --&gt; B: E(PRa, R1
    13·1 answer
  • A wireless network does not benefit like a wired network does, when it comes to collision reduction. Which device reduces collis
    6·1 answer
  • Allison wants to use equations to simplify the process of explaining something to the Sales team, but the default eq
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!