Results 1 to 15 of 15
  1. #1
    Alejandro_T is offline Member
    Join Date
    Dec 2016
    Posts
    6
    Rep Power
    0

    Default Not being taught this in class, but expected finish... confused beyond belief.

    As discussed in Section 7.4 of the text book, when you run a Java program called Foo, anything typed on the command line
    after “java Foo” is passed to the main method in the args parameter as an array of strings.
    1. Write a program Average.java that just prints the strings that it is given at the command line, one per line. If nothing
    is given at the command line, print “No arguments”.
    2. Modify your program so that it assumes the arguments given at the command line are integers. If there are no
    arguments, print a message. If there is at least one argument, compute and print the average of the arguments. Note
    that you will need to use the parseInt method of the Integer class to extract integer values from the strings that are
    passed in. If any non-integer values are passed in, your program will produce an error, which is unavoidable at this
    point.
    3. Test your program thoroughly using different numbers of command line arguments.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    Have you done the first part?
    If not then what issue are you having?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    Just to get you started, a "startable" Java program has a public static method called "main". The only parameter that it has is an array of Strings. When you apply this theory to class Foo, the class would look like this:
    Java Code:
    public class Foo {
       public static void main(String[] args) {
    
       }
    }
    If you compile this, and run it using the command java Foo, as said in the textbook, the JVM will find the main method and run it, passing in all the arguments on the commandline.

    So, if you type: java Foo Hello I am SurfMan, the args array would be length 4 and contain elements "Hello", "I", "am", and "SurfMan".

    The rest of the exercise has to do with looping over the args-array and work with it, printing, parsing it, etc,
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    @OP, so far this is your second post and both times you say this hasn't been taught in class. Can
    you please explain what in you assignment is not being taught in class?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Alejandro_T is offline Member
    Join Date
    Dec 2016
    Posts
    6
    Rep Power
    0

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    coming up with argument arrays that allow me to compile the code.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    If you are getting errors you need help with, copy the full text of the error messages and paste it here so we can see them.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Alejandro_T is offline Member
    Join Date
    Dec 2016
    Posts
    6
    Rep Power
    0

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    I haven't gotten any errors because I haven't written a code. SurfMan provided me with a starting point, but I don't even understand why he did that or anything. I am not asking for answers, I simply just want a walkthrough as to how to do this because GA's here at school always have people so getting help from them is difficult.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    The args parameter in the main method in SurfMan's post is the one you need to print out all the parts of.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    Perhaps you should start at the beginning of the tutorial:
    Lesson: The "Hello World!" Application (The Java™ Tutorials > Getting Started)
    Start by copying the HelloWorld class and getting it to compile and execute.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    I posted that example to get you started and because we're a friendly bunch who are willing to help, but dont' spoonfeed..

    So here's your walkthough:
    - Create a file called Foo.java
    - Copy/paste that code I wrote in there
    - Add this line inside the main method: System.out.println("Hello World"); Case matters!.
    - Get that to compile.
    - Get that to run.

    Try to make it that far. If you made it here, congratulations, you can now focus on your assignment. Your "Average" class should be in a file called Average.java (See the similarities?). The main method looks the same, but inside the method, you need to do what your assignment wants you to do. What often helps, is to write pseudo code. That is code that won't compile, but gives you a good feeling of what to write. For example:
    Java Code:
    if there are no arguments then {
       print "No arguments"
    }
    Or more to code, but not quite:
    Java Code:
    if the length of the args-array is zero then
       print "No arguments"
    }
    In code, this translates to :
    Java Code:
    if (args.length == 0) {
        System.out.println("No arguments");
    }
    Now, go for it :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    I am still waiting to read "what" about the assignment is not being taught. Assuming you were in a carpentry
    class, here are several possibilities:

    1. They want me to build a box but I was not taught how to measure and saw wood or use a hammer.
    And now they want me to build a box. Help!!

    2. They taught me how to measure and saw wood and use a hammer.
    And now they want me to build a box. Help!!

    3. What's a hammer? What's a saw? What's a box? Help!!

    Regards,
    Jim
    Last edited by jim829; 12-07-2016 at 07:23 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Alejandro_T is offline Member
    Join Date
    Dec 2016
    Posts
    6
    Rep Power
    0

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    We have to obviously create this program, but we haven't been introduced to args parameters or parsing

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    Ok. To do step 1, you need to know the following:

    1. How to create a class
    2. How to add a static main entry point
    3. How to use the System.out.println method
    4. How to index an array
    5. How to compile the program
    6. How to run the program using the java command and passing arguments

    If you know the above, you should be able to do part 1. Once you do
    that, post the code and we can help you either debug or proceed to part 2.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    haven't been introduced to args parameters
    See the tutorial re commandline args" Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Not being taught this in class, but expected finish... confused beyond belief.

    Quote Originally Posted by Alejandro_T View Post
    We have to obviously create this program, but we haven't been introduced to args parameters or parsing
    But the first sentence explains exactly what they are and where they come from.
    So you should have no problem doing part 1.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 04-09-2016, 01:26 AM
  2. Confused about this one class
    By Shizzle in forum New To Java
    Replies: 4
    Last Post: 09-17-2012, 07:13 AM
  3. Replies: 3
    Last Post: 01-23-2012, 11:13 AM
  4. .class expected
    By dre in forum New To Java
    Replies: 6
    Last Post: 06-27-2009, 06:01 PM
  5. Replies: 20
    Last Post: 06-03-2009, 05:33 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
  •