Create your java source file with the public class

Assignment Help JAVA Programming
Reference no: EM131274248

Assignment: Encoding a String

Skills
Looping
Branching

Use of the length(), indexOf(), and charAt() methods of class tring

Use of the static Integer.toHexString method to convert a character to its ASCII hex value

Description

In this assignment, you'll be URL encoding of a line of text. Web browsers URL encode certain values when sending information in requests to web servers (URL stands for Uniform Resource Locator).

Your program needs to perform the following steps:

Prompt for a line of input to be URL encoded

Read the line of text into a String (using the Scanner class nextLine method)

Print the line that was read.

Print the number of characters in the line (using String.length)

Create an output String that is the URL encoded version of the input line (see below for details on encoding).

Print the URL encoded String

Print the number of characters in the encoded String.

To convert a String to URL encoded format, each character is examined in turn:

The ASCII characters 'a' through 'z', 'A' through 'Z', '0' through '9', '_' (underscore), '-' (dash), '.' (dot), and '*' (asterisk) remain the same.

The space (blank) character ' ' is converted into a plus sign '+'.

All other characters are converted into the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the lower 8-bits of the character.

Use a for loop which increments it's index variable from 0 up to the last character position in the input string. Each iteration of the loop retrieves the character at the 'index' position of the input string (call the String class charAt() method on the input string). Use if statements to test the value of the character to see if it needs to be encoded. If encoding is not required, just concatenate it to the output encoded String. If encoding is required, concatenate the encoded value to the output encoded String. For example, if the input character is a blank, you want to concatenate a '+' character to the output encoded String (as described above).

Note: one technique to determine if a character is one that remains the same is to first create an initialized String containing all of the letters (both upper and lower case), digits, and other special characters that remain the same as described above. Then, call the String indexOf method on that String, passing the character to be tested. If -1 is returned from indexOf, the character was not one of those that remains the same when URL encoding.

For those characters that need to be converted into hex format (%xy above), you can call the pre-defined static Integer.toHexString method, passing the character as an argument. It returns the hex value of the character as a String, which you can concatenate to the encoded output String with the accompanying '%' symbol:

String hexValue = Integer.toHexString(srcChar);

encodedLine += '%' + hexValue;

Values that are URL encoded in this manner can be URL decoded by reversing the process. This is typically done by a web server upon receiving a request from a browser.

Sample Output

Enter a line of text to be URL encoded

This should have plus symbols for blanks

The string read is: This should have plus symbols for blanks

Length in chars is: 40

The encoded string: This+should+have+plus+symbols+for+blanks

Length in chars is: 40

Enter a line of text to be URL encoded

This should have hex 2f for /

The string read is: This should have hex 2f for /

Length in chars is: 29

The encoded string: This+should+have+hex+2f+for+%2f

Length in chars is: 31

Test Data

Use all the following test data to test your program, plus an example of your own:

This should have hex 2f for /

An encoded + should be hex 2b

123 and _-.* are unchanged

Angles, equals, question, ampersand > < = ? &

Getting started

Before you start writing Java code, it's usually a good idea to 'outline' the logic you're trying to implement first. Once you've determined the logic needed, then start writing Java code. Implement it incrementally, getting something compiled and working as soon as possible, then add new code to already working code. If something breaks, you'll know to look at the code you just added as the likely culprit.

To help you get started with this homework, here's an outline of the logic you'll need (sometime referred to as 'pseudo-code'):

Prompt for the line of input.

Read a line into the input string.

Set the encoded output string to empty.

Loop through each character in the input string.

{

Get the n'th character from the input string (use String's charAt method).

if (the character is a blank)

concatenate '+' to the encoded output string

else if (the character remains unchanged)

concatenate the character to the encoded output string

else

concatenate '%' and the hex encoded character value

to the encoded output string

}

Print the encoded output string.

Once you understand this logic, start writing your Java code. An example of the steps you could take are as follows:

Create your Java source file with the public class and main() method (like homework 1).

In the main() method, add code to prompt for the input line, read in the line of input, and print it out.

Next, add the for-loop that loops through each character of the input string. You can use the length() method on your input string to get the number of characters in it to control your loop.

