Skip to main content

Basic problem, please help.

1 reply [Last post]
Joined: 2006-04-21
Points: 0

I'm in a java class and my teacher's is horrible. Heavy accent, poorly explains things and goes very fast. Everything I've learned I've learned from the book and online. I've come into a jam here and our project is due Monday.

I have all the program written and it works just peachy, but I don't know how to create worker classes. I know I know, that's bad. I've looked every where. I have it all in the implementation class as of now. But, the only thing that can be in the impl class are input and output methods, no calculation methods. Please help me...

Here is the file:

Any suggestions or explainations would help a lot. I'm not asking for anyone to do my work, just some insight. I only need one worker class for all the calculations. I'll continue to work on my own, this is just a back up plan, thanks all.

Here's the source code if you don't want to download it...

//Steve Hofstetter. CIS 234 Assignment #1

import javax.swing.JOptionPane;

public class VendingMachineImpl

public static void main(String[] args)
String name,
int totalChange,

//get their name
name = JOptionPane.showInputDialog("What is your name?");

//get cost of time
inputString = JOptionPane.showInputDialog("How much does " + "your item cost? Example: 45 for 45 cents.");

//convert cost to an integer
cost = Integer.parseInt(inputString);

//control whether or not it is greater than 25 and a multiple of 5
(cost < 25 || ((cost % 5) != 0))
JOptionPane.showMessageDialog(null, "Your item needs to cost more than 25 cents and be a multiple of 5!!! Make a new selection.");

//if everything is okay, program continues
else {

//calclate change
totalChange = 100 - cost;

//how many quarters
quarters = totalChange / 25;

//how many dimes
dimes = (totalChange % 25) /10;

//how many nickels
nickels = (totalChange % 25 % 10) / 5;

//print name, cost and change
JOptionPane.showMessageDialog(null, "Hello " + name + ". You bought an item for $0." + cost + " with a dollar. Your change is $0." + totalChange);

//print what change consists of
JOptionPane.showMessageDialog(null, "Your $0." + totalChange + " change consists of " + quarters + " quarters and " + dimes + " dimes and " + nickels + " nickels and 0 pennies.");


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-04-14
Points: 0

You could make a class that takes an int in the constructor (the amount of change) and checks that it's a multiple of 5. Then, you could give it a method that returns an array of ints with length 3, where ints[0] is the number of nickels, ints[1] is the number of dimes, etc. That would be a worker class. The impl class would then format and print the result array of ints.