Question
and this is my code : import java.util.Scanner; import java.util.InputMismatchException; abstract class Cake { protected String name; protected double rate; public Cake(String n, double r)
and this is my code :
import java.util.Scanner;
import java.util.InputMismatchException;
abstract class Cake {
protected String name;
protected double rate;
public Cake(String n, double r) {
name = n;
rate = r;
if (rate
throw new IllegalArgumentException();
}
public abstract double getPrice();
@Override
public String toString() {
return name + "@\t" + rate + "K.D.";
}
}
class OrderCake extends Cake {
double weight;
public OrderCake(String n, double r, double weight) throws IllegalArgumentException {
super(n, r);
this.weight = weight;
}
@Override
public double getPrice() {
return super.rate * weight;
}
}
class ReadyMadeCake extends Cake {
int quantity;
public ReadyMadeCake(String n, double r, int quantity) throws IllegalArgumentException {
super(n, r);
this.quantity = quantity;
}
@Override
public double getPrice() {
return super.rate * quantity;
}
}
public class CakeTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Cake[] cakeArray = new Cake[4];
int count1 = 0;
do {
int count2 = 0;
try {
String name = in.nextLine();
double rate = in.nextDouble();
double weight = in.nextDouble();
if (count2 == 0 || count2 == 1) {
System.out.println("Enter Ordered Cake details : Name, Rate and Weight ");
cakeArray[count2] = new OrderCake(name, rate, weight);
count1++;
count2++;
}
} catch (NullPointerException e1) {
System.err.println(e1.getMessage());
} catch (IllegalArgumentException e2) {
} catch (InputMismatchException e3) {
in.nextLine();
}
try {
System.out.println("Enter Ready Made Cake details : Name, Rate and Quantity ");
String name3 = in.nextLine();
double rate3 = in.nextDouble();
int quantity = in.nextInt();
cakeArray[count2] = new ReadyMadeCake(name3, rate3, quantity);
count1++;
count2++;
} catch (NullPointerException e1) {
System.err.println(e1.getMessage());
} catch (IllegalArgumentException e2) {
} catch (InputMismatchException e3) {
in.nextLine();
}
} while (count1
for (int i = 0; i
if (cakeArray[i] instanceof ReadyMadeCake) {
System.out.println("Cake Price: " + cakeArray[i].getPrice() + " Quantity:"
+ ((ReadyMadeCake) cakeArray[i]).quantity);
}
}
}
}
I REALLY HAVE NO IDEA WHAT WENT WRONG PLS HELP ME TO FIX MY CODE (IN java programing language) thanks
Given the following abstract class: Class: Cake 1 public abstract class Cake i protected String name; protected double rate: public Cake (String n, double r) f namen rate 10 public abstract double gethrice) override public string tostring) 12 13 14 15 16 return name + "@\t" + rate + "K.D."; i. Edit constructor for the above in such a way that if the given rate is 0 or less, the constructor must throw the exception IllegalArgumentException to the caller i. Based on class Cake and the following table, define TWO subclasses named as orderCake and readymadeCake orderCake weight rate*weight ii. Write a test class that create an array of 4 cakes (two of orderCake and two of readymadeCake) readymadeCake quantity rate"quantity Additional attribute getPrice() filled with values from the user. Allow the execution to continue until you have four accepted objects. Your program should handle all exceptions that may occurs such that InputMismatchException, TilegalargumentException, and possibly NullPointerException. At the end display the price and the quantity sold for all cakes of readymadecakes onlyStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started