I am no good at this

Hello everyone out there on the internets. I am in my third week of a Java class. I am trying to show at least a small amount of progress to my instructor, so if anyone could tell me where my stupid mistakes are in this code it would be greatly appreaciated. I am getting an error in my constructor on line 36 I will put a comment there.

Thanx

import javax.swing.JOptionPane;

import java.text.*;

publicclass lunchredo

{

// Variable Declaration

privatestaticdouble appleCost;

privatestaticdouble appleQty;

privatestaticdouble breadCost;

privatestaticdouble breadQty;

privatestaticdouble cheeseCost;

privatestaticdouble cheeseQty;

privatestaticdouble wineCost;

privatestaticdouble wineQty;

privatestaticdouble budget;

privatestaticdouble totalCost;

publicstaticvoid main(String[] args)

{

}

// right here is where the error is

// constructor setting the values

Lunch()

{

appleCost = 1.09;

appleQty = 4;

breadCost = 2.55;

breadQty = 3;

cheeseCost = 5.99;

cheeseQty = 2;

wineCost = 52.06;

wineQty = 6;

String yourbudgetString;

double budget;

yourbudgetString = JOptionPane.showInputDialog(null,"Enter Your Budget.","Budget", JOptionPane.INFORMATION_MESSAGE);

budget = Double.parseDouble(yourbudgetString);

}

publicstaticdouble setTotalCost

{

totalCost = (appleCost * appleQty) + (breadCost * breadQty) + (cheeseCost * cheeseQty) + (wineCost * wineQty);

}

publicstaticdouble displayTotal(){

DecimalFormat df =new DecimalFormat("#0.00");

System.out.println(df.format(LunchCost(0.00)));

return totalCost;

}

publicstaticdouble totalExpense(double expense){

expense = budget - totalCost;

return expense;

}

publicstaticdouble displayExpense(){

DecimalFormat df =new DecimalFormat("#0.00");

System.out.println(df.format(totalExpense(0.00)));

return expense;

}

publicstaticdouble getBudget()

{

return budget;

}

publicstaticdouble setTotalCost

{

totalCost = (appleCost * appleQty) + (breadCost * breadQty) + (cheeseCost * cheeseQty) + (wineCost * wineQty);

}

publicstaticdouble getTotalCost

{

return totalCost;

}

publicstaticdouble spendBudget(double remainder)

{

double remainder = budget - totalCost;

df.format(remainder);

JOptionPane.showMessageDialog(null,"Your budget is $" + df.format(budget) +" and the total cost of the lunch is $" + df.format(totalCost) +"." +" You have $" +

df.format(remainder) +" remaining");

//If statement that decides whether or not the budget is exceeded

if (remainder < 0)

JOptionPane.showMessageDialog(null,"You have exceeded your budget. Please go home and get more money!");

else

JOptionPane.showMessageDialog(null,"You have not exceeded your budget.");

}

//Output Section

privatestaticdouble lunchOutput()

{

System.out.println("Price of Apples per pound: $" + df.format(appleCost));

System.out.println("Price of Bread per loaf: $" + df.format(breadCost));

System.out.println("Price of Cheese per pound: $" + df.format(cheeseCost));

System.out.println("Price of Wine per bottle: $" + df.format(wineCost));

System.out.println("Total Price of all items: $" + df.format(totalCost));

System.out.println("Your Budget: $" + df.format(budget));

System.out.println("You have: $" + df.format(remainder));

}

System.exit(0);

}

}

[7593 byte] By [GeoCrazya] at [2007-11-26 18:21:49]
# 1

The name of the constructor must match the name of the class:

public class Credo {

public Credo() {

//etc

}

}

DrLaszloJamfa at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...
# 2
> Lunch()That isn't a constructor. Your class is named "lunchredo", so if you want a constructor, that would have to be the name of the constructor too.
warnerjaa at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...
# 3
Also please have your classes start with an Upper case letter for readability.
Aknibbsa at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...
# 4
Your class is called 'lunchredo' so a possible constructor 'Lunch' doesn'tmake much sense.kind regards,Jos
JosAHa at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...
# 5
and who's the slowest old sod again? ;-)kind regards,Jos
JosAHa at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...
# 6
Thank you it has been a long 10 hours of paper writing already today. I think my brain is going out on me.I really appreciate it.Thank you
GeoCrazya at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...
# 7
Thank you it has been a long 10 hours of paper writing already today. I think my brain is going out on me.I really appreciate it.Thank you
GeoCrazya at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...
# 8

Also don't just make everything static because your getting problems accessing methods and variables from main. create and 'instance' of your class and then access its methods/variables via the instance. all that static is bad in this case.

// do this in your main method

lunchredo lr = new lunchredo();

lr.setTotalCost(someVal);

work like this and remove static from eveything except main. Your teacher will pull you up on this if you don't.

kikemellya at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...
# 9

>Remove static from everything except main.

Good point. The poster's code has the bad smell I dub:

static cling: you try to access a field/method from a

static context and get the syntax error "non-static

variable/method ... cannot be referenced from a static

context", so to get rid of that error you make the

field/method static but the error just spreads and clings to

your code until everything is declared static!

>Your teacher will pull you up on this if you don't.

Let's hope. But you may be expecting too much from some

teachers. We've had a few in these forums who were real

peaches.

DrLaszloJamfa at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...
# 10
> teachers. We've had a few in these forums who were> real> peaches... The teaches were peaches ?;-P
Aknibbsa at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...
# 11

> Let's hope. But you may be expecting too much from

> some

> teachers. We've had a few in these forums who were

> real

> peaches.

Not to mention authors. Can you imagine a student with a so-called teacher like some of the ones we've encountered using a textbook written by some of the authors we've encountered? That's a rather horrific thought, though it does explain the current state of software.

kablaira at 2007-7-9 5:55:42 > top of Java-index,Java Essentials,New To Java...