Question: How to fix AssertionError in this code?? public String toString() { String rs = ; // Add all MarketProducts to String for(int i = 0;
How to fix AssertionError in this code??
public String toString() { String rs = ""; // Add all MarketProducts to String for(int i = 0; i < a.length; i++) { try { if (a[i] != null) { rs = rs + a[i].getName() + "\t" + costToString(a[i].getCost()) + " "; } }catch(Error e) { } } rs = rs + " Subtotal\t" + costToString(getSubTotal()) + " Total Tax\t" + costToString(getTotalTax()) + " Total Cost\t" + costToString(getTotalCost()); return rs; }
private String costToString (int c) { String rs = ""; if (c <= 0) { rs = " - "; } else { int dollars = c / 100; int cents = c % 100; rs = dollars + "." + cents; if (c%100 == 0) { rs = rs + "0"; } } return rs; }
public static void main(String[] args) { // TODO Auto-generated method stub
Basket basket = new Basket(); basket.add(new Egg("brown", 24, 4)); basket.add(new Fruit("kiwi", 2.0, 100)); String expected = "brown\t0.08 " + "kiwi\t2.00 " + " " + "Subtotal\t2.08 " + "Total Tax\t- " + " " + "Total Cost\t2.08"; String actual = basket.toString().trim(); if (!actual.equals(expected)) throw new AssertionError("Incorrect format "); System.out.println("Basket toString test passed. "); } Output:
Exception in thread "main" java.lang.AssertionError: Incorrect format at marketplace.Driver.main(Driver.java:20)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
