RaisedToPower /* * Filename: "RaisedToPower.java" * Created by 1,000_naymes; for ICT 352 * Purpose: to gather values for x and n and then calculate the value of x raised to n */ import javax.swing.JOptionPane; public class RaisedToPower { public static void main( String[] args ) { /************************************************************** declare and initialize variables **************************************************************/ String openingMessage, xInputMessage, xString, xMessage, nInputMessage, nString, nMessage, resultMessage, resultInputMessage, resultString, choiceString, showResultMessage; double x, result; int n, choice; /************************************************************** display opening message **************************************************************/ openingMessage = "Welcome to 1KN's RaisedToPower program, which will ask you for two numbers, \n and then raise your first number to the second."; JOptionPane.showMessageDialog( null, openingMessage ); /************************************************************** input for first number (x) - a decimal number **************************************************************/ xInputMessage = "Please enter a value for x (a decimal number)."; xString = JOptionPane.showInputDialog( xInputMessage ); x = Double.parseDouble( xString ); //converting string to double /************************************************************** input for second number (n) - an integer between 1 and 10 **************************************************************/ nInputMessage = "Please enter a value for n (a whole number between 1 and 10)."; nString = JOptionPane.showInputDialog( nInputMessage ); n = Integer.parseInt( nString ); //converting string to double /************************************************************** calculating result Use a switch statement to calculate x raised to the n power (x^n). (Hint: Use Math.pow(x, n).) **************************************************************/ Page 1
RaisedToPower result = Math.pow( n, x ); resultInputMessage = "You entered: \n" + x + " raised to the " + n + ". \n \n Please enter 1 to view your result."; choiceString = JOptionPane.showInputDialog( resultInputMessage ); choice = Integer.parseInt( choiceString ); showResultMessage = "Your result is:
" + result;
switch( choice ) { case 1: JOptionPane.showMessageDialog( null, showResultMessage ); break; } System.exit(0); } //end main } //end class
Page 2