import java.awt.*; import javax.swing.*; /** * READ THE COMMENTS BELOW AND * INSERT THE NECESSARY CODE * * Ask the user to enter two (separate) integer values. * Display a chart showing the sum, difference, product, * (accurate!) quotient and modulo of these two numbers. * * THEN, by exploring the Math class display as many other * things as you can such as the square root of each number, * one number to the exponent of the other number, etc. * Google "java math api 6" and click on the first link. This will * bring you to the documentation for the Math class - you can do the * same kind of google search for any of Java's classes, such * as "Java Graphics" if you would like to learn more about * some of the methods that you can call from paint - some of * you have already been to this web site. (You may also start * typing Math. and a list of available methods will appear for * you in Eclipse. You could choose to read up on any of them * in the "Javadoc" frame at the bottom of your screen in Eclipse.) * * @author Miss Cianci & Mr. Emmell * @since Monday, September 15, 2008 (Revised for 2012) */ public class MathWithUserInput extends JApplet { // declare a global variable to store the user's first integer named num1 // declare a global variable to store the user's second integer named num2 /** * Ask the user to enter two numbers. * Store each number in the appropriate global variable as * each number will be needed by the paint method as well. */ public void init() { // Use a pop-up to ask for the user's first number and store // it in a temporary string variable called temp // Convert the string version of the user's first number into an // integer and store it in the global variable num1 // Use a pop-up to ask for the user's second number and store // it in the temporary string variable called temp // Convert the string version of the user's second number in to an // integer and store it in the global variable num2 } // end init method /** * Display a chart showing the sum, difference, product, * (accurate) quotient and modulo of these two numbers. * * Display other mathematical operations as well such as: * 5 ^ 2 = 25 * (if the user had entered 5 and 2). * * @param art Used to display on the applet. */ public void paint(Graphics art) { // calculate and display the sum of the two numbers int result = num1 + num2; art.drawString(num1 + " + " + num2 + " = " + result, ...); // calculate and display the difference of the two numbers result = art. drawString(...); } // end paint method /* * NOTE: You can also have pop-ups appear on your applet that do NOT ask for * user input and that simply display a message such as * "Welcome to my mini-calculator!" * * If you would like to do so experiment with: * JOptionPane.showMessageDialog(this, "TYPE YOUR MESSAGE HERE"); * * Just remember that ANY pop-ups must be created in your init method * and absolutely NOT in your paint method!!! */ } // end class
Quick Links
Admin