Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ArrayTest.java(attached in Moodle) provides an example on coding array basics initialize an array with predetermined values access array elements, and stay within the bounds of

ArrayTest.java(attached in Moodle) provides an example on coding array basics

  • initialize an array with predetermined values
  • access array elements, and
  • stay within the bounds of an array
  1. Compile and execute this program. Attach the execution results in Moodle
  2. Explain in your words the reason for the exception encountered

public class ArrayTest

{

public static void main(String args[])

{

// 1.declare an array

double target[] = new double[3];

double source[] = {1.0, 5.5, 7.9};

int loopIndex;

// 2.initialize the array - Copy values from source to target.

for(loopIndex = 0; loopIndex < 3; loopIndex++)

target[loopIndex] = source[loopIndex];

// 3.Assign values to two elements of target.

target[0] = 2.0;

target[1] = 4.5;

// Print values stored in source and target.

for(loopIndex = 0; loopIndex < 3; loopIndex++) {

System.out.println("Source " + source[loopIndex]);

System.out.println("Target " + target[loopIndex]);

}

// 4.Encounter ArrayException due to change loopIndex <= 3

System.out.println("======== ARRAY EXCEPTION BELOW ==========");

for(loopIndex = 0; loopIndex <= 3; loopIndex++)

{

System.out.println("Source " + source[loopIndex]);

System.out.println("Target " + target[loopIndex]);

}

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions

Question

Discuss the steps of a generic change project.

Answered: 1 week ago