CS111 Programming Assignment 2

Instructor: Trish Cornez


  Keep in mind for the semester...
  1. Programs will be evaluated on functionality and code quality.

  2. Debug and comment all source code.

  3. Please list your name and date at the top of each .java file.

  4. Execute thorough test runs for your programs to assess functionality.

  5. Assigned work is to be an individual endeavor.
    Group or shared assignments will not be accepted.
    Remember that discussion about assignments with others should be of a general nature.




Written Answers

  1. Explain the OOP meaning of aggregation and composition. Provide an example.
  2. How is inheritance specified in Java?
  3. Every class in Java has a superclass. Explain the superclass Object. Idenify the member methods of this superclass
  4. What does UML stand for? Provide an example of a UML.



Program 1: OOP and Output to a File
Construct and implement a RomanNumeral class to store a roman numeral (a string) and its computed decimal value.
For example:
    Roman numeral : XX
    Decimal Value : 20

Code Guidelines

  1. Include a default and an explicit constructor.
  2. Add the methods shown below:
  3. During Roman times, order did not matter. Use the following decimal values of roman numbers:
      M : 1000
      D : 500
      C : 100
      L : 50
      X : 10
      V : 5
      I : 1
  4. Write a test app to verify all the member methods function correctly. You may use the test code below.
    Add a last task to write the results of the test cases to a text file. This will require you to use a try/catch block.

    public class MainApp {

      public static void main(String[] args) {
        System.out.println("Test Application for the RomanNumeral class\n");

        // DECLARE TWO ROMAN NUMERALS USING EXPLICIT AND DEFAULT CONSTRUCTORS.
        RomanNumeral x1 = new RomanNumeral("DXI");
        RomanNumeral x2 = new RomanNumeral();

        // BUILD A STRING CONTAINING ALL THE REQUIRED TEST CASE SCENARIOS
        String testCaseResults = "";

        // TEST 1: VERIFY THE INITIAL VALUES OF x1 AND x2.  COMPARE BOTH OBJECTS
        testCaseResults += "x1 is " + x1.getRomanValue() + " with a decimal value of " + x1.getDecimalValue() + "\n";
        testCaseResults += "x2 is " + x2.getRomanValue() + " with a decimal value of " + x2.getDecimalValue() + "\n";
        testCaseResults += "x1 and x2 are equal?  " + x1.equalTo(x2) + "\n";

        // TEST 2: RESET THE ROMAN VALUE FOR X2 AND COMPARE BOTH OBJECTS AGAIN
        testCaseResults += "\nReset x2:" + "\n";
        x2.setRomanValue("XID");
        testCaseResults += "x2 is now " + x2.getRomanValue() + " with a decimal value of " + x2.getDecimalValue() + "\n";
        testCaseResults += "x1 and x2 are equal?  " + x1.equalTo(x2) + "\n";

        // TEST 3: INCREMENT X2 AND COMPARE BOTH OBECTS AGAIN
        testCaseResults += "\nIncrement x2:" + "\n";
        x2.increment();
        testCaseResults += "x2 is now " + x2.getRomanValue() + " with a decimal value of " + x2.getDecimalValue() + "\n";
        testCaseResults += "x1 and x2 are equal?  " + x1.equalTo(x2) + "\n";

        // DISPLAY THE TEST CASE RESULTS STRING TO THE CONSOLE
        System.out.print(testCaseResults);

        //FINAL TASK: WRITE THE TEST CASE RESULTS TO A TEXT FILE
















      }
    }

  


Program 2

Construct and implement a Fraction class.
A Fraction class will be used to model a fraction, such as 1/4. This class should contain member data for the numerator and the denominator.

Guidelines:
  1. Create both default and explicit constructors.
  2. Create setters and getters for the data members.
  3. When coding the default constructor or setter for the denominator, do not allow the denominator to be set to zero.
  4. Use the test application shown below to guide you in creating your class and implementing the appropriate class methods. After running this test application.

    public class TestApp {

      public static void main(String[] args) {

        System.out.println("Test Application for the Fraction class");

        // INSTANTIATE ELEVEN FRACTION OBJECTS.
        Fraction f1 = new Fraction();
        Fraction f2 = new Fraction(2, 7);
        Fraction f3 = new Fraction(4, 15);
        Fraction f4 = new Fraction(2, 3);
        Fraction f5 = new Fraction(5, 8);
        Fraction f6 = new Fraction(3, 10);
        Fraction f7 = new Fraction(1, 8);
        Fraction f8 = new Fraction(3, 8);
        Fraction f9 = new Fraction(3, 18);
        Fraction f10 = new Fraction(4, 7);
        Fraction f11 = new Fraction(8, 14);

        // TASK 1: DISPLAY f1, INSTANTIATED BY THE DEFAULT CONSTRUCTOR
        System.out.println("1) f1 = " + f1.toString() + "  (default constructor)");

        // TASK 2: RESET FRACTION f1 AND REDISPLAY IT
        f1.setNum(3);
        f1.setDen(5);
        System.out.println("2) f1 = " + f1.toString() + "  (modified)");

        // TASK 3: ADD FRACTION f2 TO f1
        System.out.print("3) " + f1.toString() + "  +  " + f2.toString() + " = ");
        f1.add(f2);
        System.out.println(f1.toString());

        // TASK 4: SUBTRACT f4 FROM f3
        System.out.print("4) " + f3.toString() + " - " + f4.toString() + " = ");
        f3.subtract(f4);
        System.out.println(f3.toString());

        // TASK 5: MULTIPLY FRACTION f5 BY f6
        System.out.print("5) " + f5.toString() + " * " + f6.toString() + " = ");
        f5.multiply(f6);
        System.out.println(f5.toString());

        // TASK 6: DIVIDE FRACTION f7 BY f8
        System.out.print("6) " + f7.toString() + " " + ((char) 247) + " " + f8.toString() + " = ");
        f7.divide(f8);
        System.out.println(f7.toString());

        // TASK 7: REDUCE FRACTION f9
        System.out.print("7) The fraction " + f9.toString() + " can be reduced to ");
        f9.reduce();
        System.out.println(f9.toString());

        // TASK 8: COMPARE FRACTIONS f10 and f11
        System.out.print("8) " + f10.toString() + " == " + f11.toString() + ": " + f10.isEqual(f11));
      }

    }

  



Program 3: BlackJackApp

Create a BlackjackApp that allows the user to play Blackjack against the computer, also serving as the dealer. The game of blackjack is a popular casino card game that is often referred to as twenty-one.
To simplify this assignment,please ignore betting and complicated rules.
The objective of this assignment is to implement the basic rules of the game itself and build a Card and a Deck class.

The Game
The game starts by dealing two cards to the player and the dealer from the top of the deck.
The player's cards are both revealed to the player. The dealer received the first card face down (hidden from the player) and the second card is face-up.
To win the game, the player can add cards to his hand by typing "hit". The goal is for the player to get a hand value as close to a total of 21 as possible, but do not exceed 21, and not less than the value of the dealer's hand. If the player's hand value exceeds 21, he or she immediately loses. The player signals he is done adding cards to his hand by typing "stay".

Once the player has ended his turn, and the dealer then takes his turn by adding extra cards to his hand to get as close to 21 as possible. If the sum of the dealer's cards exceeds 21, the player wins. If the player and the dealer tie, the hand is a draw.

The following is an example of a game execution: