Results 1 to 5 of 5
  1. #1
    Arete is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default In need of assistance!

    Hello all.

    I am completely confused over an assignment I am working on and hope I can be pointed in the right directions.

    For the assignment I have to write a program that prompts a user through a GUI to enter the name and weight of four objects. Then the code needs to calculate the weight of the objects on the Earth, Mercury, Moon and Jupiter. So:

    Mercury weight = Earth weight * .378
    Moon weight = Earth weight * .166
    Jupiter weight = Earth weight * 2.364

    I must us an array to store the object's weights and its names.

    It has been extremely slow going as I am completely new to the world of Java. I have been trying to go step-by-step with this. So far I have this:

    Java Code:
    package guiprogrampart1;
    import javax.swing.JOptionPane;
    
    public class GuiProgramPart1 {  
       Weight [] object;
        
        public static void main(String[] args) {
            
                    
                Weight [] object = new Weight[4];
                
                for (int i = 0; i <object.length; i++)
                {
                
                String ObjectName =
                        JOptionPane.showInputDialog( "What is the Objects name?" );
                String ObjectWeight =
                        JOptionPane.showInputDialog( "What is the Objects Weight?" );
        
                
                // display result in a JOptionPane message dialog
                JOptionPane.showMessageDialog( null, "The " + ObjectName + " weighs " + ObjectWeight + " on Earth",
                    "Weight of Object", JOptionPane.PLAIN_MESSAGE );
                }    
        }
    }
    So far I have managed to set it up where it at least comes up to prompt for a name then a weight, then displays the inputs 4 times for earth. Not even sure if that is how it is suppose to go. I would really like it to take the inputs and display all the results at the same time (totally not sure how to do that). What I'm confused about now is do I create a method in this class for calculation of the other planets? I am really not even sure where to go from here so any directions would be greatly appreciated.

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: In need of assistance!

    If you would like the output to all display like that I would recommend taking the Message Dialog code out of the FOR loop that you have set up. Since every time that it goes through that loop it will be outputting the Earth weight. Also, your prompts although correct are not being put into specific slots in your array. You are just simply assigning them to a string. You must assign it so that it is going into your array instead. Hope this helps a little to get you started again.
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    Arete is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: In need of assistance!

    Thank you Josh for your response. Now by removing MessageDialog out of the FOR loop I can only assume you basically mean moving the "}" above that line. When I do this the ObjectName and ObjectWeight are not being recognized (within the MessageDialog). Is there a method that I have to include with the MessageDialog in order to recognize these?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: In need of assistance!

    Why do you have the array? You don't seem to use it. You should be saving the user's input in those arrays.

  5. #5
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: In need of assistance!

    Also, are you trying to accomplish this using a plain array? Or were you trying to initiate an array of objects. Let me try to explain further... Do you want the objects name AND the objects weight to be stored in the same slot in the array? Or does it matter? Just trying to get some more info on what you want to do.
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. assistance needed
    By teardrop3903 in forum New To Java
    Replies: 5
    Last Post: 04-18-2011, 03:30 AM
  2. Assistance about this.
    By Syfer in forum Advanced Java
    Replies: 38
    Last Post: 06-21-2010, 05:59 PM
  3. Game assistance.
    By Sean_J in forum New To Java
    Replies: 2
    Last Post: 03-16-2010, 04:05 PM
  4. Looking for assistance
    By s_dawg101 in forum New To Java
    Replies: 32
    Last Post: 11-04-2009, 02:49 AM
  5. In need of some assistance
    By Boer84 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 04:14 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
  •