• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Swing Applet

 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My assignment is as follows:

Create a Swing based Applet for a computer company that sells computers with associated peripherals.   The user will select the peripherals that they want, choose a method of shipping, and click a button to obtain a table of costs.

GUI:

Checkboxes for at least 6 computer peripherals (printer, speakers, etc).
Three option buttons for three types of shipping (ground, air, fedex)
A button to calculate total with shipping
A button to calculate total without shipping
A large textPane to display your results.
Output:

When the user presses one of the two calculate buttons, your program should display a table for the cost of 1 unit, 100 units, 200 units, up to 1000 units.  It should have one column for the quantity, and one column for the cost.  The cost should may or may not including shipping costs, depending on which button was clicked.

calculateCost:

Create two methods called calculateCost (to demonstrate overloading).  Both should return a double.

One should take no parameters and simply calculate the cost for ONE unit (base cost plus any selected peripherals).
The other should take two parameters (quantity and a boolean that indicates whether shipping should be included or not) and it should return the cost for the specified quantity, factoring in the quantity discount and shipping costs (if applicable).  It should use the other calculateCost method to get the cost for one unit.
Obviously, you will need to determine what a base computer costs and what the peripherals cost.

If shipping is to be included, costs are:

Ground is 5% of total order if less than 100 units (otherwise, free)
Air is 10% of total order
FedEx is 20% of total order
getDiscountPercentage:

Create a helper method to determine the discount percentage based on quantity.   The discount should be 1% for 100-199, 2% for 200-299, etc.:

public double getDiscountPercentage(int quantity)

Event Handlers:

Create event handlers for the two calculate buttons.  The event handlers will loop, calling calculateCost for quantities 1, 100, 200, up to 1000 and displaying the results as a table in the GUI.  The event handlers will be virtually identical, with the exception of the boolean value passed to calculateCost, so you may find it useful to write another method to eliminate duplicate code.  I'll leave that one up to you to figure out.

You will use the TextPane for the output.  You will be building a string for the output.  Use characters "\t" for tab and "\n" for a line feed.  Each row should have the quantity and the cost separated by a tab.  You will probably find this line of code rather useful!

            outputTextPane.setText(outputTextPane.getText() + quantity + "\t" + cost  + "\n" );

The above line of code gets the previous contents of the output text pane and appends the quantity and the cost, separated by a tab.  The end of the line is a line feed so that each line of output ends up on a separate line in the output text pane.

Comments

In your comments at the top of the program, be sure to include the following information:

Cost of base computer
Cost of each peripheral
Comments are a very important part of your code so remember to include them in all your methods.


My problem is I am not sure how to create the two calculateCost methods. Are they separate methods with the same name? How does that work? Here is my code so far. (I know, it is not much).



 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much code have you actually written there? If any? Are you allowed to use the GUI builder on NetBeans?
You will have to decide how many methods there are to calculate the cost; it is after all your assignment. Start by writing down in words of one syllable (well, small bisyllables might be permissible) what those two methods are/that method is supposed to do. Remember they need to be indifferent classes from any of the display classes.

Why are you being taught to use applets? Nobody else uses them any more.
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The requirements explain what this is about:


Create two methods called calculateCost (to demonstrate overloading).  Both should return a double.

And then it goes on to explain what the different parameters are for the two different methods.
The key term here is 'overloading'.
 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am stuck on this method:

calculateCost:

Create two methods called calculateCost (to demonstrate overloading).  Both should return a double.

One should take no parameters and simply calculate the cost for ONE unit (base cost plus any selected peripherals).
The other should take two parameters (quantity and a boolean that indicates whether shipping should be included or not) and it should return the cost for the specified quantity, factoring in the quantity discount and shipping costs (if applicable).  It should use the other calculateCost method to get the cost for one unit.
Obviously, you will need to determine what a base computer costs and what the peripherals cost.

If shipping is to be included, costs are:

Ground is 5% of total order if less than 100 units (otherwise, free)
Air is 10% of total order
FedEx is 20% of total order


So far I have this in my code:



Am I on the right track?
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both should return a double.

Do both methods return double?
The other should take two parameters

