Question

In: Computer Science

Purpose: To write an application using the list data structure that sorts objects in ascending order....

Purpose: To write an application using the list data structure that sorts objects in ascending order.

Details:

Create a class called Rectangle containing the following:

  • Two instance variables,
    • An instance variable of type double used to hold the rectangle’s width.
    • An instance variable of type double used to hold the rectangle’s height.
  • Provide a constructor with two parameters used to initializes each instance variable. The constructor should verify that the specified width and height values are greater than 0.0 and less than or equal to 20.0. If they are not, indicate an exception has occurred.
  • Provide get methods that return the values of each instance variables.
  • Provide set methods that set the instance variables to new values. The methods should also verify that the specified width and height values are greater than 0.0 and less than or equal to 20.0. If they are not, indicate an exception has occurred.
  • Provide a method called calculatePerimeter that calculates the perimeter of the rectangle and return that value as a double.
  • Provide a method called calculateArea that calculates the area of the rectangle and returns that value as a double.

Create a second class called RectangleTest that contains the main method, and thoroughly tests the Rectangle class’s methods. This test class does not need to ask users for input. Just create the needed Rectangle objects to ensure that you test the Rectangle class’s methods well. The thoroughness of your testing is important.

***Throw an IllegalArgumentException indicating there is a problem with the input parameters in the constructor and the set methods.Also, rewrite the RectangleTest class from Chapter 8 to handle the exceptions thrown by the Rectangle class. Thoroughly tests the Rectangle class’s methods. This test class does not need to ask users for input. Just create the needed Rectangle objects to ensure that you test the Rectangle class’s methods well. The thoroughness of your testing in will impact your grade.***8

Then---create a second class called RectangleComparator that implements the Comparator interface to compares two Rectangle objects according to area.

Create a third class called RectangleSortTest that contains the main method. The method should create a List containing five Rectangle objects, not in order by area. The method should print the list, displaying each rectangle’s area. Then sort the list, and print the list again showing the list has been sorted by area. This test class does not need to ask users for input, nor does it need to do any additional error checking.

Solutions

Expert Solution

Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !

===========================================================================

public class Rectangle {

    private double width;
    private double height;

    public Rectangle(double width, double height) {
        if (0.0 < width && width <= 20.0 && 0 < height && height <= 20.0) {
            this.width = width;
            this.height = height;
        } else {
            throw new IllegalArgumentException("Error: Invalid dimensions provided");
        }
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        if (0.0 < width && width <= 20.0 && 0 < height && height <= 20.0) {
            this.width = width;
        } else {
            throw new IllegalArgumentException("Error: Invalid width provided");
        }
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        if (0.0 < width && width <= 20.0 && 0 < height && height <= 20.0) {
            this.height = height;
        } else {
            throw new IllegalArgumentException("Error: Invalid height provided");
        }
    }

    public double calculatePerimeter() {
        return 2 * (width + height);
    }

    public double calculateArea() {
        return width * height;
    }
}

=============================================================

public class RectangleTest {


    public static void main(String[] args) {

        // testing exception is getting thrown when width or length
        // are outside the range and printing the exception message
        try {

            Rectangle invalid = new Rectangle(-10, 21);

        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }

        // testing the perimeter and area
        Rectangle rectangle = new Rectangle(4.5, 8.7);
        System.out.println("Perimeter =  " + rectangle.calculatePerimeter());
        System.out.println("Area = " + rectangle.calculateArea());


    }
}

=============================================================

import java.util.Comparator;

public class RectangleComparator implements Comparator<Rectangle> {


    @Override
    public int compare(Rectangle o1, Rectangle o2) {
        return Double.compare(o1.calculateArea(),o2.calculateArea());
    }
}

=============================================================

import java.util.ArrayList;
import java.util.Collections;

public class RectangleSortTest {

    public static void main(String[] args) {

        ArrayList<Rectangle> rectangles = new
                ArrayList<>();

        rectangles.add(new Rectangle(4.6, 2));
        rectangles.add(new Rectangle(14.6, 5.4));
        rectangles.add(new Rectangle(1.6, 6.4));
        rectangles.add(new Rectangle(1.9, 17.8));
        rectangles.add(new Rectangle(16.9, 5.4));

        Collections.sort(rectangles, new RectangleComparator());
        System.out.println("Printing Sorted Rectangle by Area: ");
        for (Rectangle rectangle : rectangles) {
            System.out.println(rectangle.getWidth() + " x " + rectangle.getHeight() +
                    ", Area: " + String.format("%.2f", rectangle.calculateArea()));
        }

    }
}

=============================================================

=============================================================


Related Solutions

Write a program that sorts prices of 10 tacos in ascending order based on the price,...
Write a program that sorts prices of 10 tacos in ascending order based on the price, using arrays. Requirements: The user enters the name of the taco and then the price of the taco HINT: Two arrays make this problem simpler. One with the names and the other with the prices. The indices indicate the combination. For instance, a taco price at index 5 has its name also at index 5 of the other array. HINT: It is a good...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Please include comments to understand code. Requirements Implement the following functions: int readData( int **arr) arr is a pointer to pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers....
C++ Write a program for sorting a list of integers in ascending order using the bubble...
C++ Write a program for sorting a list of integers in ascending order using the bubble sort algorithm Requirements Implement the following functions: Implement a function called readData int readData( int **arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers. After the first number,...
Write a java program that randomizes a list and sorts it. The list will be randomized...
Write a java program that randomizes a list and sorts it. The list will be randomized to be at least 12 elements and no more than 30 elements with values between 1 and 100. You will submit one program with two sorts done separately within the program. The list will be re-randomized before reach sort. Write a flowchart for each sort.
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
Objective: The purpose of this assignment is to introduce declaration of list objects, the forstatement and...
Objective: The purpose of this assignment is to introduce declaration of list objects, the forstatement and the def keyword used to define functions. Problem: Write a Python module (a text file containing valid Python code) named p3.py. This file will contain the following.  Definition of a list containing strings which are in turn integers. These integers represent years, which will be used as inputs to the next item in the file  Definition of a function named isLeap. This...
Write a Y86 program in C language that sorts an array of data using Bubble Sort....
Write a Y86 program in C language that sorts an array of data using Bubble Sort. Allow the user to input up to 10 numbers from the keyboard. Sort the array in place (i.e., no need to allocate additional memory for the sorted array). Your program should be a complete one
Write a Java program that sorts an array of “Student” in an aescending order of their...
Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (Insertion sort): Student [] studs = new Student[8]; s[0] = new Student("Saoud", "Mohamed", 3.61); s[1] = new Student("Abdelkader", "Farouk", 2.83); s[2] = new Student("Beshr" , "Alsharqawy", 1.99); s[3] = new Student("Nader", "Salah", 3.02); s[4] = new Student("Basem", "Hawary", 2.65); s[5] = new Student("Abdullah", "Babaker", 2.88); s[6] = new Student("Abdelaal", "Khairy", 3.13); s[7] = new Student("Mohamedain",...
list in ascending order the levels of hierarchy of complexity. define each level and provide an...
list in ascending order the levels of hierarchy of complexity. define each level and provide an example for each.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT