this is my first program using more than one method. while i like it because of its modularity and cleanness, i despise it for its multiple new problems it introduces into my world where i was just getting the hang of the old style. i am supposed to be doing a program that calculates a persons gravity on all 9 planets. if i am really far out on some of the things that i am trying to do, please let me know. my familiarity level with all of this is pretty low.

public class WeightOnPlanetsV1
{
    
        
    
   
   public static double [] gravity()throws IOException
    {       
        
        
        int counter = 0;
        double [] gravity = new double[9];
        File fileName = new File("surfaceGravity.txt");
        Scanner inFile = new Scanner(fileName);
        while (inFile.hasNext())
        {
            gravity[counter] = inFile.nextDouble();
            counter++;
        }
        inFile.close();
        return gravity;
    }

   
    
    public static int [] pounds(double[]gravity, double []weightPounds)
    {
        for (int counter = 0; counter <= 9; counter++)
        {
            weightPounds[counter] = (140) * gravity[counter];
        }
        
        return weightPounds;
    }
    
    
    public static double [] grams(double [] weightGrams, int [] weightPounds)
    {
        for (int counter = 0; gravity.length; counter++)
        {
        
             weightGrams[counter]  = weightPounds[weightPounds] * 453.59237;
        }
        
        return weightGrams;
    }
    
    public static double mass(double [] mass, double [] weightGrams, double [] gravity)
    {
        for (int counter = 0; gravity.length; counter++)
        {
            return  mass[counter] = (weightGrams[counter] * (433.59237) / gravity[counter]);
        }
    }

        
   public static void main(String[] args)
   {
       
       
       
       
    }
    


}

I'm not a big expert in astronomy, so I can't really help you there, but obviously you will need to put something in the main method. Try calling the functions in the main method to see if they are working correctly.

well, i cant do that as of now because i am getting this error: incompatible types. its on line 33. i dont really know if there is something i can change there. oh yea, i know i have to put something in there. im just trying to get everything else working first.

The error on line 33 is because you are trying to return a double value in a method thats declared as int.

Your for loop in your grams and mass method need a conditional, like counter < gravity.length

In your grams method you dont have gravity as an @parem.

In you pounds method, you return an double but the method is an int.

For your Exception you need to import the java.io.*;

And the "weightPounds[weightPounds] " weightPounds is a double so added counter within the brackets instead of weightPounds. So weightPounds[counter] will give you the array index.

Thats what I spotted out from reading your code.

alright. i have gotten the code to the point to where it will compile. what would be the best way for me to test and see if everything is working correctly?

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class WeightOnPlanetsV1
{
    
        
    
   
   public static double [] gravity()throws IOException
    {       
        
        
        int counter = 0;
        double [] gravity = new double[9];
        File fileName = new File("surfaceGravity.txt");
        Scanner inFile = new Scanner(fileName);
        while (inFile.hasNext())
        {
            gravity[counter] = inFile.nextDouble();
            counter++;
        }
        inFile.close();
        return gravity;
    }

   
    
    public static double [] pounds(double[]gravity, double []weightPounds)
    {
        for (int counter = 0; counter <= 9; counter++)
        {
            weightPounds[counter] = (140) * gravity[counter];
        }
        
        return weightPounds;
    }
    
    
    public static double [] grams(double [] weightGrams, int [] weightPounds)
    {
        for (int counter = 0; counter <= 9; counter++)
        {
        
             weightGrams[counter]  = weightPounds[counter] * 453.59237;
        }
        
        return weightGrams;
    }
    
    public static double [] mass(double [] mass, double [] weightGrams, double [] gravity)
    {
        for (int counter = 0; counter<= 9; counter++)
        {
            mass[counter] = (weightGrams[counter] * (433.59237) / gravity[counter]);
        }
        
        return mass;
    }

Create variables of appropriate data types and pass them as parameters in the functions. Print the value returned by the function call. I'd do this in the main method.

alright, i have tried to get as far as i can. im pretty stuck now. if anyone has any advice im very open lol. thanks in advance you guys.

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class WeightOnPlanetsV1
{
    
                           
   //reads the gravity for each planet from the file surfaceGravity.txt
   public static double [] gravity()throws IOException
    {       
        
        
        int counter = 0;
        double [] gravity = new double[9];
        File fileName = new File("surfaceGravity.txt");
        Scanner inFile = new Scanner(fileName);
        while (inFile.hasNext())
        {
            gravity[counter] = inFile.nextDouble();
            counter++;
        }
        inFile.close();
        return gravity;
    }
    


   
    //determines the weight in pounds of someone on each planet
    public static double [] pounds(double[]gravity, double []weightPounds, double []mass)
    {
        for (int counter = 0; counter <= 9; counter++)
        {
            weightPounds[counter] = mass[counter] * gravity[counter];
        }
        
        return weightPounds;
    }
    
   
    
    //determines the mass of person on each planet
    public static double [] mass(double [] weightPounds, double [] mass, double [] weightGrams, double [] gravity)
    {
        for (int counter = 0; counter<= 9; counter++)
        {
            mass[counter] = (weightPounds[counter] * 433.59237)  / (gravity[counter]);
        }
        
        return mass;
    }
    

    
    //prints the results
    public static void print(double [] gravity, double [] weightPounds, String [] planetNames)
    {
        System.out.println("      My Weight on the Planets");
        System.out.println("Planet    Gravity     Weight(lbs)");
        for (int counter = 0; counter<= 9; counter ++)
        {
            System.out.println(planetNames[counter]);
            System.out.printf("        %4.1f  ",gravity[counter]);
            System.out.printf("        %4.1f  ",weightPounds[counter]);
            
        }
       
    }
    

        
   public static void main(String[] args)throws IOException
{
    String [] planetNames = new String[9];
        
        planetNames[0] = "Mercury"; 
        planetNames[1] = "Venus";
        planetNames[2] = "Earth";
        planetNames[3] = "Mars";
        planetNames[4] = "Jupiter"; 
        planetNames[5] = "Saturn"; 
        planetNames[6] = "Uranus";
        planetNames[7] = "Neptune";
        planetNames[8] = "Pluto";
        
        print(double [] gravity, double [] weightPounds, String [] planetNames);

im getting a .class expected on my the print statement inside my main method.

your missing two ending brackets

public static void main(String[] args)throws IOException
{
    String [] planetNames = new String[9];
        
        planetNames[0] = "Mercury"; 
        planetNames[1] = "Venus";
        planetNames[2] = "Earth";
        planetNames[3] = "Mars";
        planetNames[4] = "Jupiter"; 
        planetNames[5] = "Saturn"; 
        planetNames[6] = "Uranus";
        planetNames[7] = "Neptune";
        planetNames[8] = "Pluto";
        
        print(double [] gravity, double [] weightPounds, String [] planetNames);

[B]}
}[/B]

Your main method wont do anything, just some errors. It would be easier to have your main in a Driver Class and keep WeightOnPlanetsV1 as a separate Class

i really dont know how to do naything besides what i can interpret from examples given with the instructions. basically what i think i am supposed to do is have alot of methods that do each task, have a method that prints my output, and then call all the methods in my main method. i think i have everything except calling all the methods in my main method, which i have no idea how to do.

alright. im aware that this may be the dumbest thing ever attempted, but is this kind of what you mean? i read the link, but it got a little over my head.

public static void main(String[] args, double [] gravity, double [] weightPounds, String [] planetNames, print)throws IOException
{
    String [] planetNames = new String[9];
        
        planetNames[0] = "Mercury"; 
        planetNames[1] = "Venus";
        planetNames[2] = "Earth";
        planetNames[3] = "Mars";
        planetNames[4] = "Jupiter"; 
        planetNames[5] = "Saturn"; 
        planetNames[6] = "Uranus";
        planetNames[7] = "Neptune";
        planetNames[8] = "Pluto";
        
        gravity();
        weightPounds();
        print();
        
        
        

}
}

Well thats a step in the right direction. You need to create variabes that correspond to the parameters defined in the method definitions. Then pass those parameters in the method call.
Here is how a method call generally looks

name_of_method(parameters separated by commas);

Not every method needs parameters, but in your case, all of your methods do.

Edited 7 Years Ago by Grn Xtrm: n/a

alright, just another quick question, would the method call contain the same peramaters as the method itself?

i have given this my best effort, but this is the best i can come up with, between examples provided and such. im getting an error that a class is needed so obviously it is incorrect.

public class WeightOnPlanetsV1{
    
                           
   //reads the gravity for each planet from the file surfaceGravity.txt
   public static double [] gravity()throws IOException{       
        
        
        int counter = 0;
        double [] gravity = new double[9];
        File fileName = new File("surfaceGravity.txt");
        Scanner inFile = new Scanner(fileName);
        while (inFile.hasNext()){
            gravity[counter] = inFile.nextDouble();
            counter++;
        }
        inFile.close();
        return gravity;
    }
    


   
    //determines the weight in pounds of someone on each planet
    public static double [] pounds(double[]gravity, double [] weightPounds, double[] mass){
        for (int counter = 0; counter <= 9; counter++){
            weightPounds[counter] = mass[counter] * gravity[counter];
        }
        
        return weightPounds;
    }
    
   
    
    //determines the mass of person on each planet
    public static double [] mass(double [] weightPounds, double [] mass, double [] weightGrams, double [] gravity){
        for (int counter = 0; counter<= 9; counter++){
            mass[counter] = (weightPounds[counter] * 433.59237)  / (gravity[counter]);
        }
        
        return mass;
    }
    

    
    //prints the results
    public static void print(double [] gravity, double [] weightPounds, String [] planetNames){
        System.out.println("      My Weight on the Planets");
        System.out.println("Planet    Gravity     Weight(lbs)");
        for (int counter = 0; counter<= 9; counter ++){
            System.out.println(planetNames[counter]);
            System.out.printf("        %4.1f  ",gravity[counter]);
            System.out.printf("                %4.1f  ",weightPounds[counter]);
             }
       }
    

        
   public static void main()throws IOException
{
    String [] planetNames = new String[9];
        
        planetNames[0] = "Mercury"; 
        planetNames[1] = "Venus";
        planetNames[2] = "Earth";
        planetNames[3] = "Mars";
        planetNames[4] = "Jupiter"; 
        planetNames[5] = "Saturn"; 
        planetNames[6] = "Uranus";
        planetNames[7] = "Neptune";
        planetNames[8] = "Pluto";
        
        gravity = gravity();
        int pounds = pounds(gravity[], weightPounds[], mass[]);
        double mass = mass(weightPounds[], mass[],weightGrams[], gravity[]);
        print(gravity, weightPounds, planetNames);
        }
}

i suppose i might have to shoot my teacher an email for assistance, because i dont think i am understanding something.

<snipped> Yes Mikeyp926 is correct. Sorry for that post.

Edited 7 Years Ago by Grn Xtrm: n/a

Ok, I'll throw out a couple of tips to try and straighten things out.

First, I think his method declarations are fine, he's not declaring the method as an array, he's declaring the return type as an array, which is perfectly fine. However, you need to be careful about the return types. For your "gravity()" method, it returns an array, so you need to store it in an array. Try something like "double[] gravity = gravity();"

Also, when you pass parameters into methods you must first declare those variables. Similarly to how you created your planets array you will need to create "weightPounds[]","mass[]","weightGrams[]" etc.
You'll need to do that somewhere in your main method before you call the other methods.

Does that make sense? If you keep getting errors post them here, they are pretty frustrating to try and solve when you are just starting out.

-Michael

i have been playing with it and the only thing that i can get to wrok correctly is this:

double[] gravity = gravity();

anytime i try to call a method that has parameters it just tells me that there is a .class expected. it is boggling my mind. i have tried passing these parameters any way possible.

I copied your code and ran it and got the exact same errors. The reason you are getting these errors is that you have not initialized the variables that you are passing to your methods. To fix it try something like this code in your main method:

double gravity[] = gravity();
        double[] weightPounds = new double[9];
        double[] mass = new double[9];
        double[] weightGrams = new double[9];
        weightPounds = pounds(gravity, weightPounds, mass);
        mass = mass(weightPounds, mass,weightGrams, gravity);
        print(gravity, weightPounds, planetNames);

This should at least get rid of the compile errors. I'm not sure what you actually want to do with those arrays, and you'll probably have to change this code, but maybe your program will at least run now.
Did that help?

I copied your code and ran it and got the exact same errors. The reason you are getting these errors is that you have not initialized the variables that you are passing to your methods. To fix it try something like this code in your main method:

double gravity[] = gravity();
        double[] weightPounds = new double[9];
        double[] mass = new double[9];
        double[] weightGrams = new double[9];
        weightPounds = pounds(gravity, weightPounds, mass);
        mass = mass(weightPounds, mass,weightGrams, gravity);
        print(gravity, weightPounds, planetNames);

This should at least get rid of the compile errors. I'm not sure what you actually want to do with those arrays, and you'll probably have to change this code, but maybe your program will at least run now.
Did that help?


Edit: Notice that when passing the arguments we just pass the name of the variable, we don't include the brackets. It took me a little while to notice that.

Edited 7 Years Ago by Mikeyp926: n/a

Comments
life saver. great poster.

I copied your code and ran it and got the exact same errors. The reason you are getting these errors is that you have not initialized the variables that you are passing to your methods. To fix it try something like this code in your main method:

double gravity[] = gravity();
        double[] weightPounds = new double[9];
        double[] mass = new double[9];
        double[] weightGrams = new double[9];
        weightPounds = pounds(gravity, weightPounds, mass);
        mass = mass(weightPounds, mass,weightGrams, gravity);
        print(gravity, weightPounds, planetNames);

This should at least get rid of the compile errors. I'm not sure what you actually want to do with those arrays, and you'll probably have to change this code, but maybe your program will at least run now.
Did that help?


Edit: Notice that when passing the arguments we just pass the name of the variable, we don't include the brackets. It took me a little while to notice that.

i have edited everything to work like so

public static void main()throws IOException
{
    String [] planetNames = new String[9];
        
        planetNames[0] = "Mercury"; 
        planetNames[1] = "Venus";
        planetNames[2] = "Earth";
        planetNames[3] = "Mars";
        planetNames[4] = "Jupiter"; 
        planetNames[5] = "Saturn"; 
        planetNames[6] = "Uranus";
        planetNames[7] = "Neptune";
        planetNames[8] = "Pluto";
        
           
      double gravity[] = gravity();
      double[] weightPounds = new double[9];
      weightPounds = pounds(gravity, weightPounds);
      print(gravity, weightPounds, planetNames);
        
        
        
        }
}

now my only problem is that this line:

for (int counter = 0; counter <= 9; counter++){         
   weightPounds[counter] = 140 * gravity[counter];

throws an array out of bounds, that makes no sense. since there are to arrays that use counters does it have to be doubled or something?

The problem may be resulting from the fact that there are only 9 planets and the loop is iterating 10 times. That is, the array has 9 elements, but the for loop is counting 10 times. Try changing <=9 to just <9. Let me know howit goes.

Comments
great posts, very helpful

thank you guys for all this help. you have all helped me through a tough time. i hope i get as good of an understanding as you guys and can help others.

Im glad we could finally solve the problem. Thanks for your patience. You were an excellent poster and showed great effort. Please mark the thread solved if you have no further questions. Thanks once again.

wait umm... one last question. i had to add a last minute method to write to a file. would this addition be correct, it wont compile, but i think it is on the write track.

public class WeightOnPlanetsV1{
    
                           
   //reads the gravity for each planet from the file surfaceGravity.txt
   public static double [] gravity()throws IOException{       
        
        
        int counter = 0;
        double [] gravity = new double[9];
        File fileName = new File("surfaceGravity.txt");
        Scanner inFile = new Scanner(fileName);
        while (inFile.hasNext()){
            gravity[counter] = inFile.nextDouble();
            counter++;
        }
        inFile.close();
        return gravity;
    }
    


   
    //determines the weight in pounds of someone on each planet
    public static double [] pounds(double[]gravity, double[]weightPounds){
        for (int counter = 0; counter < 9; counter++){
            weightPounds[counter] = 140 * gravity[counter];
            
        }
        
        return weightPounds;
    }
    
   
    
    

    
    //prints the results
    public static void print(double [] gravity, double [] weightPounds, String [] planetNames){
        System.out.println("      My Weight on the Planets");
        System.out.println("Planet    Gravity     Weight(lbs)");
        for (int counter = 0; counter< 9; counter ++){
            System.out.println(planetNames[counter]);
            System.out.printf("            %4.1f  ",gravity[counter]);
            System.out.printf("       %4.1f  ",weightPounds[counter]);
             }
       }
       
    public static void outFile(double [] weightPounds)
    PrintWriter outFile = new PrintWriter (new File("writeFile.txt"));
    for (int counter = 0; counter<9; counter++){
        outFile.println(weightPounds[counter]);
    }
    outFile.flush();
    }


        
   public static void main()throws IOException
{
    String [] planetNames = new String[9];
        
        planetNames[0] = "Mercury"; 
        planetNames[1] = "Venus";
        planetNames[2] = "Earth";
        planetNames[3] = "Mars";
        planetNames[4] = "Jupiter"; 
        planetNames[5] = "Saturn"; 
        planetNames[6] = "Uranus";
        planetNames[7] = "Neptune";
        planetNames[8] = "Pluto";
        
           
      double gravity[] = gravity();
      double[] weightPounds = new double[9];
      weightPounds = pounds(gravity, weightPounds);
      print(gravity, weightPounds, planetNames);
      outFile(weightPounds);
      
        
        
        
        }
}

wait, nevermind that last post. i got it figured out due to the knowledge that i learned here =))))

This question has already been answered. Start a new discussion instead.