CS99

Fundamental Programming Concepts
Summer 2001

Lab 8

 

Overview

The goals of this lab are to: 

  • Complete the implementation of a class called Triangle and use it in a separate application

  • Write a class called NumberTypes and implement two of its methods, isTypeA and isTypeB.  Use the methods to perform a simple programming task.

  • Complete the implementation of a String method called reverse.

  • Write a program that solves a system of equations using Cramer's rule.
Part I: Triangle class

Part A:
Complete the implementation of class Triangle from lecture.  Note that we've changed the type of the sides of the triangle from double to int:

class Triangle {
   int a, b, c;

    /* Default constructor */
   public Triangle() {
      a = b = c = 0;
   }

    /* Constructor */
   public Triangle( int s1, int s2, int s3 ) {
     
a = s1;  b = s2; c = s3;
   }

   /* Return the area of the triangle */
   public double area() {
      /* your code goes here */
   }

   /* Return the perimeter of the triangle */
   public int perimeter() {
      /* your code goes here */
   }

   /* Returns the angle opposite side a (in degrees) */
   public double angleA() {
     
/* your code goes here */
   }

   /* Return the angle opposite side b in degrees */
   public double angleB() {
      /* your code goes here */
   }

   /* Return the angle opposite side c in degrees */
   public double angleC() {
     
/* your code goes here */
   }

   /* Return true if the triangle is a right triangle, false otherwise */
   public boolean isRight() {
      /* your code goes here */
   }
   
   /* Return true if no two sides are the same length, false otherwise */     
   public boolean isScalene() {
      /* your code goes here */
   }

   /* Return true if exactly two sides are the same length, false otherwise */ 
   public boolean isIsosceles() {
      
/* your code goes here */
   }

   /* Return true if all three sides are the same length, false otherwise */
   public boolean isEquilateral() {
      /* your code goes here */
   }

}

You should use Math.acos() in your angle methods.  The description for Math.acos() is

acos(double a)

          Returns the arc cosine of an angle, in the range of 0.0 through pi.

Hence, Math.acos( 0 ) returns pi/2, or 1.5707963267948966.  Note that the answer is given in radians.  To convert an angle in radians to degrees, one must multiply the angle by (180/pi).  (You can use Math.PI for this purpose.)  Print and save a copy of your completed Triangle.java.

Part B:
Next, create a program called TriangleTester.java.  TriangleTester should demonstrate that your class Triangle is working properly. In the main method of TriangleTester, ask the user for the lengths of the sides of a triangle and then output whatever information can be determined about the triangle using the methods described above.  Your output should mimic the following:

What is the length of the first side of your triangle? 5
What is the length of the second side of your triangle? 3

What is the length of the third side of your triangle? 4

Pondering. . . . .

Your triangle is a right triangle! 
Its area is 6. 
Its perimeter is 12. 
Its angles are 90, 36.9, and 53.1 degrees respectively.

Print and save a copy of TriangleTester.java.
  

Part II: Playing with numbers

Here are two definitions:

  • An integer is a �type A integer� if it does not have 2, 3, 5, or 7 as a divisor.

  • An integer is a �type B integer� if its ones place value, tens place value, and hundreds place value are not all distinct.

  66 is not a type A integer because it is divisible by 3; 143, on the other hand, is a type A integer because 143 = 11*13.  234 is not a type B integer because each of its three digits are distinct; on the other hand, 707, 991, 344, and 555 are all type B integers.

Part A:
In the class below, fill in the code for methods isTypeA and isTypeB.  They should each return true if the int parameter n is the type of number the method is testing for and false otherwise.

class NumberTypes {
   public static void main( String[] args) {

     
/* Print the number of type A integers <= 1,000,000 and the number of type B 
               integers in the range [100, 999]. */
   }

   /* Return true if parameter n is a type A integer */
   static boolean isTypeA( int n ) {
      /* Insert your code here */
   }

   /* Return true if parameter n is a type B integer */
      static boolean isTypeB( int n ) {
      /* Insert your code here */
   }
}

