Section 02: Control Statements Part I

PHP’s if, if...else and while statements, are three of the building blocks that allow specified logic required for member functions to perform their tasks. Also, PHP’s assignment, increment and decrement operators. These additional operators abbreviate and simplify many program statements.

  • Exercise 01: Gas Mileage

    Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording the number ofmiles driven and the number of gallons used for each tankful. Develop a script that will take as input the miles driven and gallons used (both as integers) for each tankful. The script should calculate and output HTML5 text that displays the number of miles per gallon obtained for each tankful and prints the combined number of miles per gallon obtained for all tankfuls up to this point. Use prompt dialogs to obtain the data from the user.

    Date completed: 18/05/2018

  • Exercise 02: Credit Limits

    Develop a JavaScript program that will determine whether a department-store customer has exceeded the credit limit on a charge account. For each customer, the following facts are available:

    • a) Account number (an integer)
    • b) Balance at the beginning of the month
    • c) Total of all items charged by this customer this month
    • d) Total of all credits applied to this customer's account this month
    • e) Allowed credit limit

    The program should use a while statement to input each of these facts, calculate the new balance (= beginning balance + charges − credits) and determine whether the new balance exceeds the customer credit limit. For those customer's whose credit limit is exceeded, the program should display the customer's account number, credit limit, new balance and the message “Credit Limit Exceeded.”

    Date completed: To Be Completed

  • Exercise 03: Sales Commission Calculator

    A large company pays its sales people on a commission basis. The sales people each receive $200 per week plus 9% of their gross sales for that week. For example, a sales person who sells $5000 worth of chemicals in a week receives $200 plus 9% of $5000, or a total of $650. You’ve been supplied with a list of the items sold by each sales person. The values of these items are as follows:

    Item Value
    1 239.99
    2 129.75
    3 99.95
    4 350.89

    There’s no limit to the number of items that can be sold. Develop a Java application that uses a while statement to input each sales person’s gross sales for last week and calculates and displays that sales person’s earnings. Process one sales person’s figures at a time.

    Date completed: To Be Completed

  • Exercise 04: Salary Calculator

    Develop a Java application that determines the gross pay for each of three employees. The company pays straight time for the first 40 hours worked by each employee and time and a half for all hours worked in excess of 40. You’re given a list of the employees, their number of hours worked last week and their hourly rates. Your program should input this information for each employee, then determine and display the employee’s gross pay. Use class Scanner to input the data.

    Date completed: To Be Completed

  • Exercise 05: Find The Largest

    The process of finding the largest number (i.e., the maximum of a group of numbers) is used frequently in computer applications. For example, a program that determines the winner of a sales contest inputs the number of units sold by each sales person. The sales person who sells the most units wins the contest. Write a Java application that uses a while statement to determine and print the largest number of 10 numbers input by the user. Your program should use three variables, as follows:

    counter: A counter to count to 10 (i.e., to keep track of how many numbers have been input and to determine when all 10 numbers have been processed).

    number: The current number input to the program.

    largest: The largest number found so far.

    Date completed: To Be Completed

  • Exercise 06: Find The Two Largest Numbers

    Find the two largest values among the 10 numbers. [Note: You must input each number only once.]

    Date completed: To Be Completed

  • Exercise 07: Tabular Output

    Write a Java application that uses a while statement and the tab escape sequence '\t' to print the following table of values:

    N10*N100*N1000*N
    11010001000
    22020002000
    33030003000
    44040004000
    55050005000

    Date completed: To Be Completed

  • Exercise 08: Validating User Input

    Write an application that will validate its inputs. On any input, if the value entered is other than 1 or 2, keep looping until the user enters a correct value.

    Date completed: To Be Completed

  • Exercise 09: Square Of Asterisks

    Write an application that prompts the user to enter the size of the side of a square, then displays a hollow square of that size made of asterisks. Your program should work for squares of all side lengths between 1 and 20.

    square

    Date completed: To Be Completed

  • Exercise 10: Palindromes

    A palindrome is a number or a text phrase that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write an application that reads in a five-digit integer and determines whether it’s a palindrome. [Hint: Use the division and modulus operators to separate the number into its individual digits.]

    Date completed: To Be Completed

  • Exercise 11: Printing The Decimal Equivalent Of A Binary Number

    Write an application that Inputs an integer containing only 0's and 1's (i.e., a "binary" integer) and prints its decimal equivalent. Use the modulus and division operators to pick off the "binary" number's digits one at a time from right to left. Much as in the decimal number system, where the right most digit has a positional value of 1, the next digit left has a positional value of 10, then 100, then 1000, and so on, in the binary number system the right most digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, and so on. Thus the decimal number 234 can be interpreted as 2 * 100 + 3 * 10 + 4 * 1. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8, or 13.

    Date completed: To Be Completed

  • Exercise 12: Checkerboard Pattern Of Asterisks

    Write an application that displays the following checkerboard pattern. Your program must use only three output statements, one of each of the following forms:

    • System.out.print( "*" );
    • System.out.print( " " );
    • System.out.println();
    square

    Date completed: To Be Completed

  • Exercise 13: Multiples Of 2 With An Infinite Loop

    Write an application that prints the powers of the integer 2, namely 2, 4, 8, 16, 32, 64, etc. Your while loop should not terminate (i.e., you should create an infinite loop). To do this, simply use the keyword true as the expression for the while statement. What happens when you run this program?

    Date completed: To Be Completed

  • Exercise 14: Calculating A Circle's Diameter, Circumference And Area

    Write an application that reads the radius of a circle (as a double value) and computes and prints the diameter, the circumference and the area. Use the value 3.14159 for Π.

    Date completed: To Be Completed

  • Exercise 15: Sides Of A Triangle

    Write an application that reads three non-zero double values and determines and prints whether they could represent the sides of a triangle.

    Date completed: To Be Completed

  • Exercise 16: Sides Of A Right Triangle

    Write an application that reads three non-zero integers and determines and prints whether they're the sides of a right triangle.

    Date completed: To Be Completed

  • Exercise 17: Factorial

    The factorial of a non-negative integer n is written n! (pronounced "n factorial") and is defined as follows:

    n! = n · (n – 1) · (n – 2) · … · 1 (for values of n greater than 1)

    and

    n! = 1 (for n = 0 or n = 1).

    For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120.

    Use while statements in each of the following:

    (a) Write an application that reads a non-negative integer and computes and prints its factorial.

    (b) Write an application that estimates the value of the mathematical constant e by using the formula:

    e = 1 + 1/1! + 1/2! + 1/3! + …

    Prompt the user for the desired accuracy of e (i.e., the number of terms in the summation).

    (c) Write an application that computes the value of ex by using the formula

    ex = 1 + x/1! + x2/2! + x3/3! + …

    Prompt the user for the desired accuracy of e (i.e., the number of terms in the summation).

    Date completed: To Be Completed