Hi, i am having trouble with an assignment. i need to create an array, store numbers in the array... sqaure, cube and square root those numbers individually using different methods. display the resuts with a dialog box. after that i need to find the sum of the numbers in the array and the average, and also display that with a dialog box. when that is done i need to ask the user if they want to play again, if they answer yes, to restart the program.

the problem i am having is creating a void method that finds the sum and average of the numbers in the array. method must be a void for this task. (my lecturer said to use a for loop but i can't get it to work)

``````/**
* Write a description of class sqaurerootCube here.
*
* @version (a version number or a date)
*/

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class sqaurerootCube
{

public static void main (String[]args)
{

DecimalFormat fmt   = new DecimalFormat ("0.##");
int num = 0;
do
{
String number = JOptionPane.showInputDialog("How many numbers do you want to enter?");
try
{
num = Integer.parseInt(number);
}
catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog (null, "That is not an integer");
}
}
while(num <0 );

String num1;
double[] numbers ;
numbers = new double[num];
for(int count = 0; count <= (num-1); count++)
{
num1 = JOptionPane.showInputDialog("Enter number "+ (count+1) + " between 25 and 150");
numbers[count]= Integer.parseInt(num1);
}

for(int count = 0; count<= (num-1); count++)
{
JOptionPane.showMessageDialog (null, "number " + (count+1)+ " sqaured = " + calcSqaure(numbers[count])+ "\nnumber " + (count+1)+ " cubed = "+ calcCube(numbers[count]) + "\nsqaure root of number "+(count+1)+" = "+ fmt.format(calcSqaureRoot(numbers[count])));
}

}

public static double calcSqaure(double x)
{
return Math.pow (x,2);
}

public static double calcCube(double x)
{
return Math.pow (x,3);
}

public static double calcSqaureRoot(double x)
{
return Math.pow (x,0.5);
}

public static void  average()

{

}

}``````
2
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by bokz06

ok guys i figured out my problems in the above post, but now i have 2 more.

1. the program keeps displaying the error message even if i enter a number greater then 25 or less then 150... how do i fix it to only pop up when number is less then 25 or greater then 150??

2.
replay string is suppose to restart the program if the user choses yes.. can you guys show me how to do that please?

``````/**
* Write a description of class sqaurerootCube here.
*
* @version (a version number or a date)
*/

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class sqaurerootCube
{

public static void main (String[]args)
{

DecimalFormat fmt   = new DecimalFormat ("0.##");
int num = 0;

do
{
String number = JOptionPane.showInputDialog("How many numbers do you want to enter?");
try
{
num = Integer.parseInt(number);
}
catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog (null, "That is not an integer");
}

}
while(num <0);

String num1;
double[] numbers ;
numbers = new double[num];
for(int count = 0; count <= (num-1); count++)
{
num1 = JOptionPane.showInputDialog("Enter number "+ (count+1) + " between 25 and 150");
double fig = Double.parseDouble(num1);
if(fig >= 25.00 && fig <= 150)
numbers[count]= fig;

else
count = count-1;
JOptionPane.showMessageDialog (null, "error with number, make sure it is between 25 and 150");        }

for(int count = 0; count<= (num-1) && numbers[count] >= 25 && numbers[count] <=150; count++)
{
JOptionPane.showMessageDialog (null,   numbers[count]+ " sqaured = " + calcSqaure(numbers[count])+ "\n" + numbers[count]+ " cubed = "+ calcCube(numbers[count]) + "\nsqaure root of "+numbers[count]+" = "+ fmt.format(calcSqaureRoot(numbers[count])));
}
if(num> 0 && numbers[0] >=25 )
{
average(numbers);
}

String replay = JOptionPane.showInputDialog("Do you want to play again?(y/n)");

}

public static double calcSqaure(double x)
{
return Math.pow (x,2);
}

public static double calcCube(double x)
{
return Math.pow (x,3);
}

public static double calcSqaureRoot(double x)
{
return Math.pow (x,0.5);
}

public static void average(double[] numbers)

