Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Task 2: Write a program where you create an ArrayList object that can store

ID: 3694928 • Letter: J

Question

Java

Task 2:

Write a program where you create an ArrayList object that can store letters. Show how can you add a letter to the ArrayList, remove a letter from theArrayList, replace a letter in the ArrayList, insert a letter in a specific location in the ArrayList, and display all the letters in the ArrayList.

Task 3:

Write a program where you create an ArrayList object that can store Circle object (you created circle class in your previous PAs). Create three circle objects with different radius. Show how can you add circle objects to the ArrayList, remove a circle object from theArrayList, replace circle objects in the ArrayList, insert circle objects in a specific location in the ArrayList, and call the methods of circle objects in the ArrayList.

Explanation / Answer

Task2:

ArrayListTest.java

package com.chegg.arrays;

import java.util.ArrayList;

public class ArrayListTest {
   public static void main(String[] args) {
       ArrayList<Character> arrayList = new ArrayList<>();
       arrayList.add('A');// Adding a character/letter to arrayList
       arrayList.add('C');// Adding a character/letter to arrayList
       arrayList.add('B');// Adding a character/letter to arrayList
       arrayList.add('D');// Adding a character/letter to arrayList
       System.out.println("After adding character to list:"
               + arrayList.toString());// Displaying the array list
       arrayList.remove(1);// Trying to remove a character/letter at 1st
                           // position in the list
       System.out.println("After remvoing the charater at 1st position:"
               + arrayList.toString());// After removing the character/letter
                                       // at 1st position in the list

       // Replacing the 'A' character/letter with E in array List
       arrayList.set(0, 'E');
       System.out
               .println("After replacing the charater 'A' at 0th position with 'E':"
                       + arrayList.toString());

       // Inserting 'A' character/letter at 3rd(means index[2]) position in
       // array List
       arrayList.add(2, 'A');
       System.out
               .println("After adding the charater 'A' at 3rd position in array list:"
                       + arrayList.toString());
   }
}

Output:

After adding character to list:[A, C, B, D]
After remvoing the charater at 1st position:[A, B, D]
After replacing the charater 'A' at 0th position with 'E':[E, B, D]
After adding the charater 'A' at 3rd position in array list:[E, B, A, D]

Task 3:

Circle.java

package com.chegg.arrays;

public class Circle {
   private double radius;

   public Circle(double rad) {
       this.radius = rad;
   }

   public double findArea() {
       double area = 3.141 * radius * radius;
       return area;

   }

   /**
   * @return the radius
   */
   public double getRadius() {
       return radius;
   }

   public String toString() {
       return ("Radius:" + radius + " Area:" + String
               .format("%.2f", findArea()));
   }
}

CircleArrayList.java

package com.chegg.arrays;

import java.util.ArrayList;

public class CircleArrayList {
   public static void main(String[] args) {
       ArrayList<Circle> arrayList = new ArrayList<Circle>();
       Circle circle1 = new Circle(3);
       arrayList.add(circle1);// Adding Circle object to array list
       Circle circle2 = new Circle(7);
       arrayList.add(circle2);// Adding Circle object to array list
       Circle circle3 = new Circle(9);
       arrayList.add(circle3);// Adding Circle object to array list

       System.out.println("ArrayList after adding 3 circle objects:"
               + arrayList.toString());// Displaying the array list
       // after adding all circle
       // objects

       // Removing circle2 object from array list
       arrayList.remove(1);
       System.out
               .println("ArrayList after removing circle2 object from the array list: "
                       + arrayList.toString());

       // Replacing circle1 object with circle4 object
       Circle cricle4 = new Circle(4);
       arrayList.set(0, cricle4);
       System.out
               .println("ArrayList after replacing circle1 object with circle4: "
                       + arrayList.toString());
       // Inserting circle1 object in particular location
       arrayList.add(0, circle1);
       System.out
               .println("ArrayList after inserting circle1 object at 0th position:"
                       + arrayList.toString());
       // Calling a method of circle object in the array list
       System.out
               .println("Calling find area method of circle4 object in the arraylist:"
                       + arrayList.get(1).findArea());
   }
}

Output:

ArrayList after adding 3 circle objects:[Radius:3.0 Area:28.27, Radius:7.0 Area:153.91, Radius:9.0 Area:254.42]
ArrayList after removing circle2 object from the array list: [Radius:3.0 Area:28.27, Radius:9.0 Area:254.42]
ArrayList after replacing circle1 object with circle4: [Radius:4.0 Area:50.26, Radius:9.0 Area:254.42]
ArrayList after inserting circle1 object at 0th position:[Radius:3.0 Area:28.27, Radius:4.0 Area:50.26, Radius:9.0 Area:254.42]
Calling find area method of circle4 object in the arraylist:50.256

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote