<< Chapter < Page | Chapter >> Page > |
/*File Parabola01 Copyright 2016 R.G.Baldwin
********************************************************************/import java.awt.Color;
public class Parabola01{//Driver classpublic static void main(String[] args){Parabola01Runner obj = new Parabola01Runner();
obj.run();}//end main
}//end class Parabola01//==================================================================/
class Parabola01Runner{//Instantiate the World and Turtle objects.
private World world = new World(300,300);private Turtle turtle = new Turtle(0,0,world);
//---------------------------------------------------------------//public void run(){
//Make the turtle invisibleturtle.hide();
//Prepare the penturtle.setPenColor(Color.RED);
turtle.setPenWidth(2);//Draw the axes in REDturtle.penUp();
turtle.moveTo(world.getWidth()/2,0);turtle.penDown();
turtle.moveTo(world.getWidth()/2,world.getHeight());turtle.penUp();
turtle.moveTo(world.getWidth(),world.getHeight()/2);turtle.penDown();
turtle.moveTo(0,world.getHeight()/2);turtle.penUp();
turtle.moveTo(0,0);//Prepare the variables
double xOff = 0.25;//offset relative to 1.0double yOff = -0.25;
double xScale = 0.75*world.getWidth()/2;double yScale = 1.25*world.getHeight()/2;
double yVal = 0;int row = 0;
int col = 0;double xVal = -1;
turtle.setPenColor(Color.BLUE);//Draw the parabolic function in BLUE.
for(int cnt=0; cnt<=100;cnt++,xVal += 0.02){
//Get a y-value for the given x-value.yVal = function(xVal);
//Apply the offsets and scale the resultscol = (int)((xOff+xVal)*xScale);
row = (int)((yOff+yVal)*yScale);//Move to the first point without drawing a line because the// pen is not down. Translate the origin to the center in the
// process.turtle.moveTo(col + world.getWidth()/2,
row + world.getHeight()/2);//Lower the pen in order to draw a line from each point to the
// next point.turtle.penDown();
}//end for loop}//end run method//---------------------------------------------------------------////This method evaluates and returns the y-value for each x-value
// for a parabola with no offset centered at the origin.// y = x*x
double function(double xVal){double yVal = xVal*xVal;
return yVal;}//end function
//---------------------------------------------------------------//}//end class Parabola01Runner
/*File Cubic01 Copyright 2016 R.G.Baldwin
********************************************************************/import java.awt.Color;
public class Cubic01{//Driver classpublic static void main(String[] args){Cubic01Runner obj = new Cubic01Runner();
obj.run();}//end main
}//end class Cubic01//=================================================================//
class Cubic01Runner{//Instantiate the World and Turtle objects.
private World world = new World(300,300);private Turtle turtle = new Turtle(0,0,world);
//---------------------------------------------------------------//public void run(){
//Make the turtle invisibleturtle.hide();//Prepare the pen
turtle.setPenColor(Color.RED);turtle.setPenWidth(2);//Draw the axes in RED
turtle.penUp();turtle.moveTo(world.getWidth()/2,0);
turtle.penDown();turtle.moveTo(world.getWidth()/2,world.getHeight());
turtle.penUp();turtle.moveTo(world.getWidth(),world.getHeight()/2);
turtle.penDown();turtle.moveTo(0,world.getHeight()/2);
turtle.penUp();turtle.moveTo(0,0);//Prepare the variables
double xOff = -0.5;//Offset relative to 1.0double yOff = -0.5;
double xScale = 0.6*world.getWidth()/2;double yScale = 0.6*world.getHeight()/2;
double yVal = 0;int row = 0;
int col = 0;double xVal = -1;
turtle.setPenColor(Color.BLUE);//Draw the cubic function in BLUEfor(int cnt=0; cnt<=100;cnt++,xVal += 0.02){
//Get a y-value for a given x-value.yVal = function(xVal);//Apply the offsets and scale the results
col = (int)((xOff+xVal)*xScale);row = (int)((yOff+yVal)*yScale);
//Move to the first point without drawing a line because the// pen is up. Translate the origin to the center in the
// process.turtle.moveTo(col + world.getWidth()/2,
row + world.getHeight()/2);//Lower the pen in order to draw a line from each point to the
// next point.turtle.penDown();
}//end for loop}//end run method//----------------------------------------------------//
//This method evaluates and returns the y-value for each x-value// for a cubic function with no offset centered at the origin.
// y = x*x*xdouble function(double xVal){
double yVal = xVal*xVal*xVal;return yVal;
}//end function//---------------------------------------------------------------//}//end class Cubic01Runner
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?