Comp 110L

Lab Team Project #8

Due: 10/19/2009

 

 

Form a team of two to three people for this project.  The methods required to solve the problems below should be divided among the team members and each should independently solve his or her portion of the problem including the testing of it.  To test a method outside the whole program just write a main method that invokes your method and display the results.  To test the main method before the other methods are written, just write stubs for the other methods.  Once all the methods are completed they can be combined into a single file and the solution to the whole problem can be tested.  You should add the individual methods incrementally, that is, add just one of the completed sub methods to the program with the main method, test the program, then add another sub method, etc.

 

  1. Write a program that reads in three real numbers representing the sides of a triangle.  The program determines whether the numbers represent a valid triangle, and if so computes the area of the triangle.  If the input does not represent a valid triangle an appropriate message should be displayed to the user.  Solve this problem by creating a MyTriangle class which, in addition to the main method, contains the following two methods:

           

// Returns true if the sum of any two sides is greater than the third side.

public static boolean isValid(double side1, double side2, double side3)

 

// Returns the area of the triangle.

public static double area(double side1, double side2, double side3)

 

The area can be found by taking the square root of  s(s – side1)(s – side2)(s – side3),

where s = (side1 + side2 + side3)/2

 

Include the following two sets of numbers among your test cases:

            3.0  4.0  5.0

     3.2  4.4  7.8

 

(See problem 5.19 on page 174 of the text.)

 

  1. Write a program to determine whether an input credit number is valid according to the Luhn check. Prompt the user for a credit card number as a long integer and display whether the number is valid.

 

The Luhn check is performed as follows:

 

Step 1: Double every second digit from the right to the left.  If doubling of a digit results in a           two-digit number,  add up the two digits to get a single-digit number.

Step 2: Now add all single-digit numbers from Step 1.

Step 3: Add all the digits in the odd places from right to left in the card number.

Step 4: Sum the results from Step 2 and Step 3.

Step 5: If the result from Step 4 is divisible by 10, the card number is valid; otherwise it is invalid.

 

Create and use the following four methods in your solution:

 

            // Return true if the card number is valid.

      public static boolean isValid(long number)

     

      // Get the result from Step 2

      public static int sumOfEvenPlace(long number)

 

      // Return this number if it is a single digit,

      // otherwise return the sum of the two digits.

      public static int getDigit(int number)

 

      // Return the sum of the odd place digits in number

      public static int sumOfOddPlace(long number)

 

Use the following numbers as two of your test cases:

            4388576018402625

     4388576018410707

 

  (See problem 5.31 on page 177 of the text.)

 

    

For each of the above problems be sure to do the following:

 

1.            Plan your solution by writing down the algorithm to solve the problem before you write the code.

2.            Make sure your programs are self-documented including an appropriate comment header block.  Each independently written method should have its own header.

3.            Test the programs by running at least three test cases on each.

 

For each problem, turn in your algorithm design (this can be a skeleton program with comments only or a hand written description of the algorithm), a listing of the program, and a listing of the output from running the test cases.