Homework 4 - Arrays


We will use Java to do a few programming tasks. Here is an online Java IDE -- an editor that also lets you run code right in the browser. At the top is the editor, below that is a Run button, and then below that the output. You can edit the code in the editor and run it as many times as you want.

About Java:

About the Java IDE:

About this assignment:


The following example code shows using loops in java.
// This code sets things up for a general java program with input
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
// done with setup, main code starts here   

        // an array of Strings, length 3
        String [] words = new String[3];
        words[0] = "first word";
        words[1] = "hello";
        words[2] = "world";
        
        // print all the words, numbered with index
        for (int i = 0; i < words.length; i++) {
           System.out.println(i + ". " + words[i]);

        } 
        
        // an array of ints, length 10
        int [] intlist = new int [10]; 
        
        // set each element in the array to the index
        for (int i = 0; i < intlist.length; i++) {
          intlist[i] = i;
        }
        
        // an array of doubles, length 5
        double[] dublist = new double[5];
        
        for (int i =0; i < dublist.length; i++) {
          System.out.println("What is the " + i + " number?");
          dublist[i] = scan.nextDouble();
        }
        
        

// main code ends (still need the below two lines!!)  
    }
}

Do each in its own program:
  1. Create an array to hold the names of five dogs and in your program make up the dogs' names. Create a parallel array to hold the dogs ages, and ask the user for each dog's age, by name, using a loop. At the end, tell the user the name of the oldest dog.
  2. Ask the user how many prices there are and create an array that size. In a loop, read in all the prices from the user. Tell the user the average price. Then, until the user chooses to quit using -1 as a sentinel value, let the user enter a number, and tell the user the price for that number. [EC +5] When talking to the user, use human numbers, and then translate to CS indices for the array; but still use -1 to quit.