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:
- This is java, not our pseudocode, so some things we use in class, like prompt, are not available.
Between the previous homeworks and this one, I have provided examples of all the syntax you should need to be using.
About the Java IDE:
- You have to click Run again each time you want it to run your code to test changes.
- At top left there is a save (floppy) button! This page is pretty good but not perfect, so save often and don't lose your work!
- It WILL NOT SAVE your code if you close the browser, so you need to save!!!
- As you type, the editor tries to help by providing suggestions, but these pull from all of Java instead of
just from the tools we are using, so its suggestions may not be useful! You can turn this off using the settings (gears) button
at top right and unchecking Autocomplete at the bottom of the list, if it annoys you.
- If your output has the word error in it, java is telling you that you have a syntax error you need to fix.
It will try to tell you the line number, but sometimes an error in an earlier line can cause a problem that
doesn't show up til a later line.
About this assignment:
- The editor page starts off with example code in it. REPLACE the example code with the code in the assignment. This means to
DELETE ALL of the code in the editor, and then paste in my example code. ALL.
- When you do a different part, you will need to again REPLACE ALL of the code (you can open more tabs with copies of the IDE in your browser) so make sure you SAVE.
- Attach all your java files to a single submission in blackboard, not multiple submissions.
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:
-
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.
-
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.