Programming Fundamentals in Java

Module [302] - Java Single-Dimensional Arrays

This module will explore a new concept, arrays, and its implementation in the Java programming language.

  • Textbook Reading Assignments
  • The following sections of the textbook are assigned for this module.
    • Chapter #7 – Java Single-Dimensional Arrays
  • Nick Parlante's Java Arrays & Loops
  • URL Link: Tutorial
  • This tutorial page, from the author of the JavaBat tool we are using, will provide you with a convenient summary of the textbook chapter.

Module Learning Outcomes

In this module, you will develop further your programming skills as follows;

Tracing
Ability to read and trace Java programs leveraging 1 dimensional arrays.
Implementing
Ability to write Java programs leveraging 1 dimensional arrays.
Testing
Ability to develop tests for Java programs leveraging 1 dimensional arrays.
Debugging
Ability to troubleshoot Java programs leveraging 1 dimensional arrays.
Designing
Ability to design Java programs leveraging 1 dimensional arrays.

  • A01 - Analyze Scores
  • Video Link: YouTube
  • This exercise is #7.4 in the 11th & 10th editions of our text; it was #6.1 in older editions of our text. Write a Java program that reads an unspecified number of scores from the user as floating point numbers. There is a maximum of 100 such numbers to be read. Entering a negative score will tell the program that you are done. It will then determines how many scores are above or equal to the average and how many are below.
  • A02 - Reverse a series of numbers
  • Video Link: YouTube
  • This exercise is #7.2 in the 11th; it was #7.3 in the 10th edition of our text; it was #6.3 in older editions of our text. Write a Java program that reads ten integeres and displays them in reverse order.
  • A03 - Print distinct numbers
  • Video Link: YouTube
  • This exercise is #7.5 in the 11th & 10th editions of our text; it was #6.5 in older editions of our text. Write a Java program which reads ten integers from the user then displays the number of distinct numbers and the distinct numbers separated by a space.
  • A04 - Average int & double arrays
  • Video Link: YouTube
  • This exercise is #7.8 in the 11th & 10th editions of our text; it was #6.8 in older editions of our text. Write two Java overloaded methods which return respective the average of an array of int and of and array of double.
    • public static int average(int[] data)
    • public static double average(double[] data)
    Write a Java main method to prompt the user to enter ten values twice in order to test both methods.
  • A05 - Smallest int in an array
  • Video Link: YouTube
  • This exercise is #7.9 in the latest edition of our text; it was #6.9 in older editions of our text. Write a Java method which returns the smallest element in an array. Make sure you also provide a main method to test it. Please note that the latest edition of the textbook asks you to work on an array of doubles instead of int. Since this does not modify the difficulty of the exercise, we suggest you work on an array of int as is done in the live-coding video

IMPORTANT NOTE:

  • This week, we have both Problets and Codingbat / Javabat assigned as part of your practice. However, you will automatically get credit for the Problets practice.
  • As a result, you will not have to upload any screenshots for Problets for this module, yet will receive full points for the question prompting you to do so.
  • Please note that the Javabat / Codingbat exercises are still due! Do not skip these.
  • The links below are only provided so that, if you need help from a Problet tutor, you will find it readily available. If you need more time to work through the Javabat / Codingbat exercise, you will have the flexibility to do so.

Each of the following links will lead you to a separate tutor applet. The tutors take usually 30 minutes to work through.

  • Problet #1 - Arrays - 1-Dimensional
  • URL Link: See Instructor Announcements
  • This Problet tutor will allow you to practice using 1-dimensional Java arrays

IMPORTANT NOTE:

  • Some of the Javabat exercises below are marked as [optionals]; You do not have to complete these in order to get full participation credit for the P30x assignment corresponding to this module.
  • Feel free to use them if you feel that you need a bit more practice on this module's topics and if Javabat provides you the kind of practice you need.

The first series of exercises is from the Array-1 group from the JavaBat website. They do not require loops.

  • JB1 - Largest Sum Array
  • URL Link: biggerTwo
  • Start with 2 int arrays, a and b, each length 2. Consider the sum of the values in each array. Return the array which has the largest sum. In event of a tie, return a.
  • JB2 - Reversing an Array of Ints
  • URL Link: reverse3
  • Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.
  • JB3 - Swapping First & Last Elements
  • URL Link: swapEnds
  • Given an array of ints, swap the first and last elements in the array. Return the modified array. The array length will be at least 1.

The second series of exercises is from the Array-2 group.

  • JB4 - Alone in the Dark
  • URL Link: notAlone
  • We will say that an element in an array is "alone" if there are values before and after it, and those values are different from it. Return a version of the given array where every instance of the given value which is alone is replaced by whichever value to its left or right is larger.
  • JB5 - Array of Strings
  • URL Link: fizzArray2
  • Given a number n, create and return a new string array of length n, containing the strings "0", "1" "2" .. through n-1. N may be 0, in which case just return a length 0 array. Note: String.valueOf(xxx) will make the String form of most types.
  • JB6 - Fizz Array [OPTIONAL]
  • URL Link: fizzArray3
  • Given start and end numbers, return a new array containing the sequence of integers from start up to but not including end, so start=5 and end=10 yields {5, 6, 7, 8, 9}. The end number will be greater or equal to the start number. Note that a length-0 array is valid.
  • JB7 - Centered Average of an Array of Ints [OPTIONAL]
  • URL Link: centeredAverage
  • Return the "centered" average of an array of ints, which we will say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.

IMPORTANT NOTE:

  • Some of the homework exercises below are marked as [optionals]; You do not have to complete these in order to get full participation credit for the HW assignment corresponding to this module.
  • More specifically, when the TAs select the exercise to upload, they will NOT select any of the optional ones. However, all non-optional exercises are fair game so you should complete them.

  • B01 - Counting Occurences of numbers
  • This exercise is #7.3 in the 11th & 10th editions of our text.
  • B02 - Prime Numbers Revisited
  • This exercise is #7.6 in the 11th & 10th editions of our text.
  • B03 - Counting Single Digits
  • This exercise is #7.7 in the 11th & 10th editions of our text.
  • B04 - Find Index of Smallest Element in an Array
  • This exercise is #7.10 in the 11th & 10th editions of our text.
  • B05 - Timing Execution of Programs
  • This exercise is #7.16 in the 11th & 10th editions of our text.
  • B06 - Sorting Array of Students [OPTIONAL]
  • This exercise is #7.17 in the 11th & 10th editions of our text.
  • B07 - Bubble Sort [OPTIONAL]
  • This exercise is #7.18 in the 11th & 10th editions of our text.