Question

In: Computer Science

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.

Solutions

Expert Solution

According to the problem statement,

I have coded the program in Java with all conditions satisfaction.

Below are the Snippets:

Code Snippet:

Output Snippet:

Code in Text format:

import java.io.*;
import java.util.*;

class Circle                                                                       // created class circle
{
   int radius;                                                                       // circle radius
  
   Circle(int a)                                                                   // constructor to set circle's radius
   {
       this.radius=a;
   }
  
   public String toString()                                                       // toString method to get circle radius
   {
       return "Radius: "+radius;
   }
  
}
class demo
{
   public static void main(String args[])
   {
       ArrayList<Circle> list=new ArrayList<Circle>();                               // created an ArrayList of type Circle with radius
      
       for(int i=1;i<=5;i++)                                                       // looping for 5 times to store 5 circle objects into ArrayList
       {
           Circle c=new Circle(i);
           list.add(c);                                                           // appending Circle object into ArrayList
       }
       System.out.println("5 Circle Objects are Created and Stored in ArrayList");
      
       Iterator itr=list.iterator();                                               //getting the Iterator
       System.out.println("Values of Circle Objects:");
       while(itr.hasNext())                                                       //check if iterator has the elements
       {
           System.out.println(itr.next().toString());                               // printing Circle radius by invoking toString method of Circle
       }
   }
}

I hope the above snippets and information will help you out!

Thank you!


Related Solutions

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 creates a hashtable with 10 objects and 5 data members using...
Write a java program that creates a hashtable with 10 objects and 5 data members using mutator and accessor methods for each data member.
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.
Write a program that adds and subtracts two polynomials. It creates an array of nodes and...
Write a program that adds and subtracts two polynomials. It creates an array of nodes and connects them into the freeStore. This implementation uses one array to store multiple array to store multiple polynomial instances and the free store. I need help to finish the LinkedListInArrayPolynomial class. Output should look like below: Forth test is linked list of terms in an array. linkInArray1 = 3x^11+4x^10+4x^4 linkInArray2 = 4x^19+5x^14-3x^12-78 sum of linkInArray1 and linkInArray2 = 4x^19+5x^14-3x^12+3x^11+4x^10+4x^4-78 linkInArray1 minus linkInArray2 = -4x^19-5x^14+3x^12+3x^11+4x^10+4x^4+78...
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this...
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this circle the word “GO” with Arial font and size 24, bold face needs to be printed. Justify your syntax.   answer within 25 min plzzz
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this...
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this circle the word “GO” with Arial font and size 24, bold face needs to be printed. Justify your syntax. I need this answer ASAP PLZ.
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this...
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this circle the word “GO” with Arial font and size 24, bold face needs to be printed. Justify your syntax.
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this...
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this circle the word “GO” with Arial font and size 24, bold face needs to be printed. Justify your syntax
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this...
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this circle the word “GO” with Arial font and size 24, bold face needs to be printed. Justify your syntax.  
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this...
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this circle the word “GO” with Arial font and size 24, bold face needs to be printed. Justify your syntax.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT