Lab 1: Static and Exceptions
For a video version of this lab, watch this video on exceptions (https://www.youtube.com/watch?v=JQqYzU9Ms1I ) and these on the static keyword (https://www.youtube.com/watch?v=tj3q7eVHU3M and https://www.youtube.com/watch?v=lgbQebgKH5w ).
This lab will be due on Sunday, January 14 at 12pm (noon) in your named google drive folder. You will have this week to work in class and at home on this assignment. Do note that the last assignment (number 3) is the longest so make sure to give yourself enough time for that one. As always, ensure that your method names and spellings match mine exactly. If you have any questions along the way, feel free to email me and I will try to respond as quickly as possible. I encourage you to help each other in class as well!
1. Write a class named Homework.java. Each object of this class will represent a homework assignment. This class will have the following variables:
Non-static variables (make these private):
-a String representing a description of the assignment.
-an int representing the number of points the assignment is worth.
Static variables (make these public. Remember to set up an initial value for each of these, including the ArrayList. Set it to be an empty ArrayList):
-an int counting how many Homework assignments have been created.
-an int counting the total number of points of all the assignments that have been created.
-an ArrayList<String> containing the descriptions of all Homework objects //update this with your constructor every time you create a new Homework object.
(NOTE: if you are getting odd values for your static variables and they keep adding on new values, try clicking on the loop button in the lower right corner of your bluej project window and resetting the virtual machine.)
Write the following methods:
Non-static methods:
a constructor with inputs of (String desc, int points) – your constructor should input the string describing the assignment and the number of points in that order. The constructor should set up the two instance variables AND appropriately update the three static variables!
String getDescription() //accessor methods
int getPoints()
changePoints(int n) – this void method should change the current assignment’s point value to n. Be sure that it correctly updates your static variable representing the total number of points correctly as well.
toString() – design a toString that makes sense in representing the Homework object
Static methods:
printAssignments() – this void method should simply print out the static ArrayList that represents the descriptions of all homework assignments
numAssignments() – this method should return an int representing the total number of assignments (your static variable)
getTotalPoints() – this method should return an int representing the total number of points of all Homework objects.
fewestPoints(Homework a, Homework b) - this method should input two Homework objects and return a Homework object. Specifically it should return whichever Homework is worth fewer points. (in case of a tie, you can return either one)
2. Write a driver for the Homework class. Inside this driver, write a main method that calls, tests, and prints results from each method in your Homework class. It should create several Homework objects and call each method and public variable from your Homework class. I am just going to be looking at this to make sure you did it - I want you to be able to show me you know the difference between calling static and non-static methods.
3. Write a class Exceptional.java. This class will be a collection of static methods which about working with an array/arraylist and throwing exceptions. ALL METHODS SHOULD BE STATIC (so I will call them using Exceptional.intPower(2,3) for instance). Write the following methods:
-main() - I will not be looking for anything here, but you should write and use this method to test the other methods
-worthMost(Homework[ ] projects) - this method should input an array (not ArrayList) of Homework objects and return a Homework object. It should return the Homework object that is worth the most points. It should also print out (using System.out.println) what percent of the total points (the static variable representing all the points) that assignment is worth.
For example, if projects contains four Homework assignments (h1,h2,h3,h4) that have point values of 20,60,5,15, your method should print out that the max assignment is worth .60000 (or 60%) of your overall grade (100 total points, 60/100), and then return that assignment (h2) in this case.
-zeroLeast(Homework[ ] projects) – this void method should input an array of Homework objects. It should figure out which Homework object is worth the least points and change the number of points that assignment is worth to 0. For example, if your array has four assignments with point values of 20,60,5,15, it should change the 5 so the values are now: 20,60,0,15
-removeNines(ArrayList<Homework> assignments) - this method should input an ArrayList<Homework> and return an ArrayList<Homework> with all assignments worth a number of points ending in 9 removed from the list - it should remove assignments worth 9, 49, or 4009 points but keep assignments worth 90 or 46 points. For example, if you send in an ArrayList containing Homeworks worth: 5,49,49, 909, 6 and 9 points, it should return an ArrayList containing only the two Homework objects worth 5 and 6 points. Be careful removing...
-intPower(int x, int y). This method should input 2 ints x and y then return an int representing x^y (x raised to the y power), so if I call intPower(3,4), it should return 81, which is 3^4. However, if y is negative, your answer will not be an int, so make sure that your program throws an ArithmeticException("you can choose whatever error message you want") if it is called with a negative y-value.
//you may either calculate powers yourself with a for loop or use Math.pow(x,y). If you use the latter, make sure you cast it properly.
-capFirst(String input) - this method should input a String, and return that String with the first letter capitalized. It will capitalize the first character, probably using toUpperCase(). So capFirst("kanyay west") will return the String "Kanyay west".
-diagonals(int[][] arr) – this method should input a 2d array of ints and return the product of the numbers on the diagonal from the top left to the bottom right. For example, if the array is {{4,5,0},{-2,-1,7}.{9,8,3}}, it should return 4*-1*3 = -12. If the arr is not a square array (so the number of rows and columns are not the same, so if it was 3 by 4 or 4 by 3), this method should throw an IndexOutOfBoundsException( ) and not return anything.
-biggerThan10(double[][] arr) – this method should input a 2d array of doubles and return a boolean. It should return true if every entry in arr is bigger than 10 and false if there is at least one entry that is not bigger than 10. For example, if arr is {{11,12,12.2},{16,1113.2,15}} it should return true. If arr is {{11,12,12.2},{-4,1113.2,1}} it should return false.
-sumCol(double[][] arr, int c) – this method should input a 2d array of doubles and an int and return a double representing the sum of the entries in column c of that array. For example, if arr is {{4,5,0},{-2,-1,7}.{9,8,3},{8,1,12}}, and I call sumCol(arr,2), it should return 0 + 7 + 3 + 12 = 22.
-canSplit(int[] nums) – this method should input an array of ints and return a boolean. Specifically, it should return true if the array could be split such that all the entries up to some spot in the array have the same sum as all the entries after that spot. For example:
If nums is [5,6,1,10] – this should return true since 5 + 6 = 1 + 10
If nums is [8,2,1,2,4,3] – this should return true since 8 + 2 = 1 + 2 + 4 + 3
If nums is [9,11,12,25] – this should return false since there is no way to split it in half evenly
HINT: Loop through all the possible spots in nums, and check both sides of that spot. You will probably need a loop inside your loop.
-remove(String[] words, int x) - this method will input an array of Strings and an int x, and "remove" the String in location x from the array, returning the changed array. Your solution should NOT include ArrayList objects.
Note that if:
String[] words is the array ["ham","bob","turkey","roast beef"];
words = remove(words,2);
//now words should be the array ["ham","bob", "roast beef"];
Make sure that if throws an ArrayIndexOutOfBoundsException with a cute message if x is out of bounds (like I tried to remove spot 4 or 8 or -2 from the above example).
HINT: you will need to create a new array. Have separate variables to keep track of where you are in each of the two arrays.
-mostCommon(ArrayList<String> list) - This challenging method inputs an ArrayList of String objects and returns the String object that shows up the most frequently in the list. Use .equals to compare the Strings. So if this method is sent an ArrayList that is: ["sassy","jim","james", "jane","jim","sassy","joe","jim","jane"] , it should return the String "jim" because it appears three times.
HINT: loop through the combined list and for each element, check how many times that String appears in the list. Keep track of that maximum amount!
TEST ALL OF THE METHODS IN EXCEPTIONAL THOROUGHLY! There are some tricky methods here.
Submit Homework.java, HomeworkDriver.java and Exceptional.java by Sunday!
If you finish early, you can work on assignments for other classes or you can research online how to make your own exception class, like a BorishException or a YouAreAChumpException that you can put in your own projects.