{

DecimalFormat fmt   = new DecimalFormat ("0.00");
double sum = 0.00;
for (int count = 0; count < numbers.length; count++)
sum = sum + numbers[count];
double avg = sum/numbers.length;
JOptionPane.showMessageDialog(null, "Total = " + fmt.format(sum) + "\nAverage = " + fmt.format(avg) );

}
}``````

Hey :D

``````import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Demo extends JFrame {

public Demo(){

setSize(500, 500);
setTitle("Hello World");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

do{

methodToCall();

}while(JOptionPane.showConfirmDialog(this, "Do you wan to play again ?") == JOptionPane.YES_OPTION);

}

public void methodToCall(){
JOptionPane.showInputDialog(this,"Input numbers:");
}

public static void main(String[] args) {
new Demo();
}
}``````

Regards,

hey mate, thanks for reply but i sorted those problems out, although your method looks more smarter i gotta stick to the assignment sheet, we aren't up to window boxes and graphics yet. lol

umm have a look at my second post and see if you can help me with problem #2. please coz i just fixed the first one :)

hey again,

check now..

``````/**
* Write a description of class sqaurerootCube here.
*
* @version (a version number or a date)
*/

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class squarerootCube
{

public static void main(String[] args) {
do{

doCalculate();

}while(JOptionPane.showConfirmDialog(null,"Do you wan to play again ?") == JOptionPane.YES_OPTION);

}

public static void doCalculate()
{

DecimalFormat fmt   = new DecimalFormat ("0.##");
int num = 0;
do
{
try{
num = Integer.parseInt(JOptionPane.showInputDialog("How many numbers do you want to enter?"));
}
catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog (null, "That is not an integer");
}
}while(num <0);

double[] numbers = new double[num];
for(int count = 0; count <= (num-1); count++)
{
double fig = Double.parseDouble(JOptionPane.showInputDialog("Enter number "+ (count+1) + " between 25 and 150"));
if(fig >= 25 && fig <= 150){
numbers[count]= fig;
}else{
count = count-1;
JOptionPane.showMessageDialog (null, "error with number, make sure it is between 25 and 150");
}
}

for(int count = 0; count<= (num-1); count++)
{
JOptionPane.showMessageDialog (null,   numbers[count]+ " sqaured = " + calcSqaure(numbers[count])+ "\n" + numbers[count]+ " cubed = "+ calcCube(numbers[count]) + "\nsqaure root of "+numbers[count]+" = "+ fmt.format(calcSqaureRoot(numbers[count])));
}
if(num> 0 && numbers[0] >=25 )
{
average(numbers);
}
}

public static double calcSqaure(double x)
{
return Math.pow (x,2);
}

public static double calcCube(double x)
{
return Math.pow (x,3);
}

public static double calcSqaureRoot(double x)
{
return Math.pow (x,0.5);
}

public static void average(double[] numbers)

{

DecimalFormat fmt   = new DecimalFormat ("0.00");
double sum = 0.00;
for (int count = 0; count < numbers.length; count++)
sum = sum + numbers[count];
double avg = sum/numbers.length;
JOptionPane.showMessageDialog(null, "Total = " + fmt.format(sum) + "\nAverage = " + fmt.format(avg) );

}
}``````

Regards,

hey mate, yeh that works perfectly, is there anyway to make it do the same thing but with just a normal showInputDialog box... not a click one, but a one where u type either y or n, because if my lecturer asks me how it works, i wouldn't know ahha.

hey again,

change the loop in main method to :

``````do{

doCalculate();

}while(JOptionPane.showInputDialog(null,"Do you want to play again ? (y/n)").toUpperCase() == "Y");``````

If y or Y is entered, toUpperCase() method will change it to 'Y' that means User want to continue else not.

Regards,

nope that doesnt work :s, program just exits with whatever u put in..

hey sorry my mistake :P

``````do{

doCalculate();

}while(JOptionPane.showInputDialog(null,"Do you want to play again ? (y/n)").equalsIgnoreCase("Y"));``````

tested and working perfectly.

Regards,

thanks mate, you are legendary :D