Java Java: Questions About Music & App Store Program

  • Thread starter Thread starter MarcL
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
The discussion revolves around creating a Java program for an online music and apps store that allows users to prepay and download apps and songs. The program should calculate and display the maximum number of apps and songs a user can download based on their prepaid amount, along with any remaining funds. Key coding issues are raised, including variable declaration, input handling, and output formatting. It is clarified that variables can be defined anywhere in the code, but it's best practice to define them for readability. The user is advised on proper syntax, such as avoiding the dollar sign in variable names, correcting spelling errors like "Final" to "final," and ensuring all statements end with a semicolon. Additionally, guidance is provided on how to format output to include monetary symbols correctly when displaying values to users.
MarcL
Messages
170
Reaction score
2
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 I am 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!
 
Technology news on Phys.org
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.
 
MarcL said:
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 I am 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.
 
  • Like
Likes MarcL
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?
 
MarcL said:
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$.
 
  • Like
Likes Medicol
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 0 ·
Replies
0
Views
627
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
13K