shorten a bit?
This lab is due at 11:59 PM on Monday, October 23th.
Video version of the lesson can be found here: https://www.youtube.com/watch?v=Yg0X-XfzLb0
Please write and submit the following java files. You are responsible for testing your own classes, which I recommend you do by using toString and a driver class. As always, you may put all your classes in a folder, just make sure that the names are spelled EXACTLY as I have done for all classes and methods.
1. Write a Date class (Date.java) that will have 3 instance variables (all ints) for month, day, and year. You should have the following methods written in your class:
-a constructor that inputs the month, day, year in that order ( for instance new Date(5,15,2014) should create a date that represents May 15, 2014).
-getMonth() //these are all accessors with no inputs that return the instance data (as ints)
-getDay()
-getYear()
-nextYear() //this void method takes no inputs and increases the year by one, so if your date is 5/15/2012, this should change the instance variables so the object represents 5/15/2013.
-nextDay() //this void method takes no inputs and should advance the day by 1 day on a calendar, so from 5/15/2012 to 5/16/2012 or 3/31/1991 to 4/1/1991. The tricky part is getting it to wrap around at the end of a month (including at the end of the year) and you may need to write several cases for different months. You may ignore leap years if you like.
-nextDays(int n) //this void method should advance the day by n days. I recommend calling the nextDay() method n times, where n is the input that is given to this function.
-sameDay(Date other) //this method will compare the current date to another Date and return a boolean. It should return true if the current date and other they have the same month, day, AND year. Otherwise it should return false.
-a toString() method that returns the date in the form "5/14/2019" just without the quotes (month/day/year, American style). Make sure your form is exactly like this, no spaces, as I will use this to test out your class.
You should test the methods of this class in a driver by creating a couple Date objects (Date d1 = new Date(...) ) and calling each method on them in a Driver class. Once you are satisfied, submit Date.java AND DateDriver.java
2. Write a StreetSign class which will represent a Street Sign. A sign holds a String message that is broken down into individual lines that store 10 characters each. The constructor for your class will input a String, and you will store the message in an array of Strings (each with ten characters, although the last one could be less).
(Hints for the constructor: first, divide the length of the String by 10 to help you determine the length of the array. Then loop through and fill up the array one spot at a time. You will need a separate case for the last spot as the length of that string may be shorter. The constructor will be the majority of your code in this class.)
Your class should also contain the following methods in addition to a constructor:
-numberLines() - this method takes no inputs and returns an int representing how many lines are on the sign.
-getLine(int x) - this method inputs an int and returns the String in that line. If the input would be out of bounds, this method should return a blank String ("");
-printSign() - this void method takes no inputs, and prints out each line of the sign on a separate line.
Here is an example code of what your class should produce if tested in a Driver.
//in your class, this should store the data in an array with {"WARNING: g","razing cow","s ahead"}
StreetSign caution = new StreetSign("WARNING: grazing cows ahead");
System.out.println(caution.numberLines()); //prints 3
System.out.println(caution.getLine(0)); //prints "WARNING: g"
System.out.println(caution.getLine(2)); //prints "s ahead"
System.out.println(caution.getLine(7)); //prints a blank line ""
caution.printSign(); //prints "WARNING G", "razing cow", "s ahead" on three separate lines
Test this in a driver, then submit StreetSign.java.
3. Write a Fraction class that will represent how to store a fraction in java. This class should just have 2 instance variables (int numerator and int denominator) that represents the numerator and denominator of a fraction. We are going to get java to add, multiply, subtract, and divide fractions.
The constructor method should have 2 input parameters (integers for the numerator and denominator) and set the instance variables to be those.
Write two basic accessor methods:
getNumerator()
getDenominator()
Your toString method should return your fraction in the form "3/4" for the fraction three fourths with no quotes but with a slash in the middle and no spaces.
Here are other methods you need to implement:
gcf(int a, int b) //This method should input two ints (assume both are positive) and return the greatest common factor of those numbers. For example, gcf(8,12) is 4, gcf(10,5) is 5, gcf(4,7) is 1. The easiest way to do this is to loop through every integer from 1 through a, and see which one of those is the biggest number that goes into both a and b without a remainder. Remember that x%y is 0 if y goes into x evenly.
This method has nothing to do with the instance variables of this class, but it will help you with the next method. You could have written this method a month ago.
simplify() //this void method with no inputs could change the instance variables and turn 2/4 into 1/2, for example. You will need to use the GCF method. Basically, find the GCF of the top and bottom, then divide both top and bottom by that.
NOTE: Simplify might not work very well if you have a fraction that is negative or zero. For now, please do not worry about this!
public Fraction multiply(Fraction other) //this method multiplies the current fraction by another fraction, which is the input to the method. It then creates and returns a different Fraction object representing their product (which should be simplified after you do the multiplication). This should not change the instance data of either fraction.
/* The general setup for this method should be something like this:
public Fraction multiply(Fraction b)
{
//here you would put the code to multiply this object and b, call simplify and do whatever.
Fraction ans = new Fraction(bla bla bla);
return ans;
}
So if I have:
Fraction a = new Fraction(2,3);
Fraction b = new Fraction(1,2);
If I call a.multiply(b) it should return a Fraction object representing 1/3.
*/
divide(Fraction other ) //divides the current fraction by another fraction, returns a fraction that is simplified
add(Fraction other ) //etc., this one adds your current Fraction to the input and returns another one. Think about how fractions are added. Instead of trying to find a lowest common denominator, it will probably be easier just to multiply each fraction by the denominator of the other one on top and bottom. Make sure to simplify after you do the adding. Just like with multiply and divide, it should input a Fraction and return another Fraction.
//HINT: for add, do the simplifying AFTER you combine the two fractions.
subtract(Fraction other ) //this returns the Fraction you would get by doing the subtraction of the current Fraction minus other.
4. Write a FractionMath class that will basically act as a bunch of methods using your fraction class. It will not have a constructor, it will just be a series of five static methods like we have been doing before this chapter. Particularly, these first four methods should just be one or two lines and call methods inside the Fraction class (do not, for instance, rewrite your method for fraction adding, you should be able to just call it like a.add(b) and return that).
public static void main() //this is where you will call and test the other methods
public static Fraction fadd(Fraction a, Fraction b) //each of these methods should be 1-2 lines long
public static Fraction fsubtract (Fraction a, Fraction b)
public static Fraction fmultiply(Fraction a, Fraction b)
public static Fraction fdivide(Fraction a, Fraction b)
//Also write this method, it should input an array of Fraction objects, and return the Fraction object with the largest denominator. For example, if you send it an array containing 4 fraction objects representing (1/7),(2/3),(4/5) and (-2/9) it should return the Fraction object which represents (-2/9). In case of a tie, you can return whichever you like.
public static Fraction maxDenom(Fraction[] fracs)
//Test your maxDenom method thoroughly! Create an array of Fraction objects in main and call this method on it to make sure it returns the correct thing.
Submit Fraction.java and FractionMath.java