Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Question about Java

  1. Jan 17, 2015 #1
    Hey so I have to write a program that does the following for my class ( Object oriented programing 1, so basic stuff really...)

    An online music and apps store offers all apps for 3$ each and all songs for 7$ each. The store
    requires members to prepay any amount of money they wish, and then download as many apps
    or as many songs accordingly. You are required to write a program that would ask the user for
    the amount that he/she will pay, then display two messages indicating:
    - the maximum number of apps that can be downloaded, and how much funds will remain
    in the account after that, if any.
    - the maximum number of songs that can be downloaded, the number of apps that can be
    downloaded after that if funds allow, and how much funds will remain in the account
    after that, if any.
     Notice the parenthesis in app(s) and song(s) in the output.

    And I wrote this, but I have a few question about what I wrote ...

    public class MusicStore
    { double prePay;
    prePay = x;
    int. songs, apps;
    Final int. Songs=3$ , apps=7$
    public static void main (String arg[])
    { system.out.println("How much money do you wish to prepay?")"

    Keep in mind im not done. My first (1) question was whether or not I have to define my variables and constants before or after my main method? ( and why if anyone can explain) . Secondly, I don't know how to ask / allow the user to input a number after a question. Finally, for my system output, would I be better using "How much money do you wish to prepay?" or use the define variable.

    Really sorry if this is a mess, it's my first time doing this alone!
  2. jcsd
  3. Jan 17, 2015 #2


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    The variables and contants can be defined anywhere. They don't have to be defined before the method that uses them. It's mainly a convention for readability.

    If you are going to ask questions about your code, please use the <code> block (under the +) so that it's more readable.
  4. Jan 17, 2015 #3


    Staff: Mentor

    Yes, it's pretty much a mess, and you have a lot of work ahead of you to get something that will compile without error and run. To get an idea of what some working Java code looks like, take a look at this tutorial and some of the others in this series: http://docs.oracle.com/javase/tutorial/getStarted/cupojava/netbeans.html

    Here's a list of a few of the problems I see in your code.

    x is undeclared, and you use it to set a value for prePay.
    There is no int. type (the period at the end should not be there).
    Final is misspelled - it should be final, uncapped.
    Numerica values cannot include $.
    Some statements are missing the terminating ; character.
    Every opening brace ({) must have a matching closing brace (}). You have no closing braces.

    Regarding input, see this tutorial on reading input from the command line: http://docs.oracle.com/javase/tutorial/essential/io/cl.html.

    Finally, when you post code, please put [ code ] and [ /code ] tags (without the extra spaces) around your code. These tags preserve your indentation and make your code easier to read.
  5. Jan 17, 2015 #4
    alright first time I was posting it. Sorry about that, for the closing bracket I should've done it straight at the beginning, but I didn't out of sheer laziness ( but i should put it so I definitely don't forget). I understand the idea about the "final" being misspelled. As for numerical value (the $ sign) how would I include it, or at least label it. I mean isn't that a vital part when writing units down so it is user friendly?
  6. Jan 17, 2015 #5


    Staff: Mentor

    There is a big difference between what is displayed to the user and variables in your program. The variables in your program are internal - the user never sees them, so denomination symbols are not part of variable names. Of course, when you print values, you should include the monetary denominations, but you do this by including the denomination symbol when you print the money amount, something like this:

    Code (Text):
    system.out.println("The cost is $" + itemCost);
    BTW, the denomination comes first in US monetary amounts, as in $3 or $5.95, not 3$ or 5.95$.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook