Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this assignment please using Dr.Java, all parts of it, thank you!! World and Turtle Class provided at the bottom! Part 1 Create

Need help with this assignment please using Dr.Java, all parts of it, thank you!! World and Turtle Class provided at the bottom!

Part 1 Create basic letter methods (35%)

Your job in this part of the Assignment is to create four methods, one each for the first four unique letters in your last name. (You must create four unique methods - so a last name of Waller needs methods for w, a, l and e). If you dont have four unique letters in your last name start adding the first letters of your first name. As with the example exclamation point and question mark methods in the provided code, your letter methods must take a Color parameter and use that color when drawing the letter. Use the capitalized version of each letter (i.e. you should draw A, not a). You can start by copying the code from Appendix I into the Turtle.java class file in DrJava.

Each letter method has to follow these rules:

o Assume a rectangular letter drawing area that is 100 pixels high and 80 pixels wide. Each individual letter must fit within an area this size area (one area per letter, not all letters within this one area).

o To draw a letter you may move the turtle anywhere you like within this rectangular

box and you can pick up or put down the pen as often as you like.

o Assume that the turtle is already facing north and at the bottom left of the letter area

when the method is called.

o Assume that the pen is up (so it must be put down to draw the letter).

o After the turtle is done drawing the letter, the pen should be picked up, and the

turtle must move to the bottom-right of the letter area. The turtle should then be

moved over 10 pixels (for letter spacing) with the pen up.

o Finally, the turtle should be turned to face up (i.e., ready to begin the next letter).

o Your letter method name must follow this format: draw(Color c). For

example, a method to draw A must be called drawA(Color c). We will be using

the Interactions pane to call and test your methods, so you MUST follow this format.

To test each letter method you write, use the DrJava Interactions pane. First, compile your

code and fix any syntax errors. Once the code compiles correctly, go to the Interactions

pane and create a world and a turtle. Then call your letter drawing method with your turtle to see if it looks right in your world. More details on this in Appendix I. Note that it will probably take you many tries to get the letter drawing right for each letter, especially complicated, round parts of letters like O or R. We suggest planning out the turtles path with a pen and paper. It is not necessary to actually draw the round parts of letters as circles or arcs you can approximate with a series of straight line segments if you want to.

Also, you should read Part 3 before you plan how youre going to write your code for Part 1.

In Part 3 youre going to have to draw letters that are two or three times as big as the ones youre drawing now. Keep that in mind as you design your letter drawing methods for this part.

Part 2 Create letter methods with a bold option. (10%)

Do this part of the Assignment once Part 1 is complete. Copy your four methods from Part

1 and paste them into Turtle.java so that you have two copies of each method. Rename one

of each of your methods so that it is called drawBold(Color c, boolean bold). For example if you had a drawA(Color c) method for Part 1, you should also now have a drawABold(Color c, boolean bold) method for Part 2.

The idea behind the four methods you create in this part is that they should draw the letter

exactly the same as in Part 1, except that we now give the option for the letter to be drawn bold or not. The bold parameter is a boolean parameter that can either be true or false. If it is true, then the letter should be drawn with a thicker line (thickness of 4). If the bold parameter that is passed in is false, then the pen thickness should be the default thickness, which is 1.

You will need to use an if statement like the following to test the bold parameter that is

passed in, and change the line thickness accordingly, before the turtle draws the letter.

if (bold)

{

// put code here that should execute if the bold parameter is true (set line thickness to 4)

} else {

// put code here that should execute if the bold parameter is false

(set line thickness to 1)

}

It is your job to put the code above at the beginning of each of your four new letter

methods, figure out what method you need to call in order to change the line thickness, and replace the comments in the code above with the right method call. When you think you have it right, compile your code and use the Interactions pane to test your new methods. You should test that you can use the bold methods to draw the letter bold and normally. You might want to try something like this in the Interactions pane (assuming you have a turtle object t, and letter methods for A):

> t.drawABold(Color.red, false)

> t.drawABold(Color.blue, true)

> t.drawA(Color.green)

The code above should give you a red, normal thickness A; followed by a blue, bolded A; followed by a green, normal thickness A. That last part (making sure that the green A is NOT bolded) requires you to think a bit

Part 3 Create letter methods with bold and size options. (25%)

Do this part of the Assignment once Part 2 is complete. Copy your four methods from Part

2 and paste them into Turtle.java so that you have three copies of each method. Rename one of each of your methods so that it is called drawBoldSize(Color c, boolean bold, int size). For example if you had a drawABold(Color c) method for Part 2, you should also now have a drawABoldSize(Color c, boolean bold, int size) method in Part 3.

This part of the Assignment is the trickiest. You need to figure out how to draw your letters

at three different sizes, depending on which integer is passed in. Here is how it should

work:

> t.drawABoldSize(Color.red, false, 1) // should draw normal size, just like you have been doing already

> t.drawABoldSize(Color.red, false, 2) // should draw letters twice the size (maximum 200 pixels tall, 160 pixels wide)

> t.drawABoldSize(Color.red, false, 3) // should draw letters three times the size of the original ones (maximum 300 pixels tall, 240 pixels wide)

To do this, you will need to use some kind of a length expression in your methods so that your drawing distances are normal, double the distance, or triple the distance. Inside your drawing methods you will have to figure out how to compute this expression based on the parameter that is passed in and then use it appropriately in the turtles drawing commands. Hint: remember that you dont have to limit the argument of a method like a forward() command to a single variable; you can use an expression like x+10 (as long as youve already assigned a value to the x variable). Expressions might prove useful in writing this code.

This part of the Assignment is the most challenging and we are providing the least guidance. But, we are confidant you can figure it out. We suggest you think about the problem with a pen and paper before trying to write the four methods.

If you do this part correctly, you should be able to call the following methods from the Interactions pane (assuming a turtle named t has been created in a world):

> t.drawABoldSize(Color.blue, true, 1)

> t.drawA (Color.red)

> t.drawABoldSize(Color.green, false, 3)

> t.drawABold(Color.black, true)

> t.drawABoldSize(Color.blue, false, 2)

This should draw a normal size, bolded, blue A; a normal size, normal thickness, red A; a

large size, normal thickness, green A; a normal size, bolded, black A; and finally a medium

size, normal thickness, blue A.

Part 4 Create a collage with your letter methods. (10%)

This is the most fun part of your Assignment get creative and use your letter drawing methods to make a beautiful collage with the letters in your name. You are also welcome to use any of the shape drawing methods created during class labs or any that you want to make up.

Use the provided code in Appendix II to get started. There is already an example method in the provided code that draws the character sequence ?!?!. You must create a similar method called writeName() that writes the four letters of your last name (or more

letters, if you have repeat letters if your name is Waller, we would expect your method to

write WALLE).

In the main() method of LetterCollage.java, call your writeName() method from different turtle positions and with different colors to create a collage of your last name. Overlapping and writing at interesting angles is great, if it looks good, but we must be able to see your name clearly somewhere in the image.

Be aware that the Turtle class restricts you from having the turtle move or draw beyond

the edges of the screen.

Note that you can earn a stamp for making a really interesting or beautiful collage!

Coding Style (15%)

This grade is awarded for proper coding style. This includes:

Appropriate method, variable, field, object and class names

Proper indentation

Good commenting (explains what code is doing)

Well-organized, elegant solution

HERE ARE THE CODES TO START:

Appendix I Starting Code

Copy the following code into your Turtle.java file. These two methods create a question

mark and an exclamation mark, and demonstrate how each character must take up a certain amount of space, and meet the other requirements (like the starting and ending positions of the turtle).

public void drawQuestionMark(Color c) {

// assumes we begin facing up (North) with penUp,

// and that we are at the bottom-left of where the letter is

supposed to appear

// first move over while pen is up this.turnRight();

this.forward(38);

// now put pen down, set color

this.setPenColor(c);

this.penDown();

// draw vertical part of bottom of question mark this.forward(4);

this.turnLeft(); this.forward(4); this.turnLeft();

this.forward(4); this.turnLeft(); this.forward(4); this.turn(180);

// move up a bit this.penUp(); this.forward(10);

// draw stick below curve this.penDown(); this.forward(20);

// draw round part of question mark int angle = -40;

int dist = 15; this.turnRight(); this.forward(5); this.turn(angle); this.forward(dist); this.turn(angle); this.forward(dist); this.turn(angle); this.forward(dist); this.turn(angle);

this.forward(dist); this.turn(angle); this.forward(dist);

// move to bottom right of letter area this.penUp();

this.turnLeft(); this.forward(40); this.turn(20); this.forward(30);

// move to next letter position` this.turnLeft(); this.forward(35); this.turnLeft();

} // end of question mark method

