This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Need help creating classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help creating classes" Watch "Need help creating classes" New topic
Author

Need help creating classes

Jason Rod
Greenhorn

Joined: Apr 13, 2006
Posts: 14
Hey everyone, I'm working on a program for my Java class but am having a heck of a time trying to figure out how to make classes for it. The code is a basic vending machine, inserting 25 cents to a dollar in 5 cent increments with the change being returned in the format of quarters, dimes, and nickels. Here is the code without classes:

import javax.swing.JOptionPane;

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

String userInput;
int num1, num2, num3, num4, change, nickels, dimes, quarters;

userInput = JOptionPane.showInputDialog ("Enter the price of your item, from 25" +
"cents to a dollar in 5-cent increments" +
"\nNote: For one dollar, please use 100 cents");
num1 = Integer.parseInt(userInput);

while (num1 <= 24) {
JOptionPane.showMessageDialog (null, "Please enter a valid number");
userInput = JOptionPane.showInputDialog ("Enter the price of your item, from 25" +
"cents to a dollar in 5-cent increments" +
"\nNote: For one dollar, please use 100 cents");
num1 = Integer.parseInt(userInput); }

while (num1 >= 101) {
JOptionPane.showMessageDialog (null, "Please enter a valid number");
userInput = JOptionPane.showInputDialog ("Enter the price of your item, from 25" +
"cents to a dollar in 5-cent increments" +
"\nNote: For one dollar, please use 100 cents");
num1 = Integer.parseInt(userInput); }

if (num1 == 1) {
change = 0; }
else {
change = 100 - num1; }

num2 = (change/25);
if (num2 >= 3) {
quarters = 3; }
else if (num2 >= 2) {
quarters = 2; }
else if (num2 >= 1) {
quarters = 1; }
else {
quarters = 0; }

num3 = change - quarters*25;
if (num3 >= 20) {
dimes = 2; }
else if (num3 >= 10) {
dimes = 1; }
else {
dimes = 0; }

num4 = num3 - dimes*10;
if (num4 >= 5) {
nickels = 1; }
else {
nickels = 0; }

JOptionPane.showMessageDialog (null, "Your item cost " + num1 + " cents" +
" and you inserted a dollar" + "\nYour" +
" change is: " + change + " cents" + "\nGiven in: "
+ quarters + " quarter(s), " +
+ dimes + " dime(s), and " + nickels +
" nickel(s) ");

System.exit(0);
}
}

I was wondering if anyone can help me out with creating a class or classes for the mathematical operations, or if someone can point me in the direction of an online tutorial that goes in depth into creating classes it would be greatly appreciated. Thanks in advance for your time.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
well first you would have a panel class for all the components for the GUI.

then you would just have a main class with a JFrame which displays your

"cokeMachinePanel"

so you have you main class which can be cokeMachine.java which would

just set up the frame which displays the JOptionPane.

then you would have the cokeMachinePanel..

hope this helps..


You down with OOP? Yeah you know me!
Jason Rod
Greenhorn

Joined: Apr 13, 2006
Posts: 14
ok, i think i understand, thanks for the help, i'll go try that out now.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Is your vending machine supposed to have a GUI to display it? Does it have a list of items? I'm trying to flush out the requirements a bit more. At the very least I'd probably start with an Item class that has a name and price.

Do you understand how to create a class to begin with? How to create a method? How to create a new instance of a class? How to invoke methods on that instance? How to pass parameters to that method? How to get a return value? How to return a value from a method? Need to assess what you're asking for.
Jason Rod
Greenhorn

Joined: Apr 13, 2006
Posts: 14
sorry for the ambiguity, basically the program is a VERY simple vending machine, the user inputs the price, no items are involved, so the program would be better described as a pseudo-change machine I suppose. One enters a price, between 25 cents and a dollar in 5-cent increments and the user inserts a dollar, the program then figures out the change, change = 100 - cost of item, and then converts the change into quarters, dimes, and nickels. I hope that's a little more clear than my first explanation.

As of right now I'm trying to make a class for the mathematical calculations, with a subclass for each type of coin (i.e. quarter, dime, nickel). So my class is called Change, and the subclasses are called Change.quarter, Change.dime, and Change.nickel. So I will give that a try and report back.
Jason Rod
Greenhorn

Joined: Apr 13, 2006
Posts: 14
edit: double post :roll:

[ April 13, 2006: Message edited by: Jason Rod ]
[ April 13, 2006: Message edited by: Jason Rod ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
sorry for my post, i remember using JOPtionPane in a class assignment,

and i think you can just use it in the main application,(meaning you dont

have to have a panel class). umm as far as the if(JOptionPane"blah blah")

statements you can just put those in the main class too, and just have a

different class for your methods...

like

public static int CalculateChange(int num)
{
if (num is < 25 || num is > 100)
return 0;

and so on...
Jason Rod
Greenhorn

Joined: Apr 13, 2006
Posts: 14
Ok well this is what I have so far. The helper class is:




public class ChangeCalc
{

int quarters, dimes, nickels, change, num1, num2, num3, num4;

public int change (int num1)
{
if (num1 == 1) {
change = 0; }
else {
change = 100 - num1; }
return change;
}

public int num2 ()
{
num2 = (change/25);

if (num2 >= 3)
{
quarters = 3;
}

else if (num2 >= 2) {
quarters = 2; }
else if (num2 >= 1) {
quarters = 1; }
else {
quarters = 0; }
return quarters;
}

public int num3 ()
{
num3 = change - quarters * 25;

if (num3 >= 20) {
dimes = 2; }
else if (num3 >= 10) {
dimes = 1; }
else {
dimes = 0; }
return dimes;
}

public int num4()
{
num4 = num3 - dimes * 10;

if (num4 >= 5) {
nickels = 1; }
else {
nickels = 0; }
return nickels;
}
}





and the implementation class is:

import javax.swing.JOptionPane;

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

String userInput;
int num1, num2, num3, num4, change, nickels, dimes, quarters;

userInput = JOptionPane.showInputDialog ("Enter the price of your item, from 25" +
"cents to a dollar in 5-cent increments" +
"\nNote: For one dollar, please use 100 cents");
num1 = Integer.parseInt(userInput);

while (num1 <= 24) {
JOptionPane.showMessageDialog (null, "Please enter a valid number");
userInput = JOptionPane.showInputDialog ("Enter the price of your item, from 25" +
"cents to a dollar in 5-cent increments" +
"\nNote: For one dollar, please use 100 cents");
num1 = Integer.parseInt(userInput); }

while (num1 >= 101) {
JOptionPane.showMessageDialog (null, "Please enter a valid number");
userInput = JOptionPane.showInputDialog ("Enter the price of your item, from 25" +
"cents to a dollar in 5-cent increments" +
"\nNote: For one dollar, please use 100 cents");
num1 = Integer.parseInt(userInput); }

ChangeCalc c = new ChangeCalc();
ChangeCalc q = new ChangeCalc();
ChangeCalc d = new ChangeCalc();
ChangeCalc n = new ChangeCalc();

JOptionPane.showMessageDialog (null, "Your item cost " + num1 + " cents" +
" and you inserted a dollar" + "\nYour" +
" change is: " + c + " cents" + "\nGiven"
+ " in: " + q + " quarter(s), " + d +
" dime(s), and " + n + " nickel(s) ");

System.exit(0);
}
}

But, when I run the program the values returned for quarters, dimes, and nickels look like "ChangeCalc@ab04c32." I've already tested the equations and they work fine so I'm thinking the problem arises when I call the methods.
[ April 15, 2006: Message edited by: Jason Rod ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need help creating classes
 
Similar Threads
Vending Machine Project
input is showing different than output
almost there!
Vending Machine Project
Display nothing if == 0