an array whose components are data of a specific type
two-dimensional array
an array whose componenents are arrays
array
a sequence of a specific type of elements with a fixed size of at least 0
ArrayList
a sequence of Objects with a variable size of at least 0
insertion sort
A sort that goes through a list of N elements N - 1 times. First the first two elements are put in the correct order. On each subsequent time through the list, one additional element is put in the correct position in the sorted part of the list.
selection sort
a sort that goes through the list of N elements N - 1 times. First time through it recognizes the smallest element and places it in the first spot. On each subsequent time through the list, the smallest element in the unsorted part of the list is found and put at the end of the sorted part of the list.
binary search
Starting with a sorted list, this search checks the number in the middle first and if it is smaller then this number it will check in the middle of the lower half of the list. It keeps narrowing down the range like this until it determines the subscript of the element it is looking for.
sequential search
This search checks each element individually to see if the element is equal to the value itβs looking for and return the subscript of the element that is found.
For the two-dimensional array you created in the previous exercise, write a nested for loop to print the values in the following order: 1 5 9 2 6 10 3 7 11 4 8 12. That is, print the values going down the columns instead of going across the rows.
Each of the problems that follow asks you to write a method. Of course, as you are developing the method in a stepwise fashion, you should test it. Hereβs a simple application program that you can use for this purpose:
import java.util.Arrays;
public class MethodTester {
public static String stringify(Object[] s) {
StringBuffer sb = new StringBuffer();
int width = 0;
final int MAX_WIDTH = 60;
for(Object o : s) {
if(width + o.toString().length() >= MAX_WIDTH) {
sb.append("\n");
width = 0;
}
sb.append(o.toString());
sb.append(",");
width += o.toString().length() + 1;
}
if (sb.charAt(sb.length()-1) == ',') {
sb.deleteCharAt(sb.length()-1);
sb.append("\n");
}
return sb.toString();
}
public static void main(String args[]) {
Integer[] x = new Integer[100];
Arrays.fill(x,12345);
System.out.println("the array is:\n" + stringify(x));
}
}
Just replace the stringify() method with your method. Note that you must declare your method static if you want to call it directly from main() as we do here.
Write a method that takes two parameters, an int array and an integer, and returns the location of the last element of the array that is greater than or equal to the second parameter.
Write a program that tests whether a \(3\; \times \;3\) array, input by the user, is a magic square. A magic square is an \(N\; \times\; N\) matrix of numbers in which every number from 1 to \(N^2\) must appear just once, and every row, column, and diagonal must add up to the same totalβfor example,
The merge sort algorithm takes two collections of data that have been sorted and merges them together. Write a program that takes two 25-element int arrays, sorts them, and then merges them, in order, into one 50-element array.
Challenge:: Design and implement a BigInteger class that can add and subtract integers with up to 25 digits. Your class should also include methods for input and output of the numbers. If youβre really ambitious, include methods for multiplication and division. (Do not use java.math.BigInteger for your implementation)
Challenge:: Design a data structure for this problem: As manager of Computer Warehouse, you want to track the dollar amount of purchases made by those clients that have regular accounts. The accounts are numbered from 0, 1, β¦ ,N. The problem is that you donβt know in advance how many purchases each account will have. Some may have one or two purchases. Others may have 50 purchases.
An anagram is a word made by rearranging the letters of another word. For example, act is an anagram of cat, and aegllry is an anagram of allergy. Write a Java program that accepts two words as input and determines if they are anagrams.
Challenge:: An anagram dictionary is a dictionary that organizes words together with their anagrams. Write a program that lets the user enter up to 100 words (in a TextField, say). After each word is entered, the program should display (in a TextArea perhaps) the complete anagram dictionary for the words entered. Use the following sample format for the dictionary. Here the words entered by the user were: felt, left, cat, act, opt, pot, top.
Acme Trucking Company has hired you to write software to help dispatch its trucks. One important element of this software is knowing the distance between any two cities that it services. Design and implement a Distance class that stores the distances between cities in a two-dimensional array. This class will need some way to map a city name, Boise, into an integer that can be used as an array subscript. The class should also contain methods that would make it useful for looking up the distance between two cities. Another useful method would tell the user the closest city to a given city.
Rewrite the main() method for the WordGuess example so that it allows the user to input the number of players and whether each players is a computer or a human. Use a KeyboardReader.