Assignment on java programming

Assignment Help JAVA Programming
Reference no: EM13724435

Description:
Question 1: The listing of Tree.java is supplied below as a starting point for this lab. A picture of Tree.java output is also shown. Either cut/paste the code or type it in. Run the program to verify that it works. Study the listing until you understand completely what it does. In the Java Class Library (appendix M) look up Graphics, Point, and Applet if you need to refresh yourself on how to use these classes.
Question 2: This is a good lab to make changes one at a time. In this way, you won't ever be too far from having a working program. Always make a backup copy after each step. Name your new program NewTree.java. A picture of the output of my version of this program is shown below.
Question 3: First lets tackle some easy changes.
a) An easy change is to set the background color in the init() method. The Applet class method setBackground() can be called to color the sky section. There are examples of setBackground() in the text.
b) Another easy change is to color the grass section green. First use setColor() to alter the foreground color that will be used. The graphics method fillRect() can than be called. In my completed program, whose output is shown below, the grass occupies the bottom quarter of the display. Remember that Java considers the point (0,0) to be the upper left corner of the display; the coordinate (APPLET_WIDTH, APPLET_WIDTH) is the bottom right corner (See the listing below); and 0 degrees points vertically down towards the bottom.
c) Next use setColor() in the drawTree() method to turn the bark of the tree brown.

Question 4: In this lab we will declare many constants. I would suggest that as you proceed with the implementation, you parameterize every constant. Note how APPLET_WIDTH, and APPLET_WIDTH are declared in the listing below. This way, if you want to see how different values affect the output, you will be able to simply change a single value without altering any of the programmed logic.

Question 5: Now lets concentrate enhancing the trees that are drawn. We'll need to widen the trunk, add leaves, and randomly vary the number/length/angles of the tree branches.

a. First create a method that randomly picks a number between two integers. It's a good idea to make this a method because we'll be using it many times in this lab. The signature line is: int pickRandom(int min, int max); You can use the method Math.Random() to pick a random number between 0 and 1. That value needs to be scaled between min and max; and the scaled result is returned. An example of the use of this method is x = pickRandom(3, 10). After the call, x will have some value between 3 through 10.pickRandom() needs to contain only one executable statement.
b. Use pickRandom() to effect how drawTree() works. The number of branches, the angles of the branches, and the lengths of the branches will be varied randomly at each level of recursion. In my program, I varied the number of branches between 2 and 6; I varied the angles between -40 and +40 degrees from that of the parent branch; and I varied the length of a branch between 1/10 and 9/10 of that of the parent branch.

Question 6: Next widen the branches so they are not stick figures. One way to accomplish this is to use the Graphics fillPolygon() method. You'll need four horizontal and vertical coordinates. If (x,y), and (x',y') represents the coordinates for the ends of a branch and width represents the width of the branch; the four points to be supplied to fillPolygon() are (x,y), (x+width,y), (x'+width,y'), and (x',y'). It is a good idea to tie the length of the branch to the width. In my implementation, I used an 8 to 1 ratio.
Question 7: We need a method to draw the leaves. This method is to be called by drawTree() each time it finishes drawing a branch. See if you can figure out the proper place(s) to insert this call in the Tree.java listing shown below.

a. The following signature should be used to implement the method to draw a leaf:

public void drawLeaf

(Graphics g,int x,int y,int h,int w,int angle,int order);

where: x,y = the coordinates for top left corner of a rectangle surrounding the leaf; h,w = the height and width of the leaf; angle = the angle of the direction that the leaf points, order = the level of the recursion.
b. As an initial step draw something simple without recursion. Drawing a circle or oval will suffice for this step. A simple drawLeaf() implementation allows you to establish that the leaves are being inserted properly on your tree. In this version of drawLeaf(), set the color to some shade of green. The statement Color color = new Color(102, 153, 51); instantiates a dark green. The 102, 153, and 51 in the above statement are respectively the red, green, and blue components of the instantiated color.
c. If everything works so far, we're ready to program the real drawLeaf() method. I would suggest that you debug it separately and then plug it into the program after you know it works.
i. The first requirement for this method is that it should have at least three levels of recursion. The initial call will specify the orderparameter to be total levels of recursion (ex: 3). The base case to terminate the recursion is when order < 1; Recursive calls to the leaf method specifies the last parameter as order - 1 so the base case will eventually be reached.
ii. Your part in this design is to come up with a fractal pattern that resembles a leaf. Any reasonable pattern that you come up will suffice. See the snowflake example in the text to become familiar with how to draw images using fractals. We'll also talk about some design possibilities in class.
iii. A final requirement for the drawLeaf() method, is to vary the colors at each level of recursion. A simple array colors[] = new Color[3] can be created for this purpose (Assuming 3 levels of recursion). An example of a statement that could be used is page.setColor(colors[order]); where order is the recursion level and page is the instantiated Graphics object. Remember to instantiate the array object itself and also to instantiate each index of the color array with a color. Lots of rgb color patterns can be found on the Internet. Let me know if you can't find a color that you need.
iv. The final step relating to the tree phase of the project is to add a For loop in you paint program to draw multiple trees. Randomly select the size and position of each tree. The base of the tree should be in the grass area. In my program, I varied the initial tree size between 20 and 100 pixels. Make sure to draw at least 3 trees. You can draw more if you like. I chose to draw 8 in my implementation.

Question 8: Now we move to the task of drawing the clouds. The first step is to succeed drawing one cloud. A good java method that can be used is fillOval(), which is called using the variables x, y, width, and height. Note that the x, y arguments given to fillOval() point to the pixel at the top left corner of the oval; not the middle. The signature for the method to use for drawing clouds is:

void drawCloud

(Graphics page, int x, int y, int w, int h, int order)

where: x, y is the coordinate to the top left pixel of the cloud; w,h indicates the width and height of the cloud; order is the level of recursion.

You can design your drawCloud method as you wish, but I've included the following pseudo code as a possibility.

RETURN if level <= 1

Randomly pick a color and fill the oval using x, y, w, and h

Find the middle of the oval (x+w/2,y+h/2)
FOR a random number of recursive calls

Randomly pick an angle

Randomly pick a distance from the middle

Compute a new point (newX, newY) based on angle/distance from x/y

Randomly pick a new width (newW) and height (newH)

Recursively call drawCloud using newX, newY, newW, newH

Question 9: The final step is to draw multiple clouds using a For loop. Randomly pick the coordinates and the size of each cloud. In my program, I chose to draw 30 clouds having a random width and height with a 75 pixel maximum. All of my clouds were drawn starting on the top half of the screen so they would not likely expand to the grass section.

Question 10: Answer the synthesis questions in an rtf or doc file (answers.rtf or answers.doc). Type your name and the lab number on this file and include the questions with the answers.

Question 11: Zip your Eclipse project along with the synthesis answers and email to [email protected].

Listing of Tree.java
// Program to create a recursive tree.

// This is the base program to be used to start the cs258 project on recursion.

import java.applet.Applet;

import java.awt.*;

public class Tree extends Applet

{ private final int APPLET_WIDTH = 320;

private final int APPLET_HEIGHT = 320;

private final double STARTSIZE = 110.0;

private final double STARTANGLE = 180.0;

private final double CHANGEANGLE = 30.0;

private final double FACTOR = 2.0;

private final double MINSIZE = 10.0;

// Initialize the applet.

public void init() { setSize(APPLET_WIDTH, APPLET_HEIGHT); }

// Create the drawing that displays on the applet.

public void paint(Graphics page)

{ drawTree(page, APPLET_WIDTH/2, APPLET_HEIGHT, STARTSIZE, STARTANGLE);
}

public void drawTree( Graphics page, int x, int y, double size, double angle )

{ Point endPoint = calculatePoint(x, y, size, angle );

page.drawLine(x, y, endPoint.x, endPoint.y);

if (size > MINSIZE)

{ drawTree(page, endPoint.x, endPoint.y

, size/FACTOR, angle+CHANGEANGLE);

drawTree(page, endPoint.x, endPoint.y
, size/FACTOR, angle-CHANGEANGLE);

} }

public Point calculatePoint( int x, int y, double size, double degree )

{ Point point = new Point(x, y);

double radians = Math.PI/180. * degree;

point.x += (int)(size * Math.sin(radians));

point.y += (int)(size * Math.cos(radians));

return point;

}

}

Reference no: EM13724435

Questions Cloud

Discuss business that successfully communicates its products : In a well-written 2-3 page paper, at least two additional credible scholarly references, discuss a business that successfully communicates its products or services
Determine how much firm is likely to spend for warehousing : When comparing performance during the first five months of 2010 with the performance during the first five months in 2009, which warehouse shows the most improvement?
Create a report explaining the research process : A company is considering creating and selling a new type of cookies. Create a report explaining the research process that should be used by the company to make this decision. Your report should answer the following questions:
What are susan growth options : You are Susan Feller. Should you grow your business? Why? What are Susan's growth options? What are the pros and cons of each
Assignment on java programming : The listing of Tree.java is supplied below as a starting point for this lab. A picture of Tree.java output is also shown. Either cut/paste the code or type it in. Run the program to verify that it works. Study the listing until you understand comp..
What is the equilibrium price and quantity in the market : What is the equilibrium Price and Quantity in the market and describe what would happen in this market in terms of the supply and demand curve.
Prepare a marketing plan for htc unveils premium : Prepare a marketing plan for HTC unveils premium One M9 Smartphone in 400 words count
Discussing animal ethics : What basic things do they agree about? What do they disagree about? How would their different views result in different conclusions about how animals should be treated? Which of the two do you think gives a more rationally justifiable approach to ..
Traditional international trade theories : Question 1: What is globalization, and what are some of the traditional international trade theories that support the concept of globalization?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Difference for class linked bag and test into main program

write a method difference for the class linked bag and test into main program.

  Define java implementation to implement the requirements

Produce a Java implementation to implement the requirements of Question 1, that is, to perform the 32-bits two complement and 32-bit floating-point conversion of a given number.

  Program that allows a user to input coordinates

In Java make a program that allows a user to input coordinates as to make a Hexagon via an array, and my professor has set the standards of: Write a class Polygon which draws a hexagon for a set of numbers given by the user.

  The objective is to implement a menu-based system

The objective is to implement a menu-based system for Bank Accounts in order to simulate a very simple banking system. Many structures have to be declared to manage bank accounts.

  Project is for designing and developing a college

project is for designing and developing a college registration program. write a java program to register students for a

  Class that stores information about a report

Create a class that stores information about a report containing multiple currency transactions in multiple currencies. This includes the name of the report and each of the transactions that occurred.

  Create java application which creates random phone number

Create and implement Java application which creates and prints a random phone number of the form xxx-xxx-xxxx. Include the dashes in the output.

  Create your listarray class

Create your ListArray class that is functionally similar to the formal Java ArrayList Utility class. here are the requirements

  Design a class named magazinesubscription

Design a class named MagazineSubscription that has fields for a subscriber's name, the magazine name, and number of months remaining in the subscription.

  A jsp expression can contain any java expression

A JSP expression can contain any Java expression that evaluates to a

  Java socket hello i need to this assignment done in net

hello i need to this assignment done in net beans . and i want comment in code .also screen shots of running program

  How to write and incorporate string

How to write and incorporate string - custom function

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd