• Post Reply Bookmark Topic Watch Topic
  • New Topic

Counting Change program  RSS feed

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying this counting change program and it is not nearly done, but testing a few things. I am getting confused here. I am getting this error

"Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot make a static reference to the non-static field tAmount
Cannot make a static reference to the non-static field tAmount
total cannot be resolved to a variable"

It is saying that "Cannot make a static reference to a non-static field TAmount"

I made tAmount accessible to the whole program by putting it under the class, so I am not sure what this means.

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make all your fields 'static'. Without 'static' an instance of the class is required, and, seeing as how main() is declared 'static' and doesn't create any new instances, you have no object instance to operate on. As your programming evolves you will leave almost all fields as non-static and create an instance somewhere (e.g. main()) to operate on, this is the preferred object oriented approach.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than making all the fields static, I would recommend getting used to getting out of the main class.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I get out of Main class, what is it used for mostly? Calling methods?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main class is a well defined place for your program to start.

It needs to be static so that it can be called without having an instance of the class.
And that is all it is for.

Typically the first(only?) thing the main class should do is create a new object, and call a method on it.

It is quite different programming in a static method to a non-static one.
Seeing as most java programming is done dealing with objects and object instances static methods are the exception rather than the norm, which is why I recommend this approach.


 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I completed the program, took about 4 hours and most will laugh, but I got through it. It is not elegant and I am sure it doesn't need as many lines of code as I have used. I am also quite convinced that I have not used most of the things programmers take for granted, but I am doing it the only way I know how at this juncture.

1) The next thing I need to do is make sure the user enters a number and not a string - I think I am going to try Boolean for that and a do while loop, but not sure how to approach it. Can I ask to DO something WHILE the input is a DOUBLE?

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas what to try next?
I want to expand on my limited skill set, but I am not sure of what kind of project to try.
 
Liutauras Vilda
Sheriff
Posts: 4929
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan already gave you a wonderful suggestion do not put all your code to a main method, but rather split your program to a smaller methods.
It is significant when program gets bigger, so you could easier extend it without braking down something else. Moreover, it is easier to understand what is happening.

1. Class names suppose to start with an Upper case
2. Code indentation needs to be improved

I am looking at each while loop body statements and trying to understand, what if statements is doing (more precise - body of the if statement). Could you explain?
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
The "IF" statements are to check to see if the "remainder" is zero. If so, it has to stop looking further for change. At least that is what I wanted to set up.
Methods are definitely something that is simple, but I can't really wrap my head around "when to use it". For me, it just was simpler to continue to writing. I understand the concept, but WHEN to use them is another thing.
I am going to use ContrlShiftLeft to format (I thought I saw that short cut) and I am working on using Class name properly.
 
Liutauras Vilda
Sheriff
Posts: 4929
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:The "IF" statements are to check to see if the "remainder" is zero.
It is checking if leftOver is greater than 0. You making confusion for yourself with these misleading checks.
This is a classic example whats happening when start coding, rather than researching what has to be done.

1. All your program at the moment is just the same code repeated 6 times (all this could be shortened ~ 6 times)
2. Quite poorly chosen variable names as gc - just name it as givenCents if it is what is meant to store and comment becomes redundant
3. Think about how to put while loop and the body to a single method (which would take parameter/-s)
4. While loop body could be shortened by checking remainder in if statement
5. Leave if statements body empty is not a good practice as well, you can simplify this
5. break statement is redundant in your current case

Try to come up with something better, and show us different approach to this.
Actually I'd start over, you could just keep this code for your references - probably would be quicker.

-edit- sorry if I disappointed you, just trying to help
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am more disappointed in myself. I have been learning for a month and a half and still don't know what I am doing it seems! I am sure the people on this forum are frustrated with me asking the same question over and over, but if it is any consolation to anyone here, I beat myself up over this daily because of my stupidity.

1) I see now that the IF STATEMENT did nothing and have eliminated them. Thanks for that.
2) I know this could be shortened and said so in the original post, I would have liked nothing better than to have shortened code.
3) I do think you are right about variable names and will consider full words in the future.
4) I did not put the while loop in the main body because it escapes me how to do it, that is why I did it in little chunks, testing each to see if they would work. When it worked, I moved to the next piece.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I went back to the program and found a lot of superfluous stuff. Here is the cut down code :

1) It does not work right, yet.
2) What I would like to do is create a variable "arrayVar" that changes so I only need one Print statement that will go through the entire code. If there is a way around this, play let me know.
3) If you look at the bottom print statement, I want it to grab the value of i, j, k, l, m, n (they are int) and place it there.

I hope that makes sense. And yes I am well aware there is probably the a way easier version of doing this, but I am trying.

 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick feedback:
Take a look at how much is happening in your while loop.
How much of it actually changes based on the 'count' variable?
Anything that doesn't depend on count should not be in this loop as you will just be recalculating things needlessly.

I can see what you are trying to do with the array, but java doesn't work like that with primitive data types.
I'm sure you could do it with C and pointers, but not in java.

Actually I think the variables i,j,k etc are superfluous.
And the tonnies, loonies, etc could be int data types. By having them double and casting the value to int you are effectively doing that anyway.

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have lost me a bit, although I appreciate the quick reply.

1) Are you saying I should cut down on the calculations?

2) OR put the calculations outside the loop?

3) The count variable is there so I can change the name from twoonies, loonies, quarters etc from the array - does that seem fine? It works, so I hope so.

4) I did not think about INT as data types because I thought I was working with remainders and needed to use DOUBLES for the calculations.

5) I guess I will scrap the array/variable attempt if it doesn't work. It was just a thought.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Are you saying I should cut down on the calculations?

2) OR put the calculations outside the loop?

Put the calculations outside the loop.
They don't depend on count at all.
The only reason for the loop right now is to print out the values in the array.

>4) I did not think about INT as data types because I thought I was working with remainders and needed to use DOUBLES for the calculations.

I would suggest your try some experiments to get an understanding of how integer division works.
In a nutshell the / operator gives you the division, the % operator gives you the remainder
so 10 / 3 is 3, and 10 % 3 is 1 (because 3 goes into 10 three times, with one left over)


>5) I guess I will scrap the array/variable attempt if it doesn't work. It was just a thought.
It was a good thought, and is probably the right way to do things - just not with arrays in this approach.
The data structure you are after is called a Map. Its in the java.util package. But you'll learn about those eventually.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I went back in and fixed it the best I could. I liked the Arrays for the names of the change, but it just didn't work, so I took that out. The code is smaller now, which I suppose is what I am really going for. I got rid of the doubles so prevent the decimals for a few things as well because I didn't need decimals. Anything else?
 
Liutauras Vilda
Sheriff
Posts: 4929
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Class named incorrectly
2. Too much code in your main method

Your main method should only execute your program and nothing else.
The rest of the code should be in other non static methods if you run from the same class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!