i have a computer science 201  assignmenti need to use java to modify a program 

profileabutid
cmpt_201_homework_2.htm

 

CMPT 201 Westminster College Homework 2 Due Thursday, September 4, 2014 at 11 pm

 

Before You Begin - Time Estimate

Before you begin this assignment, read through this description and estimate how long it will take you to complete this assignment (designing, programming, commenting, testing, and debugging). Write this estimate down in your README. Then keep track of the time you spend on this assignment. While completing this estimate and measuring your time will be part of your grade, you will not be graded on how long the assignment took or how close your estimate was.

Estimating the time it takes to develop software is very difficult, even for experienced programmers. Comparing your estimates to your real development time should help you improve your estimates.

Install the MediaClasses folder 

This homework requires the media classes from the class website. They are already installed on the F:\ drive, but if you plan to use your own computer, you will want to download this zip file to your own computer. You will then need to unzip the zip file and add the new directory to Dr. Java's classpath:

  • In the Edit menu, click on Preferences.
  • In the Categories list, Resource Locations should be highlighted. Click on Add.
  • Select the F:\CLASSAPS\javaclasses\cmpt201\mediaClasses folder. Click on OK. (On your own computer, select the location where you have the mediaClasses folder.)
  • Click on Reset in the top right of the Dr. Java window, to reset the Interactions Pane.
Double-check that your classpath is set correctly, type the following in Dr. Java's Interactions Pane:
    World world = new World();

    If a window pops up, everything worked and you can move on to the rest of the homework.

    On your own machine, you may need to open the Turtle.java class in Dr. Java and compile it, to get this code to work on your own computer. This step is not necessary if you are using the F:\ drive.

    Program 1: Turtle.java

    You should start with your Turtle.java class from this week's lab and this Homework2.java class. If you are starting with the answer to this week's lab, MAKE A COPY of Turtle.java (the answer to the lab) before modifying it for homework. Store the two Turtle.java files in different folders. If you don't have your answer to the lab, you can start with the original Turtle.java instead.

    You will be writing two methods in the Turtle class (Turtle.java). The second file, Homework2.java, will look like Lab2.java. It should only have one method: a main method. 

    drawTriangle

    Begin by writing the drawTriangle method:

         public void drawTriangle(int side)
    
    This method should draw an equilateral Triangle with the specified side: Hint: use the turn() method to get the correct angle of rotation.  Remember, each interior angle of an equilateral triangle has an angle of 60 degrees. Don't forget to use the interactions pane to figure out how to get your turtle to draw the triangle. Most people will not get this on the first try, so experiment!

    Save and compile.

    main

    Then modify the main method in Homework2.java so the Turtle object draws an equilateral triangle. Compile and test your code.

    Once you have your code working, comment out the line of code that calls drawTriangle in your main method, and start working on drawStar.

    drawStar

    Now write a drawStar method that draws a 5-pointed star with the specified side.

         public void drawStar(int side)
    
    The star should be drawn “upright” no matter what the Turtle’s current heading may be. In other words, the same star should be drawn if the Turtle is facing up or if it is facing down. The diagram below shows what I mean by upright:

     

    Hints:  The lines forming a point of the star form an angle of 36 degrees.  The Turtle class has a method:      setHeading(double heading) that can be used to set a Turtle’s heading.  Turtles are initially created with a heading of 0.0.

    main

    Then modify the main method in Homework2.java so it draws a star with a side of 100.

    Compile and test your code.

    If it works, then modify the main method so it draws three stars. The smallest star should have a side of 100 and the largest should have a side of 260.  The picture should look something like this:

    It's okay if your three stars are close but not exactly centered over the same point, as long as you are drawing the three stars with THREE drawStar method calls.

    Hint: There are many ways of writing the drawStar method. The easiest may be to have the Turtle begin and end at the top of the star (facing up). Between each drawStar method call, you should have the turtle was forward 40 steps. A harder way would be to modify your drawStar method so it begins with the Turtle in the middle of the star. That will make it easier to draw the three stars centered at the same place.

     

     Program 2: TimeConverter.java

    We have learned about the arithmetic operations % ("mod") and / (integer and floating-point division). To help you understand how useful the operations % and / are, examine the ChangeMaker program developed in your textbook in Section 2.1 of your textbook (pages 74-78 in the sixth edition or pages 69-73 in the online fourth edition). In a Dr Java interactions pane, manually try out the calculations from page 76 (72-73), to see how this math works. Notice we are purposely not typing the semi-colon at the end of each line so that we can see the value of each expression:

        int amount, originalAmount;
        int quarters, dimes, nickels, pennies;
        amount = 87
        originalAmount = amount
        quarters = amount / 25
        amount = amount % 25
    

    In your Readme.txt file, explain what amount represents at this point. Your answer should be more than "the amount is the remainder after dividing by 25", but rather how amount relates to the coins.

    Continue typing in the Interactions pane:

         dimes = amount / 10
         amount = amount % 10
         nickels = amount / 5
         amount = amount % 5
         pennies = amount
    

    Using ChangeMaker.java as a model, now start a new class called TimeConverter.java. This class will consist only of a main method. You should prompt the user for the number of seconds, and then calculates the number of hours, minutes and seconds that time represents. Then print out in a user-friendly manner, the number of hours, minutes and seconds.

    Finally, to double-check the accuracy of your work, perform the reverse calculation at the end of your main method: using the number of hours, minutes, and seconds, calculate the total number of seconds. Print out the new number of seconds. Hopefully, it matches the original entered number!

    Here is an example of my program running:

    > run TimeConverter Enter the total amount of seconds. 9999 There are exactly 2 hour(s), 46 minute(s), and 39 second(s) in 9999 second(s). Double checking... 9999 seconds

    Extra Credit

    Start a new class called ElapsedTime.java. Your main method should:

    • Ask the user for two times, in military time. Military time is the 24-hour clock system, where midnight is 00:00:00, and noon is 12:00:00, and 7 pm is 19:00:00. You can read more about the 24-hour clock system here.

      You can ask the user for hours, minutes, and seconds separately. (In other words, first ask the user for the first time's hours and read that value. Then ask for the first time's minutes, and so on.) You do not need to handle invalid times (for example, 27:00:00 or 13:83:75).

       

    • Check which time is earlier. Your program should calculate the same elapsed time, regardless of whether the user enters the earlier time first or second.

       

    • Report the amount of time (hours, minutes and seconds) between the two input times.

      For example, the time between 19:30:45 (45 seconds past 7:30 pm) and 12:00:00 (noon) is 7 hours, 30 minutes and 45 seconds, so your program might print out:

      The elapsed time between 19:30:45 and 12:00:00 is 7 hours, 30 minutes, and 45 seconds.

        

    Upload the following files to Canvas:

    • Readme - a text file with:
      • whether you completed the assignment successfully
      • whether you completed the extra credit
      • a time estimate and the actual amount of time it took to complete the assignment (see the top of this assignment)
      • your answer to the green question (what does amount represent at this point?)
      • one sentence on how the assignment went (did you learn anything?)
    • Turtle.java
    • Homework2.java
    • TimeConverter.java

    Don't forgot to add a comment at the top of every Java program, with your name and a short explanation of what the program does. You should use descriptive variable names and indent your code properly (in Dr. Java, select all the code (control-a on a PC, command-a on a Mac) and hit "tab"). Comment any part of your code that may be confusing.