Java: Questions About Music & App Store Program

  • Context: Java 
  • Thread starter Thread starter MarcL
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around a programming assignment for an introductory object-oriented programming class, specifically focusing on creating a program for an online music and apps store. Participants are addressing questions related to variable declaration, user input handling, and output formatting in Java.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster (OP) inquires about the placement of variable and constant definitions in relation to the main method, seeking clarity on conventions and readability.
  • The OP expresses uncertainty about how to prompt the user for input in Java.
  • The OP questions whether to use a hardcoded string or a defined variable in the output message.
  • One participant suggests that variables and constants can be defined anywhere, emphasizing that it is a matter of convention.
  • Another participant points out several issues in the OP's code, including undeclared variables, incorrect syntax, and the need for proper formatting.
  • The OP acknowledges the feedback and admits to some mistakes, particularly regarding code structure and spelling errors.
  • There is a discussion about the appropriate way to handle monetary values in code, with one participant explaining that the dollar sign should not be part of variable names but should be included in output statements.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper syntax and structure in the code. However, there is some disagreement regarding the handling of monetary symbols in variable names versus output formatting.

Contextual Notes

Some participants note specific issues in the OP's code that need addressing, such as undeclared variables and syntax errors, but do not resolve these issues definitively.

Who May Find This Useful

Students learning Java programming, particularly those working on object-oriented programming assignments or seeking guidance on coding conventions and user input handling.

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   Reactions: 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   Reactions: Medicol

Similar threads

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