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);
}
}

}









share|improve this question




















  • 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















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);
}
}

}









share|improve this question




















  • 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













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);
}
}

}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 4:25









Roshana Pitigala

4,55562346




4,55562346










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














  • 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












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 the if 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 the compare 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 and betterDeal class out of the Main 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 the betterDeal class and call the compare method inside your main method to print the variable better.



    new betterDeal().compare(shell, exxon);



  • But still if you need to print the price and the distance of the better variable you'll have to override the toString() method inside the gasStation 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;
}

}





share|improve this answer























  • 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 the toString() method on your will.
    – Roshana Pitigala
    Nov 11 at 7:20











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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 the if 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 the compare 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 and betterDeal class out of the Main 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 the betterDeal class and call the compare method inside your main method to print the variable better.



    new betterDeal().compare(shell, exxon);



  • But still if you need to print the price and the distance of the better variable you'll have to override the toString() method inside the gasStation 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;
}

}





share|improve this answer























  • 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 the toString() method on your will.
    – Roshana Pitigala
    Nov 11 at 7:20















up vote
1
down vote













There are a couple of mistakes in your code.





  • Initialize the variable better outside the if 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 the compare 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 and betterDeal class out of the Main 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 the betterDeal class and call the compare method inside your main method to print the variable better.



    new betterDeal().compare(shell, exxon);



  • But still if you need to print the price and the distance of the better variable you'll have to override the toString() method inside the gasStation 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;
}

}





share|improve this answer























  • 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 the toString() method on your will.
    – Roshana Pitigala
    Nov 11 at 7:20













up vote
1
down vote










up vote
1
down vote









There are a couple of mistakes in your code.





  • Initialize the variable better outside the if 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 the compare 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 and betterDeal class out of the Main 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 the betterDeal class and call the compare method inside your main method to print the variable better.



    new betterDeal().compare(shell, exxon);



  • But still if you need to print the price and the distance of the better variable you'll have to override the toString() method inside the gasStation 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;
}

}





share|improve this answer














There are a couple of mistakes in your code.





  • Initialize the variable better outside the if 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 the compare 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 and betterDeal class out of the Main 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 the betterDeal class and call the compare method inside your main method to print the variable better.



    new betterDeal().compare(shell, exxon);



  • But still if you need to print the price and the distance of the better variable you'll have to override the toString() method inside the gasStation 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;
}

}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 4:32

























answered Nov 11 at 4:14









Roshana Pitigala

4,55562346




4,55562346












  • 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 the toString() 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










  • 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python