Part B:
In the main method, write code which will print the number of type A integers that are less than or equal to one million and the number of type B integers that are from the set {100, 101, �, 999}.  Use the methods you defined in Part A to do this.
 

Part III: Reversing a String

Complete the implementation for the String method reverse below:

/* *
*           Return a string whose characters are the reverse of the input string s
*           @param s  the string to be reversed
*/
static String reverse( String s )
{
            String answer;
            /* insert your code here */
            return answer;
}

To test your code, place the method in a class called Reverse.java and use it directly like so:

class Reverse {

   public static void main( String[] args) {
      String start = "abcdefghijklmnopqrstuvwxyz";
      String end = reverse( start );
      System.out.println( end );
   }

   static String reverse( String s ) {
      /* Your String reverse code here */
   }

}

If you use that particular program to test your method, it should print the alphabet in reverse order on the computer monitor.

Save Reverse.java and print it.  

For your convenience, here are some methods that pertain to strings

  • length          The number of characters that �make up� the string.
    s.length() yields the number of characters in the string object referenced by s.
    Thus, �a� has length 1, �abc� has length 3, and ��(the empty string) has length 0.

  • indexOf         Used to locate the first occurrence of one string in another.
    Characters in a string are indexed from zero starting with the leftmost character. If s = �abcdbc� then s.indexOf(�a�) yields 0, s.indexOf(�bc�) yields 1, and s.indexOf(�cb�) yields �1 because no occurrences are found.

  • substring          Used to extract a contiguous portion of a string.
    If i and j are integers then s.substring(i,j) yields a string that is made up of the characters in positions i thru j-1. Thus if s = �abcdef� then s.substring(3,4) yields �d�, s.substring(3,6) yields �def�, substring(3,3) yields the empty string, and s.substring(3,10) is an error.

  • equals               Used to find out if one string equals another.
    If s1 and s2 refer to string objects then s1.equals(s2) is true if the two strings are equal in each position.

  • concatenation       Used to �glue� one string to another.
    If s1 =�ab�, s2 = �xyz�, and s3 = s2+s1+s2, then s3 references �xyzabxyz�.

Part IV: Cramer's Rule

Write a program that solves a system of two equations using Cramer's rule.  The input will be the coefficients and constants in the system, and they will all be integers. The output will be the solution to the system.  Call your program EquationSolver.java.

Background: Cramer's Rule

Cramer's Rule is a method for solving a system of linear equations. If you have two equations with variables x and y written as:

ax + by = c

dx + ey = f

then the solution for x and y can be found using the following equations:

x = (ce-bf)/(ae - bd), y = (af - cd)/ (ae - bd).

Note that Cramer's rule does not work if the expression (ae - bd) is equal to zero.  We shall ignore that possibility.

Part A:
In your class EquationSolver, write the following method:

/**
*              Returns the determinant of the 2x2 matrix [a b; c d], or ad - bc.
*              @param a b c d                    The coefficients of the 2x2 matrix
*/
public static int det( int a, int b, int c, int d ) {

               
/* Your code goes here */

}  

Part B:
Use the method you wrote in Part A to complete this part.  In method main of EquationSolver, write code so that the output looks like the following:

  Enter the coefficients and constants for this system of equations:
    ax + by = c
    dx + ey = f

Enter a: 1 
Enter b: 2
Enter c: 5
Enter d: 2
Enter e: -1
Enter f: 0

The solution to the system of equations:

    1x + 2y = 5
    2x + -1y = 0

is:
    x = 1.0
    y = 2.0
 

Additional Requirements

  • You should divide your program into sections: the first section should accept input, the second section should be responsible for the processing, and third section prints the output.

 

Part V: Submit Your Lab

Hand in printouts for  

  • Triangle.java, TriangleTester.java

  • NumberTypes.java

  • Reverse.java

  • EquationSolver.java

As usual, don't forget to put your name, net ID, and date at the top of each file.

This lab is due next Thursday, 26 July 2001 at the beginning of the lab session.  That means you have one week to complete this assignment.  Hand it in to Siddharth as usual.