-
July 23rd, 2012, 10:57 AM
#1
issue with a CashRegister
I am having a problem with getting this to work.
does anyone have any ideas?
here is the code:
Code:
import java.io.*;
import java.math.*;
import java.util.*;
public class CashRegisterTester
{
public static void main(String[] args)
{
CashRegister register = new CashRegister();
register.recordPurchase(0.75);
register.recordPurchase(1.50);
register.enterPayment(2,0,5,0,0);
System.out.print("Change= ");
System.out.println(register.giveChange());
}
public class CashRegister
{
public CashRegister()
{
purchase = 0;
payment = 0;
}
public void recordPurchase(double amount)
{
purchase = purchase + amount;
}
public void enterPayment(int dollars, int quarters, int dimes, int nickels, int pennies)
{
payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE
+ nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
}
public double giveChange()
{
double change = payment - purchase;
purchase = 0;
payment = 0;
return change;
}
public static final double QUARTER_VALUE = 0.25;
public static final double DIME_VALUE = 0.1;
public static final double NICKEL_VALUE = 0.05;
public static final double PENNY_VALUE = 0.01;
private double purchase;
private double payment;
}
}
and the output:
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable this cannot be referenced from a static context
at CashRegisterTester.main(CashRegisterTester.java:13)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
Any help would be greatly appreciated.
-
July 23rd, 2012, 02:13 PM
#2
Re: issue with a CashRegister
Nevermind about the previous code, I managed to figure that one out.
This is the one i need help with.
Code:
import java.io.*;
import java.math.*;
import java.util.*;
public class CashRegister
{
public static void main(String[] args)
{
CashRegister register = new CashRegister();
register.recordPurchase(20.37);
register.enterDollars(20);
register.enterQuarters(2);
System.out.print("Change= ");
System.out.println(register.giveChange());
}
{
purchase = 0;
payment = 0;
}
public void recordPurchase(double amount)
{
purchase = purchase + amount;
}
public void enterPayment(int dollars, int quarters, int dimes, int nickels, int pennies)
{
payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE
+ nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
}
public double giveChange()
{
double change = payment - purchase;
purchase = 0;
payment = 0;
return change;
}
public static final double QUARTER_VALUE = 0.25;
public static final double DIME_VALUE = 0.1;
public static final double NICKEL_VALUE = 0.05;
public static final double PENNY_VALUE = 0.01;
private double purchase;
private double payment;
}
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
|