java1.docx

In this assignment, you may use your knowledge of the following concepts:

1) Math (+, %, ++, +=) and relational operators (!=, ==, <,>)

2) For loop, if-else

3) Arrays – accessing elements in a location, finding the length of the array

4) String class – length(), toCharArray(), charAt(), substring()

5) Making a simple method call to built-in classes and sending an input (known as argument)

Create a class called Hw1FirstLastName.java in a project called hw1firstlastname

In the main method of this class file, create a string called sentence and assign it a word or sentence of your choice. The rest of the assignment involves working with the contents of this sentence. You should test and ensure that your program works with sentences of different lengths and characters (letters/numbers/special characters).

We will use this sentence as a password starting phrase to automatically create a new password as follows.

<10>Task 1. Create a new string called passwd formed by concatenating every alternate non-space character in sentence starting with the first. To do this, you need to a loop to go through the string sentence and retrieve characters from alternate index positions. If the extracted alternate character is NOT a space, then add it to the new String. Do not include spaces.

Print out the following.

<Output> The starting sentence is: ___________

<Output> The password in task 1 is: ___________

<10>Task 2. If the length of passwd is even, extract a substring which is the first half of passwd and use this as the new passwd. Leave passwd as is if its length is odd. Print this passwd.

<Output> The password in task 2 is: ___________

<10>Task 3. Convert passwd from task 2 to a character array called pwdArray. Sum the numeric value (Unicode value) of all the characters in pwdArray. To do this, create a new int variable called sum, and in a loop, add each char obtained from the array to sum (the underlying int value of the char will be obtained and added to sum). Once the loop has ended, divide this sum by 2 if the sum is even, or leave it as it is if the sum is odd. Then append the sum to the end of the passwd.

Print out the following.

<Output> The new password in task 3 is: ___________

Sample output 1 (passwd after step 1 is odd length)

The starting sentence is :B@n@n@_nut

The password is :Bnn_u

The password is :Bnn_u

The new password is :Bnn_u249

_______________________________________

Sample output 2 (passwd after step 1 is even length)

The starting sentence is :B@n@n@_nuts

The password is :Bnn_us

The password is :Bnn

The new password is : Bnn143

________________________________________

Sample output 3 (if there are spaces in sentence, output)

The starting sentence is :B@n@n@ nuts

The password is :Bnnus

The password is :Bnnus

The new password is :Bnnus259

Basic style guidelines - It goes without saying, be neat in writing code. Use white space, indent blocks of code (hitting Alt-Shift-F in Netbeans will auto indent for you) – and use comments as needed to document what you are doing. Follow the naming style conventions discussed in class.

Deliverables – please zip your entire project folder containing the src sub-folder and the nbproject subfolder (the others are not necessary). Name the zip file hw1_firstlastname.zip and submit to Canvas dropbox.