Results 1 to 3 of 3

Thread: can someone look at this script? JAVA

  1. #1
    Join Date
    Feb 2009
    Posts
    96

    can someone look at this script? JAVA

    import java.util.*;
    import javax.swing.JOptionPane;
    //create a class
    public class prices {

    //make the string input
    public static void main(String[] args){

    String Input;
    double[] numbers; // An array for storing the input values.
    int numCt; // The number of numbers saved in the array.
    double num; // One of the numbers input by the user.

    numbers = new double[4]; // Space for 100 ints.
    numCt = 0; // No numbers have been saved yet.

    Input = JOptionPane.showInputDialog(null, "Please enter a price.");

    while (numCt < numbers.length) { // Get the numbers and put them in the array.
    Input = JOptionPane.showInputDialog(null, "Please enter a price.");
    num = Double.parseDouble(Input);
    if (num <= 0)
    break;
    numbers[numCt] = num;
    numCt++;
    }
    sumArray(numbers);
    aveArray(numbers);
    highPrices(numbers);
    }

    public static void sumArray(double arr[])
    {

    double sum = 0; // Start the total sum at 0.
    for (int i=0; i<arr.length; i++) {
    sum = sum + arr[i]; // Add the next element to the total
    }
    JOptionPane.showMessageDialog(null,"The sum of the prices is " + sum);

    }

    public static void aveArray(double rad[]){
    double avg = 0;

    double sum = 0; // Start the total sum at 0.
    for (int i=0; i<rad.length; i++) {
    sum = sum + rad[i]; // Add the next element to the total
    avg = sum/rad.length;
    }
    JOptionPane.showMessageDialog(null,"The average of the prices is " + avg);

    }

    public static void highPrices(double rad[]){

    // double avg; // Start the total sum at 0.

    // for (int i=0; i<rad.length; i++) {
    // sum = sum + rad[i]; // Add the next element to the total
    // avg = sum/rad.length;
    // }
    JOptionPane.showMessageDialog(null,"ddd" + avg);





    System.exit(0);


    }

    }


    The high price array doesnt work --- i need it to recieve and average from the aveArray() and the Array numbers[] and retuen the numbers whose value is greater then the average to the main method.

    Can anyone point me in the right direction?

  2. #2
    Join Date
    May 2008
    Posts
    3,971

    Re: can someone look at this script? JAVA

    Did you received any error message talking about variable "sum"?

    I said so because you didn't declared "sum" in "public static void highPrices" function.

  3. #3
    Join Date
    Feb 2009
    Posts
    96

    Re: can someone look at this script? JAVA

    import java.util.*;
    import javax.swing.JOptionPane;
    //create a class
    public class prices {

    //make the string input
    public static void main(String[] args){

    String Input;
    double[] numbers; // An array for storing the input values.
    int numCt; // The number of numbers saved in the array.
    double num; // One of the numbers input by the user.

    numbers = new double[4]; // Space for 5 doubles.
    numCt = 0; // No numbers have been saved yet.

    Input = JOptionPane.showInputDialog(null, "Please enter a price.");

    while (numCt < numbers.length) { // Get the numbers and put them in the array.
    Input = JOptionPane.showInputDialog(null, "Please enter a price.");
    num = Double.parseDouble(Input);
    if (num <= 0)
    break;
    numbers[numCt] = num;
    numCt++;
    }
    sumArray(numbers);
    aveArray(numbers);
    //highPrices();
    }

    public static void sumArray(double arr[])
    {

    double sum = 0; // Start the total sum at 0.
    for (int i=0; i<arr.length; i++) {
    sum = sum + arr[i]; // Add the next element to the total
    }
    JOptionPane.showMessageDialog(null,"The sum of the prices is " + sum);

    }

    public static void aveArray(double rad[]){
    double avg = 0;

    double sum = 0; // Start the total sum at 0.
    for (int i=0; i<rad.length; i++) {
    sum = sum + rad[i]; // Add the next element to the total
    avg = sum/rad.length;
    }
    JOptionPane.showMessageDialog(null,"The average of the prices is " + avg);

    }

    public static double highPrices(double arr[]){

    double avg = 0;

    double sum = 0; // Start the total sum at 0.
    for (int i=0; i<rad.length; i++) {
    sum = sum + rad[i]; // Add the next element to the total
    avg = sum/rad.length;
    }
    int len;
    int a, b;
    numbers temp;
    double high = len - 1;
    for(a = 0;a <high; ++a)
    for(b = 0; b < high; ++b)
    if(numbers[b] >[b + 1])
    {
    temp = numbers[b];
    numbers[a] = numbers[b + 1];
    numbers[b+1] = temp;
    }

    JOptionPane.showMessageDialog(null,"ddd" + ave);





    System.exit(0);

    }

    }


    im having trouble with the highprices () it is supposed to take the ave and the array and display the numbers higher then the average.
    If anyone can show me how to do this it would be great.

    Thanx

Similar Threads

  1. Is there any future for java script?
    By Gag$nesh in forum Software Development
    Replies: 4
    Last Post: 28-05-2011, 07:58 PM
  2. In Java Script how to Check Null Value?
    By taher in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 03:18 AM
  3. What is the procedure to disable JAVA script?
    By Bharat89 in forum Windows Software
    Replies: 5
    Last Post: 09-12-2009, 02:28 PM
  4. Java Script Addition Problem
    By Magnus in forum Software Development
    Replies: 3
    Last Post: 01-06-2009, 11:38 AM
  5. How to Bypass Java Script
    By Dr.pter in forum Software Development
    Replies: 3
    Last Post: 19-03-2009, 11:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,242,874.18510 seconds with 16 queries