Results 1 to 5 of 5
  1. #1
    CFW
    CFW is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default How I can return an object?

    Hello, Actually.. I have a project, and I'm going to submitted tomorrowm,..
    every thing was clear, except the part (2), help me, How I can do it ..

    thanks so much...


    USA has 52 states. Each state has a population between 2 million and 20 millions. Create class States that has two instance variables: name which is an array of type String and population which is array of type Double. For example, if name[0]="California" and population[0]=15.5, then it means that the population of California state is 15.5 millions. Provide the following methods:

    1. public void inputStates(): inputs the names of the states of the USA and the respective population from the user. Use the below table as sample.
    2. public States findMaxState(): finds the state which has the maximum population then returns that state and its population.
    3. public double findTotalPopulation(): calculate the total population of the USA. Then returns the result.
    4. public double findAvgPopulation(): calculate the average population of the USA. Then returns the result.
    5. public void printStates(): prints all the states and their population in a table format as follows:

    States Its population in million
    California 15.5
    Texas 12.225
    New York 18.9
    Washington DC 2.3
    Florida 6.55

    6. public static void main(String args[]): create an object UStates of class States and perform the following operation:

    a- Input a sample of the states of the USA and their respective population.
    b- Find the state with the largest population and its population
    c- Find the total population of USA
    d- Find the average population of the states of the USA
    e- Find all the states of USA and the respective population

  2. #2
    mox8iro is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    you need to store in hashMap<String,float>
    then get all the values from hashmap, sort them and you have everything

  3. #3
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    create a method that takes a array of objects, does your search and returns the particular object.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Follow the specifications!

    No, do what the assignment says, even though the design of States is really ugly. Also, the method required is very bizarre. Essentially, it is telling you to create a new States instance with just one entry in its arrays. I'm not sure the professor realized what they were asking for.

    To find the state in question (you will have to add a method void addState(String name, double population), which you can use from inputStates() as well),

    Java Code:
    public States findMaxState() {
      int index = -1;
      double maxPop = 0d;
      for (int i = 0; i < name.length; i++) {
        if (population[i] > maxPop) {
          index = i;
          maxPop = population[i];
        }
      }
      States newStates = new States();
      newStates.addState(name[index], population[index]);
      return newStates;
    }
    This is a very strange request. Please make sure you have the assignment description correct.

  5. #5
    amit7apr08 is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default

    Hi,
    try this code


    import java.util.Arrays;

    public class States {
    //================================================== ======= main
    String[] stateNames = {"Utah", "Nevada", "Washington","Oregon","Idaho","Colorado","Wyoming" ,"Maine","Missouri"};
    Double[] population = {120.0, 5555.5, 234.0, 999.0, 1177.3, 2345.5, 4456.6, 6543.8, 7654.4};

    public static void main(String[] args) {
    States st1 = new States();
    st1 = st1.findMaxState();
    System.out.println(st1.population[0]);
    System.out.println(st1.stateNames[0]);

    }
    public States findMaxState(){
    double currentmax = 0.0;
    int currentMaxIndex = 0;

    States st2 = new States();
    currentmax = population[population.length -1];
    currentMaxIndex = population.length -1;

    //finding max population
    for(int j = population.length -2 ;j>=0;j-- ){
    if(currentmax < population[j]){
    currentmax = population[j];
    currentMaxIndex = j;
    }
    }

    st2.population = new Double[1];
    st2.stateNames = new String[1];

    st2.population[0] = currentmax;
    st2.stateNames[0] = stateNames[currentMaxIndex];

    //System.out.println(Arrays.toString(st2.stateNames) );

    return st2;
    }
    }

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. Replies: 1
    Last Post: 07-22-2008, 05:27 PM
  3. Replies: 1
    Last Post: 04-17-2008, 11:10 PM
  4. how to return an object from an arraylist
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:57 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •