assignment 7 Your task in this assignment is to write a simple class definition
ID: 674879 • Letter: A
Question
assignment 7
Your task in this assignment is to write a simple class definition for a vending machine. The class should have:
Attributes
Soda count
Money credit (how much money has been inserted)
Methods
Add money
Vend
Restock
Constructor
For simplicity sake, you may assume that the machine can hold up to 50 of one type of soda; you may also assume that all sodas cost $0.65. When adding money, simply take in a floating-point number representing how much money to add (i.e. you do not have to manage different coins).
Be sure to make sure that enough money was added to the machine before a soda is vended. Also, when a soda is vended, return the appropriate change (i.e. if more than $0.65 was added to the machine, then return the extra money). Again, don't worry about managing different kinds of coins just display the amount returned.
When the machine is "restocked," just set the soda count to the maximum that the machine will hold.
Once you have written the class, create a main/driver program that interacts with an instance of the class. Your program should present a menu of options to the user and process the user's choice; use creativity and object-oriented design in your solution.
Assignment 8:
Submit 1 java file corresponding to the main program as requested below.
Your task for this assignment is to define multiple classes via inheritance. Your classes should implement various "snacks" including "M&Ms", "Popcorn", etc.
To begin, create an abstract "Snack" class. Then, create two classes "Salty" and "Sugary" that inherit from the "Snack" class. Then create classes "M&Ms", "Popcorn", "Snickers", "Gum", "Crackers", and "Chips" that inherit from "Salty" or "Sugary" as appropriate.
Once you have your inheritance hierarchy defined, implement the toString method for each class.
Finally, let's presume that each snack has two additional properties: "calories" and "cost". Calories is an integer that represents how many calories the snack contains, and cost is a floating-point number that represents how much the snack costs to buy. Implement these properties within your hierarchy as appropriate
Assignment9_main.java:
Your task for this assignment is to extend the "Vending Machine" you wrote for assignment seven to add the ability to hold the snacks that you implemented in assignment 8 (i.e. combine the seventh and eighth assignments).
The resultant vending machine class should hold 100 snacks.
Your program should present the user with a menu of choices ("Add money", "Vend", and "Restock" as before in assignment seven with appropriate modifications) and process the user's choice until he/she selects "Quit'.
Utilize good modularity and object-oriented design/programming in your solution.
Explanation / Answer
import javax.swing.*;
import java.text.DecimalFormat;
public class Assignment7_main {
public static void main(String[] args) {
//Create Scanner
Scanner input = new Scanner(System.in);
DecimalFormat money = new DecimalFormat("$0.00");
System.out.print("Soda's cost: $" + vendingMachine.getSodaCost() + " Enter payment: ");
float sodaPayment = input.nextFloat();
if (sodaPayment >= .65){
vendingMachine.setSodaPayment(sodaPayment);
int sodaCount = vendingMachine.getSodaCount();
System.out.print(sodaCount + " " + money.format(vendingMachine.getChange()));
}
else
System.out.println("Invalid amount");
}
}
class vendingMachine{
//Create field to store soda count
private static int sodaCount = 50;
//Create field to store soda cost
private static float sodaCost;
//Create field to store soda payment
public static float sodaPayment;
public vendingMachine(){
sodaCount--;
}
public vendingMachine(float newSodaPayment){
sodaPayment = newSodaPayment;
sodaCount--;
}
//Return soda payment
public static float getSodaPayment(){
return sodaPayment;
}
//Set soda payment
public static void setSodaPayment(float newSodaPayment){
sodaPayment = (newSodaPayment >= .65) ? newSodaPayment : 0;
}
//Return soda count
public static int getSodaCount(){
for (int i = 0; i <= 50; i++){
sodaCount = i;
}
return sodaCount;
}
//Return soda cost
public static float getSodaCost(){
return sodaCost = .65f;
}
//Return method for calculating change
public static float getChange(){
float change = 0;
if (sodaPayment != 0){
change = sodaPayment - sodaCost;
}
return change;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.