Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Understand the Application

We would like to demonstrate our ability to control strings and use methods.
There are times when a program has to search for and replace certain characters in a string with other characters. This program will look for an individual character, called the key character, inside a target string. It will then perform various functions such as replacing the key character with a dollar-sign ($) wherever it occurs in the target string. For example, if the key character were
'a'
and the string were
"He who laughs last, laughs fast, faster, FASTEST."
then the operation of replacing the 'a' by a dollar-sign would result in the new string:
"He who l$ughs l$st, l$ughs f$st, f$ster, FASTEST."
As you can see, only the lower-case 'a' was detected and replaced. The letter 'a' and 'A' are considered different characters. This is called "case-sensitivity" and this entire program spec is case-sensitive.
This was only one possible task we might perform. Another would be to remove all instances of the key character rather than replace each with a dollar-sign. Yet a third might be to count the number of key characters. We are going to do it all.

Modifying vs. Rebuilding

Whenever we deal with strings, we have to decide whether we are going to modify an existing string or create a second string which has the desired changes. Since one cannot modify any string in the Stringclass, the first option does not really exist. There is a second class, called StringBuffer which does have this ability, but we don't really need to use it. We will not attempt to touch the original string in our operations. Instead, we will declare a new string object and build that up in stages, replacing or removing the desired characters of the original string by simply taking action on the new string we are building. When I say we "build" a string, I mean that we initialize the string to be empty, "", and then use concatenation to replace it with ever longer versions of itself. This is what you will discover and practice in the current assignment: You will use concatenation to build a string by repeatedly tagging new characters onto its end. This technique has some advantages that allow it to be used for a variety of purposes, so it's good to learn now.
It might seem like we are breaking the rule that String objects cannot be modified, but we will not be changing anything. Instead, we will be replacing what the String reference points to, with a slightly longer version of the old String at every phase. For example, consider this statement, which appends an exclamation point to the end of a string, myStr:
myStr = myStr + "!";
This statement uses the old value of myStr on the RHS, then completely throws away the old value on the LHS and replaces it with the new, longer, String. This is similar to a more familiar kind of numeric statement:
n = n + 3;
where we replace the old contents of n with new contents.
The Methods
We will be writing methods. Some will get input from the user (which take no parameters) and others will take arguments: the String and/or key character. Depending on the method we write, it will return one of the following types: a String, a char or an int. For example, one of the methods we write will take the key character and the target string as parameters and will return a new String which has all the occurrences of the key character replaced by dollar-signs. Its signature would look like this:
public static String maskCharacter(String theString, char keyCharacter)

We will be careful at all stages: input methods will only deal with user input, and not attempt to do computation. Computations will not do any input or output.

The exception is always main(). In main() we may do input and output directly if we are not required to use a method to do so by the spec. In our spec, this week, we will use input methods to get the input (not main()), but we will allow main() to do the output directly.

The Program Spec
Ask the user to enter both a key character and a target string (phrase, sentence, etc.). Then, show the user three things::
The target string with the key character replaced by dollar-signs.
The target string with the key character removed.
The number of occurrences of the key character (case sensitive) in the target string.
Clarification and Other Requirements
This program does not loop for different strings. Once it processes a string, the program ends.
Here, "character" meansanyprintable character. They can be letters, numbers or even special symbols.
Each target input string should be complex with a mix of characters, special symbols, numbers to show how the program handles each category.
All methods that are used will be static.
Input Method Specs
char getKeyCharacter()
This method requests a single character from the user and continues to ask for it until the user gets it right: this method will test to make sure the user only types one single character. 0, 2, 3 or more characters will be flagged as an error and the method will keep at the user until he types just one character. You are not required to use a char as an input variable -- in fact, you cannot solve the problem using a char as input (you must think about this and make the appropriate choice here). What matters is that a char is returned, as a functional return, to the client, main().
String getString()
This method requests a string from the user and continues to ask for it until the user gets it right: this method will test to make sure the user only types a string that has at least 4 characters. Make this minimum size a constant (final), and use that symbolic constant, not the literal (4) wherever it is needed. The acquired string will be returned as a functional return.
Processing Method Specs
You must write the methods below from scratch (based on the few available tools that I mention in the modules and the links to the allowable character and string methods provided in the module page "A Nice Example" ). Do not rely on any other built-in or pre-existing methods that appear to provide any of this functionality for you. There is a high value to you in practicing such logic.
String maskCharacter(String theString, char keyCharacter)
This method will take both a string and a character as parameters and return a new string that has each occurrence of the key character replaced by a dollar-sign, '

.
String removeCharacter(String theString, char keyCharacter)
This method will take both a string and a character as parameters and return a new string that has each occurrence of the key character removed, but all other characters left intact.
int countKey(String theString, char keyCharacter)
This method will take both a string and a character as parameters, and return the number of key characters that appear in the string (case sensitive).
Input Errors
Whenever the user makes an input error, keep at them until they get it right. Do not return from an input method until you have acquired a legal value, even if it takes years ... .
Test Run Requirements:
Submit at least four runs. In at least one of the four runs, intentionally commit input errors to demonstrate both kinds of illegal input described above.
A (partial) sample run is given at the bottom of this page.
Sample Output
Here is an example of a partial run sample:
/* ---------------------- Sample run ---------------------------------------

 

Please enter a SINGLE character to act as key: dgkfpo
Please enter a SINGLE character to act as key: O*(U
Please enter a SINGLE character to act as key: P
Please enter a phrase or sentence >= 4 and <= 500 characters:
sdf
Please enter a phrase or sentence >= 4 and <= 500 characters:
sfd sf jko POIJ JKOLP lkjsdf psadfjP sdfj erP sdfp

String with 'P' masked:
sfd sf jko $OIJ JKOL$ lkjsdf psadfj$ sdfj er$ sdfp

String with 'P' removed:
sfd sf jko OIJ JKOL lkjsdf psadfj sdfj er sdfp

# Ps: 4

(more runs supplied by student)

------------------------------------------------------------------------ */

 

Java, Programming

  • Category:- Java
  • Reference No.:- M92406801
  • Price:- $10

Priced at Now at $10, Verified Solution

Have any Question?


Related Questions in Java

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Object-oriented software development1 introduction 11

OBJECT-ORIENTED SOFTWARE DEVELOPMENT 1. Introduction 1.1 Assignment Requirement 1.2 Deliverables and Structure (what to submit) 1.3 Software Restrictions 1.4 How to score high... 1.5 Assumptions 2. System Requirements 2. ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Assignment - java program using array of objectsobjectives

Assignment - JAVA Program using array of objects Objectives - This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details - For this assignment, you are required to develop a Menu ...

Assignment - method in our madnessthe emphasis for this

Assignment - "Method in our Madness" The emphasis for this assignment is methods with parameters. In preparation for this assignment, create a folder called Assign_3 for the DrJava projects for the assignment. A Cityscap ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As