<< Chapter < Page Chapter >> Page >

Preview

In this lesson, I will explain programs that use a Turtle object to graph the following mathematical functions:

  • A straight line
  • A parabola
  • A cubic
  • A circle (two approaches)
  • A cosine

I will also show you how to graph a circle without using a Turtle object and compare the two approaches.

General background information

You learned about the World class, the Turtle class, and the Picture class in an earlier lesson titled Java3002: Creating and Manipulating Turtles and Pictures in a World Object . You learned the fundamentals of causing the turtle to move and to draw a linewith different colors and different line widths in the process. For review, Figure 1 shows an image that you learned to create in that earlier lesson.

Figure 1. Two turtles in a World object.

Missing image

In comparison, Figure 2 shows the sort of thing that you will learn to do in this lesson.

Figure 2. Graph of a cubic function.

Missing image

The blue line in Figure 2 shows a graph of the following cubic function in a Cartesian coordinate system:

y = x*x*x

The red lines in Figure 2 show the x and y axes.

The graph in Figure 2 was created by computing the x and y coordinate values (points) for a set of x values uniformly spaced along the x axis and causing the Turtle object to move and connect those points with a blue line. The Turtle object was made invisible.

As a practical matter, computing the coordinate values is relatively simple. The more complex problem is to get everything scaled properly to produce avisually pleasing graph in the space provided by the World object.

Discussion and sample code

I will explain six different programs in this lesson. Complete listings of those programs are provided in Listing 15 through Listing 20 in the section titled Complete program listings .

As is my custom, I will break the programs down and explain them in fragments. However, I will explain only those portions of the programsthat are new to this lesson. I will let the comments in the listings speak to those portions of the code that you should already understand.

Much of the code repeats from one program to the next. I will explain new code the first time that it appears in a program and won't explain that code in theexplanation of programs that follow.

A straight line

In this section, I will present and explain a program named Line01 that can be used to draw a straight line using the standard formula for a straight line given below:

y = slope*x + yIntercept

A complete listing of the program is provided in Listing 15 near the end of the lesson.

Program output for Line01

Before getting into the code, Figure 3 shows the program output.

Figure 3. Program output for Line01.

Missing image

As you can see, in addition to the red axes, this program graphs three different lines in different colors with different slopes and differenty-intercept values. I will touch on the slopes and the y-intercept values as I explain the code.

May need multiple points

At this point, I will explain a practical issue before it becomes a concern to you. As we all know, only two points are required to define a line.However the program that I will explain uses multiple points to define and draw a line. This requirement results from the behavior of a Turtle object when moving from one point to another point.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask