1answer.
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
Stolb23 [73]
3 years ago
12

Write a recursive method that finds the number of occurrences of a specified letter in a string using the following method heade

r: public static int count(String str, char a) For example, count("welcome",' e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the string.
Computers and Technology
1 answer:
Dafna1 [17]3 years ago
3 0

Answer:

//Java program to find the number of occurrences of a specified character in a string.

// Import the required packages.

import java.io.*;

import java.util.*;

//class declaration

class CharacterCount

{

// Main method.

public static void main (string args[ ] )

{

  try

      {

     // Taking the input from the user.

         System. out.print ("Type a string to analyze: ");

     // Creating the scanner class object.

         scanner input = new scanner (system. in);

         String str = input .next();

         System.out.print("Type a character to check: ")

         String temp = input.next();

         char c = temp.charAt(0);

     // Calling the method to get the character count.

         int count = <em>count</em>(str,c);

     // Displaying the result.

         system.out.print In("There are " + count + " " + c + " 's. ");

     // catching the exception.

         catch(Exception e)

         System.out.print In("Exception has occured in the class . Program will exit. ");

      // Exiting the system.

         System.exit(0);

         }

}

// Method to calculate the character count.

public static int count(String str, char a)

  {

    // checking for null string input.

      if(str.equals(""))

      {

       return 0;

      }

      else

      {

      int k = 0;

    // Character matching check.

      if ( str.substring(0, 1) . equals (Character.<em>toString</em>(a)) )

      {

   // Incrementing the counter.

      k++;

      }

   // Recursive call

      k += <em>count</em>(str.substring(1) , a);

              return k;

       }

     }

}

Explanation:

Output should show: Type a string to analyze: welcome

                                   Type a character to check: e

                                   There are 2 e's.

You might be interested in
Tom's Art Supplies used to sell art supplies through mail order catalogs, but the company's order takers often had difficulty de
olganol [36]
The answer is:bbbbbbbb
6 0
3 years ago
A(n) __________ is a popular way to describe relationships? (i.e., one-to-one,? one-to-many) in a relational database.
Leto [7]
A link is a popular way to describe relationships in a relational database.
There are three types of relationships (links) between tables:
1. One-to-one <span>relationship , that allows only one record on each side of the relationship.
2. </span>One-to-many <span>relationship, that allows a single record in one table to be related to multiple records in another table.
3. </span>Many-to-many<span> relationship, in which many records in a table can link to many records in another table. F</span>
6 0
3 years ago
1. Before operating any power tool, you need to
Anna007 [38]

Answer:

where glasses gloves turn on the power tool

Explanation:

7 0
2 years ago
2. Using the Book class developed in HW 4, write an application, BookLibrary, that creates a library (array) of up to 10 books a
qwelly [4]

BookLibrary, that creates a library (array) of up to 10 books. If the user selects the add option, issue an error message if the library is full.If the book with that title exists, issue an error message

Explanation:

import java.util.Scanner;

import java.util.Arrays;

public class Derro_BooksLibrary {

public static void main(String[] args) {

 Derro_Books[] Library = new Derro_Books[10];

 Scanner scan = new Scanner(System.in);

 Derro_Books book1 = new Derro_Books("", 20);

 Derro_Books book2 = new Derro_Books("hi", 10);

 Library[0] = book1;

 Library[1] = book2;

 System.out.println("Enter 1 for add, 2 for delete, 3 for change, 4 to stop application: ");

 int answer = scan.nextInt();

 int counter = 0;

 int z = 0;

 while (true) {

  if (answer == 1) {

   for (Derro_Books book : Library) {

    counter = counter + 1;

   }

   if (counter == 10) {

    System.out.println("Library full");

   }

   System.out.println("Enter Title: ");

   String title = scan.nextLine();

   for (int i = 0; i < Library.length; i++) {

    if (title == Library[i].getTitle()) {

     System.out.println("No Duplicated Books Titles Are Allowed");

     break;

    }

    else {

     z++;

    }

   }

   if (z < Library.length) {

   System.out.println("Enter number of pages: ");

   int pages = scan.nextInt();

   Library[z + 1] = new Derro_Books(title, pages);

  }

  if (answer == 2){

   if (Library.length == 0) {

    System.out.println("Library is empty");

   }

   System.out.println("Enter title of book: ");

   String title2 = scan.nextLine();

   for (int d = 0; d < Library.length; d++) {

    if (title2 == Library[d].getTitle())

     // Delete book

     System.out.println("Deleted book: " + title2);

    else

     System.out.println("Book does not exist");

   }

  }

  if (answer == 3) {

   if (Library.length == 0) {

    System.out.println("Library is empty.");

   } else {

    System.out.println("Enter book title: ");

    String title3 = scan.nextLine();

    for (int y = 0; y < Library.length; y++) {

     if (title3 != Library[y].getTitle())

      continue;

     else {

      System.out.println("Enter number of pages: ");

      int pages = scan.nextInt();

      Library[y] = new Derro_Books(title3, pages);

     }

    }

   }

  }

  String[] StringList = new String[10];

  for (int y = 0; y < Library.length; y++) {

   StringList[y] = Library[y].getTitle();

  }

  Arrays.sort(StringList);

  for (String bookt : StringList) {

   System.out.println(bookt + Library);

  }

 }

}

}

}

6 0
3 years ago
In 5-10 sentences, describe the purposes, design considerations, and common elements of most brochures.
lozanna [386]
The primary thing to consider when designing a brochure is the target audience. <span> Brochures are meant to capture attention and deliver information, so it's important that the audience will want to read it in the first place.</span> When planning the design, consider the placement of elements and how they are arranged, or the 'white space'. Also take into consideration whether photos will be used. This ensures readability. <span>Also plan for the brochure's color scheme and fonts. These capture the audience so these are core parts of the design.</span>
4 0
3 years ago
Other questions:
  • In Java :
    11·1 answer
  • Read the scenario, and then answer the question that follows.
    10·1 answer
  • Suppose you have an int variable called number. What Java expression produces the second-to-last digit of the number (the 10s pl
    15·1 answer
  • Convert the following pseudocode to C++ code. Be sure to define the appropriate variables. Store 25 in the speed variable. Store
    5·1 answer
  • What is the best temperature to set your air conditioner on?
    12·1 answer
  • Write a program that extracts the last three items in the list sports and assigns it to the variable last. Make sure to write yo
    8·1 answer
  • Which of the following is NOT part of the URL for an Internet web site?
    13·1 answer
  • What is meant by byte in computer terminology​
    6·2 answers
  • Question 1 (1 point)
    9·2 answers
  • What is pseudocode? O A way of describing a set of instructions in text form using plain english O A diagrammatic representation
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!