Question

In: Computer Science

In Java, what is the advantage of using ArrayList over arrays? Write a program to perform...

In Java, what is the advantage of using ArrayList over arrays? Write a program to perform following operations
a. Create a class named Rectangle with two
properties “height” and “width”. Create a parameterized
constructor to initialize “height” and “width” values.
b. Write method area() to calculate and return area of Rectangle.
c. Create an ArrayList to store Rectangle objects.
d. Create three Rectangle objects of width and height set to
(2, 3), (3, 3) and (4, 5) and add them to ArrayList.
e. Calculate and print area of all the Rectangle objects in
ArrayList. (Hint: Area = height x width)

Solutions

Expert Solution

The advantages of using arrayList over arrays are as follows:

i) arrayList is resizable while arrays are of the fixed lenght, with array you can not change the size of arrays once its created.
ii) In arrayList, we have various types of methods to do manipulation on objects.
iii) Multiple insertion and deletion can be done successfully from arrayList.
iv) ArrayList can also be used to traverse in both the directions of the list.


//class to get rectangle area
class GetRectangleArea
{
    public static void main(String arg[])
    {
        Rectangle rectObj = new Rectangle(10, 20);     
        System.out.println("Area of Rectangle = " + rectObj.getArea());
       //adding object to array list
       ArrayList<Rectangle> rectList = new ArrayList<Rectangle>();
       rectList.add(rectObj);
    }
}

//Rectangle class
class Rectangle
{
    double height;
    double width;

    Rectangle(double hgt, double wdt)
    {
        this.height = hgt;
        this.width = wdt;
    }

    double getArea()
    {
        return height * width;
    }

}


Related Solutions

how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.   public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) { } Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.
java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
Write a Java program to do the following USING ARRAYS 1) input 15 integers (input validation,...
Write a Java program to do the following USING ARRAYS 1) input 15 integers (input validation, all numbers must be between 0 and 100) 2) find the largest number. 3) Find the Smallest Number 4) Find the Sum of all numbers in the Array Display: Largest Number Smallest Number Sum of all numbers the original Array DO NOT USE METHODS OR FUNCTIONS
Explain the difference between Arrays and ArrayList. What are the limitation of arrays? How many primitive...
Explain the difference between Arrays and ArrayList. What are the limitation of arrays? How many primitive data types Java have? Please list them out and list the corresponding wrapper classes for each type. What is the different between String class and StringBuilder class?
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT