Question

Can someone finish this class for me? There are comment instructions with what to do in...

Can someone finish this class for me? There are comment instructions with what to do in the methods already.

public class ArrayDemo   {
              
               public void demonstrateTask1(){
                               System.out.println("Demonstrate   Task   1");
                              
                               int[]   numbers =   null;   //   note   that   numbers   references   nothing   (null)   initially
                               numbers   =   new int[5]; //   a   reference   to   an   Array   object   containing   int   values   is   assigned
                              
                               System.out.println("Array   of   primitives   before   assigning   values");
                               //   TODO:   loop   to   print   the   values   in   the   array   elements   on   one   line   separated   by   spaces
                              
                               //   TODO:   loop   to   assign   values   1   to   5   into   the   array   elements
                               System.out.println("\nArray   of   primitives   after   assigning   values");
                               //   TODO:   loop   to   again   print   the   values   on   one   line   separated   by   spaces
                           //   TODO:   Draw   a   memory   map   for   int[] numbers =   new int[5];   at   this   point
               }
              
               public void demonstrateTask2(){
                               System.out.println("\n\nDemonstrate   Task   2");
                              
                               Goldfish[]   fishes =   new Goldfish[5];
                              
                               System.out.println("\nArray   of   references   before   assigning   references");  

//   TODO:   loop   to   print   the   reference   value   within   each   array   element separated   by   a   space                      
                              
                               //   TODO:   loop   to   initialize   each   array   element   with   a   Goldfish   object,   each   with
                               //   weights   of   1,   2,   3,   4,   5              
                              
                               System.out.println("\nArray   of   references   after   assigning   references   to   Goldfish");
                               //   TODO:   loop   to   print   the   each   referenced   objects   weight   separated   by   a   space
                           //   TODO:   Draw   a   memory   map   for   Goldfish[] fishes   =   new   Goldfish[5];   at   this   point
               }
              
               public void demonstrateTask3(){
                               System.out.println("\nDemonstrate   Task   3");
                              
                               int[][]   numbers =   new int[5][5];
                              
                               System.out.println("\n2D   Array   of   primitives   before   assigning   values");
                               //   TODO:   use   nested   loops   to   display   the   25   default   int   values   in   the   array elements,
                               //   5   per   row,   5   rows   separated   by   spaces (Tip:   Put   a   println   within   the   outer   loop
                               //   below   the   inner   loop)
                              
                               //   TODO:   use   nested   loops   to   initialize   each   element   with   the   multiplication   of
                               //   the   loop   couters   +   1,   e.g.   element[0][0]   should   have   a   value   of   1,   element[4][4]  
                               //   should   have   a   value   of   25
                              
                               System.out.println("\n2D   Array   of   primitives   after   assigning   values");
                               //   TODO:   use   nested   loops   to   display   the   25   default   int   values   in   the   array   elements,
                               //   5   per   row,   5   rows   separated   by   spaces
                           //   TODO:   Draw   a   memory   map   for   int[][]   numbers =   new int[5][5];   at   this   point
               }
              
               //   BONUS   (4)   for   this   exercise.   2   points   for   code,   2   points   for   correct   memory   map.
               //   Note:   Task   4   leads   into   Assignment   1,   the   Battlefield   that   uses   a   2D   array   of   references
               //                           to   objects   instantiated   from   class   Square.
               public void demonstrateTask4(){
                              
                               System.out.println("Demonstrate Task   4");
                              
                               Goldfish[][]   fishes =   new Goldfish[5][5];
                              
                               System.out.println("\n2D   Array   of   references   before   assigning   references");
                               //   TODO:   use   nested   loops   to   display   the   25   default   reference   values   in   the   array
                               // elements, 5   per   row,   5   rows   separated   by   spaces
                              
                               //   TODO:   use   nested   loops   to   store   a   reference   to   25   Goldfish   objects,   each   one   with   a
                               // weight set   to   be   the   multiplication   of   the   loop   counters   +   1,   for   example   the   Goldfish
                               // object   referenced   by   element[0][0]   would   have   a   weight   of   1,   while   the   Goldfish   object
                               //   referenced   by   element[4][4]   would   have   a   weight   of   25.
                               System.out.println("\n2D   Array   of   references   after   assigning   references to   Goldfish");
                               //   TODO:   use   nested   loops   to   display   the   weight   of   the   25   referenced   Goldfish   objects,
                               //   5   per   row,   5   rows   separated   by   spaces.
                           //   Draw   a   memory   map   for   Goldfish[][]   fishes   =   new Goldfish[5][5]; at   this   point
               }
}

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

Program Screen Shot:

------------------------------------------------------------------------------------------------------

Program Code:

public class ArrayDemo

{

     public void demonstrateTask1()

     {

          System.out.println("Demonstrate   Task   1");

          int[]   numbers =   null;   // note   that   numbers   references   nothing   (null)   initially

          numbers   =   new int[5]; //   a   reference   to   an   Array   object   containing   int   values   is   assigned

          System.out.println("Array   of   primitives   before   assigning   values.");

          //   TODO:   loop   to   print   the   values   in   the   array   elements   on   one   line   separated   by   spaces

          for(int i=0;i<numbers.length; i++)

              System.out.print("   "+numbers[i]);

          System.out.println();

          //   TODO:   loop   to   assign   values   1   to   5   into   the   array   elements

          for(int i=0;i<numbers.length; i++)

          {

              numbers[i]=i+1;

          }

          System.out.println("\nArray   of   primitives   after   assigning   values.");

          //   TODO:   loop   to   again   print   the   values   on   one   line   separated   by   spaces

          for(int i=0;i<numbers.length; i++)

              System.out.print("   "+numbers[i]);

          System.out.println();

          //   TODO:   Draw   a   memory   map   for   int[] numbers =   new int[5];   at   this   point

     }

     public void demonstrateTask2(){

          System.out.println("\n\nDemonstrate   Task   2");

          Goldfish[]   fishes =   new Goldfish[5];

          System.out.println("\nArray   of   references   before   assigning   references");

          //   TODO:   loop   to   print   the   reference   value   within   each   array   element separated   by   a   space                     

          for(int i=0;i<fishes.length; i++)

              System.out.print("   "+fishes[i]);

          System.out.println();

          //   TODO:   loop   to   initialize   each   array   element   with   a   Goldfish   object,   each   with

          //   weights   of   1,   2,   3,   4,   5             

          for(int i=0;i<fishes.length; i++)

          {

              fishes[i]=new Goldfish(i+1);

          }

          System.out.println("\nArray   of   references   after   assigning   references   to   Goldfish");

          //   TODO:   loop   to   print   the   each   referenced   objects   weight   separated   by   a   space

          for(int i=0;i<fishes.length; i++)

              System.out.print("   "+fishes[i].getN());

          System.out.println();

          //   TODO:   Draw   a   memory   map   for   Goldfish[] fishes   =   new   Goldfish[5];   at   this   point

     }

     public void demonstrateTask3(){

          System.out.println("\nDemonstrate   Task   3");

          int[][]   numbers =   new int[5][5];

          System.out.println("\n2D   Array   of   primitives   before   assigning   values");

          //   TODO:   use   nested   loops   to   display   the   25   default   int   values   in   the   array elements,

          //   5   per   row,   5   rows   separated   by   spaces (Tip:   Put   a   println   within   the   outer   loop

          //   below   the   inner   loop)

          for(int i=0;i<numbers.length;i++)

          {

              for(int j=0;j<numbers[i].length; j++)

                   System.out.print("   "+numbers[i][j]);

              System.out.println();

          }

          //   TODO:   use   nested   loops   to   initialize   each   element   with   the   multiplication   of

          //   the   loop   couters   +   1,   e.g.   element[0][0]   should   have   a   value   of   1,   element[4][4]

          //   should   have   a   value   of   25

          for(int i=0;i<numbers.length;i++)

          {

              for(int j=0;j<numbers[i].length; j++)

                   numbers[i][j]=(i+1)*(j+1);

          }

          System.out.println("\n2D   Array   of   primitives   after   assigning   values");

          //   TODO:   use   nested   loops   to   display   the   25   default   int   values   in   the   array   elements,

          //   5   per   row,   5   rows   separated   by   spaces

         

          for(int i=0;i<numbers.length;i++)

          {

              for(int j=0;j<numbers[i].length; j++)

                   System.out.print("   "+numbers[i][j]);

              System.out.println();

          }

          //   TODO:   Draw   a   memory   map   for   int[][]   numbers =   new int[5][5];   at   this   point

     }

     //   BONUS   (4)   for   this   exercise.   2   points   for   code,   2   points   for   correct   memory   map.

     //   Note:   Task   4   leads   into   Assignment   1,   the   Battlefield   that   uses   a   2D   array   of   references

     //                           to   objects   instantiated   from   class   Square.

     public void demonstrateTask4(){

          System.out.println("Demonstrate Task   4");

          Goldfish[][]   fishes =   new Goldfish[5][5];

          System.out.println("\n2D   Array   of   references   before   assigning   references");

          //   TODO:   use   nested   loops   to   display   the   25   default   reference   values   in   the   array

          // elements, 5   per   row,   5   rows   separated   by   spaces

          for(int i=0;i<fishes.length;i++)

          {

              for(int j=0;j<fishes[i].length; j++)

                   System.out.print("   "+fishes[i][j]);

              System.out.println();

          }

          //   TODO:   use   nested   loops   to   store   a   reference   to   25   Goldfish   objects,   each   one   with   a

          // weight set   to   be   the   multiplication   of   the   loop   counters   +   1,   for   example   the   Goldfish

          // object   referenced by   element[0][0]   would   have   a   weight   of   1,   while   the   Goldfish   object

          //   referenced   by   element[4][4]   would   have   a   weight   of   25.

          for(int i=0;i<fishes.length;i++)

          {

              for(int j=0;j<fishes[i].length; j++)

                   fishes[i][j]=new Goldfish((i+1)*(j+1));

          }

          System.out.println("\n2D   Array   of   references   after   assigning   references to   Goldfish");

          //   TODO:   use   nested   loops   to   display   the   weight   of   the   25   referenced   Goldfish   objects,

          //   5   per   row,   5   rows   separated   by   spaces.

          for(int i=0;i<fishes.length;i++)

          {

              for(int j=0;j<fishes[i].length; j++)

                   System.out.print("   "+fishes[i][j].getN());

              System.out.println();

          }

          //   Draw   a   memory   map   for   Goldfish[][]   fishes   =   new Goldfish[5][5]; at   this   point

     }

     //main method to execute the code

     public static void main(String args[])

     {

          ArrayDemo ad=new ArrayDemo();

          ad.demonstrateTask1();

          ad.demonstrateTask2();

          ad.demonstrateTask3();

          ad.demonstrateTask4();

         

     }

}

//the Goldfish has been assumed to contain only one variable

class Goldfish

{

     int num;

     Goldfish(int n)

     {

          num=n;

     }

     public int getN()

     {

          return num;

     }

}

---------------------------------------------------------------------------------------------------------

Sample output:

Demonstrate   Task   1

Array   of   primitives   before   assigning   values.

   0   0   0   0   0

Array   of   primitives   after   assigning   values.

   1   2   3   4   5

Demonstrate   Task   2

Array   of   references   before   assigning   references

   null   null   null   null   null

Array   of   references   after   assigning   references   to   Goldfish

   1   2   3   4   5

Demonstrate   Task   3

2D   Array   of   primitives   before   assigning   values

   0   0   0   0   0

   0   0   0   0   0

   0   0   0   0   0

   0   0   0   0   0

   0   0   0   0   0

2D   Array   of   primitives   after   assigning   values

   1   2   3   4   5

   2   4   6   8   10

   3   6   9   12   15

   4   8   12   16   20

   5   10   15   20   25

Demonstrate Task   4

2D   Array   of   references   before   assigning   references

   null   null   null   null   null

   null   null   null   null   null

   null   null   null   null   null

   null   null   null   null   null

   null   null   null   null   null

2D   Array   of   references   after   assigning   references to   Goldfish

   1   2   3   4   5

   2   4   6   8   10

   3   6   9   12   15

   4   8   12   16   20

   5   10   15   20   25

Add a comment
Know the answer?
Add Answer to:
Can someone finish this class for me? There are comment instructions with what to do in...
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
  • create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array...

    create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows. Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Use a method containing a nested for loop to compute the average of the doubles in each row. Use a method to...

  • please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to...

    please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to be done! * * TODO#1: Change the name and date accordingly * Created by , 11/25/2019 */ public class CS140_Arrays_Done { //TODO#2: Define two private static constants: rows (set to 5) & cols (set to 3) //array for the values private static double[][] values = { { 9, 10, 8 }, { 3.5, 10, 8.5 }, { 10, 8.5, 9 }, { 8.5, 10,...

  • Create a new ArrayPractice project in Eclipse and a class named ArrayPractice.java. Put everything that follows...

    Create a new ArrayPractice project in Eclipse and a class named ArrayPractice.java. Put everything that follows into the main method except the method arrayAverage. Create a 5 element array of doubles called grades that contains the following numbers in this order: 81.2, 92.5, 48.9, 78.8, and 45.5. Create a 7 element array of ints called numbers that contains the following numbers in this order: 12, 42, 33, 67, 92, 58, and 33. Create a 9 element array of Strings called...

  • Open a new file in your text editor, and start a class that will demonstrate a...

    Open a new file in your text editor, and start a class that will demonstrate a working two-dimensional array: import java.util.Scanner; class TwoDimensionalArrayDemo { public static void main(String[] args) { 2. Declare a three-by-three array of integers. By default, the elements will all be initialized to 0. int[][] count = new int[3][3]; 3. Declare a Scanner object for input, variables to hold a row and column, and a constant that can be used to indicate when the user wants to...

  • Assume the following declarations have been made in main(): int howmany; Scanner kb = new Scanner(System.in);...

    Assume the following declarations have been made in main(): int howmany; Scanner kb = new Scanner(System.in); Random r = new Random(); Write code to do the following: Use a while loop to prompt the user for how many numbers are to be entered. If the user does not enter a positive number (a number greater than 0), display a message saying the number must be positive and to try again. Store this value in the howmany variable. Declare an integer...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

    The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...

  • Can someone let me know what to do for this? In C Programming. HERE ARE INSTRUCTIONS:...

    Can someone let me know what to do for this? In C Programming. HERE ARE INSTRUCTIONS: it is for a yahtzee game program. Write function sumChance to do the following a. Return type int b. Parameter list array of dice c. Declare int variable sum d. Declare int variable die e. Loop through the dice array adding the value of each die to the variable sum f. Return variable sum Write function checkLgStraight to do the following a. Return type...

  • In this problem you'll get to use one of Java's random number generators, java.util.Random (see the...

    In this problem you'll get to use one of Java's random number generators, java.util.Random (see the docs for Random). This class generates a random double precision number uniformly distributed in the range [0.0 ... 1.0). That is, the numbers go from 0 to 1, including 0 but excluding 1. It is a common need in Java programming to generate a random integer number that's uniformly distributed in the range of [0 ... n), such as we saw in the dice...

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
ADVERTISEMENT