Mypages.valdosta.edu



CS 1302 – HW 1 This homework has 5 problems which deal with writing custom classes (Ch 9)OverviewYou will write these five classes: BasketballPlayer, Car, Product, Employee, EmployeeUtilities. Documentation for these class is found here. is referred to as Javadoc. In Appendix 1, I provide a brief introduction on how to use Javadoc.I have provided you a complete test class for each of these classes: BasketballPlayer, Car, Employee, EmployeeUtilities. In addition, I have provided you the skeleton for ProductTest which you will write.Draw a class diagram for the Employee and EmployeeUtilities classes.You will fill in the “Status” of each method in each class by typing “C” (for method is correct) or “I” (for method is incorrect or not done) in the indicated cells in the spreadsheet provided in the code download, hw1_completion_status.xlsx.Also, in hw_completion_status.xlsx you will type this Academic Honesty statement followed by your full name:“This homework represents my own work. I understand that I may receive help, but I did not copy any portion of this assignment from anywhere. I understand that a violation of this will result in a Report of Academic Dishonesty.—YOUR FULL NAME HERE”Steps to complete – BasketballPlayer ClassSetup – do the following:Download and unzip: hw01_code.zip found on the HW Page. Inside, you will find a prob1 folder that contains the test classes.Create a Java Project in Eclipse with the name: hw1_lastName.Drag the prob1 folder to the src node in the Package Explorer in Eclipse.Do a quick read of the entire documentation for the BasketballPlayer class. Be sure to select the hyperlink on each instance variable, constructor, and method to see its detail. Or, just scroll down through the page and the details follow the summary. the following:Open BasketballPlayerTest and read the first test:private static void testShootFreeThrows() {System.out.println("-->testShootFreeThrows()");BasketballPlayer p = new BasketballPlayer("Paul");p.shootFreeThrow(true);p.shootFreeThrow(true);p.shootFreeThrow(false);p.shootFreeThrow(true);p.shootFreeThrow(false);int numMade = p.getFreeThrowsMade();int numAtt = p.getFreeThrowsAttempted();double percent = p.getFreeThrowPercent();String msgExpected = String.format("Expected: made=%d, attempted=%d, percent=%.1f%%", 3, 5, 60.0);String msgActual = String.format(" Actual: made=%d, attempted=%d, percent=%.1f%%", numMade, numAtt, percent);System.out.println(msgExpected);System.out.println(msgActual);}Decide what code you need to write to make this test pass and then write the code.Hints: You need these instance variables: name, freeThrowsAttempted, freeThrowsMadeYou need the constructorYou need these methods: getFreeThrowsAttempted, getFreeThrowsMade, getFreeThrowPercent, shootFreeThrow.Re-read the documentation of these membersPsuedo-code for shootFreeThrow:freeThrowsAttempted++if(isMade)freeThrowsMade++Psuedo-code for getFreeThrowPercent:if(freeThrowsAttempted>0)percent = 100.0*freeThrowsMade/freeThrowsAttemptedreturn percentelsereturn 0Do the following in BasketballPlayerTest:Comment out the calls to all tests in main except the first.Hint (from Lab 2, Stage 4): To comment out a block of text, select the text and then press: Ctrl+/. Pressing: Ctrl+/ again removes the comments. Or, you can choose: Source, Toggle ment out all test methods except the first.Make sure and do not comment out the two helper methods at the bottom: buildEmployee1 and buildEmployee2.Run the test and verify the output, debugging as necessary.Open BasketballPlayerTest and read the second test, testShootTwoPointers. Proceed exactly as Step 2 above.Open BasketballPlayerTest and read the third test, testShootThreePointers. Proceed exactly as Step 2 above.Open BasketballPlayerTest and read the fourth test, testGetTotalPoints_OnlyFreeThrows. Proceed exactly as Step 2 above.Hints: The total points is simply: 1*freeThrowsMade + 2*twoPointersMade + 3*threePointersMadeNote in the test code, the example player, p, hit the first two free throws, and missed the third. Thus, they should have 2 points.Tests 5, 6, & 7 are similar. Proceed as above.Finally, write and test the toString method. Generally, I would be writing this as I go along, adding to it as I add new pieces to the class.Steps to complete – Employee ClassDo the following:Do a quick read of the entire documentation for the Employee class. Be sure to select the hyperlink on each instance variable, constructor, and method to see its detail. Or, just scroll down through the page and the details follow the summary. EmployeeTest and read the first test:public static void testGetNumDaysWorked() {System.out.println("-->testGetNumDaysWorked()");Employee e;e = new Employee("Janice", 40.0);e.setHours(0, 2);e.setHours(1, 4);e.setHours(2, 6);e.setHours(6, 8);System.out.println("Expected: num days worked=4");System.out.println(" Actual: num days worked=" + e.getNumDaysWorked());}Decide what code you need to write to make this test pass and then write the code.Hints: You need these instance variables: hours, name, payrateMake sure you understand what the hours array represents.You need to make sure and create the hours arrayYou need the constructorYou need these methods: setHours & getNumDaysWorked.Psuedo-code for setHours(day,hoursWorked)this.hours[day] = hoursWorkedHint for: getNumDaysWorked – Loop over the hours array and count how many days that the hours are greater than 0. Right? There are seven elements representing Mon-Sun, the number of hours worked on each day.Do the following in EmployeeTest:Comment out the calls to all tests in main except the ment out all test methods except the first.Make sure and do not comment out the two helper methods at the bottom: buildEmployee1 and buildEmployee2.Run the test and verify the output, debugging as necessary.Write the getHours(day) method. This is a very simple method, it just returns the number of hours worked on day: hours[day]. Note: I neglected to write a test method that tests this method. Generally, we don’t test methods unless they have some logic. This one doesn’t.Repeat the steps above for the second test, testGetTotalHoursHints: You need to write the getTotalHours method. This method simply loops over the hours array and adds up each of the elements.Repeat the steps above for the third test, testGetWeekdayHoursHints: You need to write the getWeekdayHours method. This is the same as the previous method except that you only loop over the first five elements (Mon-Fri).Repeat the steps above for the fourth test, testGetWeekendHoursRepeat the steps above for the fifth test, testNewWeekHints: You need to write the newWeek method. Loop through all the elements in the hours array and set them all to 0.Repeat the steps above for the sixth test, testMergeEmployeeHints: You need to write the mergeEmployee method. Read the documentation carefully. This is really simple.Repeat the steps above for the next 8 tests, which test the getPay methodHints: Read the documentation carefully for the getPay method.Read the first two test methods carefully. The first one, testGetPay_0_weekday_0_weekend, calculates the pay for someone who didn’t work, thus, their pay should be 0. The second one, testGetPay_lessthan40_weekday_0_weekend, calculates the pay for someone who worked 24 hours over Mon-Wed. Thus, no overtime is needed.Write the getPay method with just enough code to pass these two tests. Do not put in overtime calculations, nor 7-day bonus yet. Also, don’t reinvent the wheel! You need the number o weekday hours, and you have a method to calculate that for you – so use it!Repeat these steps incrementally for the next test method, then the next, etc.Steps to complete – Car ClassDo the following:Do a quick read of the entire documentation for the Car class. Be sure to select the hyperlink on each instance variable, constructor, and method to see its detail. Or, just scroll down through the page and the details follow the summary. CarTest and read the first test:public static void testConstructorWith3Parameters() {System.out.println("-->testConstructorWith3Parameters()");Car c = new Car(15.0, 20.0, 10.0);System.out.println(c);}Decide what code you need to write to make this test pass and then write the code.Hints: You need all four instance variablesYou need the constructor that takes 3 arguments.You need this method: toStringComment out the appropriate code in CarTest, run the test, and verify the output.Repeat for the constructor that takes 2 arguments.Look at the third test method, testFillUp_AmountLessThanRemainingCapacity. Admittedly, that is a mouthful. It could be expressed as, “fill up without attempting to overfill the tank”. No, write the fillUp method. You’ll also need to write the getFuelLevel getter. You probably should go ahead and write the other 3 getters if you haven’t already. Uncomment the appropriate test code, run the tests, and verify the output.Repeat for the next 2 testFillUp… methods.Repeat for the 4 testDrive… methods. The drive method might be a bit challenging. In Appendix 2, I have provided some hints and examples. You should read these carefully. Your approach should be the same as previously: look at one test method (the first, the easiest) and write code to make that test pass. Gradually, add in the complexity of not having enough fuel.Steps to complete – EmployeeUtilities ClassFollow the usual approach: read the documentation carefully, read the first test class, write the method being tested.Hint: See Section 9.18 of my text, Item 3 and Practice Problem 7. There is a similar problem we worked in class and the solution is in an Appendix of the text. There is also a Practice Problem whose solution is in the code download for the chapter.Draw a class diagram for the Employee class and the EmployeeUtilities class. This can be hand drawn. Scan or photograph the diagram (or two if you draw two separate pages). Put the class diagrams in the prob1 package. Note: many times, people do not make these diagrams. I can’t remember the penalty, but it is 10-20 points.Steps to complete – Product & ProductTest ClassesFollow the usual approach: read the documentation carefully. For this problem, you have to write the test cases. However, I wrote the signature of each test method and gave comments that say what the test should do.Hints:The constructor does most of the work.Look at the 3rd character in the code. If it is a digit, then you can assume the plant is 2 characters long. Otherwise, the plant is 3 characters.You will need: Character.isDigit or Character.isAlphabetic, as well as the substring method.Write the constructor, and then write the test methods. Repeat.Note: The best way to go about coding is to write the first test before writing the code! Yes, the code will not compile because you haven’t written the class/code yet. This is called Test Driven Development (TDD) and is part of the software process for many/most companies.Using File Explorer, drag the prob1 into the master branch on the GitHub repo for this assignment by the due date on the Schedule.Grading CriteriaGrading CriteriaPointsBasketballPlayer15Car15Employee15EmployeeUtilities15Product15ProductTest15Class Diagram10Total100SubmissionSubmission guidelines – Make sure:The two class diagrams are in the prob1 package.The completed hw_completion_status.xlsx spreadsheet is in the prob1 package.All java files (including test classes) are in the prob1 package.Your prob1 folder is zipped into a file name: hw1_yourLastName.zip. See Lab 2, Stage 7 if you have question about what should be zipped.Submit in the hw1 dropbox on Blazeview by the deadline.Appendix 1 – Using JavadocJavadoc is a special way to comment your code. Javadoc comments beings with, “/**” and end with the usual, “*/”. When you do this, you can generate a website that shows this documentation nicely formatted in HTML. As an example, this is the Javadoc for the getFreeThrowPercent method in the BasketballPlayer class:/** * Gets the free throw percent made. For example if there were 5 free * throws attempted and 3 were made, then the percent would be 60.0. * If no attempts have been made then the percent is 0.0. * @return the free throw percent made. */public double getFreeThrowPercent() {...}You will not write Javadoc in this class. However, we will point out something important below pertaining to this.When you follow the link for the documentation for this homework: will see a page like this:When you select the BasketballPlayer link, it shows documentation for that classScroll down to the first method, getFreeThrowPercentImportant point: The first sentence of the Javadoc is shown in the Method Summary (same for the Field Summary above). If you follow the link (or simply scroll down), it shows you the details of that method (or field). More specifically, if there are more sentences in the Javadoc, they will be displayed here. For example, look back at the Javadoc for the getFreeThrowPercent method above. Notice that it is several sentences. When you follow the link, the details are shown:Take-away: Don’t forget to follow the links (or simply scroll down the page) to see the details for fields and methods. Some/many fields/methods will not have any additional information (i.e. the Javadoc is only one sentence), but some will and you don’t want to miss that.Appendix 2 – Hints & Examples for drive method in Car classFirst, let’s look at the documentation: public?void?drive(double?time, double?rate)Drives the car for a specified time at a specified speed (rate) provided there is enough fuel in the tank. Otherwise, only drives until the car runs out of gas. The totalDistance and fuelLevel are updated. See the homework assignment for some examples.Parameters:time - The time in hours that the car is being requested to drive.rate - The speed (miles per hour) that the car is being requested to drive.It should be clear that there are two things this method should do:Determine the distance the car travelled and update totalDistance.Determine how much fuel was used and update fuelLevel.It should be clear that there are two cases that need to be considered: There is…enough fuelnot enough fuelHow far did the car travel? Next, consider a basic physics concept. Let:d=distance miles r=rate miles per hour t=time (hours) Then:d=r*t = Distance travelled at given a rate, r, for a length of time, tThus, it is easy to see how far someone wants to drive the car.How much fuel was used? Let:e=fuel efficiency (miles per gallon) Then:f=d/e – Amount of fuel required to travel a distance of d.Example 1 – There is enough fuel. Suppose that a car has a fuel efficiency of 25 mpg and currently there are 10 gal of fuel left in the tank: e=25 mpg fL=10 gal=amount of fuel in the tank, fuel level. Next, suppose someone wants to drive the car for 2 hours at 50 mph:myCar.drive(t=2 hrs, r=50 mph) The desired distance to travel is:d=50*2=100 miles Which requires:f=100/25=4 gal of fuel. Since, f≤fL, then the entire distance, 100, can be driven. Thus:The total distance traveled will be increased by 100 miles.The fuel level will be reduced by 4 gallons: Example 2 – There is not enough fuel. Continuing the previous example, there are now 6 gallons of fuel in the tank:e=25 mpg fL=6 gal=amount of fuel in the tank, fuel level. Next, suppose someone wants to drive the car for 5 hours at 40 mph:myCar.drive(t=5 hrs, r=40 mph) The desired distance to travel is:d=40*5=200 miles Which requires:f=200/25=8 gal of fuel. Since there are only 6 gallons in the tank (f>fL), then the car cannot be driven 200 miles. It can be driven:d=fL*e – Maximum distance that can be travelled for an amount of fuel in tank, fLIn this case:d=6*25=150 miles Thus:The total distance traveled will be increased by 150 miles.The fuel level will be set to 0. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download