Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
Like Tree1Likes

Thread: Add doubles in an array

  1. #21
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    Ok so if I understand this correctly, I need to add this in the method main of my driver class?
    Java Code:
    new ClassName().start();
    Then create a method called start in my checkout class?

  2. #22
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: Add doubles in an array

    No I will try to be more clear:

    Java Code:
    public SomeClass {
         double finalTotal[] = new double[10];
         public static void main(String[] args) {
                new SomeClass().start();
         }
         public void start() {
          // Now do here what you would normally do
          // in main.  Leave the rest of your
          // class alone.
         }
    }
    Think of it this way. You are basically trading a static method called main for a non-static method called start. But you won't get
    those nasty static context errors when you access your instance fields from start().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #23
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    Quote Originally Posted by jim829 View Post
    No I will try to be more clear:

    Java Code:
    public SomeClass {
         double finalTotal[] = new double[10];
         public static void main(String[] args) {
                new SomeClass().start();
         }
         public void start() {
          // Now do here what you would normally do
          // in main.  Leave the rest of your
          // class alone.
         }
    }
    Think of it this way. You are basically trading a static method called main for a non-static method called start. But you won't get
    those nasty static context errors when you access your instance fields from start().

    Regards,
    Jim
    I don't understand what its doing though, what do I've put in the start() method?
    This is so frustrating

  4. #24
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: Add doubles in an array

    Well, what do you have in your main method? Just move everything there except for the instantiation statement (as I said in the comment in the code snippet). Do you understand the difference between a static and non-static context? You may not access an instance field from a static context(i.e. inside a static method) without qualifying it with an instance of the class.

    Somewhere you said you were getting static context errors. I am trying to help you resolve them with the code you have shown me.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #25
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    I thought the method main was only suppose to have the new
    Java Code:
    SomeClass().start();
    No my teacher didn't teach them :\

  6. #26
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: Add doubles in an array

    Yes. new SomeClass.start(); That is doing an instantiation and calling the start method at the same time. Look at my code snippet and read the comments
    I put in the start method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #27
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    Quote Originally Posted by jim829 View Post
    Yes. new SomeClass.start(); That is doing an instantiation and calling the start method at the same time. Look at my code snippet and read the comments
    I put in the start method.

    Regards,
    Jim
    Oh ok so i moved everything out of my method main except for that line of code and everything else in now in the start method.
    I compiled it and its till giving me an error in the checkout class

  8. #28
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: Add doubles in an array

    Oh ok so i moved everything out of my method main except for that line of code and everything else in now in the start method.
    I compiled it and its till giving me an error in the checkout class
    And your Checkout class extends your restaurant class, and you moved the finalTotal to just above main. I don't know what is going on without seeing your code. I suggest you do an Short, Self Contained, Correct Example to demonstrate the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 12-20-2013, 08:17 PM
  2. Help adding doubles to an array
    By HalfAZN in forum New To Java
    Replies: 2
    Last Post: 10-18-2012, 08:47 PM
  3. Replies: 3
    Last Post: 06-18-2012, 09:44 AM
  4. Arithmetic using doubles
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 08:35 AM
  5. Cant use doubles
    By Freakzoyd in forum New To Java
    Replies: 27
    Last Post: 07-24-2010, 04:48 AM

Posting Permissions

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