public void drawExclamationMark(Color c) {

// assumes we begin facing up (North) with penUp,

// and that we are at the bottom-left of where the letter is

supposed to appear

// first move over while pen is up this.turnRight();

this.forward(38);

// now put pen down, set color this.setPenColor(c); this.penDown();

// draw bottom of exclamation mark this.forward(4);

this.turnLeft(); this.forward(4); this.turnLeft(); this.forward(4); this.turnLeft(); this.forward(4); this.turn(180);

// move up a bit this.penUp(); this.forward(10);

// move right a bit

this.turnRight();

this.forward(2);

this.turnLeft();

// draw top of exclamation mrk this.penDown(); this.forward(65);

// move to bottom right of letter area this.penUp();

this.turn(180); this.forward(75); this.turnLeft(); this.forward(25);

// move to next letter position`

//this.turnLeft(); this.forward(35); this.turnLeft();

} // end of exclamation mark method

To test this code, compile Turtle.java and then go to your Interactions pane and create a world and a turtle. Assuming you called your turtle t, you should be able to see how the above methods work by typing:

> t.drawQuestionMark(Color.red)

> t.drawExclamationPoint(Color.blue)

(You will be prompted to import the java.awt.Color library, so go ahead and click okay

to do this.) All your letters will be drawn in the world you created.

Appendix II Collage Starting Code

Create a new file in DrJava and copy the following code into it. Save the file as LetterCollage.java in the same directory at Turtle.java. Compile the code and run it. Right now it creates a collage using only the question mark and exclamation point methods, and its not a very interesting collage. You can use this file to create methods that will allow you to make a much more creative collage. Have fun!

import java.awt.Color;

public class LetterCollage {

public static void main(String [] args)

{

World w = new World();

/* Create a turtle named t */

Turtle t = new Turtle(100, 100, w);

t.penUp();

drawPattern(t, Color.orange);

t.moveTo(65, 300); t.drawQuestionMark(Color.red); t.drawExclamationMark(Color.blue);

}

public static void drawPattern(Turtle t, Color c) {

t.drawQuestionMark(c);

t.drawExclamationMark(c);

t.drawQuestionMark(c);

t.drawExclamationMark(c);

}

}

World Class:

import javax.swing.*; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.util.Observer; import java.awt.*;

/** * Class to represent a 2d world that can hold turtles and * display them * * Copyright Georgia Institute of Technology 2004 * @author Barb Ericson ericson@cc.gatech.edu */ public class World extends JComponent implements ModelDisplay { ////////////////// fields /////////////////////// /** should automatically repaint when model changed */ private boolean autoRepaint = true; /** the background color for the world */ private Color background = Color.white; /** the width of the world */ private int width = 640; /** the height of the world */ private int height = 480; /** the list of turtles in the world */ private List turtleList = new ArrayList(); /** the JFrame to show this world in */ private JFrame frame = new JFrame("World"); /** background picture */ private Picture picture = null; ////////////////// the constructors /////////////// /** * Constructor that takes no arguments */ public World() { // set up the world and make it visible initWorld(true); } /** * Constructor that takes a boolean to * say if this world should be visible * or not * @param visibleFlag if true will be visible * else if false will not be visible */ public World(boolean visibleFlag) { initWorld(visibleFlag); } /** * Constructor that takes a width and height for this * world * @param w the width for the world * @param h the height for the world */ public World(int w, int h) { width = w; height = h; // set up the world and make it visible initWorld(true); } ///////////////// methods /////////////////////////// /** * Method to initialize the world * @param visibleFlag the flag to make the world * visible or not */ private void initWorld(boolean visibleFlag) { // set the preferred size this.setPreferredSize(new Dimension(width,height)); // create the background picture picture = new Picture(width,height); // add this panel to the frame frame.getContentPane().add(this); // pack the frame frame.pack(); // show this world frame.setVisible(visibleFlag); } /** * Method to get the graphics context for drawing on * @return the graphics context of the background picture */ public Graphics getGraphics() { return picture.getGraphics(); } /** * Method to clear the background picture */ public void clearBackground() { picture = new Picture(width,height); } /** * Method to get the background picture * @return the background picture */ public Picture getPicture() { return picture; } /** * Method to set the background picture * @param pict the background picture to use */ public void setPicture(Picture pict) { picture = pict; } /** * Method to paint this component * @param g the graphics context */ public synchronized void paintComponent(Graphics g) { Turtle turtle = null; // draw the background image g.drawImage(picture.getImage(),0,0,null); // loop drawing each turtle on the background image Iterator iterator = turtleList.iterator(); while (iterator.hasNext()) { turtle = (Turtle) iterator.next(); turtle.paintComponent(g); } } /** * Metod to get the last turtle in this world * @return the last turtle added to this world */ public Turtle getLastTurtle() { return (Turtle) turtleList.get(turtleList.size() - 1); } /** * Method to add a model to this model displayer * @param model the model object to add */ public void addModel(Object model) { turtleList.add((Turtle) model); if (autoRepaint) repaint(); } /** * Method to check if this world contains the passed * turtle * @return true if there else false */ public boolean containsTurtle(Turtle turtle) { return (turtleList.contains(turtle)); } /** * Method to remove the passed object from the world * @param model the model object to remove */ public void remove(Object model) { turtleList.remove(model); } /** * Method to get the width in pixels * @return the width in pixels */ public int getWidth() { return width; } /** * Method to get the height in pixels * @return the height in pixels */ public int getHeight() { return height; } /** * Method that allows the model to notify the display */ public void modelChanged() { if (autoRepaint) repaint(); } /** * Method to set the automatically repaint flag * @param value if true will auto repaint */ public void setAutoRepaint(boolean value) { autoRepaint = value; } /** * Method to hide the frame */ // public void hide() // { // frame.setVisible(false); // } /** * Method to show the frame */ // public void show() // { // frame.setVisible(true); // } /** * Method to set the visibility of the world * @param value a boolean value to say if should show or hide */ public void setVisible(boolean value) { frame.setVisible(value); } /** * Method to get the list of turtles in the world * @return a list of turtles in the world */ public List getTurtleList() { return turtleList;} /** * Method to get an iterator on the list of turtles * @return an iterator for the list of turtles */ public Iterator getTurtleIterator() { return turtleList.iterator();} /** * Method that returns information about this world * in the form of a string * @return a string of information about this world */ public String toString() { return "A " + getWidth() + " by " + getHeight() + " world with " + turtleList.size() + " turtles in it."; } } // end of World class

Turtle Class:

import java.util.*; import java.awt.*;

/** * Class that represents a turtle which is similar to a Logo turtle. * This class inherts from SimpleTurtle and is for students * to add methods to. * * Copyright Georgia Institute of Technology 2004 * @author Barb Ericson ericson@cc.gatech.edu */ public class Turtle extends SimpleTurtle { ////////////////// constructors /////////////////////// /** Constructor that takes the x and y and a picture to * draw on * @param x the starting x position * @param y the starting y position * @param picture the picture to draw on */ public Turtle (int x, int y, Picture picture) { // let the parent constructor handle it super(x,y,picture); } /** Constructor that takes the x and y and a model * display to draw it on * @param x the starting x position * @param y the starting y position * @param modelDisplayer the thing that displays the model */ public Turtle (int x, int y, ModelDisplay modelDisplayer) { // let the parent constructor handle it super(x,y,modelDisplayer); } /** Constructor that takes the model display * @param modelDisplay the thing that displays the model */ public Turtle (ModelDisplay modelDisplay) { // let the parent constructor handle it super(modelDisplay); } /** * Constructor that takes a picture to draw on * @param p the picture to draw on */ public Turtle (Picture p) { // let the parent constructor handle it super(p); } /////////////////// methods ///////////////////////

public static void main(String[] args) { World earth = new World(); Turtle t1 = new Turtle(earth); t1.forward(); }

public void drawT() { this.forward(100); this.turnLeft(); this.penUp(); this.forward(40); this.turn(180); this.penDown(); this.forward(80); this.hide(); } public void drawSquare(){ this.turnRight(); this.forward(30); this.turnRight(); this.forward(30); this.turnRight(); this.forward(30); this.turnRight(); this.forward(30); } public void drawSquare2(){ int width = 30; this.turnRight ( ) ; this.forward ( width ) ; this.turnRight ( ) ; this.forward ( width ) ; this.turnRight ( ) ; this.forward ( width ) ; this.turnRight ( ) ; this.forward ( width ) ; } public void drawSquare(int width){ this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); }

} // this } is the end of class Turtle, put all new methods before this

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

What did you learn about yourself?

Answered: 1 week ago