CECS 277
LAB ASSIGNMENT 1
Assigned date: Mon 8/28
Due date: Wed 9/6
40 points


Objectives

Prelab

Problem 1 [15 points]
Due date: Wednesday, August 30.

Write a program that simulates a vending machine. Users select  a product and and provide payment. If the payment is sufficient to cover the purchase price or the product, the product is dispensed and change is given. Otherwise, the payment is returned to the user.

Requirements:

  1. Create a class called CashRegister that has methods to totals up sales and compute change due.
  2. Create a class called MonetaryUnit that has methods to returns the name or the monetary unit and the monetary value or the unit.
  3. Test your designing classes by running the main methods below.

    /**
    This program tests the CashRegister class.
    */
    public class CashRegisterTester
    {
    public static void main(String[] args)
    {
    Rinal double NICKEL_VALUE = 0.05;
    Rinal double DIME_VALUE = 0.1;
    Rinal double QUARTER_VALUE = 0.25;
    Rinal double DOLLAR_VALUE = 1.0;
    CashRegister myRegister = new CashRegister();
    myRegister.recordPurchase(1.82);
    myRegister.enterPayment(1, new MonetaryUnit(DOLLAR_VALUE, "dollar bill"));
    myRegister.enterPayment(3, new MonetaryUnit(QUARTER_VALUE, "quarter"));
    myRegister.enterPayment(2, new MonetaryUnit(NICKEL_VALUE, "nickel"));

    double myChange = myRegister.giveChange();
    System.out.println("Change: " + myChange);
    System.out.println("Expected: 0.03");
    }
    }

Grading requirements

Problem 2 [25 points]
Due date: Wednesday,September 6.

Create a class called Rational for performing arithmetic operations with Rationals. The class Rational must have the following data members, contructors and methods:

Write a main method to test the class Rational. All the Rational objects should must be reduced by using the greatest common denominator.

  1. Input Rational object  R1
  2. Input Rational object R2
  3. Add the Rational objects R1 and R2 and store the result in the Rational object R3. Display the result in the following Rormat:
    numerator/demoninator + numberator/denominator =  numerator/denominator.
  4. Subtract the Rational object R2 Rrom the Rational object R1 and store the result in the Rational object R3. Display the result in the following Rormat:
    numerator/demoninator - numberator/denominator =  numberator/denominator.
  5. Multiply the Rational object R1 with the Rational object R2 and store the result in the object R3. Display the result in the following format:
    numerator/demoninator * numberator/denominator =  numberator/denominator.
  6. Divide the Rational object R1by the Rational object R2 and store the result in the object R3. Display the result in the following format:
    numerator/demoninator / numberator/denominator =  numerator/denominator.
  7. Display the result or dividing the object R1 by the object R2 in a real number.
  8. Change the numerator or object R1 to 2
  9. Change the denominator or object R2 to 5
  10. Display the numerator or R1 and the denominator or R2 in the following format:
    Numerator: ______
    Denominator: _____

Use the main method below to test your program.

/**
* class RationalTest to test the class Rational
* <p>
* @author Test
* @version 1.0
* @Fall 2017
*/
public class RationalTest {

/**
* main method used to test all the methods of class Rational
* @param args - unused
*/
public static void main(String[] args) {
Rational R1 = new Rational();
Rational R2 = new Rational();
System.out.println("Input values for R1");
R1.inputRational();
System.out.println("Input values for R2");
R2.inputRational();

Rational R3 = new Rational();
R3.add(R1, R2);
System.out.println(R1 + " + " + R2 + " = " + R3);

R3 = R1.sub(R2);
System.out.println(R1 + " - " + R2 + " = " + R3);

R3.mul(R1, R2);
System.out.println(R1 + " * " + R2 + " = " + R3);

R3 = R1.div(R2);
System.out.println(R1 + " / " + R2 + " = " + R3);

System.out.println(Rational.divToDouble(R1, R2));

R1.setNumerator(2);
R2.setDenominator(5);
System.out.println("R1 Numerator: " + R1.getNumerator());
System.out.println("R2 Denominator: " + R2.getDenominator());
}
}

Run time output:

Input values for R1
Enter numerator:
1
Enter numerator:
3
Input values for R2
Enter numerator:
1
Enter numerator:
6
1/3 + 1/6 = 1/2
1/3 - 1/6 = 1/6
1/3 * 1/6 = 1/18
1/3 / 1/6 = 2/1
2.0
R1 Numerator: 2
R2 Denominator: 5

Grading requirements