CSC 17 Lab 1b. Due one week from date assigned This lab will allow you to practice writing basic Java programs, this time from scratch. Arrays in Java are declared as in: String[] S; // declares S; S is now just a null pointer, not yet an array S = new String[10]; // S is now an array of 10 null pointers. for(int i=0;i<10;i++) S = "string"+i; // string0, string1, string2, etc. // this + operator converts anything to a string. Strings in Java are special kinds of objects. Two strings can be compared by s1.compareTo(s2) (where s1,s2 are non-null string objects). The compareTo method returns an integer. It returns 0 if s1 and s2 are equal, a negative int if s1 comes alphabetically before s2 and a positive int if s1 comes after s2. s1.equals(s2) also tests for string equality. However, s1==s2 only tests for pointer equality. To access individual chars in a string, use s1.charAt(i), where i ranges from 0 to s1.length()-1. Note that you need to call .length() to get the length of a String, but .length to get the length of an array. //////////// PART I 1. Write a (static) function public static boolean inarray(String s, String[] SA) that determines if string s is found in String[] SA. for example, if SA is {"ab","cd","ef"} and s is "cd", then the function should return true. *** A "static" function or variable is something that is independent of *** any specific instance of a class. For example, say we have objects *** that represent bank accounts, then the balance value of each bank account *** is NOT static because each account will have a different balance. But the *** interest rate value can be represented by a static variable because all *** accounts will have the same interest rate. This does NOT mean that a *** static variable cannot change its value, but that any changes are *** shared by all instances of the class. A function that changes the *** interest rate can also be called static, but a function that changes *** the balance cannot be static. *** A static function can only refer to other functions and variables *** within the same class if they are also static. In particular, when *** we call a function from inside main directly, without referring to *** any object, then that function must also be static. That is, if we *** call f() inside main instead of n.f(), where n is some object, then *** f must also be static (because main is always static). 2a. Write a function that takes an array of integers and returns its sum. For example, if the array A points to {2,4,6}, then calling sum(A) should return 12. This should be a static function in the same class as main: public static int sum(int[] A) 2b. Write a function to return the smallest string (as determined by the compareTo function) in an array of strings. If the input String[] is empty, return null. public static String smallest(String[] SA) 3. Write a function to reverse a string: public static String reverse(String s) Hint: given a string s, s='a'+s; will add the character 'a' to the front of the string. When writing these functions, do not print inside the function except for debugging. Return the string to be constructed and print where it's called. 3b. A "palindrome" is a string that reads the same forwards and backwards. For example, "noon" and "bob" are palindromes. Write a boolean function that determines if a string is a palindrome (returns true if it's a palindrome and false otherwise): public static boolean palindrome(String s) 4. Write a function to determine if an array of strings contain duplicates. For example, {"ab","cd","ef","cd"} contain duplicates. 5. Write a public static void main that demonstrates these functions. /////////////// PART II (practice with classes and objects) You're to write a class "team" so that the following code will work. team team1 = new team("giants"); team team2 = new team("jets"); team1.lose(); team2.win(); team2.lose(); team2.printrecord(); // should print "W-L: 1-1" team1.play(team2); // should print "giants win" or "jets win" System.out.println(team1.winningpercentage()); // prints .000 to 1.00 In other words, you must write a class that "implements" the following interface: interface Playable { void win(); void lose(); // optionally, you may also allow ties. void printrecord(); void play(team x); double winningpercentage(); } class team implements Playable ... Write a reasonable simulation of the .play function. To generate a random number, use Math.random(), which returns a double between 0 and 0.9999.... This random number has a "uniform" probability distribution. To generate a random number with a normal, or Gaussian distribution, do: Random rnd = new Random(); double r = rnd.nextGaussian(); This gives a random number with expected average 0 and standard deviation 1. To change the average (ave) and deviation (dev), use r*dev+ave. To write a more sophisticated simulation, you may want to bias the random number based on, for example, the existing w-l record of each team. Now construct an array of teams with the following names String[] fbn = {"Giants","Jets","Rams","Patriots","Falcons","Steelers","Packers","Eagles","Chiefs","Bills","Seahawks","Cowboys","Chargers","Raiders","Dolphins","Saints","49ers","Broncos"}; ** write code to have every team play every other team once (each team will play 17 games) write code to print out the team with the highest winning percentage. write code to print the teams in sorted order, by winning percentage (this part is a challenge unless you know a sorting algorithm). --- // part II challenge. Add ties using inheritance.