Why can’t i get a result for gasStation better?
up vote
-1
down vote
favorite
I’m new with java, and my compiler doesn’t tell me what the errors are for this code:
public class Main {
public static void main(String args) {
class gasStation {
double price;
double distance;
}
gasStation shell = new gasStation();
shell.price = 2.72;
shell.distance = 1.25;
gasStation exxon = new gasStation();
exxon.price = 2.35;
exxon.distance = 1.75;
class betterDeal {
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
if (shellRating > exxonRating) {
gasStation better = shell;
} else if (shellRating < exxonRating) {
gasStation better = exxon;
}
return better;
}
System.out.println (better);
}
}
}
java
add a comment |
up vote
-1
down vote
favorite
I’m new with java, and my compiler doesn’t tell me what the errors are for this code:
public class Main {
public static void main(String args) {
class gasStation {
double price;
double distance;
}
gasStation shell = new gasStation();
shell.price = 2.72;
shell.distance = 1.25;
gasStation exxon = new gasStation();
exxon.price = 2.35;
exxon.distance = 1.75;
class betterDeal {
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
if (shellRating > exxonRating) {
gasStation better = shell;
} else if (shellRating < exxonRating) {
gasStation better = exxon;
}
return better;
}
System.out.println (better);
}
}
}
java
1
Please format your code. Also what is your question?Why can’t i get a result for gasStation better?
doesn't make any sense to me
– GBlodgett
Nov 11 at 4:05
You're trying to put classes inside the main. This is incorrect. If I were you I would refer to the javadoc tutorials: docs.oracle.com/javase/tutorial/java/javaOO/classes.html
– GBlodgett
Nov 11 at 4:08
Your returning better but not calling the compare method and storing the returns result
– Nick Parsons
Nov 11 at 4:08
Why cant i print the object ‘better’ at the end of my code. Im sorry im a complete newb
– user10635007
Nov 11 at 4:10
@user10635007 As I said you are trying to put everything inside of your main. You need to create the classes separate, and then construct the objects in the main and call the methods on the objects
– GBlodgett
Nov 11 at 4:12
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I’m new with java, and my compiler doesn’t tell me what the errors are for this code:
public class Main {
public static void main(String args) {
class gasStation {
double price;
double distance;
}
gasStation shell = new gasStation();
shell.price = 2.72;
shell.distance = 1.25;
gasStation exxon = new gasStation();
exxon.price = 2.35;
exxon.distance = 1.75;
class betterDeal {
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
if (shellRating > exxonRating) {
gasStation better = shell;
} else if (shellRating < exxonRating) {
gasStation better = exxon;
}
return better;
}
System.out.println (better);
}
}
}
java
I’m new with java, and my compiler doesn’t tell me what the errors are for this code:
public class Main {
public static void main(String args) {
class gasStation {
double price;
double distance;
}
gasStation shell = new gasStation();
shell.price = 2.72;
shell.distance = 1.25;
gasStation exxon = new gasStation();
exxon.price = 2.35;
exxon.distance = 1.75;
class betterDeal {
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
if (shellRating > exxonRating) {
gasStation better = shell;
} else if (shellRating < exxonRating) {
gasStation better = exxon;
}
return better;
}
System.out.println (better);
}
}
}
java
java
edited Nov 11 at 4:25
Roshana Pitigala
4,59062346
4,59062346
asked Nov 11 at 4:05
user10635007
1
1
1
Please format your code. Also what is your question?Why can’t i get a result for gasStation better?
doesn't make any sense to me
– GBlodgett
Nov 11 at 4:05
You're trying to put classes inside the main. This is incorrect. If I were you I would refer to the javadoc tutorials: docs.oracle.com/javase/tutorial/java/javaOO/classes.html
– GBlodgett
Nov 11 at 4:08
Your returning better but not calling the compare method and storing the returns result
– Nick Parsons
Nov 11 at 4:08
Why cant i print the object ‘better’ at the end of my code. Im sorry im a complete newb
– user10635007
Nov 11 at 4:10
@user10635007 As I said you are trying to put everything inside of your main. You need to create the classes separate, and then construct the objects in the main and call the methods on the objects
– GBlodgett
Nov 11 at 4:12
add a comment |
1
Please format your code. Also what is your question?Why can’t i get a result for gasStation better?
doesn't make any sense to me
– GBlodgett
Nov 11 at 4:05
You're trying to put classes inside the main. This is incorrect. If I were you I would refer to the javadoc tutorials: docs.oracle.com/javase/tutorial/java/javaOO/classes.html
– GBlodgett
Nov 11 at 4:08
Your returning better but not calling the compare method and storing the returns result
– Nick Parsons
Nov 11 at 4:08
Why cant i print the object ‘better’ at the end of my code. Im sorry im a complete newb
– user10635007
Nov 11 at 4:10
@user10635007 As I said you are trying to put everything inside of your main. You need to create the classes separate, and then construct the objects in the main and call the methods on the objects
– GBlodgett
Nov 11 at 4:12
1
1
Please format your code. Also what is your question?
Why can’t i get a result for gasStation better?
doesn't make any sense to me– GBlodgett
Nov 11 at 4:05
Please format your code. Also what is your question?
Why can’t i get a result for gasStation better?
doesn't make any sense to me– GBlodgett
Nov 11 at 4:05
You're trying to put classes inside the main. This is incorrect. If I were you I would refer to the javadoc tutorials: docs.oracle.com/javase/tutorial/java/javaOO/classes.html
– GBlodgett
Nov 11 at 4:08
You're trying to put classes inside the main. This is incorrect. If I were you I would refer to the javadoc tutorials: docs.oracle.com/javase/tutorial/java/javaOO/classes.html
– GBlodgett
Nov 11 at 4:08
Your returning better but not calling the compare method and storing the returns result
– Nick Parsons
Nov 11 at 4:08
Your returning better but not calling the compare method and storing the returns result
– Nick Parsons
Nov 11 at 4:08
Why cant i print the object ‘better’ at the end of my code. Im sorry im a complete newb
– user10635007
Nov 11 at 4:10
Why cant i print the object ‘better’ at the end of my code. Im sorry im a complete newb
– user10635007
Nov 11 at 4:10
@user10635007 As I said you are trying to put everything inside of your main. You need to create the classes separate, and then construct the objects in the main and call the methods on the objects
– GBlodgett
Nov 11 at 4:12
@user10635007 As I said you are trying to put everything inside of your main. You need to create the classes separate, and then construct the objects in the main and call the methods on the objects
– GBlodgett
Nov 11 at 4:12
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
There are a couple of mistakes in your code.
Initialize the variable
better
outside theif
scope.
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
return better;
All the statements should be either inside a method or a block. Put your
System.out.println(better);
statement inside thecompare
method, before the return statement.
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
System.out.println(better);
return better;
}
You can get your
gasStation
class andbetterDeal
class out of theMain
class.
Why can't I print the object ‘better’ at the end of my code? That's because you never call the
compare
method. Create a new object of thebetterDeal
class and call thecompare
method inside yourmain
method to print the variablebetter
.
new betterDeal().compare(shell, exxon);
But still if you need to print the
price
and thedistance
of thebetter
variable you'll have to override thetoString()
method inside thegasStation
class.
class gasStation {
double price;
double distance;
public String toString() {
return "Price: " + price + "nDistance: " + distance;
}
}
Full Code:
public class Main {
public static void main(String args) {
gasStation shell = new gasStation();
shell.price = 2.72;
shell.distance = 1.25;
gasStation exxon = new gasStation();
exxon.price = 2.35;
exxon.distance = 1.75;
new betterDeal().compare(shell, exxon);
}
}
class gasStation {
double price;
double distance;
public String toString() {
return "Price: " + price + "nDistance: " + distance;
}
}
class betterDeal {
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
System.out.println(better);
return better;
}
}
The code helps, but it still doesnt print ‘better’
– user10635007
Nov 11 at 5:02
It definitely does. I've updated my answer. Observe the mistakes. Check the full code.
– Roshana Pitigala
Nov 11 at 5:29
@user10635007 It prints the better price and the distance. Change thetoString()
method on your will.
– Roshana Pitigala
Nov 11 at 7:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
There are a couple of mistakes in your code.
Initialize the variable
better
outside theif
scope.
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
return better;
All the statements should be either inside a method or a block. Put your
System.out.println(better);
statement inside thecompare
method, before the return statement.
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
System.out.println(better);
return better;
}
You can get your
gasStation
class andbetterDeal
class out of theMain
class.
Why can't I print the object ‘better’ at the end of my code? That's because you never call the
compare
method. Create a new object of thebetterDeal
class and call thecompare
method inside yourmain
method to print the variablebetter
.
new betterDeal().compare(shell, exxon);
But still if you need to print the
price
and thedistance
of thebetter
variable you'll have to override thetoString()
method inside thegasStation
class.
class gasStation {
double price;
double distance;
public String toString() {
return "Price: " + price + "nDistance: " + distance;
}
}
Full Code:
public class Main {
public static void main(String args) {
gasStation shell = new gasStation();
shell.price = 2.72;
shell.distance = 1.25;
gasStation exxon = new gasStation();
exxon.price = 2.35;
exxon.distance = 1.75;
new betterDeal().compare(shell, exxon);
}
}
class gasStation {
double price;
double distance;
public String toString() {
return "Price: " + price + "nDistance: " + distance;
}
}
class betterDeal {
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
System.out.println(better);
return better;
}
}
The code helps, but it still doesnt print ‘better’
– user10635007
Nov 11 at 5:02
It definitely does. I've updated my answer. Observe the mistakes. Check the full code.
– Roshana Pitigala
Nov 11 at 5:29
@user10635007 It prints the better price and the distance. Change thetoString()
method on your will.
– Roshana Pitigala
Nov 11 at 7:20
add a comment |
up vote
1
down vote
There are a couple of mistakes in your code.
Initialize the variable
better
outside theif
scope.
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
return better;
All the statements should be either inside a method or a block. Put your
System.out.println(better);
statement inside thecompare
method, before the return statement.
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
System.out.println(better);
return better;
}
You can get your
gasStation
class andbetterDeal
class out of theMain
class.
Why can't I print the object ‘better’ at the end of my code? That's because you never call the
compare
method. Create a new object of thebetterDeal
class and call thecompare
method inside yourmain
method to print the variablebetter
.
new betterDeal().compare(shell, exxon);
But still if you need to print the
price
and thedistance
of thebetter
variable you'll have to override thetoString()
method inside thegasStation
class.
class gasStation {
double price;
double distance;
public String toString() {
return "Price: " + price + "nDistance: " + distance;
}
}
Full Code:
public class Main {
public static void main(String args) {
gasStation shell = new gasStation();
shell.price = 2.72;
shell.distance = 1.25;
gasStation exxon = new gasStation();
exxon.price = 2.35;
exxon.distance = 1.75;
new betterDeal().compare(shell, exxon);
}
}
class gasStation {
double price;
double distance;
public String toString() {
return "Price: " + price + "nDistance: " + distance;
}
}
class betterDeal {
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
System.out.println(better);
return better;
}
}
The code helps, but it still doesnt print ‘better’
– user10635007
Nov 11 at 5:02
It definitely does. I've updated my answer. Observe the mistakes. Check the full code.
– Roshana Pitigala
Nov 11 at 5:29
@user10635007 It prints the better price and the distance. Change thetoString()
method on your will.
– Roshana Pitigala
Nov 11 at 7:20
add a comment |
up vote
1
down vote
up vote
1
down vote
There are a couple of mistakes in your code.
Initialize the variable
better
outside theif
scope.
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
return better;
All the statements should be either inside a method or a block. Put your
System.out.println(better);
statement inside thecompare
method, before the return statement.
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
System.out.println(better);
return better;
}
You can get your
gasStation
class andbetterDeal
class out of theMain
class.
Why can't I print the object ‘better’ at the end of my code? That's because you never call the
compare
method. Create a new object of thebetterDeal
class and call thecompare
method inside yourmain
method to print the variablebetter
.
new betterDeal().compare(shell, exxon);
But still if you need to print the
price
and thedistance
of thebetter
variable you'll have to override thetoString()
method inside thegasStation
class.
class gasStation {
double price;
double distance;
public String toString() {
return "Price: " + price + "nDistance: " + distance;
}
}
Full Code:
public class Main {
public static void main(String args) {
gasStation shell = new gasStation();
shell.price = 2.72;
shell.distance = 1.25;
gasStation exxon = new gasStation();
exxon.price = 2.35;
exxon.distance = 1.75;
new betterDeal().compare(shell, exxon);
}
}
class gasStation {
double price;
double distance;
public String toString() {
return "Price: " + price + "nDistance: " + distance;
}
}
class betterDeal {
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
System.out.println(better);
return better;
}
}
There are a couple of mistakes in your code.
Initialize the variable
better
outside theif
scope.
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
return better;
All the statements should be either inside a method or a block. Put your
System.out.println(better);
statement inside thecompare
method, before the return statement.
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
System.out.println(better);
return better;
}
You can get your
gasStation
class andbetterDeal
class out of theMain
class.
Why can't I print the object ‘better’ at the end of my code? That's because you never call the
compare
method. Create a new object of thebetterDeal
class and call thecompare
method inside yourmain
method to print the variablebetter
.
new betterDeal().compare(shell, exxon);
But still if you need to print the
price
and thedistance
of thebetter
variable you'll have to override thetoString()
method inside thegasStation
class.
class gasStation {
double price;
double distance;
public String toString() {
return "Price: " + price + "nDistance: " + distance;
}
}
Full Code:
public class Main {
public static void main(String args) {
gasStation shell = new gasStation();
shell.price = 2.72;
shell.distance = 1.25;
gasStation exxon = new gasStation();
exxon.price = 2.35;
exxon.distance = 1.75;
new betterDeal().compare(shell, exxon);
}
}
class gasStation {
double price;
double distance;
public String toString() {
return "Price: " + price + "nDistance: " + distance;
}
}
class betterDeal {
public gasStation compare(gasStation shell, gasStation exxon) {
double shellRating = shell.price * shell.distance;
double exxonRating = exxon.price * exxon.distance;
gasStation better = null;
if (shellRating > exxonRating) {
better = shell;
} else if (shellRating < exxonRating) {
better = exxon;
}
System.out.println(better);
return better;
}
}
edited Nov 11 at 4:32
answered Nov 11 at 4:14
Roshana Pitigala
4,59062346
4,59062346
The code helps, but it still doesnt print ‘better’
– user10635007
Nov 11 at 5:02
It definitely does. I've updated my answer. Observe the mistakes. Check the full code.
– Roshana Pitigala
Nov 11 at 5:29
@user10635007 It prints the better price and the distance. Change thetoString()
method on your will.
– Roshana Pitigala
Nov 11 at 7:20
add a comment |
The code helps, but it still doesnt print ‘better’
– user10635007
Nov 11 at 5:02
It definitely does. I've updated my answer. Observe the mistakes. Check the full code.
– Roshana Pitigala
Nov 11 at 5:29
@user10635007 It prints the better price and the distance. Change thetoString()
method on your will.
– Roshana Pitigala
Nov 11 at 7:20
The code helps, but it still doesnt print ‘better’
– user10635007
Nov 11 at 5:02
The code helps, but it still doesnt print ‘better’
– user10635007
Nov 11 at 5:02
It definitely does. I've updated my answer. Observe the mistakes. Check the full code.
– Roshana Pitigala
Nov 11 at 5:29
It definitely does. I've updated my answer. Observe the mistakes. Check the full code.
– Roshana Pitigala
Nov 11 at 5:29
@user10635007 It prints the better price and the distance. Change the
toString()
method on your will.– Roshana Pitigala
Nov 11 at 7:20
@user10635007 It prints the better price and the distance. Change the
toString()
method on your will.– Roshana Pitigala
Nov 11 at 7:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245748%2fwhy-can-t-i-get-a-result-for-gasstation-better%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please format your code. Also what is your question?
Why can’t i get a result for gasStation better?
doesn't make any sense to me– GBlodgett
Nov 11 at 4:05
You're trying to put classes inside the main. This is incorrect. If I were you I would refer to the javadoc tutorials: docs.oracle.com/javase/tutorial/java/javaOO/classes.html
– GBlodgett
Nov 11 at 4:08
Your returning better but not calling the compare method and storing the returns result
– Nick Parsons
Nov 11 at 4:08
Why cant i print the object ‘better’ at the end of my code. Im sorry im a complete newb
– user10635007
Nov 11 at 4:10
@user10635007 As I said you are trying to put everything inside of your main. You need to create the classes separate, and then construct the objects in the main and call the methods on the objects
– GBlodgett
Nov 11 at 4:12