This program should create a GUI that has 5 classes together on a grid layout. The problem that I am having is that the user input class has the input for kwh, hours, and gallons. I am having problems getting that information from the user input class to the totals class. Can anyone help me?
user input class:
and the totals class:
The totals class should take the info from the kwh hours and gallons and do the math to get a total and display it in a JTextField(?).
There is something very wrong with your design, I am afraid.
You should have display classes which display the data and logic classes which do the actual calculations. You should separate the two. In fact, I think you should not even try to write a GUI until you have the calculations working correctly from the command line.
You should not try to have one unified listener for everything, but separate listeners for hours kWh and gallons. Those simply take the inputs in the text fields and convert them to numbers and pass them to the calculations class. Please count how many listeners you have. You should have one pre action required. You appear to have one in the totals class. What is that doing? Why do you want new panels?
The overall look is of one panel that has the user input panel on the west, a checkbox panel in the center, the totals on the east, a greeting in the north and an exit button in the south. I have been trying to follow a workbook example and yes, this is for a class. The workbook does not give me enough information outside of the examples to get all of this together.
the user input class should accept input for kwh, hours, and gallons, the checkbox class will offer selections of appliances to choose from and the totals class would repeat the input from the user input class with a total athe the bottom. I have not got the checkbox done yet, but it is not necessary for the operation of hte user input or totals classes. I am fully confused now and trying to catch up.
Write a Java™ GUI application using a NetBeans® IDE that does home utility auditing.
The user should be able to do the following:
Enter the cost per kilowatt-hour.
Select from a list of 5 to 7 home appliances, which include a washer, among other appliances.
Enter the power needed in kilowatts (kW) for the selected appliance.
Enter the number of hours used per day for the selected appliance.
If the washer is selected, enter both the number of gallons of water used by the washer per hour and the cost per gallon.
The program should do the following:
Validate that the data entered has the correct format, and is within a reasonable range.
Calculate and display the cost for operating a home appliance as soon as the data is entered.
Create a listing area to display each of the following for each appliance entered:
Number of hours per day
Display and update the total cost of all appliances, as soon as a new entry is added to the listing area.
The Java™ application should meet these technical requirements:
The application should have at least one additional method, in addition to the application's controlling class (a controlling class is where the main function resides).
There should be proper documentation in the source code.
M Gerber wrote:The overall look is of one panel that has the user input panel on the west, a checkbox panel in the center, the totals on the east, a greeting in the north and an exit button in the south...
Sounds to me like you haven't read Campbell's post, because you're still preoccupied with the display.
GUIs are verbose and fiddly, and require a lot of thought, so if you're trying to design it AND your "utility audit" logic at the same time, one of them is going to suffer.
His advice (and it's the same as I would have given) is part of the business of breaking problems down into discrete chunks that you can focus on individually; and you're not going to be able to give your utility audit logic proper attention if you're also worrying about buttons, icons and layouts.
Concentrate on the audit logic for now, and get that working; and don't include any 'J' classes (JPanel, JButton, etc) in the classes you write to implement it. When you have got it working, then will be the time to start thinking about your GUI.
Alternatively, write the GUI with dummy values, or logic that simply displays a pop-up message that says what it's doing, and forget all about the audit logic for now. Personally, I suspect it's a harder way to go, but if you're a "visual" guy, it might be easier for you.
But whatever you do, separate the audit logic from the display completely.
Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
If you create each of your classes as panels [UserInputPanel, CheckBoxPanel, TotalsPanel](extending JPanel)... then you create a class as the frame [GUI](extending JFrame) and add the panels to it you'll end up with a nice modular design...
All that's left to do now is hook up the frame to a controller class that grabs the values in the UserInputPanel and the CheckBoxPanel and use the values to apply your formula that produces the results which flows back to the TotalsPanel for user feedback.
You can use the following snippets to get a better idea of the description:
Now I'll leave you to figure out how you're going to connect the dots and polish up the output in the following snapshot to meet your requirements
Rico, that is great. I have somewhere to start at least. Too bad most of this I do not understand. I am only 3 weeks into Java and some things are over me. I guess that your controller class is the main class. Not the main method, but where the math is done? when I added your code to the IDE some of the errors I do not understand, could you help me out with them?
The frame and panels are just views that are used to retrieve information from the user, they contain no logic... There should be a specific class that contain the application logic... The controller is a class that is set up to collaborate with the views and the application's logic class... what this mean is that it gets information from the panels using the panel's interface which are the methods such as getKwhFieldText() and appliance1IsSelected(), etc... It then takes this retrieved information and passes it over the application logic class which decides what should be done based on whatever rules are in place... from there the application logic class passes back the calculated data to the controller class which updates the total panel using its interface which is the setDisplayText(String) method...
Note: The panels all extends JPanel and not JFrame... and add(Component) is a method inherited which requires you to wrap its argument between parentheses
And also I was just trying to show you how you can solve this:
M Gerber wrote:I am having problems getting that information from the user input class to the totals class
while trying to motivate you by showing you a proposed implementation... Please take the time and try to figure out a solution by using the posts as guidelines and not a copy and paste solution.
You are quite correct, and Thank You again for your directions, I work a lot better hands on, especially with something like this. The controller class is the class that will have the setters and getters, and will also be the logic portion of the program right?
I am not trying to copy your code, but by typing it out and trying to add code to a working example I can see where I need to focus on,
I am new to Java and would like to learn it right, not just copy and paste.
M Gerber wrote:If I can figure out how to fix it, I can figure out how to write it...
Actually, that's not quite true. A mechanic can know how to fix a car without necessarily knowing how to build one from scratch; and the same is true of programming. Rico's suggestion - nice as it is (and I've given him a cow for it) - is only one of many ways to do it, and it's good to understand why he did it, not just how; otherwise you may just end up doing things "someone else's way".
At this early stage it's probably not wildly important, but at some point you'll run into a problem that isn't covered by examples you've seen - and that's when "knowing why" becomes important.
Thank you for your responses. What better way to learn code than to write it? I could read a book, I could post to forums, but what better way to learn code than to take examples and see why they work? If 2
+2=4 then in a simple code, total can = 2+2. I am a greenhorn and learning the basics can help to avoid easy mistakes.
Any help can and will be appreciated. Especially examples that I can use to learn Java.
Everyone is different and therefore the best way for one person to learn is not necessarily the best way for someone else to learn. Having said that I think it's fair to say that most people can only really learn to program by writing code, making mistakes, learning from them and then repeating the process time and time again. Reading other peoples code is an aid to learning for sure but reading and modifying someone else's code isn't really teaching you to program, to use Winston's analogy that just makes you a mechanic and not a car designer.
Scanning through your current implementation there is one flaw in the code that I must point out... In the setter methods you should be validating the arguments passed in and not the class fields
This is a logical error:
Validate k instead of kwh (check all setter methods)
I also understand what you mean by having a place to start, but as the Bartenders said as they know better... your focus should be on actual coding instead of modifying code... The code you see should just be used as jump-starters to get your creative juices running...
To share with you a little secret of mine... I am a newbie to programming also and I use this site as a puzzle book to gather new problems which I try to solve to help train myself as well as attempt to provide help to the OP (My personal Java puzzle book )
My advice to you is try do be dynamic as the field is also dynamic... Do a lot of reading, coding and research in iteration and you should be able in due time to gather enough knowledge and techniques to become fluent in the art where it becomes an artless art growing out of the unconscious...
Thank you for telling me how not to do it, but really all I want to know is how to do it not why not to. The mistakes that I am making I do not understand the underlying problems with it, and all I wanted to know is how something works so I can understand. I have taken my engine apart and learned how to change a head gasket by hands on following the repair manual and now I am confident that I can change different head gaskets because I did the one and have the knowledge from doing the first one. I am asking for help on my first Java program experience, and Rico has given me a direction to head. Now I am confused on some other problem and all you are telling me is how not to learn? Can I get some help from people that have possibly experienced the same problem or one related please?
Sorry guys for my previous outburst, I am frustrated that I cannot grasp these concepts. Time is ticking and it is getting hard to get this done. I stare at this stupid monitor for hours and hours and still see red errors
Wow there are a lot of errors.
I did start to use your example to add code to the program, but I am having trouble setting the action listeners. Do the event listeners method have to be in the gui interface class? Trying to empliment the listeners for the text area with the code:
but the code: text = kwh.getText();
I cnnot figure out how to call the other classes constructor to retrieve the info
I understand that the User input class panel is used to ask input from the user, The totals class panel will just regurgitate the data, a gui class will create a panel for the display of the other classes and the controller class will use setters and getters to store and retrieve the data, its just getting them all to work together that I have a problem figuring out.
M Gerber wrote:Sorry guys for my previous outburst, I am frustrated that I cannot grasp these concepts.
It's a common problem when you're starting out, so don't worry too much. We're used to it by now , but thanks for the apology.
I have to say though that this looks like a tough assignment for a "first Java program experience". Most courses wouldn't even think of introducing GUI material (except maybe message boxes) until at least a month or two into the fray, because (as I believe has already been said) GUI code is verbose, tricky and needs a lot of thought (and I still loathe it ).
The totals class panel will just regurgitate the data, a gui class will create a panel for the display of the other classes and the controller class will use setters and getters to store and retrieve the data, its just getting them all to work together that I have a problem figuring out.
And that's almost certainly because you're already trying to code them in your head before you have a clear idea of WHAT they need to do. It's a very difficult habit to break when you're starting out, because you're just dying to write code, but there are two major aspects to pretty much any programming problem: WHAT needs to happen, and HOW you're going to code it, and of the two 'what' is much more important than 'how' - especially in the early stages of writing a program.
Now this probably sounds like more nebulous advice that doesn't tackle your coding problem, but I suspect that the real issue is that you already have far too much code, so: StopCoding (←click).
As with anything in life, you can sometimes be too close to something; and that's the point when you have to stop and step back. Go play a game of squash, or have a couple of pints down the pub (if you're old enough), or just sleep on it. You'll be surprised how often answers come when you're not obsessing about a problem.
I've also written a page called WhereDoIStart, which shows you a technique for writing new classes that puts off a lot of these coding decisions until you're really ready to tackle them. If you've already written 300 lines, it may be too late for this program, but you might want to read it anyway ... or look at it before you start your next one.
Programming is about thinking, not coding; and you'll never solve a problem in Java until you can write it down in English (or your native language).
The code just provides functionality and should be improved upon to handle parsing exceptions (NumberFormatException) and to be optimized for greater efficiency... (This you should attempt to do)
The GUI also needs to be improved as there is a bug where the totals panel disrupts the layout as its being updated... (This you should also attempt to do)
Also keep in mind that our mission is to help you learn and solve problems on your own through guidance and not to give you direct answers... It makes much more sense learning to fish and becoming a fisherman yourself than be given a fish all the time... for the fisherman that you are getting the fishes from will not always be around and other fishermen might not be as generous...
Thank you Rico, I will use your gift and work on my understanding of Java. I am in the MIS course for the University of Phoenix and the current classes that I am in run for 6 weeks. UoP jumps around a lot in their studies and the last time that I had to study Java for UoP was 2 years ago. I am very appreciative for all the help that you guys have given me here on the Moose Ranch, I have tried StackOverflow and they were not as helpful.
UoP is a good school, but the classes assume that you know more than they have given. I know that we are supposed to study on our off time and expand our horizons, but there is only so many hours in the day.
I usually work out problems in my head without thinking to much so there will be stuff in there that needs refining... also you will have to hook up the check-boxes and provide your logic to the Application class as I didn't
In my limited knowledge, I understand little of this, but I am working on it, one question though, you have an array here:
and numbers here:
Is the array for the different panels/classes?
I am trying to add the checkboxes to the totals area and then the math for the washer (add gallon usage and price only if washer is selected), but this is outside of my UoP reading. Where can I look for help?
I've tried to make the classes as cohesive as possible...
The GUI class was made just to supply the view adding the panels to it for user input and feedback...
The Application class was made to contain the logic for the application...
The Controller class was made to connect the both together by setting up a channel for them to communicate with each other (the view and the logic classes)... In order to achieve this the GUI class gives the Controller class access to each of its panel components by packaging them into an array of their Parent type JPanel and delivering it by using the getPanels() method, hence the reason for the array notation after the method call and the cast back to its original type (UserInputPanel userInputPanel = (UserInputPanel) gui.getPanels();)... Now using this reference userInputPanel we have access to each component on the panel itself and we use it to get each field to add the appropriate action listener... they are numbered 1, 3, 5 according to how they were added to the panel in accordance to this method getComponent(int)...
You can probably follow the same pattern to connect the check-boxes if you like...
I've updated the source code to incorporate the check-boxes event handlers and improved the GUI (can be downloaded at the same link) all for free ... Now all that's left for you to do is to apply your business logic
WOW dude that was awesome (at least from my chair). Thank you for your demonstration on how Java is supposed to work. I know that there are different ways to do similar things, but I have been working with Java for 5 weeks. Thanks for pointing me in a direction... Id give you a ton of cows if i could...
I have hope that you will dig around and understand the design approach that I've made and come up with a much improved implementation for the application which you can probably share so that I can learn new things also from your side of the spectrum
stevey green wrote:I've not read a lot of this thread, but what comes to mind straight away is... bad design.
All data should be separated from your GUI - if I was you I would be taking a look at some design patterns - MVC in particular,
a pattern which is used to separate application's concerns.
I've always wanted to learn the MVC pattern but never got the time to do the reading... My design was actually done through intuition on what I suppose MVC is suppose to be... guess I should actually learn how its formally implemented