0

For my class I need to write code that adds up the numbers 1-100 which will produce the number 5050. He told us not to use a main method and use a for loop. Is a main method 'public static void main (String[] args)' or something else. I am just still very confused on what exactly the main method is. Here is the code I have so far that works

public class SumAndAverageForLoop{
    public static void main (String[] args) {
        int sum = 0; 
        for (int i = 1; i <= 100; i++) sum += i; 
        System.out.println("The sum is " + sum);
    }
}

From what I read more and after talking to some other kids in the class they said I need to make another class with a main method that calls this class but how do you call one class from another?

How to call one class from another that has a main method?

3
  • In your code, yes, you have that "main method"; the JDK requires it to be able to run a program. The fact that you use a for loop or not is irrelevant to the fact that you use it in a main() or not. I don't really understand what your teacher is up to. Commented Feb 6, 2015 at 23:24
  • The requirement to not use a main method is really weird. But then again, some teachers are lame. So, screw it: go ahead and write a main method, and invoke your AddOneToOneHundred method from it, to make sure it works, and then remove the main method before turning in the assignment. Commented Feb 6, 2015 at 23:28
  • possible duplicate of Printing message on Console without using main() method (but note that this will not work in versions 7 and above) Commented Feb 6, 2015 at 23:37

5 Answers 5

2

Yes the public static void main(String[] args) signature is the main method.

It sounds like you're writing library style code. So just create a method with a meaningful name for what your code does. Like AddOnetoOneHundred or similar.

You can create a method like this:

public static int CountUp() {
    // code that was in your main method before
}

Double check your assignment, your teaching might have specified a class and method name and already has a program that will test your code.

Sign up to request clarification or add additional context in comments.

1 Comment

How exactly do you create a method?
1

A main method is fundamental to any given program as the Java compiler searches for the method as a starting point of execution. However, you are trying to make a utility class so:

class SumAndAverageForLoop {

    // no main method

    public static int sum() {
        int sum = 0; 
        for (int i = 1; i <= 100; i++) sum += i; 
        System.out.println("The sum is " + sum);
    }
}

class MainClassProgram {

    // main in another class

    public static void main() {
        SumAndAverageForLoop.sum();
    }
}

Comments

0

Try to create a method to do the calculation. The idea is create code that is unit testable.

public class SumAndAverageForLoop{
  public static void main (String[] args) {
      int returned = SumUp(1, 100);
      System.Out.Println("sum is " + returned);
  }
  public int SumUp(int startInt, int endInt) {
      int sum = 0;
      if (endInt > startInt) {
         for (int i = startInt; i <= endInt; i++) sum += i; 
      }
      return sum;
  }
}

Comments

0

Short Answer:

Yes, that (public static void main (String[] args)) is the main method.

Long Answer:

The main method is the entry point for an application. Without it, you may have code, but that code must somehow link to a main method or it cannot be ran. Counter-intuitively, the main method doesn't actually have to be a method. In C, you can create an integer array called main and execute it. It will translate the integers into hexadecimal and execute a corresponding Assembly command for each one, iteratively.

If you do what the professor says, you will not be able to test the program as an executable. He probably has a program made to run your code and test it for him, which is why he doesn't want a main method.

Comments

0

I'm not sure by what your teacher means by, "not using a main method". But you can declare a method outside of you main method and call it from there;

public class SumAndAverageForLoop{
    public static void main (String[] args) {
        makeSum();
    }

    public static void makeSum() {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        System.out.println(sum);
    }
}

Also, since one of your comments asked "How to create a method?".

I suggest you read up on some books for beginners. I'd recommend the book Head First Java.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.