CECS 277
LAB ASSIGNMENT 1
Assigned date: Mon 8/28
Due date: Wed 9/6
40 points
Objectives
- Be Familar with classes and objects
- Be able to design a class for a given problem
- Design a functional and efficient methods
- Choose appropriate data representations
Prelab
- Read chapter 8 - Classes and objects
- Read the javadoc note posted on the class website
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:
- Create a class called CashRegister that has methods to totals up sales and compute change due.
- Create a class called MonetaryUnit that has methods to returns the name or the monetary unit and the monetary value or the unit.
- 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
- A hard copy or your designing classes.
- Document your program with Javadoc and other comments
- Demonstrate the result to the instructor
- Submit the lab assignment including the main method to the Beachboard.
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:
- Data members:
private
int numerator
int denominator
- Constructors
Default-argument constructor
Two-argument constructor to initialize numerator and denominator
- Accessors (Getters)
public int getNumerator() - return the numerator
public int getDenominator() - return the denominator
- Modifiers (Setters)
public void setNumerator(int value) - set the numerator to value
public void setDenominator(int value) - set the denominator to value
- public inputRational()
Input the numerator and the denominator from the keyboard
- public String toString()
return a string in the form numerator/denominator
- private int gcd(int m, int n) - return the greatest common divisor
int r;
while(n != 0)
{ r = m % n;
m = n;
n = r;
}
return m;
- Add two Rational numbers
pubic void add(Rational, r1, Rational r2)
- Subtract two Rational numbers
public Rational sub(Rational r)
- Multiply two Rational numbers
public void mul(Rational r1,Rational r2)
- Divide two Rational Numbers
public Rational div(Rational R)
- Return the real value or Rational r1 divided by Rational r2
public static double divToDouble(Rational r1,Rational r2)
Write a main method to test the class Rational. All the Rational objects should must be reduced by using the greatest common denominator.
- Input Rational object R1
- Input Rational object R2
- 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.
- 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.
- 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.
- 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.
- Display the result or dividing the object R1 by the object R2 in a real number.
- Change the numerator or object R1 to 2
- Change the denominator or object R2 to 5
- 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
- A hard copy or your source code, and runtime output.
- Document your program with Javadoc and other comments
- Demonstrate the result to the instructor
- Submit the lab to the Beachboard.