Does one of your methods take two parameters?
 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NetBeans tells me that my returns in my first calculate cost are wrong but I am not sure what is wrong with them? Does anyone know what may be wrong?

 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the type in front of the variable name:
 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You don't need the type in front of the variable name:


What do you mean by the type?
 
Tim Moores
Bartender
Posts: 3128
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code currently has:

"double" is the type, and it's not necessary here (it's also invalid syntax).
 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed the double from the code, and it still is incorrect? What else could be wrong? I defined all the values for each of them above.
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please copy and paste the error text.  If you can't do that, type exactly what it says, at least the first few lines.
 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code:



And this is what NetBeans says is wrong with it:

 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
And this is what NetBeans says is wrong with it:



Errors such as something is expected, are syntax errors. Basically, something isn't valid Java.

Take a look at the line listed in the error. Is it valid Java? and if you need more context, then take a look at the code around line 241 of the MidTermProject.java file.

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Lexi Turgeon wrote:
And this is what NetBeans says is wrong with it:



Errors such as something is expected, are syntax errors. Basically, something isn't valid Java.

Take a look at the line listed in the error. Is it valid Java? and if you need more context, then take a look at the code around line 241 of the MidTermProject.java file.

Henry


I looked at the line and I can not for the life of me find what is wrong. Do you see it?
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
I looked at the line and I can not for the life of me find what is wrong. Do you see it?


In Java, an "if" statement takes a single boolean condition.... and also, although Java was based on C/C++ originally, it doesn't actually support the concept of a comma operator.

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

In Java, an "if" statement takes a single boolean condition.... and also, although Java was based on C/C++ originally, it doesn't actually support the concept of a comma operator.

Henry


How would I fix this? I tried making separate if statements but NetBeans is not even recognizing any of my radiobuttons.
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have more than one problem.  I see radio buttons declared as "air", "ground", and fedex", not airRadioButton or fedexRadioButton or groundRadioButton.
 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You may have more than one problem.  I see radio buttons declared as "air", "ground", and fedex", not airRadioButton or fedexRadioButton or groundRadioButton.


That fixed my problem within that method. My code is now:



NetBeans is telling me the computer and order in my calculateCost() method is incorrect. Any reason why? Here is the compile error.

 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So let's look at the error.  First it says it can't find a symbol.  Kind of cryptic, but then it shows the line of code with the problem:

So we know it's a problem in that line.  Then it says that the symbol is the variable computer in the call MidTermProject.  So is there a variable called "computer" in that class?  What should you do about it?
 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
So we know it's a problem in that line.  Then it says that the symbol is the variable computer in the call MidTermProject.  So is there a variable called "computer" in that class?  What should you do about it?


The variable computer is defined in the above method. That is why I am confused as to it not working?
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
The variable computer is defined in the above method. That is why I am confused as to it not working?


By "above method", do you mean the prices() method? There is indeed a variable "computer" in the prices() method. It is a parameter for the prices() method -- which is also a local variable.  And local variables can't be used outside of the block where they are declared. It is simply not available in the calculateCost() method.


... and ... trying not to sound like a broken record, here is the same recommendation that I gave in one of your previous topics, when you had the exact same issue...

Henry Wong wrote:
Lexi Turgeon wrote:
Henry Wong wrote:
Perhaps you should consider using variables with a bigger scope? Such as instance or static variables?

How would I do this?

The Java tutorial on variables, from Oracle, is a good place to start regarding the different variable types ... https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Henry
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a variable is declared in a method or as a parameter, it is called a local variable.  Local variables are only visible in the methods where they are declared.  To get the values of variables from one method to another, you need to either pass the variable in as an argument or declare the variable outside of any method, constructor, or initializer.  These variables are called class variables if they're static and instance variables if they're not.
 
Lexi Turgeon
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still confused as to how it works and what I did wrong, I looked at the website and it confused me more.
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The variable a can be seen inside of foo() because it was declared outside of any method.  Very broadly, a variable can only be seen inside the block it was declared in.  (Blocks are between braces.)  Since a was declared inside the class block but not any method block, it can be seen in all methods.

In your program, I think you're supposed to declare non-local variables here:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic