In: Computer Science
Please create a Java Eclipse code and show output and explanation.
MUST USE EXCEPTION!
Step 1. Ask users how much money was spent on an orange.
Step 2. Then ask the user how many oranges they purchased
Step 3. Lastly calculate the price for each orange purchased.
package cost;
import java.util.*;//for use of scanner funtion
public class Orange {
public static void main(String[] args) {
try//may throw
exception
{
Scanner sc=new
Scanner(System.in);//creating object for scanner
System.out.println("Enter total cost of all oranges:");
double
cost=sc.nextDouble();//taking cost
System.out.println("Enter no.of oranges purchase:");
double
item=sc.nextDouble();//taking the no.of items
double
each=cost/item;//calculating the each orange price
if(item<=0 ||
cost<=0.0)//throw arthimetic exception
throw new ArithmeticException("please enter
valid input");
System.out.println("each orange price is:"+each);//displaying each
orange price
}
catch(Exception e)//handling exception
{
System.out.println("exeception is: "+e);
}
}
}