Question

In: Computer Science

In this assignment you must submit TWO different programs both of which accomplish the same thing...

In this assignment you must submit TWO different programs both of which accomplish the same thing but in two different ways.

Using NetBeans, create a program that prompts the user for a sentence. Then the program displays the position of where the lowercase letter 'a' appears everywhere in the sentence. Here is a sample of the input and output:

Enter a sentence
for the night is dark and full of terrors.
The lowercase letter 'a' appears at character position 18
The lowercase letter 'a' appears at character position 22

Write TWO different programs that accomplishes the above using different techniques. Take a look at the JavaDocs for the String methods available to you. For example, one way is you might use the 'charAt()' method while your second program uses 'indexOf()'.

please send me the solution in java as soon as possible.

Solutions

Expert Solution

Code:

Technique1: Using charAt()

import java.util.*;
class Technique1
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a sentence");
        String s=sc.nextLine();
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)=='a')
                System.out.println("The lowercase letter 'a' appears at character position "+i);
        }
    }
}

Technique 2: Using indexOf()

import java.util.*;
class Technique2
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a sentence");
        String s=sc.nextLine();
        int index = s.indexOf('a');
        while (index >= 0) {
            System.out.println("The lowercase letter 'a' appears at character position "+index);
            index = s.indexOf('a', index + 1);
        }
    }
}

Output:


Related Solutions

Assembly Language homework assignment has 2 parts which require 2 separate programs. Submit both source code...
Assembly Language homework assignment has 2 parts which require 2 separate programs. Submit both source code files . Question 1 of 2. Integer arithmetic: [20 marks] Write an assembly language program that performs the following arithmetic operation on 4 integer numbers. ?????? = ((2∗?)+? )/(?+?) 1. Reserve memory for 4 integers and initialize them to numbers of your choice. 2. Reserve 4 bytes of memory for a single integer to store the result of the operation in a READWRITE section...
Do mortgage forbearance and a foreclosure moratorium accomplish the same thing and what is the difference...
Do mortgage forbearance and a foreclosure moratorium accomplish the same thing and what is the difference between the two?
For this assignment, you will review two different situations that both involve comparing a sample to...
For this assignment, you will review two different situations that both involve comparing a sample to a population. Consider the information available for each situation, choose the correct test, and use the data to conduct it. A researcher wants to compare the email use of the employees at one company to the corresponding population. Data is collected on the number of emails received by the company's employees. It will be necessary to use this sample data, as well as population...
Create a program (or set of programs) which accomplish the following for each complex data type...
Create a program (or set of programs) which accomplish the following for each complex data type (list,tuple,set,frozenset, dictionary): create the item with at least 4 elements Append an element Remove an element Insert an element in the middle somewhere Append another array of the same data type Append another array of a different data type (for example, if you have a dictionary, append a set or tuples And do the following: Output the results after each step. Report and explain...
Create a program (or set of programs) which accomplish the following for each complex data type...
Create a program (or set of programs) which accomplish the following for each complex data type (list,tuple,set,frozenset, dictionary): create the item with at least 4 elements Append an element Remove an element Insert an element in the middle somewhere Append another array of the same data type Append another array of a different data type (for example, if you have a dictionary, append a set or tuples And do the following: Output the results after each step. Report and explain...
For the assignment you have to do two thing Passing arguments when running the jar or...
For the assignment you have to do two thing Passing arguments when running the jar or class file To run the client, command to run from CMD will be: java –jar Client.jar <server-IP> <server-Port> OR java Client <server-IP> <server-Port> To run the server, command to run from CMD will be: java –jar Server.jar <server-Port> <dns-table-file-name> OR java Server <server-Port> <dns-table-file-name> In the server, Read the file and search for specific IP or URL based on the command received from client...
I need you to do both of the programs. Please specify which program is which! Array...
I need you to do both of the programs. Please specify which program is which! Array Allocator: Write a function that dynamically allocates an array of integers. The function should accept an integer argument indicating the number of elements to allocate. The function should return a pointer to the array. Call the function in a complete program. Reverse Array: Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of...
This assignment covers file I/O. Must design and code a program that can accomplish four things:...
This assignment covers file I/O. Must design and code a program that can accomplish four things: copy one file to another (i.e., place the contents of file A into file B), ‘compress’ a file by removing all vowels (a, e, i, o, u, and y) from the file, merge two files, and finally double each line of an input file, saving into an output file. Also make it so the input file's content can be changed from the example given....
For this assignment you must research and explain in detail the different methods How to hire...
For this assignment you must research and explain in detail the different methods How to hire people It is recommended that you research different training companies to develop an outline and complete an analysis of the steps which need to be taken. As you know it is important that you use several different methods to teach one of the topics because people learn in different ways. Be creative. Introduction – 5 marks Detail of training methods used – 30 marks...
a. If you have two chromatograms of the same mixture, but they are from two different...
a. If you have two chromatograms of the same mixture, but they are from two different GC instruments, then how do you determine which one gives you better separation. For example, if the first one gives short, broad peaks and the 2nd one gives tall, sharp peaks then which instrument gives you better separation? Explain your answer. b. What is the retention time of peaks in the Gas Chromatograph tell you about the compound? For example, if you have a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT