Question

Java Instructions of assignment: Inside 1st class - Find the int width and height of a...

Java

Instructions of assignment:

Inside 1st class

- Find the int width and height of a rectangle; include methods to set and get the width and height; and a method to calculate and return the area

Inside 2nd class

- Use an ArrayList to store 10 unique dimensions of the rectangle class

- Set the width & height using a constructor or setter methods

- Create a bubble sort algorithm list inside a method to set the areas in descending order; print the areas using a for loop; store the areas in new int array

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Rectangle.java

public class Rectangle {

   // Data members
   private int width;
   private int height;

   // Constructor to initialize data members
   public Rectangle(int width, int height) {
       this.width = width;
       this.height = height;
   }

   // Setter and getter method for data members
   public int getWidth() {
       return width;
   }

   public void setWidth(int width) {
       this.width = width;
   }

   public int getHeight() {
       return height;
   }

   public void setHeight(int height) {
       this.height = height;
   }

   // Calculate area and return
   public int area() {
       return height * width;
   }
}

RectList.java

import java.util.ArrayList;

public class RectList {

   public static void main(String[] args) {
       // Create an array list and add 10 rectangle objects to it
       ArrayList<Rectangle> rect_list = new ArrayList<Rectangle>();
       rect_list.add(new Rectangle(2, 3));
       rect_list.add(new Rectangle(12, 3));
       rect_list.add(new Rectangle(5, 4));
       rect_list.add(new Rectangle(2, 1));
       rect_list.add(new Rectangle(10, 13));
       rect_list.add(new Rectangle(7, 3));
       rect_list.add(new Rectangle(3, 8));
       rect_list.add(new Rectangle(12, 13));
       rect_list.add(new Rectangle(12, 3));
       rect_list.add(new Rectangle(1, 3));

       // Create an array to store area of rectangles
       int areaArray[] = new int[10];
       int length = rect_list.size();

       // Iterate over arraylist, get area and store in areaArray
       for (int i = 0; i < length; i++) {
           areaArray[i] = rect_list.get(i).area();
       }

       // Bubble sort to sort areaArray in descending order
       for (int i = 0; i < length - 1; i++) {
           for (int j = i + 1; j < length; j++) {
               // Compare each element with every other element
               // if area at i < area at j
               // swap them
               if (areaArray[i] < areaArray[j]) {
                   int temp = areaArray[i];
                   areaArray[i] = areaArray[j];
                   areaArray[j] = temp;
               }
           }
       }

       // Iterate over areaArray and print area
       System.out.println("Area in descending order: ");
       for (int i = 0; i < length; i++) {
           System.out.print(areaArray[i] + " ");
       }
   }
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Java Instructions of assignment: Inside 1st class - Find the int width and height of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • In Java please! Problem You will be using the point class discussed in class to create...

    In Java please! Problem You will be using the point class discussed in class to create a Rectangle class. You also need to have a driver class that tests your rectangle. Requirements You must implement the 3 classes in the class diagram below and use the same naming and data types. Following is a description of what each method does. Point Rectangle RectangleDriver - x: int - top Left: Point + main(args: String[) - y: int - width: int +...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • please do in java and comments the code so i can understand Requirements: Create a Java...

    please do in java and comments the code so i can understand Requirements: Create a Java class named “MyRectangle2D.java”. Your class will have double two variables named x and y. These will represent the center point of your rectangle. Your class will have two double variables named width and height. These will represent the width and height of your rectangle. Create getter and setter methods for x, y, width, and height. Create a “no argument” constructor for your class that...

  • java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named...

    java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named "MyRectangle3d" Your class must have the following attributes or variables · x (the x coordinate of the rectangle, an integer) * y (the y coordinate of the rectangle, an integer) * length (the length of the rectangle, an integer) * width (the width of the rectangle, an integer) * height (the height of the rectangle, an integer) * color( the color of the rectangle,...

  • JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects...

    JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following fields and methods: int width, int height Where width and height are the dimensions of the rectangle. Write accessor methods (i.e. getWidth(), getHeight()) that retrieve the values stored in the fields above. And mutator methods (i.e. setWidthl), setHeight() ) that change the field's values. Finally create a method called getAreal) that returns the area of the rectangle. Like we did...

  • Making a rectangle class in java write a simple class that will represent a rectangle. Later...

    Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • create a java class with the name Brick class: For this part of the assignment, you...

    create a java class with the name Brick class: For this part of the assignment, you will create a class that allows you to specify a colored brick that is capable of drawing itself given a specified Graphics object. Note that this is a regular Java class and does not extend the JApplet class. Your class should a constructor with the following signature: Identifier: Brick(int xPosition, int yPosition, int width, int height, Color color) Parameters: xPosition – an int representing...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT