a little help

i have a program which calculate Milespergallon. this is my code.

import java.util.Scanner;

//import Car;

class Car{

//instance variable

double startMiles;

double endMiles;

double gallons;

boolean =

//constructor

Car (double first,double last,double gals){

startMiles = first;

endMiles = last;

gallons = gals;

}

//methods

double calculateMPG(){

return (endMiles - startMiles)/gallons;

}

boolean gasHog(){

if(calculateMPG() < 15.0 ){

gasHog();

}

return gasHog();

}

boolean economyCar(){

if(calculateMPG() >30.0 ){

economyCar();

}

return economyCar();

}

}

class MilesPerGallon{

publicstaticvoid main(String args[]){

double startMiles, endMiles, gallons;

boolean gasHog;

boolean economyCar;

Scanner s =new Scanner (System.in);

System.out.println("Enter the first reading ");

startMiles = s.nextDouble();

System.out.println("Enter the second reading ");

endMiles = s.nextDouble();

System.out.println("Enter the gallons ");

gallons = s.nextDouble();

Car car =new Car( startMiles, endMiles, gallons);

System.out.println("Miles per gallon is " + car.calculateMPG());

}

}

i am very new to methods. i am solving some exercise problems. and whenever i am stuck i post my question in the forum to clearify myself and to get some more details.

my program works fine it calculates milesper gallon perfectly. what i really want to know is that i have 2 boolean methods gasHog() and economycar(). if the miles per gallon is less than 15 then gasHog(). if milespergallon is greater than 30 then economy car. i have 2 boolean methods i wanted to know if its rite or not.

boolean gasHog(){

if(calculateMPG() < 15.0 ){

gasHog();

}

return gasHog();

}

boolean economyCar(){

if(calculateMPG() >30.0 ){

economyCar();

}

return economyCar();

}

if its rite then how can i tell my main program to go to my boolean method after it calculates milespergallon to see if its gasHog or economy car. i know i can do it easily with an if/else statement directly in the main but like i said i wanted my main function to go to my boolean method. any help will be really good and some advices on methods will be great and a little bit detail on the constructor also will be great.

Car car =new Car( startMiles, endMiles, gallons);// y do i have to declare my variables again since i have declared them already in my car class..

[4731 byte] By [fastmikea] at [2007-11-26 13:36:22]
# 1

boolean gasHog() {

if(calculateMPG() < 15.0 ) {

return true;// Is a gas hog

}

return false;// Not much of a gas hog

}

boolean economyCar() {

if(calculateMPG() >30.0 ) {

return true;// Is an economy car

}

return false;// Not so economical

}

It would make sense to return a boolean from the methods to indicate if, for example, the car is a gas hog or it is an economy car.

aniseeda at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 2

> boolean gasHog() {

> if(calculateMPG() < 15.0 ) {

> gasHog();

>

> }

> return gasHog();

> }

Basically your method 'gasHog()' calls itself when 'calculateMPG()'

returns a value less than 15. Otherwise it calls itself again before

returning anything. Resuming: your method 'gasHog()' always calls

itself no matter what before returning. This is called recursion that has

gone berzerk. Note that quite a few other methods in your code exhibit

the same behaviour. You have to fix that.

kind regards,

Jos

JosAHa at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 3

> // y do i have to declare my variables again since i

> have declared them already in my car class..

A particular note on this comment: Even though you are using the same variable names, they are not the same variables. It's a different class and a different method too.

And finally, to address the real problem, you should call the boolean returning methods on the instance you created (i.e. the car using the new Car).

To check how your car works:

if(car.gasHog()) {

// Eats more fuel

} else {

// Car not a gas hog; check for economy

if(car.isEconomy()) {

// Car is economy

} else {

// Not an economy car; and not a gas hog too (like my car!)

}

}

aniseeda at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 4

well thanks for the reply aniseed and josah. recursion you are rite. but you said i had to fix my other methods? y ? when i think everything is fine it calculates mpg perfectly but any suggestions will be great so i can implement them in my futureprogramming. secondly my question was how can i call my boolean method in main? i tried.

if(gasHog()) {

System.out.println(" bad gas ");

} else {

System.out.println("Economy car ");

}

but it says the compiler cannot find the symbol method gasHog(). ?

fastmikea at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 5

To build on Jos's reply, look at this method:

public void tester()

{

System.out.println("tester() is running");

tester();

}

Observe what happens. This is basically what youre doing in your gasHog() method.

CaptainMorgan08a at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks for the replies. aniseed thanks for the explanation and captain i will see rite now what josah was saying appreciate that. i will post back .
fastmikea at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 7

> but it says the compiler cannot find the symbol

> method gasHog(). ?

Its because gasHog() is a method in the Car class. You need to call it on a Car object (unless it was a static method).

if(car.gasHog())

{

//do stuff

}

Also, if the car is not a gas hog, it is not necessarily an economy car (see aniseed's reply).

CaptainMorgan08a at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 8

maybe,

boolean isGasHog() {

return calculateMPG() < 15.0;

}

or have one method that takes as argument mpg, returns an enum of various gas guzzling terms?

mchan0a at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 9

> or have one method that takes as argument mpg, returns an enum of various gas guzzling terms?

Refactor to strategy pattern, allowing for different implementations decided at runtime for determining what constitutes a "gas guzzler"...

yawmark (<-- [url=http://forum.java.sun.com/thread.jspa?threadID=788127&start=1]SBWAPD[/url])

yawmarka at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 10
maybe, http://ssel.vub.ac.be/br/
mchan0a at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 11

ok. i tried your example captain . here it is

import java.util.Scanner;

//import Car;

class Car {

//instance variable

double startMiles;

double endMiles;

double gallons;

//constructor

Car (double first, double last, double gals) {

startMiles = first;

endMiles = last;

gallons = gals;

}

//methods

double calculateMPG() {

return (endMiles - startMiles)/gallons;

}

public void gasHog() {

if(calculateMPG() < 15.0 ) {

//return true;

System.out.println(" bad gas ");

gasHog();

}

//return false;

}

publicvoid economyCar() {

if(calculateMPG() >30.0 ) {

System.out.println("Economy car ");

economyCar();

//return true;

}

//return false;

}

}

class MilesPerGallon {

public static void main(String args[]) {

double startMiles, endMiles, gallons;

//boolean gasHog;

//boolean economyCar;

Scanner s = new Scanner (System.in);

System.out.println("Enter the first reading ");

startMiles = s.nextDouble();

System.out.println("Enter the second reading ");

endMiles = s.nextDouble();

System.out.println("Enter the gallons ");

gallons = s.nextDouble();

Car car = new Car( startMiles, endMiles, gallons);

System.out.println("Miles per gallon is " + car.calculateMPG());

if(Car.gasHog()) {

System.out.println(" bad gas ");

} else {

if(Car.economyCar()) {

System.out.println("Economy car ");

}

}

}

}

Error

:\Java programs\MilesPerGallon.java:58: non-static method economyCar() cannot be referenced from a static context

if(Car.economyCar()) {

this is really very new for me can't understand.

Second i tried this :

import java.util.Scanner;

//import Car;

class Car {

//instance variable

double startMiles;

double endMiles;

double gallons;

//constructor

Car (double first, double last, double gals) {

startMiles = first;

endMiles = last;

gallons = gals;

}

//methods

double calculateMPG() {

return (endMiles - startMiles)/gallons;

}

public boolean gasHog() {

if(calculateMPG() < 15.0 ) {

System.out.println(" bad gas ");

//gasHog();

return true;

}

return false;

}

public boolean economyCar() {

if(calculateMPG() >30.0 ) {

System.out.println("Economy car ");

//economyCar();

return true;

}

return false;

}

}

class MilesPerGallon {

public static void main(String args[]) {

double startMiles, endMiles, gallons;

boolean gasHog;

boolean economyCar;

Scanner s = new Scanner (System.in);

System.out.println("Enter the first reading ");

startMiles = s.nextDouble();

System.out.println("Enter the second reading ");

endMiles = s.nextDouble();

System.out.println("Enter the gallons ");

gallons = s.nextDouble();

Car car = new Car( startMiles, endMiles, gallons);

System.out.println("Miles per gallon is " + car.calculateMPG());

if(Car.gasHog()) {

gasHog();

} else {

if(Car.economyCar()) {

economyCar();

}

}

}

}

still giving me same thing. i searched on google also but

fastmikea at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 12
use the instance of car you created in the if statements.> Car car = new Car( startMiles, endMiles, gallons);> if(Car.gasHog()) {> gasHog();> } else {> if(Car.economyCar()) {> economyCar();
mchan0a at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 13

public void gasHog() {

if(calculateMPG() < 15.0 ) {

//return true;

System.out.println(" bad gas ");

gasHog();

}

//return false;

}

Youre still causing recursion, dont do that. You were already given an example of how this method should look (reply 2 or something like that).

if(Car.gasHog()) {

System.out.println(" bad gas ");

} else {

if(Car.economyCar()) {

System.out.println("Economy car ");

}

}

Your close. Your Car object is car, not Car.

CaptainMorgan08a at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 14
Got it. ahhhh how stupid of me instead of callingcar.gasHog()i was sayingCar.gasHog()The capital messed me up. Thanks for the replies and thanks for your time.
fastmikea at 2007-7-7 22:21:33 > top of Java-index,Java Essentials,New To Java...
# 15
> maybe,> http://ssel.vub.ac.be/br/Exactly! We can deploy an EE version using AOP on an n-tier clustered provider with AJAX UI, web service functionality, and a paradigm-aware XBRL multi-distributed protocol layer.~
yawmarka at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 16

working now thanks aniseed and thanks josah and captain. i hope you guys know that i am very energetic in learning java and giving all my time before i go back to school and graduate this semester.

import java.util.Scanner;

//import Car;

class Car {

//instance variable

double startMiles;

double endMiles;

double gallons;

//constructor

Car (double first, double last, double gals) {

startMiles = first;

endMiles = last;

gallons = gals;

}

//methods

double calculateMPG() {

return (endMiles - startMiles)/gallons;

}

public boolean gasHog() {

if(calculateMPG() < 15.0 ) {

System.out.println(" bad gas ");

//gasHog();

return true;

}

return false;

}

public boolean economyCar() {

if(calculateMPG() >30.0 ) {

System.out.println("Economy car ");

//economyCar();

return true;

}

return false;

}

}

class MilesPerGallon {

public static void main(String args[]) {

double startMiles, endMiles, gallons;

boolean gasHog;

boolean economyCar;

Scanner s = new Scanner (System.in);

System.out.println("Enter the first reading ");

startMiles = s.nextDouble();

System.out.println("Enter the second reading ");

endMiles = s.nextDouble();

System.out.println("Enter the gallons ");

gallons = s.nextDouble();

Car car = new Car( startMiles, endMiles, gallons);

System.out.println("Miles per gallon is " + car.calculateMPG());

if(car.gasHog()) {

car.gasHog();

} else {

if(car.economyCar()) {

car.economyCar();

}

}

}

}

fastmikea at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 17
> if(car.gasHog()) {> car.gasHog();Someone doesn't listen very well.
warnerjaa at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 18

What are you intending by the following code?

if(car.gasHog()) {

car.gasHog();

} else {

if(car.economyCar()) {

car.economyCar();

}

}

~

yawmarka at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 19
separate the need to check 'guzziliness' from the need to println
mchan0a at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 20

> if(car.gasHog()) {

> car.gasHog();

> Someone doesn't listen very well.

before the captain replied me i actually saw The capital was messing me up i can't listen since i was working on the program but thanks for the compliment i wll try to listen and work at the same time thanks warneja

fastmikea at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 21

> > maybe,

> > http://ssel.vub.ac.be/br/

>

> Exactly! We can deploy an EE version using AOP on an n-tier clustered

> provider with AJAX UI, web service functionality, and a paradigm-aware

> XBRL multi-distributed protocol layer.

For an application that hardly outweighs "hello world"? You must be a loony ;-)

kind regards

Jos (prefers a simple no frillies Spring framework for those purposes ;-)

JosAHa at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 22
> Jos (prefers a simple no frillies Spring framework> for those purposes ;-)Hibernate could help keep everything nice and lightweight.;o)~
yawmarka at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 23

> > Jos (prefers a simple no frillies Spring framework

> > for those purposes ;-)

>

> Hibernate could help keep everything nice and lightweight.

>

> ;o)

I prefer my own lightweight home brew ORM for that thank you ;-)

kind regards,

Jos (< not invented here)

JosAHa at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 24
I prefer a heavyweight home brew along with a hearty meal. yawmark (<-- gaining weight)~
yawmarka at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 25

> I prefer a heavyweight home brew along with a hearty meal.

Me too, but I'm on a zero tolerance diet now, i.e. I'm not allowed to

scavenge the Christmas leftovers that are bambi eyed begging to be

devoured by me when I open up the fridge door.

> yawmark (<-- gaining weight)

Uhuh ...

kind regards,

Jos (< involuntarily on a hunger strike ;-)

ps. I did negotiate a Grolsch pardon though.

JosAHa at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 26
> ps. I did negotiate a Grolsch pardon though.And what's your miles per gallon? (Hint: you can look up those antique units of measure in Wikipedia or something.)
DrClapa at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...
# 27

> > ps. I did negotiate a Grolsch pardon though.

>

> And what's your miles per gallon?

Dunno; We're metric overhere, we do m/ml == km/l which makes much

more sense because roughly 1/10,000 the distance from the Equator

to the North Pole happens to be a kilometre and raising the temperature

of one litre of water takes one Kilo ( == 1,000) Calories. See? ;-)

> (Hint: you can look up those antique units of measure in Wikipedia or

> something.)

Nah, I prefer Newtons per square root of hecto Pascals times seconds

squared. Per Tesla or some other modern thingie instead ;-)

kind regards,

Jos (<-- per Bequerel Lumen second)

ps. My old Ford times 13 kilometre equals 1 litre.

JosAHa at 2007-7-7 22:21:35 > top of Java-index,Java Essentials,New To Java...