Chapter 1-9 Practice
Objects
- Make a
Shoeclass that has the fields size (a double), isForMen (boolean), and name (String), with 2 or three constructors and accessor methods - Make a TestingClass to see if your
Shoeclass is working properly - implement the Measureable interface that returns the size.
- make a method
isBiggerthat has a parameterShoe otherreturns true if the other shoe is a larger size. - make a method
biggestthat takes anArrayListofShoeand returns the largest shoe using theisBiggermethod.
Loops
- Make a while loop that prints
2,5,8,11,14,17,20 - Make a for loop that prints
2,5,8,11,14,17,20
Arrays
Four methods to write:
arrayToStringwhich is a string with all the values of the array separated with commas and spacesattachToEndwhich makes a new array that is one longer, adding the value to the last positionattachToFrontwhich makes a new array that is one longer, adding the value to the first positionmaxwhich returns the value of the biggest elementminwhich returns the value of the smallest element
Starter Code
public class ArrayTester {
public static String arrayToString(int[] a){
// your code here
}
public static int[] attachToEnd(int[] a, int x){
// your code here
}
public static int[] attachToFront(int[] a, int x){
// your code here
}
public static int min(int [] a){
// your code here
}
public static int max(int [] a){
// your code here
}
public static void main(String[] args) {
int[] nums = {3,2,1};
System.out.print ("array nums has "+arrayToString(nums));
System.out.println ("--Expected [ 3, 2, 1 ]");
nums=attachToEnd(nums, 0);
System.out.print ("array nums has "+arrayToString(nums));
System.out.println ("--Expected [ 3, 2, 1, 0 ]");
nums=attachToFront(nums, 4);
System.out.print ("array nums has "+arrayToString(nums));
System.out.println ("--Expected [ 4, 3, 2, 1, 0 ]");
System.out.print ("the max is "+max(nums));
System.out.println ("--Expected 4");
System.out.print ("the min is "+min(nums));
System.out.println ("--Expected 0");
}
}
Other things to try
- add another method
maxIndexthat returns the index of the largest element - add another method
minIndexthat returns the index of the smallest element - add another method
averagethat returns the average value of the array - Do all of the above with an array of
Stringinstead of anintarray - Do all of the above with an
ArrayList<Integer>instead of and integer Array.
Interfaces
- Make the classes Fish, Cat, and Dog that implement the
Petinterface. We need to compare these different classes according to their life span and the price of their food. - Consider the interface
public interface Checker
{
boolean acceptable(String text)
}
Make a class that implements the Checker interface called Alliteration where it checks if every word in a String starts with the same letter. If is does, it would considered acceptable.
Design
Try The ChessRating project
