Just a bit of help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Just a bit of help

  1. #1
    Join Date
    Apr 2013
    Posts
    1

    Just a bit of help

    Hey, I read the FAQs, and as I will post what I have, I did try to do this code, but I am having some trouble with it. If someone could help, I'd be extremely grateful. I think I have majority of it done, but, I might be wrong. What I need to is here:

    Write a program that determines which of 4 geographic regions within a major city (north, south,
    east, and west) had the fewest reported traffic accidents last year. It should have the following
    two functions, which are called by main.
    The first function, getNumAccidents, is passed the name of a region. It asks the user for
    the number of traffic accidents reported in that region during the last year and returns that
    integer. It should be called once for each city region.
    The second, findLowest, is passed the four accident totals. It determines which is the
    smallest and prints the name of the region, along with its accident figure.

    This is what I've done so far:
    import javax.swing.JOptionPane;


    public class douglassProgramFour {

    public static void getNumAccidents(String[] locations)
    {
    int [] accidents=new int [4];

    for(int counter=0;counter<accidents.length;counter++)
    {
    String input=JOptionPane.showInputDialog("How many accidents were in the " + (locations) + " district last year?");
    accidents[counter]=Integer.parseInt(input);
    System.out.println(accidents);

    }

    }

    public static void findLowest(int accidents)
    {
    int lowest = 1000;

    if(accidents < lowest)
    {
    lowest = accidents;
    }
    System.out.println(lowest);

    }

    public static void main(String[] args)
    {
    String [] locations={"East","South","North","West"};
    getNumAccidents(locations);
    findLowest(0);

    }

    }

    Again, if I can get any help, I'd be very thankful.

  2. #2
    Join Date
    Apr 2013
    Location
    Cambridge
    Posts
    4

    Re: Just a bit of help

    Your method signatures for getNumAccidents and findLowest don't match the description given - getNumAccidents should take one string name for a region and then ask the user for the number of accidents in that region. The problem says the method should be called for all four regions, so you should make a loop in the main method which calls it once for each location.

    Meanwhile, findLowest should be taking four integers as arguments rather than one and then looping through them to find which is lowest. Your logic for this is roughly right, but is only returning the minimum of the one input you're passing in which isn't much use

    Also, getNumAccidents is supposed to return the number the user enters rather than printing it - that way, your main method can record the values it gets for each region and pass them in to the findLowest method afterwards

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center