Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java question. Can someone write this code? Write a class definition for a class

ID: 3915582 • Letter: J

Question

Java question. Can someone write this code?

Write a class definition for a class named CandyMachine that models a candy machine.

You should break this problem down into multiple methods so that you practice returning information from a method and passing information to a method.

Specifically, you should have a method for each of the following in addition to the main:

a method to print the introductory text

a method that prompts and returns the amount of money that the user has entered

a method that displays all the options in the candy machine

a method that prompts for the chosen candy and returns the cost of the candy chosen

a method that takes the amount of money the user has and the cost of the candy chosen, printing to the screen that the candy is dispensed (or not) and the amount of change given (or money that is required)

Remember that your main should read like an outline to the program (meaning it should primarily being calling other methods)

here are the Example Outputs:

or:

please help me

Explanation / Answer

import java.util.*;

class CandyMachine
{
public static void main (String[] args)
{


introText();

double money = getMoney();

candyMachineOptions();

double price = chooseCandy();

dispenseCandy(price,money);
}

public static void introText()
{
  System.out.println("Welcome to My's Computer Candy Machine! (All candy provided is virtual.");


}
public static double getMoney()
{
  Scanner scnr = new Scanner(System.in);
  System.out.println("How much money do you have? $");
  double money = scnr.nextDouble();
  System.out.println("$"+money+", that's all?");
  return money;

}
public static void candyMachineOptions()
{
  System.out.println("Well, let me tell you what we got here.");
  System.out.println("A $0.65 Twix");
  System.out.println("B $0.50 Chips");
  System.out.println("C $0.75 Nutter Butter");
  System.out.println("D $0.65 Peanut Butter Cup");
  System.out.println("E $0.55 Juicy Fruit Gum");


}
public static double chooseCandy()
{
  Scanner scnr1 = new Scanner(System.in);
  System.out.println("So, What do you want? ");
  String candy = scnr1.nextLine();
  
  
  double price = 0;
  
  switch(candy) // compute candy price
  {
   case "A" : price = 0.65;
    break;
   case "B" : price = 0.50;
    break;
   case "C" : price = 0.75;
    break;
   case "D" : price = 0.65;
    break;
   case "E" : price = 0.55;
    break;
   default: System.out.println("Invalid option");
    break;
  }
  System.out.println("price = "+price);
  return price;
}


public static void dispenseCandy(double price,double money)
{
  if(money > price) // have enough money to purchase candy
  {
  System.out.println("Thanks for purchasing candy through us.");
  System.out.println("Please take your candy and your $"+(money-price)+" change!");
  }
  else // do not have sufficient money to purchase
  {
  System.out.println("You are short $"+(price-money)+", you are unable to purchase your snack");
  }
}
}

Output:

Welcome to My's Computer Candy Machine!
(All candy provided is virtual.)
How much money do you have? > $1.00
$1.00, that's all?
Well, let me tell you what we got here.
A $0.65 Twix
B $0.50 Chips
C $0.75 Nutter Butter
D $0.65 Peanut Butter Cup
E $0.55 Juicy Fruit Gum
So, What do you want? > C
Thanks for purchasing candy through us.
Please take your candy and your $0.25 change!

Do ask if any doubt. Please upvote.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote