简体   繁体   中英

Java program without main method

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?

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.

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();
    }
}

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;
  }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM