tags:

views:

162

answers:

4

I am very new to Java. My assignment is to create my own method and then create a class to test it in. My question, do I create the method separate of the class, or within the class? If it is separate of the class, how do I get the class to access my method?

(Are they saved as two separate files?)

This is what I have so far, but I am getting an error that I have to initialize KILOWATT in class DWindmill. I thought I did already in the method??? Any suggestions?

//This is the method Windmill

import java.util.*;
import static java.lang.Math.*;

class DWindmill {



    public static void Windmill(){
    //create the method for the Windmill class

    int miles = 50;
    //int miles = 200;
    //int miles = 250;
    int KILOWATT = (miles / 50);}


    static Scanner console = new Scanner(System.in);
    {


    System.out.println("Enter miles per hour:");
    miles = console.nextInt();

    Windmill();

    System.out.println(+ KILOWATT + "kilowatts");

    }
}
+1  A: 

In Java, ALL methods exist within classes. So in order to create a class, you write something like:

public class MyClass {

   public static void Hello() {
      //This is your method!
   }

   public static void main (String[] args) {
       Hello();  //This is how you call your method.
   }
}
Dave Markle
+5  A: 

For a simple assignment such as this, you can probably create your method in the same class as your class. Create a class with a static main method, which will be your programs starting point, and then create your method which will be called.

Seems like you are quite new to programming I would take advantage of any tutorials that are offered in your program. They are usually taught by junior, senior, or grad level students, and are meant to give you a good introduction to the material, as well as give you time outside class to ask questions. Make sure you go to class, and try to read the textbook you were supposed to buy for the course. The information can often be found there.

Kibbee
+1  A: 

OR you can create like follows

public class MyClass {
  public int myMethod() {
    ,,,,,
  }
}

public class myTest {
  public void testMyMethod() {
   MyClass testClass = new MyClass();
   int output = testClass.myMethod();
 . 
. 
  }
}

In Java, all methods need to be inside a class. You can have a separate test class or test it in the same class.

Things can get more complicated if you use something like jUnit(www.junit.org) for unit testing your methods.

anjanb
+3  A: 

please go to class or read a textbook or something because your code illustrates a fundamental misunderstanding of what a class is, what a method is, and how to use braces for code blocks. Here is a corrected (but untested) version of your code -

class Windmill
{
    public static void Main()
    {
        Scanner console = new Scanner(System.in);
        System.out.println("Enter miles per hour:");
        int miles = console.nextInt();
        int KILOWATT = (miles / 50);
        System.out.println(KILOWATT + " kilowatts");
    }
}

seriously, anything should be helpful at this point

Steven A. Lowe