Results 1 to 14 of 14

Thread: Arrays

  1. #1
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default Arrays

    Hello, me again with yet another issue with Java syntax! I am about to give up with Java because I am really having an issue with grasping the syntax, but I'm not defeated yet!

    Basically this is what I need to do:

    Write a program that prompts a user for a list of 5 prices. The program is then to compute and display the sum of all prices, the average of the prices, and all prices that are higher than the calculated average. Additionally, I need to break my code out by using the following methods: sumArray, aveArray, and highPrices. Can anyone please give me a hand with this? It would be greatly apprciated!

    Heres what I have:
    Java Code:
    import javax.swing.*;
    public class PriceArray
    {
    	public static void main(String[] args)
    	{
    		String[] eventTypes = {"", "Pricesum", "Priceaverage", "Higherprices"};
    		String choicesString = "";
    		String strSelectedEvent;
    		int selectedEvent;
    		Event[] someEvent = new Event[5];
    		int x;
    		
    	//for loop that builds the Strung to be used as part of the user prompt//
    	for(x = 1; x < eventTypes.length; ++x)
    	choicesStrubg = choicesString + "\n" + x + " " + eventTypes[x];
    	
    	//for loop that executes one time for each Event object//
    	for(x = 0; x < someEvents.length; ++x)
    		{
    		strSelectedEvent = JOptionPane.showInputDialog(null, "Event #" + (x + 1) +
    		" Enter the number for the type of event you want" + choicesString);
    		selectedEvent = Integer.parseInt(strSelectedEvent);
    		if (selectedEvent < 1 || selectedEvent > 3)
    		seleectedEvent = 0;
    		someEvents[x] = new Event(selectedEvent);
    		}
    		System.exit(0);
    	}
    }
    I need a commented example to help me fully understand this. This forum has been great and I just want to say thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You've given us the assignment requirements but have not given us any specific questions. Also this:
    I need a commented example to help me fully understand this.
    almost sounds as if you are asking for someone to provide you with full solution code with comments, and I'm sure that's not what you have intended here.

    To get best results from this forum or any learning experience, you really should try to solve this yourself first -- try to create the methods, then if it doesn't work, come back with very specific questions regarding problems in your code. If you have no idea how to even get started then you should talk to your instructor about getting tutoring. Much luck.

  3. #3
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    almost sounds as if you are asking for someone to provide you with full solution code with comments, and I'm sure that's not what you have intended here.

    To get best results from this forum or any learning experience, you really should try to solve this yourself first -- try to create the methods, then if it doesn't work, come back with very specific questions regarding problems in your code. If you have no idea how to even get started then you should talk to your instructor about getting tutoring. Much luck.


    Fubarable,

    I do not want a hand out or anyone to do any assignment for me. If that was what was gathered from the portion of my post about commented examples, I apologize. To clarify that is not what I am requesting. I simply would like an example with comments so I can see where it is I am going wrong and I would like to compare it to the script I posted. As can be seen from the script I pasted in my post, I have tried to do it on my own - over the past 4 days. Its just not clicking for me with the more advanced stuff. I do know where to start, I just can not finish and this is where I need the help. As for speaking to my professor re: tutoring, yes that is a fine suggestion, only problem is my term is up in a week - no time for tutoring. If I want to continue to pursue java I will definitely use one.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Well, to start with, nothing make code harder to understand than bad formatting. Your code formatting is so-so, which might just be because it was posted onto the web. However, most IDE's have the ability to clean up your formatting for you. I suggest you copy/paste your code into NetBeans Welcome to NetBeans and use the Format command under the source menu.

    This will help you read your own code, which for me at least, really improved my understanding of structure. Once you can read structure easily, places that need comments become more obvious. Essentially, every class and method needs a description, and any method that contains more than a few lines of code really needs in-line comments.

    The point being: if someone else were to look at your code, you should be spelling out to them why you are doing each bit.

    Good Luck, hang in there!

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Also, once code is clean, it will be easier to see what is happening in the code, line by line. System.out.println() is your friend. Print things out in between lines of unintelligible code to get an understanding of what the code does.

  6. #6
    javaDuke21 is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Thumbs up Sample code

    Hope this example helps:
    ==================================================

    package PriceCalc;

    import javax.swing.*;

    public class calc {

    public static void main(String args[]) {
    //prompt user for amt of prices to be computed
    int amt = howManyprices();

    // declare variables and array
    int choice = -1;
    String option;
    double[] priceArray = new double[amt];
    double sum = 0;
    double avg = 0;
    int count = 1;
    //put prices into array

    for (int i = 0; i < priceArray.length; i++) {

    String inputString = JOptionPane.showInputDialog("Enter the price (xxx.xx format) of item #" + count);
    double priceInput = Double.parseDouble(inputString);
    priceArray[i] = priceInput;
    count++;
    }

    // user menu driven loop
    while (choice != 0) {
    try {
    // present options and get choice
    option = JOptionPane.showInputDialog(null,
    "Enter :\n1 -- to display the sum of all prices\n" +
    "2 -- to display the average of all prices\n" +
    "3 -- to display all prices that are higher than the avg of all prices\n" +
    "0 -- to quit");
    // convert to integer
    count = 0;
    choice = Integer.parseInt(option);

    // do options from user input
    switch (choice) {
    case 1:
    for (int i = 0; i < priceArray.length; i++) {
    double price = priceArray[i];

    sum += price;
    }
    JOptionPane.showMessageDialog(null, " The sum of all prices is: " + sum);
    break;

    case 2:
    for (int i = 0; i < priceArray.length; i++) {
    double price = priceArray[i];

    //sum += price;
    avg = sum / amt;
    }


    JOptionPane.showMessageDialog(null, " The average of all prices is: " + avg);
    break;

    case 3:
    for (int i = 0; i < priceArray.length; i++) {

    if (priceArray[i] > avg) {
    JOptionPane.showMessageDialog(null, "Price: $" + priceArray[i] + " IS HIGHER than avg of all prices." + "($" + avg + ")");
    }

    }

    break;

    case 0:
    JOptionPane.showMessageDialog(null, " Goodbye. ");

    break;

    default:
    System.out.println("Error");
    }
    } catch (Exception e) {
    System.out.println(e);
    JOptionPane.showMessageDialog(null, "Please enter a price with numbers and decimals only, no special characters or text (0 to quit)",
    "Error", JOptionPane.INFORMATION_MESSAGE);
    }
    }
    }

    static int howManyprices() {

    String numString = JOptionPane.showInputDialog("How many prices would you like to compute?");
    int num = Integer.parseInt(numString);

    return num;
    }
    }

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What a bad looking piece of code!
    And who formatted it? A firecracker?

  8. #8
    javaDuke21 is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Cool

    I put this together in about 30 minutes to help the person who started this thread. Being a 'senior member', you should know that the same functionality can be achieved in a number of different ways. While this is not the most ideal use of object-oriented programming, it does work. Format was fine in my IDE; it was changed when I pasted the code into the reply applet on this web site.

    This solution I provided satisfies the requirements given. The code complies and is completely functional.

    You are missing the point and should keep quiet unless you have something positive and constructive to contribute, thanks.

    Regards,
    A
    :eek:

  9. #9
    javaDuke21 is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    ****Correction to the above********

    *The code compiles and is completely functional.

    ~Booya

  10. #10
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    JavaDuke, it was posted earlier that well-formatted code would be helpful, and your code was not.

    In the future, use code tags [noparse]([code] and [/code])[noparse]. Placing them around your code preserves formatting and makes it much easier to read the code.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  11. #11
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by javaDuke21 View Post
    Hope this example helps:
    javaDuke - THANKS SO MUCH. This does help me out, immensely! I really appreciate the 30 minutes u spent helping me with this. The example does more than I really need it to do, but then again, I will just be using it to point me in the right way. Again, thank you!

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by javaDuke21 View Post
    ...

    You are missing the point and should keep quiet unless you have something positive and constructive to contribute, thanks.

    Regards,
    A
    :eek:
    You have absolutely zero control as to who posts what on forums.
    If you can't accept the fact that your "help" is misleading to newbies and can't accept corrections from people who know better then you will take longer to learn.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by javaDuke21 View Post
    Java Code:
    package PriceCalc;
    
    import javax.swing.*;
    
    public class calc {
    Your case is wrong: priceCalc, Calc.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Quote Originally Posted by javaDuke21 View Post
    I put this together in about 30 minutes to help the person who started this thread. Being a 'senior member', you should know that the same functionality can be achieved in a number of different ways. While this is not the most ideal use of object-oriented programming, it does work. Format was fine in my IDE; it was changed when I pasted the code into the reply applet on this web site.

    This solution I provided satisfies the requirements given. The code complies and is completely functional.

    You are missing the point and should keep quiet unless you have something positive and constructive to contribute, thanks.

    Regards,
    A
    :eek:
    Tell you what, I hope it's you that ends up having to correct the code of people you've spoonfed to the point that they haven't learnt as much as they need to to be able to actually do this as a job. Sadly I expect it'll be the likes of me who has to wade through the garbage...again...

    You're not helping either the OP or the rest of us, frankly.

Similar Threads

  1. Need help regarding Arrays Help pls....
    By shaggyoo7 in forum New To Java
    Replies: 3
    Last Post: 01-14-2009, 04:36 AM
  2. Need help with Arrays
    By dietgal in forum New To Java
    Replies: 21
    Last Post: 10-08-2008, 01:59 PM
  3. Help with Arrays
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 05:12 AM
  4. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 02:45 PM
  5. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 10:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •