Modify the final version of the percentFat program so that each panel has a different background color. You can do this using the setBackground() method with each panel. Try various colors such as Color.red, Color.yellow, and Color.green
Click here to go back to the main menu.Modify the final version of the percentFat program so that when the user clicks on the doit button, the program checks if there is text in each text field before attempting to use parseInt(). If either or both fields are empty, the program does nothing.
You will need to check if a String is empty by using
userIn.equals("").
Improved version: when the button is clicked, check the text fields as above, but now when an empty field is detected, change the background color of the field's panel to red (to show that it is in error.) Set the background of non-empty fields to white (so previous errors are cleared.) Alternatively, you can use setText() with the empty textfield to write an error message into it.
This will be more work than the previous exercises.
Modify the fat calculator so it works as follows:
Start out the program with the doIt button disabled.
This is done by using doit.setEnabled( false );
This method will dim the image of the button and disable it
from responding to user clicks.
Enable the button using doit.setEnabled( true )
only after both text fields contain text.
To do this each text field needs to register the object as a listener. Modify actionPerformed( ActionEvent evt ) so that it can handle events coming from the text fields as well as from the button. Use getText() inside actionPerformed() to check if both text fields contain text. If they do, enable the Button.
A disadvantage of this version is that the user must hit the "enter" key after putting text into a text field.
Click here to go back to the main menu.Modify the percentage fat application so that it accepts input text that contains a decimal point. For example, the user might enter "6.5" for grams of fat and "230.9" as calories. To do this, review chapter 11 on floating point input. You will have to do something like:
Click here to go back to the main menu.tempDouble = Double.valueOf( userIn ) ; value = tempDouble.doubleValue() ;
The formula that relates the pressure of a gas (P) to its volume (V) and its temperature (T) is:
wherePV = nRT
Write an application with a GUI that lets the user enter V, n, and T (in floating point) and then calculates P. Test: one mole of gas occupying a volume of 22.414 liters at a temperature of 273.15 K should have a pressure of one atmosphere. Click here to go back to the main menu.P is the pressure in atmospheres V is the volume in liters n is the number of moles R is the universal gas constant of 0.082057 T is the temperature in degrees Kelvin
The solutions for the equation
areax2 + bx + c = 0
Write an application with GUI that allows the user to enter a, b, and c and then writes out both solutions, if they exist. Not all equations have solutions in the real numbers. Ifx = ( -b + Sqrt( b2 - 4ac ) ) / 2a, and x = ( -b - Sqrt( b2 - 4ac ) ) / 2a
b2 - 4ac is negative, don't perform the calculation
(maybe write "*******" in both result fields.)
Also,
if "a" is zero the formula does not apply, so also indicate an error.
You will need to use Math.sqrt() to calculate the square root.
Click here to go back to the main menu.