|
-
February 21st, 2009, 12:31 AM
#1
[RESOLVED] how do i pass a value into a function and return an operation done on it
im having a bit of a problem. i have 2 classes. one called machine and the other is just the main class.
im trying to pass a value from the main class to the machine class where an operation is done on it then it is returned and displayed to the user. whats happening is that it returns an incorrect value.
i found a work around but my professor said even though it does what is required it doesnt follow the procedure outlined. below is my code. im not asking you guys to do this for me. im just asking for a hint maybe. guide me. im poofed ive been making modifications and nothing works.
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package vendingmachine10;
public class VendingMachine {
private String brand = "Fanta";
private double price = 1.50;
private double balance = 100;
private int number_of_sodas = 200;
public VendingMachine(String sbrand,double sprice,double bal,int numsodas)
{
brand = sbrand;
price = sprice;
balance = bal;
number_of_sodas = numsodas;
}
public String getBrand()
{
return brand;
}
public double getPrice()
{
return price;
}
public double getBalance()
{
return balance;
}
public int getSodaNumbers()
{
return number_of_sodas;
}
public void setBrand(String b)
{
brand = b;
}
public void setPrice(double p)
{
price = p;
}
public void setBalance(double bal)
{
balance = bal;
}
public void setNumOfSodas(int numofsodas)
{
number_of_sodas = numofsodas;
}
public double buy(double c)
{
double change;
change = c - price;
balance = balance + price;
number_of_sodas = number_of_sodas -1;
return change;
}
}
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package vendingmachine10;
import javax.swing.JOptionPane;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String brand;
double money;
brand = JOptionPane.showInputDialog("Enter brand choice.");
VendingMachine V = new VendingMachine(brand,1.50,50,100);
double b=V.getBalance();
JOptionPane.showMessageDialog(null,"Current balance is="+ b);
String sbrand=V.getBrand();
JOptionPane.showMessageDialog(null,"Current brand is="+ sbrand);
double p=V.getPrice();
JOptionPane.showMessageDialog(null,"Current price of "+sbrand+" is="+ p);
int sodanum=V.getSodaNumbers();
JOptionPane.showMessageDialog(null,"Current amount of "+sbrand+" is="+ sodanum);
V.buy(50);
double newb=V.getBalance();
int newsodanum=V.getSodaNumbers();
JOptionPane.showMessageDialog(null,"New balance is="+ newb);
JOptionPane.showMessageDialog(null,"New number of "+sbrand+" soda left is="+ newsodanum);
JOptionPane.showMessageDialog(null,"Your Change is=$"+ V.buy(p));
}
}
-
February 21st, 2009, 02:20 AM
#2
Re: how do i pass a value into a function and return an operation done on it
I don't know what's the exact problem is but when you do Vbuy(50) you don't note the change.
Instead later when you want to know what the change was you do this calls,
JOptionPane.showMessageDialog(null,"Your Change is=$"+ V.buy(p));
This means the V.buy method is called again performing a second buy. Is that what you intend?
Becaus p in this call,
V.buy(p)
equals the actual price, the method will always return a change of 0.0.
-
February 21st, 2009, 01:50 PM
#3
Re: how do i pass a value into a function and return an operation done on it
 Originally Posted by _uj
I don't know what's the exact problem is but when you do Vbuy(50) you don't note the change.
Instead later when you want to know what the change was you do this calls,
JOptionPane.showMessageDialog(null,"Your Change is=$"+ V.buy(p));
This means the V.buy method is called again performing a second buy. Is that what you intend?
Becaus p in this call,
V.buy(p)
equals the actual price, the method will always return a change of 0.0.
Actually no. when i quoted v.buy(50) i wanted to pass 50 into the function buy which would calculate the change. so change=50-price("which is set it 1.5"). all i want to do is to use joption pane to give the user the change. so id expect the answer to be 48.5.
so to summarise i want to pass 50 and return the change and display it to the user.
Last edited by caesar2004; February 21st, 2009 at 01:58 PM.
Reason: added stuff
-
February 21st, 2009, 04:04 PM
#4
Re: how do i pass a value into a function and return an operation done on it
haha i found a simple fix.
all i did was move around a statement
JOptionPane.showMessageDialog(null,"change is:"+V.buy(50));
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|