hts.StevenWood.com

[ Last Page ]

Looping Assignment

  1. Make a table of values for the following equation:
     

    for values of x from 3 to -3, increasing by 1. Put titles on the columns:  x         y.

    NOTE: When x = 2 and x = -2 you will end up "dividing by zero" this will give you an error when you declare the variable x as an int. To solve this problem, declare it as a double.

    Note: Don't worry about lining up the columns; it is very difficult to do.

     

  2. To find the remainder of the division:
     
    we can use the following Java structure:

                    y % x = r

    This new math symbol, % (percent sign), is read as "mod". The above statement is read: "y mod x = r", where r is the remainder of the division (a whole number). e.g. 4 mod 3 = 1; 12 mod 5 = 2.

    When r is zero, we know that x is a factor of the number y: e.g. 4 mod 2 = 0; 12 mod 4 = 0.
     
    Required: Build a class that will ask the user for a number greater than 1. Use a for loop that starts at 1 and ends at the given number. Inside the loop use the above mod structure (as part of an if) to test all of the numbers to see which are factors.  Display any number that is a factor.  NOTE: This works best if your variables are all int.

    E.g.

    Enter a number greater than 1: 8
    The factors are:
    1
    2
    4
    8

     
  3. This is similar to question #8 on the while Loops page:
    Create a program that asks for a digit. If the user enters a letter instead of a number, the loop should stop. Otherwise, it should ask for another number.
    HINT
    : you may want to use a Commonly Used String method named indexOf() that will test and see if the entered item is in the String "0123456789". You would end or continue looping based on the result of this test. 
    NOTE: You cannot see if strings are equal using the '==' comparison, you MUST use another Commonly Used String method that will see if the two strings are equal.

    e.g.
    Enter a digit: 2
    Enter a digit: 5
    Enter a digit: a
    Program stopping...


 

[Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+