Question

Add another method public static void displayObject(ArrayList list, int n) that will display then information on...

Add another method public static void displayObject(ArrayList list, int n) that will display then information on the nth object of list. If n is not a valid index, the method should generate and throw and exception that the main can then process. You get to decide what exception (one built into Java or a custom exception) and how you would like to “handle” the exception (terminate the program, prompt for more input, etc).

Here is the program so far:

import java.util.ArrayList;

public class TestGeometricThing
{
   public static void main (String[] args)
   {
       Square1 s1 = new Square1 (5);
       System.out.println(s1.toString());

       Square1 s2 = new Square1 (3);
       System.out.println(s2.toString());

       ArrayList<GeometricObject> go1 = new ArrayList<GeometricObject>();

       go1.add(new Square1(3));
       go1.add(new Square1(4));
       go1.add(new Square1(5));
       go1.add(new Square1(6));
       go1.add(new Circle(2.0));
       go1.add(new Circle(3.0));
       go1.add(new Circle(4.0));
       go1.add(new Rectangle(2,3));
       go1.add(new Rectangle(3,4));
       go1.add(new Rectangle(4,5));

       System.out.println("Total Area: " + sumArea(go1));
       System.out.println("Biggest Object: " +FindBiggestThing(go1));

   }

   public static double sumArea(ArrayList<GeometricObject>list)
   {
       double totalArea = 0.0;
       for (GeometricObject object:list)
       {
           totalArea += object.getArea();
       }

       return totalArea;


   }

   public static GeometricObject FindBiggestThing(ArrayList<GeometricObject>list)
   {
       GeometricObject biggest = list.get(0);
       for (GeometricObject object:list)
       {
           if (object.getArea() > biggest.getArea())
           {
               biggest = object;
           }
       }

       return biggest;
   }

   public static void displayObject(ArrayList<GeometricObject> list, int n)
   {
   }

}

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

try this:

public static void displayObject(ArrayList<GeometricObject> list, int n)
{
   if(n< list.size) {
       System.out.print(list.get(n));
   }
   else {
       while(n>= list.size) {
       Scanner scanner = new Scanner(System.in);
       System.out.print("Enter n less than "+n );
       n=scanner.nextInt();
         
   }
}

Add a comment
Know the answer?
Add Answer to:
Add another method public static void displayObject(ArrayList list, int n) that will display then information on...
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
  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

  • import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int...

    import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int temp;                       for (int i = list.length - 1; i > 0; i--)    {    for (int j = 0; j < i; j++)    {    if (list[j] > list[j + 1])    {    temp = list[j];    list[j] = list[j + 1];    list[j + 1] = temp;    }    }    }   ...

  • What will the following program display? public class checkpoint { public static void main(String urgs[]) {...

    What will the following program display? public class checkpoint { public static void main(String urgs[]) { int n = 1776; doubled = 110.0901; System .out.println(n + “\t” + d); myMethod(n, d); System.out.println(n + “\t” + d); } public static void myMethod(int i, double x) { System.out.printing + “\t” + x); i = 1250; x= 199.99; System.out.println(i + “\t" + x); } } Write the following two methods: i. compute Diameter; This method accepts the radius (r) of a circle, and...

  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write...

    The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write code to show how the JCF class ArrayList and LinkedList are used to maintain a grocery list. Verify List methods such as "add()", get(), "remove()", "isEmpty()" and "size()" by implementing a test program. import java.util.ArrayList; import java.util.Iterator; public class GroceryList { static public void main(String[] args){ ArrayList<String> groceryList = new ArrayList<String>(); Iterator<String> iter;    groceryList.add("apples"); groceryList.add("bread"); groceryList.add("juice"); groceryList.add("carrots"); groceryList.add("ice cream");    System.out.println("Number of items...

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