borozco94
borozco94

Reputation: 23

returning and receiving methods

I'm a beginner coder and I'm trying to achieve the output at the bottom by calling different methods in my main method but I keep getting errors. Can somebody please point me in the right direction. Not sure if I need to list the parameters inside the calling methods in the main header or not.

import java.util.Scanner;

public class CityOrozcoB52 
{ // begin class
  private static Scanner input = new Scanner(System.in);

  public static void main(String[] args) 
  { // begin main method

    String city, state;
    float cityPopulation, statePopulation;



    cityName();
    stateName();
    cityPopulation(city);
    statePopulation(state);
    cityPercState(cityPopulation, statePopulation);
    displayCityStateStats(cityName, stateName, cityPopulation, statePopulation, cityPercState);



 } // end main method

  public static String cityName()
  {
    String city = "";
    System.out.printf("What is the name of your city:");
     city = input.nextLine();

     return city;
  }

  public static String stateName()
  {
    String state = "";
    System.out.printf("What is the name of your state:");
    state = input.nextLine();

    return state;
  }

  public static float cityPopulation(String city)
  {
    float cityPopulation = 0;
    System.out.printf("what is the population of %s:\n", city);
    cityPopulation = input.nextFloat();

    return cityPopulation; 
  }
  public static float statePopulation(String state);
  {
    float statePopulation = 0;
    System.out.printf("what is the population of %s:", state);
    statePopulation = input.nextFloat();

    return statePopulation;
  }

  public static float cityPercState(float cityPopulation, float statePopulation)
  {
   float cityStatePercentage = (cityPopulation / statePopulation) * 100; 
  }

  public static void displayCityStateStats(String cityName, String stateName, float cityPopulation, float statePopulation, 
                                      float cityPercState)
  {
   System.out.printf("POPULATION STATISTICS\n\n"
                    + "City: %s"
                    + "State: %s"
                    + "City Population: %f"
                    + "State Population: %f"
                    + "City to State Population: %.2f%%", cityName, stateName, cityPopulation, statePopulation, 
                                                          cityPercState);

  }
} // ends CityOrozcoLE52

Upvotes: 0

Views: 141

Answers (2)

Justin Eldracher
Justin Eldracher

Reputation: 162

Not knowing what errors you are getting this is a little tricky, but I think you are having trouble with the scope of your variables. Like in the following calls from your main() method:

cityName();
stateName();
cityPopulation(city);
statePopulation(state);
cityPercState(cityPopulation, statePopulation);
displayCityStateStats(cityName, stateName, cityPopulation, statePopulation, cityPercState);

you are not "catching" the returned values from cityName() or any of the other functions. The following would most likely work:

float cityPercState;
city = cityName();
state = stateName();
cityPopulation = cityPopulation(city);
statePopulation = statePopulation(state);
cityPercState = cityPercState(cityPopulation, statePopulation);
displayCityStateStats(city, state, cityPopulation, statePopulation, cityPercState);

So whenever you have a function that returns a value, if you want to access the returned value you need to store it in a variable as I have shown. Variables you use inside methods are not available in your other methods unless you declare them just inside the class, like your input variable.

Upvotes: 1

fahimg23
fahimg23

Reputation: 86

You declared the variables in your main method, but you didn't initialize them. Since you created methods with return types and intend to return values such as the name of a city, you must catch the values being returned by each method using the variables you have declared.

For example, in main:

city = cityName();

Also, it seems that your program intends to model data/ information about a city. You are missing a constructor for your class. Have you learned about creating classes/objects? If not, I suggest doing so. This program would be a lot cleaner and more organized if programmed using constructors, setters, and getters (accessor and mutator methods).

Upvotes: 3

Related Questions