Question about Java

  • Java
  • Thread starter MarcL
  • Start date
  • #1
170
2

Main Question or Discussion Point

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!
 

Answers and Replies

  • #2
Borg
Science Advisor
Gold Member
1,871
2,263
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.
 
  • #3
33,270
4,975
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!
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.
 
  • #4
170
2
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?
 
  • #5
33,270
4,975
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?
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:
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$.
 

Related Threads on Question about Java

  • Last Post
2
Replies
28
Views
1K
Replies
6
Views
3K
Replies
2
Views
3K
Replies
6
Views
528
Replies
4
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
2
Views
2K
Replies
1
Views
1K
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
5
Views
4K
Top