//******************************************************************** // BasicArray.java Author: Lewis and Loftus // // Demonstrates basic array declaration and use. //******************************************************************** public class BasicArray { final static int LIMIT = 15; final static int MULTIPLE = 10; //----------------------------------------------------------------- // Creates an array, fills it with various integer values, // modifies one value, then prints them out. //----------------------------------------------------------------- public static void main (String[] args) { int[] list = new int[LIMIT]; // Initialize the array values for (int index = 0; index < LIMIT; index++) list[index] = index * MULTIPLE; list[5] = 999; // change one array value for (int index = 0; index < LIMIT; index++) System.out.print (list[index] + " "); System.out.println (); } } //******************************************************************** // ReverseNumbers.java Author: Lewis and Loftus // // Demonstrates array index processing. //******************************************************************** import java.util.Scanner; public class ReverseNumbers { //----------------------------------------------------------------- // Reads a list of numbers from the user, storing them in an // array, then prints them in the opposite order. //----------------------------------------------------------------- public static void main (String[] args) { Scanner scan = new Scanner (System.in); double[] numbers = new double[10]; System.out.println ("The size of the array: " + numbers.length); for (int index = 0; index < numbers.length; index++) { System.out.print ("Enter number " + (index+1) + ": "); numbers[index] = scan.nextDouble(); } System.out.println ("The numbers in reverse:"); for (int index = numbers.length-1; index >= 0; index--) System.out.print (numbers[index] + " "); System.out.println (); } } //******************************************************************** // TravelLog.java // // Demonstrates array index processing. //******************************************************************** import java.util.Scanner; import java.text.DecimalFormat; public class TravelLog { public static void main (String[] args) { int sum = 0, entry; double average; Scanner scan = new Scanner (System.in); int[] travelLog = new int[8]; System.out.println ("Let's fill the travel log for the first " + travelLog.length + " hours."); for (int i=0; i0 && entry<=travelLog.length) { System.out.println ("The number of kilometers covered was " + travelLog[entry-1]); System.out.print("Which part of the trip would you like to review (-1 to exit)? "); entry = scan.nextInt(); } } } //******************************************************************** // LetterCount.java Author: Lewis and Loftus // // Demonstrates the relationship between arrays and strings. //******************************************************************** import java.util.Scanner; public class LetterCount { //----------------------------------------------------------------- // Reads a sentence from the user and counts the number of // uppercase and lowercase letters contained in it. //----------------------------------------------------------------- public static void main (String[] args) { final int NUMCHARS = 26; Scanner scan = new Scanner (System.in); int[] upper = new int[NUMCHARS]; int[] lower = new int[NUMCHARS]; char current; // the current character being processed int other = 0; // counter for non-alphabetics System.out.println ("Enter a sentence:"); String line = scan.nextLine(); // Count the number of each letter occurance for (int ch = 0; ch < line.length(); ch++) { current = line.charAt(ch); if (current >= 'A' && current <= 'Z') upper[current-'A']++; else if (current >= 'a' && current <= 'z') lower[current-'a']++; else other++; } // Print the results System.out.println (); for (int letter=0; letter < upper.length; letter++) { System.out.print ( (char) (letter + 'A') ); System.out.print (": " + upper[letter]); System.out.print ("\t\t" + (char) (letter + 'a') ); System.out.println (": " + lower[letter]); } System.out.println (); System.out.println ("Non-alphabetic characters: " + other); } } //******************************************************************** // Primes.java Author: Lewis and Loftus // // Demonstrates the use of an initializer list for an array. //******************************************************************** public class Primes { //----------------------------------------------------------------- // Stores some prime numbers in an array and prints them. //----------------------------------------------------------------- public static void main (String[] args) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19}; System.out.println ("Array length: " + primes.length); System.out.println ("The first few prime numbers are:"); for (int prime = 0; prime < primes.length; prime++) System.out.print (primes[prime] + " "); System.out.println (); } } //******************************************************************** // GradeRange.java // // Demonstrates the use of an array of String objects. //******************************************************************** public class GradeRange { //----------------------------------------------------------------- // Stores the possible grades and their numeric lowest value, // then prints them out. //----------------------------------------------------------------- public static void main (String[] args) { String[] grades = {"A", "A-", "B+", "B", "B-", "C+", "C", "D", "F"}; float[] gradePoint = { 4.0f, 3.7f, 3.3f, 3.0f, 2.7f, 2.3f, 2.0f, 1.0f, 0.0f}; int[] cutoff = {85, 80, 75, 70, 65, 60, 55, 50, 0}; System.out.println ("GRADE" + "\t" + "GP" + "\t" + "CUTOFF"); for (int level = 0; level < cutoff.length; level++) System.out.println (grades[level] + "\t" + gradePoint[level] + "\t" + cutoff[level]); } } //******************************************************************** // NameTag.java Author: Lewis and Loftus // // Demonstrates the use of command line arguments. //******************************************************************** public class NameTag { //----------------------------------------------------------------- // Prints a simple name tag using a greeting and a name that is // specified by the user. //----------------------------------------------------------------- public static void main (String[] args) { System.out.println (); System.out.println (" " + args[0]); System.out.println ("My name is " + args[1]); System.out.println (); } } //******************************************************************** // FeedTheLitter.java // // Demonstres the use of an array of objects. //******************************************************************** public class FeedTheLitter { private final static int NUM_CATS = 3; public static void main (String[] args) { Cat[] litter = new Cat[NUM_CATS]; for (int i=0; i19) title = theTitle.substring(0,19); else title = theTitle; artist = theArtist; value = theValue; tracks = theTracks; } //----------------------------------------------------------------- // Returns a description of this CD. //----------------------------------------------------------------- public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); String description; description = fmt.format(value) + "\t" + tracks + "\t"; description += title; //Add necessary spaces to align the artist's name for (int i = title.length(); i <= 20; i++) description += " "; description += artist; return description; } } //******************************************************************** // TwoDArray.java Author: Lewis and Loftus // // Demonstrates the use of a two-dimensional array. //******************************************************************** public class TwoDArray { //----------------------------------------------------------------- // Creates a 2D array of integers, fills it with increasing // integer values, then prints them out. //----------------------------------------------------------------- public static void main (String[] args) { int[][] table = new int[5][10]; // Load the table with values for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) table[row][col] = row * 10 + col; // Print the table for (int row=0; row < table.length; row++) { for (int col=0; col < table[row].length; col++) System.out.print (table[row][col] + "\t"); System.out.println(); } } } //******************************************************************** // Beatles.java Author: Lewis and Loftus // // Demonstrates the use of an ArrayList object. //******************************************************************** import java.util.ArrayList; public class Beatles { //----------------------------------------------------------------- // Stores and modifies a list of band members. //----------------------------------------------------------------- public static void main (String[] args) { ArrayList band = new ArrayList(); band.add ("Paul"); band.add ("Pete"); band.add ("John"); band.add ("George"); System.out.println (band); int location = band.indexOf ("Pete"); band.remove (location); System.out.println (band); System.out.println ("At index 1: " + band.get(1)); band.add (2,"Ringo"); System.out.println (band); System.out.println ("Size of the band: " + band.size()); } }