15,029,990 members
See more:
This problem is composed of two parts.
1a. Enforce the precondition for the splitBil();
1b. Enforce the precondition for the calculateMonthlyTakehomePay();

Here is the code:
Java
```	 * @precondition: amount > 0 AND tipRate >= 0 AND numDiners >= 1
* @postcondition: none
*
* @param amount the amount of the bill before tip
* @param tipRate the tip rate to be applied to the bill, as a percentage (e.g., 20.0 for 20% tip, 15.0 for 15% tip, etc)
* @param numDiners the number of people splitting the bill
* @return the amount of money each diner must pay
*/
public double splitBill(double amount, double tipRate, int numDiners) {
// TODO #4a

double totalBill = amount * (1 + (tipRate / 100));
double fairShare = totalBill / numDiners;
return fairShare;
}

/**
* Calculates monthly take-home pay given an annual salary and a tax rate.
*
* @precondition yearlySalary > 0 AND 0 <= taxRate <= 1
*
* @param yearlySalary an annual salary
* @param taxRate tax rate, expressed as a number between 0 and 1 (i.e., a 25% tax rate would be 0.25)
* @return take-home pay for one month
*/
public double calculateMonthlyTakehomePay(double yearlySalary, double taxRate) {
// TODO #4b
double taxes = yearlySalary * taxRate;
double netPay = yearlySalary - taxes;
return netPay / 12;
}

Here are the tests for the splitbillmethod() code for 1A.:

public class TestSplitBill {

@Test
public void testShouldNotAllowZeroAmount() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act & Assert: assert that calling splitBill with zero amount throws
//		an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.splitBill(0, 10, 2);
});
}

@Test
public void testShouldNotAllowNegativeAmount() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act & Assert: assert that calling splitBill with negative amount throws
//		an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.splitBill(-1, 10, 2);
});
}

@Test
public void testShouldNotAllowNegativeTipRate() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act & Assert: assert that calling splitBill with negative tipRate throws
//		an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.splitBill(10, -1, 2);
});
}

@Test
public void testShouldNotAllowZeroDiners() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act & Assert: assert that calling splitBill with zero diners throws
//		an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.splitBill(10, 10, 0);
});
}

@Test
public void testShouldNotAllowNegativeDiners() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act & Assert: assert that calling splitBill with negative diners throws
//		an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.splitBill(10, 10, -1);
});
}
@Test
public void testShouldSplitBillWithOneDiner() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act: call our method with proper parameter value for our test
double actualBill = theCalculator.splitBill(100.00, 10, 1);

// Assert: assert that our expected value is equal to the actual value
assertEquals(110.00, actualBill, 0.001);
}

@Test
public void testShouldSplitBillWithSeveralDiners() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act: call our method with proper parameter value for our test
double actualBill = theCalculator.splitBill(100.00, 10, 5);

// Assert: assert that our expected value is equal to the actual value
assertEquals(22.00, actualBill, 0.001);
}

@Test
public void testShouldSplitBillWithZeroTip() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act: call our method with proper parameter value for our test
double actualBill = theCalculator.splitBill(100.00, 0, 2);

// Assert: assert that our expected value is equal to the actual value
assertEquals(50.00, actualBill, 0.001);
}

@Test
public void testShouldSplitBillWith100PercentTip() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act: call our method with proper parameter value for our test
double actualBill = theCalculator.splitBill(100.00, 100, 2);

// Assert: assert that our expected value is equal to the actual value
assertEquals(100.00, actualBill, 0.001);
}

}```

For 1B here is the tests for the calculateMonthlyTakeHomePay method:

Java
```public class TestCalculateMonthlyTakehomePay {

@Test
public void testShouldNotAllowNegativeSalary() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act & Assert: assert that calling calculateMonthlyTakehomePay with negative salary throws
//		an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.calculateMonthlyTakehomePay(-1, 0.3);
});
}

@Test
public void testShouldNotAllowZeroSalary() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act & Assert: assert that calling calculateMonthlyTakehomePay with zero salary throws
//		an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.calculateMonthlyTakehomePay(0, 0.3);
});
}

@Test
public void testShouldNotAllowNegativeTaxRate() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act & Assert: assert that calling calculateMonthlyTakehomePay with negative tax rate throws
//		an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.calculateMonthlyTakehomePay(25000, -0.3);
});
}

@Test
public void testShouldNotAllowTaxRateOverOne() {
// Arrange: create a FinancialCalculator object
FinancialCalculator theCalculator = new FinancialCalculator();

// Act & Assert: assert that calling calculateMonthlyTakehomePay with tax rate over one throws
//		an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> {
theCalculator.calculateMonthlyTakehomePay(25000, 1.3);
});
}```

What I have tried:

I have tried to type in the if statements as asked in the precondition, but when I have tried the some of the j units failed and some of them passed.
Posted
Updated 23-Feb-21 11:57am
v3

## Solution 1

Those "validations" should be done during the "input" phase; not when you're calculating; that's why it's a mess. You're letting "garbage in".