These labs are due by 12pm (noon) on Sunday, September 24th. Please make sure that your file names and method names are EXACTLY the same (including capitalization) as I have written them. I have written a java program to grade your assignments automatically, but it will not grade yours correctly if you name your classes and methods differently than I have. Make sure that all files (MathMethods.java, HelloBlueJ.java, StringManipulate.java and Puzzler.java) are in your google drive folder.
Here are some special notes on managing files:
1. PLEASE REMOVE ALL OLDER ASSIGNMENTS FROM YOUR NAMED FOLDER IN GOOGLE DRIVE ONCE THEY ARE GRADED – THEY WILL ONLY CAUSE YOU PROBLEMS. You should save a copy for yourself somewhere else.
2. Because of the way my grader works, you may receive a 0 on this lab (before revisions) if there are any .java files in your folder that do not compile or you have not written all the methods of each class. You have been warned.
3. If you upload a new version of a file, please delete the older version BEFORE uploading the new one. That will prevent version errors and make sure your file saves with the correct name.
--------------------------------------------------------------------------------------------------------------
You can watch a video version of this lesson here (https://www.youtube.com/watch?v=u-6Bv6ODPT8) and (https://www.youtube.com/watch?v=7FLQKJMAq3Q)
For each of these assignments, you should also include a main method in each class, although I will not be grading your main method. Use main to help you test each of your methods before submitting them. Some of these programs include miniature snippets of code you have written in previous labs. You are welcome to go back and copy paste those.
1. Write a program named MathMethods.java (public class MathMethods). This program is a collection of methods which will do various mathematical functions. I have given the method headers for each method and a comment, you will filling out the content of these methods. Your program should contain each of the following methods (start with this code skeleton I have written out):
//use main to call and test your methods. You should include calls to each other method in your main so you can test your methods and make sure they are working. I will not be grading this method.
public static void main(String[] args)
//this function returns the value of the input times 3, then plus 7. So if you call improve(4), it returns 19 (4 times 3 + 7 is 19). You should not use Scanner in this or the rest of the methods - the input is just the part in parentheses.
public static int improve(int num)
//this function returns the sum of a and b
public static double sum(double a, double b)
//this method returns the decimal equivalent of a/b when a and b are both integers (since normally 3/2 would give 1 instead of 1.5, this should return 1.5)
public static double quotientDouble(int a, int b)
//this method returns a to the third power (a cubed)
public static int cube(int a)
//This function should return true if a + b equals c. Otherwise, it should return false. For example, doSum(3,4,7) should return true because 3+4=7, but doSum(3,4,8) should return false.
public static boolean doSum(int a, int b, int c)
//this method should return the distance between points (x1,y1) and
// (x2,y2) using the distance formula (woo math!)
public static double distance(double x1, double y1, double x2, double y2)
//this program returns the nth root of x, recall that the nth root of x is x to the power of 1/n (the third root of x is x to the 1/3 power). Test carefully!
public static double nRoot(double x, int n)
//this method returns the absolute value of x, |x|
public static double abs(double x)
//this function returns the difference between a and b as a positive value. So absDifference(2,4) = absDifference(4,2) = 2
//you can call the abs method you just wrote!
public static double absDifference(double a, double b)
//this method returns x!, so factorial(5) should return 120 and so on. Don't worry about doing anything special if x is negative; you may assume the input will be a positive number. You can assume that x will be greater than or equal to zero. Feel free to copy your code from the last lab.
public static int factorial(int x)
//this method should just print out "computer science is awesome!" x times if x is > 0, if x is 0 or negative it should print a mean message to the user
public static void isAwesome(int x)
//this method returns either a or b, whichever is larger. If they are tied, you may return either.
public static int max(int a, int b)
//this method returns either a or b, whichever is smaller
public static int min(int a, int b)
//this method returns a String that is either "odd" or "even" depending on whether the integer is odd or even (%)
public static String oddOrEven(int a)
//this method returns true if a is a multiple of 10 (like 40, or 6000) but false if a is not a multiple of 10. %
public static boolean isMult10(int a)
Once you have written and tested each of your methods by calling them in main and double checked the spelling, inputs and outputs of each method as well as the name of the class, submit MathMethods.java
2. Write a program called StringManipulate.java that has the following methods. Note that you will have to type in the method headers yourself this time, I am only giving you the name of the methods and the types they input and return:
//again, you should use this for testing your code as you go, i don't care what's in here
main
//this method takes in 2 strings, and concatenates them and returns a string, so concat("cat","dog") should return the string "catdog"
concat
//this void method should input a string and an integer, then print the string that many times
printX
// this method takes in a String and then another String (which you can assume is only one letter), then returns an integer saying how many times that letter appears in the first String, so countLetter("batman","a") should return 2.
countLetter
//this method should input a string and return an integer telling you how many spaces there are in the string
//call the method you just wrote!
countSpaces
//this method should input two strings and return a boolean - true if the first letter is the exact same (including case) and false if the first letter is different
firstLetterEqual
//this method inputs 1 string, and then outputs a string – which should be the same string plus an "s" on the end, or an "es" if it already ends in an 's', or if it ends in a "y", remove the y and add "ies"
//so makePlural("cat") returns "cats", makePlural("happy") returns "happies" and makePlural("dogs") returns "dogses"
makePlural
// this method should input two strings and return a boolean – true if they are the same regardless of upper/lower case, so caseEquals("app","ApP") should be true although .equals() would return false
//remember the .toUpperCase() and .toLowerCase() methods?
caseEquals
//This method inputs 2 String objects in order and then returns a boolean representing true if the first String comes before the second (you will need to use .compareTo to determine which comes first), so comesBefore("bo","call") would return true but comesBefore("call","bo") and comesBefore("call","call") would each return false. You will have to use compareTo.
comesBefore
Once each method works satisfactorily (and you have double checked spelling, capitalization, inputs and outputs), submit StringManipulate.java
3. Write a program called HelloBlueJ.java using BlueJ. Your program should simply output "Hello Blue J" using a println. The point of this exercise is to have you get used to using BlueJ. Make sure that you submit the HelloBlueJ.java file. This is a good time to download BlueJ at home as well - see the resources page on this site!
(if you are having trouble uploading or downloading your bluej projects to google drive, check out this video I made: https://www.youtube.com/watch?v=Hc0B4EHVuCU&feature=youtu.be&hd=1)
4. Write a program named Puzzler.java with the following methods:
firstHalf – this method inputs a string and returns a string representing the first half of the input string, except this rounds UP if there is an odd input. For instance, if you call firstHalf("happy") it will return "hap" but if you call firstHalf("Borish") it will return "Bor"
firstTwo – this method inputs two Strings and returns a boolean representing whether the first two characters are identical. If either String has a length less than 2, it should return false (and NOT cause a runtime error). So firstTwo("boo","b") is false.
caughtSpeeding – this method inputs an int representing your speed, and a boolean representing whether it is your birthday or not, in that order, and returns an int representing the cost of your ticket. If your speed is between 61 and 80 (inclusive), this returns 300 unless it is your birthday, in which case it returns 0. If your speed is greater than 80, it returns 1000, unless it is your birthday, in which case it returns 300. If the speed is less than 61 it returns 0.
Here are some examples:
caughtSpeeding(45,true) returns 0
caughtSpeeding(65, false) returns 300
caughtSpeeding(61, true) returns 0
caughtSpeeding(83, true) returns 300
caughtSpeeding(93,false) returns 1000
countAlike – this method inputs two Strings and then counts how many identical characters they have in the same spots and returns that int. So countAlike("happy", "damp") would return 2, because the characters in locations 1 and 3 (a and p) match.
countAlike("hilly","I am awesome cool") would return 0 because they have no characters in the same spots. Make sure your method does not go out of bounds if the Strings have different lengths!
Write yourself a main method if you like to test your other methods, make sure all your class and method names EXACTLY match what I have, and then submit Puzzler.java
As a reminder, if you are having trouble uploading/downloading with BlueJ, click here: https://www.youtube.com/watch?v=Hc0B4EHVuCU&feature=youtu.be&hd=1
Are you all done, double checked the names of your methods and classes, and want some extra credit? Try this:
Sign up for a codingbat account (codingbat.com), and under preferences, type in my email address (gborish(at)hartdistrict.org) where it says "teacher share."
On the bottom left of the main page, click on all java sections to open all of their exercises, then click on "Strings-3."
Under this heading, complete the assignments:
withoutString
sameEnds
If you take advantage of this extra credit, please email me to let me know!