The first statement in the loop should call charAt() on the input string to get the next character to examine.

To check things are working ok so far, make the next statement in the loop print out the character position (the for-loop index variable) and character from the string.

Get this much compiled and running first.

With this much done, if you read in a line containing "hello", you'd get output something like this from the temporary output statement within the loop:

char 0 is h

char 1 is e

char 2 is l

char 3 is l

char 4 is o

Once you've got this compiled and working, starting adding the if/else-if/else logic inside the loop to build the encoded output string.

Reference no: EM131274248

Questions Cloud

Why could this rewrite be considered plagiarism : Explainwhat it means to paraphrase and why this is an important practice for all individuals participating in academic writing. What are some additional features of academic writing that should be considered as you write papers within your program?
Ip address of the client : "Your IP address is:" followed by the IP address of the machine that has connected. The IP address of the client may be obtained by using the getInetAddress method in the Socket class.
Multilayer infrastructure architecture : Using Visio, create a high level large enterprise data center design that follows the Cisco®multilayer infrastructure architecture. Please use the Insert/Comment or Insert/Text Box to provide a brief explanation of each component with detail.
Approach within an organization : Specify at least three (3) approaches for marketing IT's value. Propose (1) method for implementing each approach within an organization. Provide one (1) example of each approach to support your answer.
Create your java source file with the public class : Create your Java source file with the public class and main() method (like homework 1). In the main() method, add code to prompt for the input line, read in the line of input, and print it out.
Discuss how you will evaluate and hold employees accountable : Suppose you have a supervisee who has earned a negative quarterly evaluation. Discuss how you would talk with this supervisee, including three examples of statements or questions you would ask during your discussion. Keep the ethics codes and mult..
Create a japplet that contains two parallel arrays : Create a "JApplet" that contains two parallel arrays with at least five friends' names and phone numbers. Allow the user to enter either a name or phone number and to click a "JButton" to display the other.
Hexadecimal notation to represent data in computing : Provide an example of where we continue to use hexadecimal notation to represent data in computing and explain why we do not use binary
Process done inside the program : Assignment: A program is to be made such that the process done inside the program should be seen as command like processing, finding etc. Please do specify every shortcuts and you can apply any design as you want.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Accepts a scanner representing an input file

Write a method in JAVA called stripHtmlTags that accepts a Scanner representing an input file containing an HTML web page as its parameter, then reads that file and prints the file's text with all HTML tags removed. A tag is any text between the c..

  Generates n random numbers

Create a VI that generates n random numbers, where n is a user input. Display the n random numbers in an array, and in a second array display the same random umbers in ascending order.

  Create a program in java that displays hello world

Create a program in Java that displays "Hello world!" Take a screen shot that shows the program's successful compilation and execution

  Write a method called range that returns the range of value

Write a method called 'range' that returns the range of values in an array of integers. The range is defined as 1 more than the difference between the maximum and minimum values in the array. For example, if an array called list contains the value..

  Simulating the behaviour of a train management system

You will develop the component classes of a program for simulating the behaviour of a train management system - You must implement these classes as if other programmers were, at the same time, implementing the code that instantiates them and calls t..

  Implement a personal address book

How to use the program. As soon as an address book is loaded, print out a concise but complete list of the commands a user can enter. One of them should be a "help" command to print out the list again.

  Create your listarray class

Create your ListArray class that is functionally similar to the formal Java ArrayList Utility class. here are the requirements

  Write a java application to display the following gui

Write a Java application to display the following GUI. At this point you are only implementing the display. We are not ready to make the calculator actually do any calculations

  Write a program to convert kilograms into pounds

Write a program that prompts the user to enter the mass of a person in kilograms and outputs the equivalent weight in pounds. Output both the mass and the weight rounded to two decimal places.

  The class date was designed and implemented

In Programming Exercise 2, the class Date was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class date so that, it addition to the operations already defined, it can perform the following operation..

  Difference between the two following statements

What exactly is the difference between the two following statements and which is preferred, please provide details: 1) frame.setSize(400, 300); // Set JFrame Size

  What is method overloading

1. What is method overloading? 2. What is the purpose of a constructor? 3. How do you call a method of one class from a method of another class?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd