Question: Given the following code, how should the toString methods in the classes H2ClassA and H2ClassB be written to give the indicated output and take advantage
Given the following code, how should the toString methods in the classes H2ClassA and H2ClassB be written to give the indicated output and take advantage of the natural toString method in H2ClassB?
1 import java.util.ArrayList;
2
3 public class H2ClassA {
4 ArrayList list = new ArrayList ();
5
6 public static void main (String args []) {
7 H2ClassA y = new H2ClassA ();
8 int [] v = {4, 3, 7, 5, 99, 3};
9 for (int m: v)
10 y.list.add (new H2ClassB (m));
11 System.out.println (y);
12 } // end main
13
14 } // end class H2ClassA
15
16 class H2ClassB {
17 int x;
18 H2ClassB (int a) { x = a;}
19 } // end H2ClassB
OUTPUT: 4 3 7 5 99 3
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
