Suggested Projects


Skill-Building Exercises

  1. if Statement:   Write a program that reads four string values from the console, converts them into int values using the Integer.parseInt() method, and assigns the int values to variables named int_i, int_j, int_k, and int_l. Write a series of if statements that perform the conditional expressions shown in the first column of the following table and executes the operations described in the second column. (See programming examples 7.1 through 7.3 for hints on how to complete this exercise.)

    Conditional Expression

    Operation

    int_i < int_j

    Print a text message to the console saying that int_i was less than int_j. Use the values of the variables in the message.

    (int_i + int_j) <= int_k

    Print a text message to the console showing the values of all the variables and the results of the addition operation.

    int_k == int_l

    Print a text message to the console saying that int_k was equal to int_l. Use the values of the variables in the text message.

    (int_k != int_i) && (int_j > 25)

    Print a text message to the console that shows the values of the variables.

    (++int_j) & (--int_l)

    Print a text message to the console that shows the values of the variables.

    Run the program with different sets of input values to see if you can get all the conditional expressions to evaluate to true.

  2. if/else Statement:   Write a program that reads two names from the command line. Assign the names to string variables named name_1 and name_2. Use an if/else statement to compare the text of name_1 and name_2 to each other. If the text is equal print a text message to the console showing the names and stating that they are equal. If the text is not equal print a text message to the console showing the names and stating they are not equal. (See programming example 7.4 for an example of an if/else statement.)

    Hint: Use the String.equals(Object o) method to perform the string value comparison. For example, given two String objects:

     String name_1 = "Coralie"; String name_2 = "Coralie";

    You can compare the text of one String object against the text of another String object by using the equals() method in the following fashion:

     name_1.equals(name_2)

    The String.equals(Object o) method returns a boolean value. If the text of both String objects match it will return true, otherwise it will return false.

  3. switch Statement:   Write a program that reads a string value from the command line and assigns the first character of the string to a char variable named char_val. Use a switch statement to check the value of char_val and execute a case based on the following table of cases and operations: (See programming example 7.6 to see a switch statement in action.)

    Case

    Operation

    ‘A’

    Prompt the user to enter two numbers. Add the two numbers the user enters and print the sum to the console. (Hint: Study example 7.13 to see how to process user console input. Remember, the user will enter a string which must be converted to the proper type before performing the add operation.)

    ‘S’

    Prompt the user to enter two numbers. Subtract the first number from the second number and print the results to the console.

    ‘M’

    Prompt the user to enter two numbers. Multiply them and print the results to the console.

    'D’

    Prompt the user to enter two numbers. Divide the first number by the second and print the results to the console.

    default

    Prompt the user to enter two numbers, add them together, and print the sum.

    Don’t forget to use the break keyword to exit each case. Study the CheckBookBalancer application shown in example 7.13 for an example of how to process console input.

  4. while Statement:   Write a program that prompts the user to enter a letter. Assign the letter to a char variable named char_c. Use a while statement to repeatedly print the following text to the console so long as the user does not enter the letter ‘Q’:

     "I love Java!"

  5. do/while Statement:   Write a program that prompts the user to enter a number. Convert the user’s entry into an int and assign the value to an int variable named int_i. Use a do/while loop to add the variable to itself five times. Print the results of each iteration of the do/while loop. (Programming example 7.10 shows the do/while statement in action. Hint: You may need to use a separate variable to control the do/while loop.)

  6. for Statement:   Write a program that calculates the following summation using a for statement.

    image from book

  7. Chained if/else Statements:   Rewrite skill-building exercise 1 using chained if/else statements.

  8. Mixed Selection and Iteration Statements:   Rewrite skill-building exercise 3. Use a while loop to repeatedly process the switch statement. Exit the while statement if the user enters the character 'E'.

  9. Mixed Selection and Iteration Statements:   Rewrite skill-building exercise 3 again. This time make the while loop execute forever using the following format:

                               while(true){                           }

    Add a case to the switch statement that exits the program when the user enters the character 'E'. (Hint: Use the System.exit() method.)

  10. Mixed Selection and Iteration Statements:   Rewrite skill-building exercise 6. Repeatedly prompt the user to enter a value for n, calculate the summation, and print the results of each step of the summation to the console. Keep prompting the user for numbers and perform the operation until they enter a zero.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net