Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Can anyone help ....please

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Can anyone help ....please

    Let’s see how that would work with an annulus with outer radius 2 and inner radius
    1.
    Let’s place this annulus on a coordinate system (sort of like a map).
    And now imagine a grid laid over the top of that (like the grid lines on a map).
    x
    y
    CSP1150/4150 Programming Principles page 3
    Now (the centre points of) some of these grid cells are on the annulus, and some
    are not. Let’s put a mark on those cells that are on the annulus.
    In this case 94 cells are marked, out of 224 cells (16 across by 14 down). As a
    fraction (94/224) this is about 0.42 i.e. about 42% of the cells are marked.
    We can work out the area of the rectangle formed by all the grid cells (it’s about
    24.9). So the area of the annulus is about 0.42 times 24.9, which is about 10.45.
    We could have used any rectangle that covers the whole annulus for this
    calculation, and we could have made the grid cells smaller (the smaller they are,
    the more accurate the calculation should be).
    Using the mathematical formula for the area, we should actually get about 9.43.
    What went wrong? Well, my drawing is not very accurate! But we could do it better:
    we could write a program to do the calculation for us. In pseudocode, here is what
    the program should do:
    BEGIN Area of Annulus
    Set gridSize = 20 (we could use 20 by 20 grid cells)
    Get r1 from user (outer radius)
    Get r2 from user (inner radius)
    Set counter = 0 (will count how many grid cells are on the annulus)
    For each grid cell
    if the centre of the cell is on the annulus
    Set counter = counter + 1
    Set area = (rectangle area) * counter/(gridSize * gridSize)
    Output “Area : ” area
    END Area of Annulus
    Before we could turn this into a Java program, we have to fill in some of the details.
    1. How big should the rectangle of grid cells be?
    2. How do we figure out where the centre of each grid cell is?
    3. Once we know that, how do we figure out whether it is on the annulus or
    not?
    Let’s tackle these one at a time:
    1. How big should the rectangle of grid cells be?
    Any rectangle that completely covers the annulus will do. Let’s say the rectangle
    goes from minx to maxx in the x direction, and from miny to maxy in the y direction.
    We might as well use this rectangle:
    so minx = -r1, maxx = r1, miny = -r1, maxy = r1.
    2. How do we figure out where the centre of each grid cell is?
    This is tricky to work out so I’ll just give you the answer:
    If you start counting the columns of grid cells from the left, starting at zero, then the
    x-coordinate of a cell is min x + (column +0.5) × ((max x − min x) / gridSize) .
    And is you start counting the rows of grid cells from the bottom, starting at zero,
    then the y-coordinate of a cell is min y + (row +0.5) × ((max y − min y) / gridSize) .
    x
    y
    and finally
    3. Once we know that, how do we figure out whether it is on the annulus or
    not?
    Again, tricky. To be on the annulus, a point has to be inside a circle of the outer
    radius, but not inside a circle of the inner radius. Using a formula worked out by
    Pythagoras, this means that if the point has x-coordinate x and y-coordinate y, then
    for it to be on the annulus:
    x × x + y × y < r1× r1 (the point is inside the outer circle), and
    x × x + y × y > r2 × r2 (the point is outside the inner circle).
    Putting all this together in pseudocode:
    BEGIN Area of Annulus
    Set gridSize = 20 (we could use 20 by 20 grid cells)
    Get r1 from user (outer radius)
    Get r2 from user (inner radius)
    Set minx = -r1
    Set maxx = r1
    Set miny = -r1
    Set maxy = r1
    Set counter = 0 (will count how many grid cells are on the annulus)
    For column = 0 to gridSize-1
    Set x = -minx + (column + 0.5)*((maxx–minx)/gridSize)
    For row = 0 to gridSize-1
    Set y = -miny + (row + 0.5)*((maxy–miny)/gridSize)
    Set test = x*x + y*y
    if test < r1*r1 and test > r2*r2
    Set counter = counter + 1
    Set area = (maxx-minx)*(maxy-miny)*counter/(gridSize * gridSize)
    Output “Area : ” area
    END Area of Annulus
    Notice that when this is written in Java, it will use nested for loops.

  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone help ....please

    // Import all required packages
    import javax.swing.JOptionPane;
     
    public class Stage3
    {
        public static void main(String[] args)
        {
            //Declare Variables
            double gridSize = 20; // use 20 by 20 grid cells
            double r1;
            double r2;
     
            String inputStr;
     
            //Get r1 from user (outer radius)
            inputStr = JOptionPane.showInputDialog ("Enter outer radius of the Annalus (r1)");
            r1 = Double.parseDouble(inputStr);
     
            //Get r2 from user (inner radius)
            inputStr = JOptionPane.showInputDialog ("Enter inner radius of the Annalus (r2)");
            r2 = Double.parseDouble(inputStr);
     
     
     
                double minx = -r1;
                double maxx = r1;
                double miny = -r1;
                double maxy = r1;
                double x;
                double y;
                double area;
                double test;
     
     
                double counter = 100; //(will count how many grid cells are on the annulus)
     
                for (int column= 0; column <gridSize; column++)
                {
     
                    for (int row= 0; row <gridSize; row++)
                {
     
                    //For column = 0 to gridSize-1
                    x = -minx + (( column + 0.5 ) * ( maxx - minx )/ gridSize);
                    //For row = 0 to gridSize-1
                    y = -miny + (( row + 0.5 ) * ( maxy - miny )/ gridSize);
     
     
                    test = x*x + y*y;
     
                    if (test < r1*r1 && test > r2*r2)
                    {
                  counter++;
                }
                }
               }
     
                area = (maxx-minx)*(maxy-miny)*counter/(gridSize * gridSize);
                String result = "Area of Annalus: \n";
                JOptionPane.showMessageDialog(null, result + area );
     
    }
    }

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Can anyone help ....please

    Welcome to the forums rockchild.

    There is no use just posting your assignment specification. No one is going to read through all that then go through your code attempting to figure out whats missing etc.
    Please tell us exactly what you are stuck on. Break it down into parts and take it one step at a time.

    If you show an effort and make this easy to understand, you will get good replies.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    3
    My Mood
    Yeehaw
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone help ....please

    Well, there is just under a week before it is due... Good luck! :-P