0

I am new to coding and I have a question in Java. Here is my homework problem: Write a program that calls a method called Sum100 that returns the sum of the integers from 1 to 100, inclusive. I have written my method

    public int sum100()
     {
        int sum = 0;
        for (int count=1; count <= 100; count++)
        sum += count;
        return sum;
     }

Here is my question: Can I print out my answer from here or do I need to put something in the main like this

    public static void main(String[] args) {

    System.out.println(sum100());//not working

}    
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28

6 Answers6

1

Your code works perfectly if you just change the sum100 method to be static:

public static int sum100()
 {
    int sum = 0;
    for (int count=1; count <= 100; count++)
    sum += count;
    return sum;
 }

If you need more information on the subject I would recommend the Java Tutorials section Understanding Class Members.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • Ok that worked, now it is saying that the return sum; has a confusing indentation – DragonAge99 Mar 07 '14 at 12:38
  • @AshwinMukhija can you please explain to us why static is considered bad, especially in this case ? – Christophe Roussy Mar 07 '14 at 12:40
  • @DragonAge99: I'm not familiar with that message. Is _it_ some kind of automatic grading tool? – Keppil Mar 07 '14 at 12:40
  • @ChristopheRoussy Oh boy. If you are not using Object Oriented concepts in Java by turning everything into statics, why even use Java? Statics break OO fundamentals, statics make testing harder by a magnitude and statics are okay ONLY to declare constants in Java. For any other purpose if you are using statics, there is always a better way. – Achrome Mar 07 '14 at 12:45
  • 2
    @AshwinMukhija: I strongly disagree with you here. Static methods definitely have a place in Java, and are completely ok to use in a lot of places. Good examples include the `Collections` and `Math` classes. – Keppil Mar 07 '14 at 12:48
  • @AshwinMukhija then please explain why Java itself uses static functions for things like `public static double sin(double a)` ? For a function computing a sum the input and output is clearly defined and a static function is perfect. – Christophe Roussy Mar 07 '14 at 13:03
0

You can sysout anywhere but I recommend you output outside the function as you may want to separate logging from the rest of the code (in case another part of the code wants to call this function but not log anything).

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
0

Main is static, and it cannot call another method which is non-static, it can reffer to an object, and not to a class.Create an object of your class and call the method using the object

class myclass{
public static void main(String[] args) {
 myclass obj=new myclass();
System.out.println(obj.sum100());//it will  working

}    
public int sum100()
   {
    int sum = 0;
    for (int count=1; count <= 100; count++)
    sum += count;
    return sum;
   }

 }
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
0

Your method main is static, and it cannot call another method which is non-static, only if it reffers to an object, and not to a class.

So therefore, you should either instantiate an object of your class, and call your non-static method of this object, either make your sum100 method static.

Ioan
  • 5,152
  • 3
  • 31
  • 50
0

Use as Below-It work for me.

class SumClass {
    public int sum100() {
        int sum = 0;
        for (int count = 1; count <= 100; count++)
            sum += count;
        return sum;
    }
    public static void main(String[] args) {
        SumClass obj = new SumClass();
        System.out.println("Sum->" + obj.sum100());

    }


}
Butani Vijay
  • 4,181
  • 2
  • 29
  • 61
0

class Yourclass{

public static void main(String[] args) {
    SumClass obj = new SumClass();
    int currrent_sum=obj.sun100();
    System.out.println("Sum of 100 numbers is: " +current_sum);

}
public int sum100() {
    int sum = 0;
    for (int count = 1; count <= 100; count++)
        sum += count;
    return sum;
}

}