Java代写:CSE8A Turtle Shapes and Turtle Fun

使用Turtle库绘制形状,练习GUI开发编程。

Preface

This is the second Cse 8a assignment! This assignment is meant to get you working with Objects, methods, and Object references. This assignment should take longer than PA1, so start early!
Working with a partner is MANDATORY for ALL assignments so you should link on vocareum EARLY!
You will work together on ONE version of the code. You will only submit ONE version of the code between the two of you, however you can submit as much as you want before the deadline. Take advantage of that! (Vocareum syncs submits and uploads between partners, so working separately will result in overwriting each others code!)

How to Get Help

There are two primary ways to get help in this class:

  1. Go to the lab and seek help from a tutor on duty using autograder.ucsd.edu
  2. Go to the discussion board that was outlined during the Lab

Piazza should be used for any questions regarding clarification of the writeup. Especially for long and complex assignments, vague or hard to understand directions are unfortunately inevitable.

Part 0: Commenting and Magic Numbers

Code Comments

You may have noticed that in PA1, drawing 8 letters meant that you code was verrry long. Many of you realized that commenting each letter made things a lot clearer to fix or refer to later. Now, comments will be worth points in your code!

  • We want to see sufficient inline and general comments (using // or / /) in your code!

Another thing that many of you probably noticed in PA1 was the comment block at the top of the file. It had a space for File Name, Authors, and Due Date.

  • Fill these in with the relevant information for EVERY file.
  • Include your cs8as logins after your names in the Authors section

Finally you’ll notice in Turtle.java, once you’ve downloaded it, that certain blocks of code are documented in a special way with block comments to give some information about that section.

  • We will go over these (called header comments) in the next PA, but try to mimic the description when you are writing the methods for this assignment.

Magic Numbers

When you write code, using arbitrary numbers is really bad practice. Why? Because if you ever wanted to change something, you would have to go through and change every time you used that number, along with possibly every line of code relevant to it.
Ideally, we want to set everything up so that variables and numbers used are predefined as constants or multiples of these constants. (0, 1, and 2 are exempt though, depending on how they are used)

  • We’ll explore this more as we dive deeper in the course, but the way to create constants in
    Java is to type the following line right after you declare the class:
    • public static final int VAR_NAME = 10;
  • Make sure that your variable names are meaningful!

Setup

From this homework onwards, we will assume that you have already figured out the setup for your machine and the lab computers. See the writeup from PA1 or post on Piazza if you are still having issues. We will probably stop including this section in future assignments unless there is something different that you have to do.

Getting the Files

If you are not on the Lab computers, you can manually download the starter files from the link provided in the Important Links section.

  1. Getting the files for the assignment will follow the same steps for every assignment (we’ll let you know if it’s different for some reason). Recall from the Labs and PA1, that you copied files from various folders in ~/../public/ to your home directory. Read the Terminal Commands section of PA1 to understand how that works.

  2. Right click on the desktop and select “Open in Terminal” from the drop down menu. Use the following commands to make a directory called Assignments/PA2 and copy the needed files there.

  3. The only difference is to change PAx to match the current PA. a. If you actually go into the public folder, you’ll see some other folders. Don’t use those, they are outdated! We will update the contents of each folder as they become relevant.

Part 1: Turtle Shapes

Part A: Creating Shapes

This part has two subproblems, (The First Method, and Method Overloading and Random Shifts)

The first method

This program requires you to modify CreateShapes.java and Turtle.java. The goal of this part is to draw four copies of the same shape with varying sizes, colors, and line thicknesses. You can see some example output at the end of this document.

Specifications

  1. You must have at least FOUR shapes drawn
    • At least 2 of the 3 parameters must differ from each other
  2. The shapes must be drawn concentrically
    • The center can be defined as any point inside of your shape. It cannot be one of the edges, too close to the edges, or outside of the shape
    • The center does not have to be a real center, but all of the shapes need to be “centered” around that point
  3. The shapes must be fairly complex (ie, more than 5 lines per shape)
    • a. Shapes should not be symmetric
    • b. Curved shapes are ok, but circles are not
    • c. The base shape should be the same (other than the size, color, and line thickness)

Things You’ll Need to Do

  1. You must first modify the Turtle class
    • a. Add a method to the class, called drawShape
      • i. This method will go at the bottom of the file, before the closing brace ‘}’
    • b. This method needs to have the following parameters (See the Sample Code part for some hints on how to do this):
      • i. Int size
      • ii. Color color // you will need to use Java.awt.color;
      • iii. Int lineWidth // how thick the line will be
  2. You must then modify the CreateShapes class
    • a. The code you will be writing for this class will be in the main method
    • b. Create a World for your Turtles
      • i. Don’t make your World too big, it should be no bigger than 1280720 px
    • c. Create Four Turtles (you can name them whatever you want this time)
    • d. For each turtle, call the drawShape method with different parameters to nest the shapes, so that they have the same center point.
      • i. Hint: you may want to define a common starting position in drawShape to get the same center

Method Overloading and Random Shifts

During the Lab (and in class) we talked about the using Random. This time, we’ll be adding and additional functionality to our drawShape method. The catch is, we won’t be modifying the original method at all!

The modification is to accept another parameter: int shift that will shift your shape in a random direction by the number of pixels passed in.

But how do we do it without modifying the original method? Java has the answer, called method overloading. If you have two methods with the same name but different parameters, then Java will be able to differentiate which method to use based on the number and types of input parameters passed!

Things You’ll Need to Do

  1. Copy your original drawShape method, and paste it after the first one. Do not change the name or anything in it yet
  2. In the parameters list, add int shift as the last parameter.
  3. Create a new Random generator Object with the following code (at the beginning of your drawShape method. The import has already been done for you)
  4. Now, you just need to get the random angle that your shape will be shifted in.
  5. It’s up to you to figure out how to use these variables!
    • a. It will be different based on how you did drawShape, there are multiple right ways to do it!
    • b. Hint: one way to do it is to change your Turtle’s initial starting position first by turning by randAngle and moving forward(shift); and then the rest of the logic will usually stay the same!
    • c. Hint2: you can call another method from the same file, even if it has the same name by using this.method().
  6. In your CreateShapes class, you will create one more new Turtle and call drawShapes on it with the additional shift parameter using parameters of your choice (shift must be nonzero).
    • a. When you are done, your final output should be four concentric shapes of varying attributes , and one additional shape that has a different center than the rest.

Part 2: Turtle Fun

Note that this part is separate from Part 1. You do not have to use the drawShape method, but you are welcome to. However, you do have to use a method (don’t just write everything in main)! The goal of this part is to work with references and their relationship to Objects. We’ll be using just two Turtle Objects, but multiple Turtle references! Files to Modify: TurtleFun.java

Specifications

  1. You must create a World for your Turtle in TurtleFun.java
  2. You must create Exactly Two Turtles, and at least Three different turtle references. Name one after you, one after your partner, and call the third stranger.
    • a. Use your official name, instead of nicknames
    • b. Remember that variable names start with lowercase letters, but you can use camelCase for multi-part names!
  3. The two references that are named after you must both point to the same Turtle Object
    • a. The third Turtle reference will be the only reference assigned to the other Turtle
    • b. If you are confused about References and Objects, the lecture slides and discussion slides are a good resource
  4. You must use these three references to draw at least three shapes. Each shape must be drawn with a different reference
    • a. This means that one of the two Turtles will draw two shapes, while stranger Turtle will draw its own shape.
    • b. The shapes drawn must be distinguishable, so think about how to get the shapes to have different center points!
    • c. The shapes for this part must still have 5 or more edges, but for this part only, they can also be symmetric
  5. You may find the documentation for the SimpleTurtle class that we are using helpful. This should be in your textbook, or available to view in the Important Links Section

Star Point

In this course, Star Points are optional, and not worth very many points. They are to challenge those of you who want to do more. The tutors will not be expected to give you any help with these parts, but you may be partially graded on effort if you do them.

For this assignment, there are two different choices for you to pick from. You will only get points for doing one of them.

  1. Draw an elaborate shape for Part A (house, tree, face, 3D shape etc.) and call the method elaborateShape. Whether or not the shape is considered elaborate enough for securing a star point, is up to the judgment and discretion of the graders.
  2. Create a single method(call it drawStarPointShape) which uses more than one Turtle to draw a single shape. You can’t create any Turtle objects inside this method. HINT: you will need to pass at least one Turtle object into your draw method (and thus you will have access to two Turtles–the calling Turtle and the one you passed in). Also both of the turtle should draw half of that shape, so the finished shape should be a symmetric shape. An example is shown in sample outputs

Notes:

  1. Put the star point method into the same Turtle.java file
  2. Create a main method where you called the star point method in a new file called PA2StarPoint.java
    • a. Write in the file header comments which Star Point you chose to do!