CE00371-1 INTRODUCTION TO SOFTWARE DEVELOPMENT


Lab/Tutorial 5

Assessment weight : Q1 to Q5 (3%), Q6 (6%)

Due : Week 7

Programming exercises.

  1. What is the value of x after executing each of the following:

    	a.	x = 3 + 4 - 6 / 2 * 3;	b.	x = 12 % 5;
    
    	c.	y = 4;				d.	x = 3;
    		x = y << 2;				y = x++ + 4;
    
    	e.	y = 7;				f.	x = 5;
    		x = (y <= 4) ? 3 : 8;			x *= 3;
    
    Write a Java program to check your answers.

  2. Given the following algorithm

    Algorithm PRIME. Given a positive integer NUM, this algorithm determine whether or not the numbers is prime. LOOP is an integer loop variable

    1. input the number 
       Read(NUM)
    
    2. determine that the NUM is prime or has divisors other than 1 
       2.1 determine if number is divisible by 2
           If NUM = 2                       
           Then NUM is a prime number
           Else If NUM is even
                Then NUM is not a prime number and halt
                Else 2.11  determine if (NUM divisible by odd integers >= 3 and  <=  (square root of NUM))
                           For LOOP := 3 to sqrt(NUM) by 2
                           Do If (NUM mod LOOP) = 0
                              Then NUM is not a prime number and halt
    
    3. if we get here, print a message that NUM is a prime number 
       Write(NUM,"is a prime number");
    
    4. finished
       Halt
    
    Write a Java program to determine whether a number is a prime number.

  3. Second order algebraic equations are very commonly used in describing phenomena in the physical world and in engineering. They have the following form: aX2 + bX + c = 0; where a, b, and c are known values and X is the unknown. The value of X can be found by the so called binomial equation which states that

    X = -b + sqrt(b*b- 4*a*c) / 2*a ,
    and
    X = -b - sqrt(b*b- 4*a*c) / 2*a

    Thus X can be found if a, b, and c are known. For any set of values for a, b, and c there are two possible answers for X.

    Write a Java program to solve the binomial equation. Prompt the user to enter the values a, b, and c. Use only 2 decimal places for all values.

    Your output should be formatted as the example below:

    When a = 1.00, b = 0.00, and c = -1.00: X = 1.00 or -1.00

    Run your program with the following 3 sets of sample data:

    1) 1.0 0.0 -1.0
    2)-5.0 -2.3 4.7
    3)13.5 32.4 8.6

  4. Write a method called compArea() which will compute the area of a circle given the radius. The area of a circle is computed as follows:

    area = radius * radius * Math.PI

    The method should receive the radius in a parameter list and return the computed area as the method return value. A sample call to the method would be as follows:

    float area, radius;
    area = compArea(radius);

    Modify the program CirArea given in Lab2 to make use of this method.

  5. Write a Java program to play the Hi-Lo number guessing game. The program will randomly generate a secret positive number. The user will repeatedly guess the number, and for each guess, the program responds either HIGHER, LOWER, or YOU WIN. When the correct number is guessed, the program will output the total number of guess and then exit.

  6. The basic loan has three parameters: the annual interest rate, monthly payment, and loan balance.

    As an example, consider a $300 loan at 12% annual (i.e., 1% monthly) interest, which is being paid off at $100 per month.
    After the first month, the balance is $200, but after interest, it increases to $202.
    After the second month, the balance is $102, but after interest, it increases to $103.02.
    After the third month, the balance is 3.02, which after interest increases to $3.05.
    The remainder is paid off in the fourth month.

    Write a Java program that prompts for the three parameters and conveys this information in a nice table. You can assume the loan will be paid off in less than five months. For example:

    	The loan amount is : 300
    	The annual interest rate is: 12
    	The monthly payment is: 100
    
    		Starting            Middle              Ending
    	Month	Balance   Payment   Balance   Interest  Balance
    	-------------------------------------------------------
    	1.	300.00	   100.00    200.00       2.00   202.00	
    	2.	202.00    100.00    102.00       1.02   103.02
    	3.	103.02    100.00      3.02       0.03     3.05
    	4.	  3.05      3.05      0.00       0.00     0.00
    
    Run your program with the following 3 sets of sample data:

    1)300 12 100
    2)1000 15 350
    3)12345 33.25 3888.88


Students Notes:

Chose either console I/O, GUI dialog box, or Applet when you write your program. Upon completion, publish answers/programs for Q1 to Q5, and submit hard copy of the following: