• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with GUI assignment.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code. I am very much a greenhorn at java. The text we are having to use for school is not explanatory enough for me to comprehend ...so I am seeking help to understand what is wrong with my code. Here are the errors I'm getting. I really need to get this GUI working, as I have to add to it for this weeks assignment, and if last weeks don't work....this weeks won't either. Please help me. THANKS in advance.
C:\Users\Bert\Documents\NetBeansProjects\GUI Week 7\src\gui\week\pkg7\GUIWeek7.java:56: error: class WeightCalculator is public, should be declared in a file named WeightCalculator.java
public class WeightCalculator {
1 error
C:\Users\Bert\Documents\NetBeansProjects\GUI Week 7\nbproject\build-impl.xml:603: The following error occurred while executing this line:
C:\Users\Bert\Documents\NetBeansProjects\GUI Week 7\nbproject\build-impl.xml:245: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Bertha, and welcome to the Ranch!

What part of this error message did you not understand?

"class WeightCalculator is public, should be declared in a file named WeightCalculator.java "
 
Bertha Figard
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel really stupid at this moment. I don't know how to. I thought that is what I did when I did this public class WeightCalculator {....but obviously not.

Our text is about useless, to me. I am in chapter 14 and (throughout the entire book)...it says they will explain stuff in chapter xxx. Today's reading says it will explain in chapter 22 I'm in chapter 14...ummm? I think that is why I'm having such a hard time grasping things.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can't have 2 top level public classes in the same file.

You'll need one file, WeightCalculator.java and one GuiWeek7.java -- each with their own top level public class.
-OR-
The other option is to remove the public keyword from the WeightCalculator class.

Either should work.
 
Bertha Figard
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, going to try it now....

I hope I can get it figured out...so depressed over this. ***best sad face I can muster***
 
Bertha Figard
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YAY, I got it to work, now can someone point me in the right direction so that I can add the close line to my project...LOL
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bertha Figard wrote:now can someone point me in the right direction so that I can add the close line to my project...LOL



What do you mean?
 
Bertha Figard
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Modifications I have to do to the program for this weeks assignment:

I have to A) if the weight of the object is greater than 99999, code should print a GUI driven error message and force end user to re-enter weight of object.
B) if user enters a negative weight , again, it needs to print a GUI driven error
C) have the X work when the end user hits the X to close the box

Then....the second part of the program I have to add a logo to the input GUI using Java Graphics.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so we won't "do" your homework for you. We've all done an extraordinary amount of homework ourselves.

If you need help with a portion, you'll need to do two things... well, 2 1/2.

1. Formulate a specific question
2. Tell us what you tried
2.5. Tell us why you think it should/shouldn't work
 
Bertha Figard
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't want anyone doing my homework, I won't learn anything that way. I have not tried anything YET....I had to get last weeks GUI totally functioning so I can try to do the modifications to it. I just need to find a vid or something that will show me...(visual learner), how to do the things I need to do to modify the program.

By the way THANK YOU THANK YOU THANK YOU...the little bit you guys posted back to me, I was able to get the GUI functioning properly. You guys ROCK~!~~~
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, we do rock, but I am not totally happy about your WeightCalculator class, I am afraid.

You have fields which you set to 0 and null. I think you ought to be initialising those in a constructor, so you never have those 0 and null values running around. 0s are’t too bad, but a null can escape and cause an Exception a long way away from where the null was created. You would call it as new WeightCalculator("Elephant", 5000). That allows you to miss out the two setXXX calls, because those values are already set. By only giving the WeightCalculator class the one constructor which takse the two arguments, you can make sure those fields are always initialised.

You can see an example of a constructor here, but if it has the bit about speed cadence and gear in, that isn’t quite mathematically sound because speed = gear × cadence × 60 × π ÷ 63360, so cadence = 30 and speed = 0 don’t quite add up
It also says there that you don’t have to provide a constructor, but that is hazardous. If you look in other documentation, eg this “How to Write Documentation Comments” page, they say you should always write a constructor. Agree. It is a good policy always to write a constructor for every class, because you can then control instantiation of that class, ensuring you always get the correct values for your fields.
If you ever have a class you wish to prevent from being instantiated, look here, but it isn’t easy to read.
 
Bertha Figard
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to add 'something' that will create an error if a person enters 99999 for a weight. The rest of my code works...however I can't remember or don't know how to get it to do this for me. I know I need something prior to my error_message....I am really stuck. Have been at this all day, have made no progress other than error_message.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use an if statement to check the value of d

The only problem with this is that when a weight of greater than 99999 is entered you will skip one of your WeightCalculator objects.
You will be better to use an old style for loop for the first loop and decrement the counter when you get the error.
 
Bertha Figard
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is why I am here...I tried the if statement, and kept getting an error. I will try your alternative, and see if I can't get it to do what I need/want. THANK YOU.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first you need to sit down with a pencil and paper.

you need 2 things from the user:
1) description
2) weight

for each you pop up an input box where the user can return 4 things (or has 4 options)
a) select 'X', Cancel or 'Esc' - these will return null
b) click OK with nothing entered
c) click OK after entering invalid data
d) click OK after entering valid data

you need to work out how you're going to handle each of a,b,c, and d

one way may be:
set a boolean: validData = false
then in a while(validData == false) you put your input box
you must firstly check for null - now what? has user cancelled program and wants to exit?
nothing or invalid data - reshow the input box and add an error message?
when the entered data is tested to be valid you change validData to true, so the while() exits

once you've covered/handled all options on paper, convert it to code.

(just a quick glance at the code - I don't see why the for()'s are needed)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic