You will read a line of data with either 1 3 or 4 doubles


Exercise -

You will read a line of data with either 1, 3 or 4 doubles numbers. Numbers will be separate with a space, last number terminates with a newline When you read in a line with only number it will have a -1, once you read this line terminate your program. If you have 3 doubles on the line the values represent a square with x, y and radius x, y is center of the square

Radius is the distance from the center to the four sides.

The height and width are twice the radius.

If you have 4 doubles the values represent x, y, width, height x, y are the center of the rectangle, they can have negative values; radius, width and height are always positive.

Output consists of 4 lines...

perimeter: doubleValue with 3 digits right of decimal point

area: doubleValue with 3 digits right of decimal point

then read in another rectangle 3 or 4 doubles

contains: true or false (true if second rectangle is contained with first rectangle, otherwise false )

overlaps: true of false ( true if second rectangle overlaps first, if rect 2 is contained in rect 1, then it also overlaps, otherwise false)

Sample Run: (user enters 4 5 2 then 1 1 3 1 then 5 6 7 then 6 6 6 6 then -1 )

Enter rectangle: 4 5 2

perimeter: 16.000

area: 16.000

Enter rectangle: 1 1 3 1

contains: false

overlaps: false

Enter rectangle: 5 6 7

perimeter: 56.000

area: 196.000

Enter rectangle: 6 6 6 6

contains: true

overlaps: true

Enter rectangle: -1

Hints: for reading line you need to use a nextLine, then parse the incoming string using Scanner( line ) w hasNext, nextDouble, and/or parse into individual words using String.split and then wrapper classes.

Exercise is a little harder than most of the previous assignments, you need to move from solving problems using a sequential brute force, into OOP. With OOP we break up a problem into Classes, each class has methods and properties. Each method accomplishes a single task. You may want to refer to this API, but in general never violate these guidelines....

Never perform the same task twice, you should be creating a method and calling it twice.

If else should never be more than 3 levels deep.

Avoid too many variables, if something is used once or easily calculated it should not be a separate variable. Rectangle should have only 4 global variables. (x,y,width, height).

More than 30 lines of sequential code.

If you violate any of the above guidelines, then you are not breaking your code down into the proper number of classes and corresponding methods.

After analyzing the API, and thinking about this problem, it should be evident that the key method for solving this is boolean contains( double x, double y).

You should use this code for testing your contains...

public static boolean testMyContains( )

    {

        Rectangle rect = new Rectangle( 0, 0, 2, 4 );

        /* gives rect with test four corners...

             -1,2 ...       1,2

                  0,0 center

             -1, -2 ....  1, -2

        */

        double[][] point = {  {0,0}, {1,0},    {1,1}, {0,-2},

                            {0,3}, {1.1, 0}, {1,2.1}, {0, 3},

                            {0,1.5}, {.75,0}, {0,1.75}, {-0.9, 1.5}  };

        boolean[] solution = { true, true, true, true,

                           false, false, false, false,

                           true, true,   true,  true };

        for( int i=0; i < point.length; ++i ) {

            if( rect.contains( point[i][0], point[i][1] ) != solution[i] ) {

                System.out.println("Error on point " + i + " x,y " +

                        point[i][0] + ", " + point[i][1] + " contains:" +

                        solution[ i ] );

                return false;

            }

        }

        return true;

    }

Attachment:- Exercise.rar

Request for Solution File

Ask an Expert for Answer!!
Dissertation: You will read a line of data with either 1 3 or 4 doubles
Reference No:- TGS01711611

Expected delivery within 24 Hours