Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Dr.Java Objective: Write a bunch of classes that will draw a bunch of line

ID: 3807796 • Letter: U

Question

Using Dr.Java

Objective:

Write a bunch of classes that will draw a bunch of lines! Don’t worry you don’t have to write any graphics, as that part is provided in the driver. Each line is drawn based on a math function that takes in a given x coordinate and will return its y coordinate.

First download the driver and put it in your project

DO NOT ALTER THE DRIVER!

Write an interface called Line

Create the following method definition

getYPoint: This takes in a decimal value and returns a decimal value depending on the type of line.

Write a class called SlopedLine

This should implement Line
Instance variable

slope: a decimal value corresponding to the line’s slope

Create the following Constructors

Default
Parameterized Constructor

Accessors and Mutators for each variable
Create the following Methods

getYPoint – this method takes in a decimal value corresponding to a x-coordinate and returns the y-coordinate based on the slope equation (y = slope*x)

Write a class called ExponentialLine

This should implement Line
Instance variable

exponent: a decimal value corresponding to the line’s exponent

Create the following Constructors

Default
Parameterized Constructor

Accessors and Mutators for each variable
Create the following Methods

getYPoint – this method takes in a decimal value corresponding to a x-coordinate and returns the y-coordinate based on the slope equation (y = x^exponent)

Write a class called SineLine

This should implement Line
Instance variable

amplitude: a decimal value corresponding to the line’s amplitude
frequency: a decimal value corresponding to the line’s frequency

Create the following Constructors

Default
Parameterized Constructor

Accessors and Mutators for each variable
Create the following Methods

getYPoint – this method takes in a decimal value corresponding to a x-coordinate and returns the y-coordinate based on a sine wave equation (y = amplitude*sin(x*frequency))

Write a class called SawLine

This should implement Line
Instance variable

modValue: a decimal value corresponding to the modulo peak of the wave

Create the following Constructors

Default
Parameterized Constructor

Accessors and Mutators for each variable
Create the following Methods

getYPoint – this method takes in a decimal value corresponding to a x-coordinate and returns the y-coordinate based on the equation (y = x mod modValue)

Write a class called StaircaseLine

This should implement Line
Instance variable

width: a decimal value corresponding to the stair’s width
height: a decimal value corresponding to the stair’s height

Create the following Constructors

Default
Parameterized Constructor

Accessors and Mutators for each variable
Create the following Methods

getYPoint – this method takes in a decimal value corresponding to a x-coordinate and returns the y-coordinate based the width and height. HINT(using integer division and multiplying that by the height will achieve the effect).

HINT: If the lines are looking weird it may be a good idea to print out each of the coordinates and observing what is going on in the method getYPoint

//The Driver

/*

*

*/

import java.applet.*; //Needed to create the window

import java.awt.*; //Needed for drawing

import java.util.*;

public class DrawingLines extends Applet{//When it extends applet it creates a window and calls certain methods

private Image display; //Used as the flat image

private Graphics drawingArea; //Used to draw item in the image

/*

*This is called by the parent Applet, as its an overridden method, and it initializes all of the instance variables.

* Think of this as a variation of a constructor, but called by another, hidden piece of code

*/

public void init()

{

//get the height and width from the Applet

int height = getSize().height;

int width = getSize().width;

//Creates an image using the height and width in the applet

display = createImage(width,height);

//Sets up the drawing area for the image above to be drawn on

drawingArea = display.getGraphics();

//This draws the lines

drawLines(drawingArea);

}

/*

* This is also called by

* the applet as it is an overridden method.

*/

public void paint(Graphics g)

{

g.drawImage(display,0,0,null);

}

/*

* This Draws line!

*/

public void drawLines(Graphics g)

{

Color[] colors = {Color.black,Color.blue,Color.red,Color.green,Color.darkGray};

Line[] lines = new Line[5];

lines[0] = new SlopedLine(1);

lines[1] = new SineLine(100,0.25);

lines[2] = new ExponentialLine(1.25);

lines[3] = new StaircaseLine(20,20);

lines[4] = new SawLine(50);

  

final int X_POINTS = 1000;

  

for(int i=0;i<X_POINTS;i++)

{

for(int j=0;j<lines.length;j++)

{

if(lines[j]==null)

continue;

g.setColor(colors[j]);

g.drawLine(i, getSize().height - (int)lines[j].getYPoint(i), i+1, getSize().height - (int)lines[j].getYPoint(i+1));

}

}

}

}

Example Dialog

Explanation / Answer

/// Find below the above mentioned class

interface Line

{
double getYpoint(double x);
}

class SlopedLine implements Line
{
double slope;
  
public SlopedLine() // default Constructor
{
slope = Double.MIN_VALUE;
}
public SlopedLine(double x) // Parameteraized Constructor
{
slope = x;
}
  
public void setSlope(double s)
{
slope = s; // Mutator
}
  
public double getSlope() // Accesssor
{
return slope;
}
  
@Override
public double getYpoint(double x)
{
return x * slope;
}
}

class ExponentialLine implements Line
{
double exponent;
  
public ExponentialLine() // default Constructor
{
exponent = Double.MIN_VALUE;
}
public ExponentialLine(double x) // Parameteraized Constructor
{
exponent = x;
}
  
public void setExponent(double e)
{
exponent = e; // Mutator
}
  
public double getExponent() // Accesssor
{
return exponent;
}
  
@Override
public double getYpoint(double x)
{
return Math.pow(x,exponent);
}
}

class SineLine implements Line
{
double amplitude;
double frequency;
  
public SineLine() // default Constructor
{
amplitude = Double.MIN_VALUE;
frequency = Double.MIN_VALUE;
}
public SineLine(double a, double f) // Parameteraized Constructor
{
amplitude = a;
frequency = f;
}
  
public void setAmplitude(double a)
{
amplitude = a; // Mutator
}
  
public void setFrequency(double f)
{
amplitude = f; // Mutator
}
  
public double getAmplitude() // Accesssor
{
return amplitude;
}
public double getFrequency() // Accesssor
{
return frequency;
}
  
@Override
public double getYpoint(double x)
{
return amplitude * Math.sin(x * frequency);
}
}

class SawLine implements Line
{
double modValue;
  
  
public SawLine() // default Constructor
{
modValue = Double.MIN_VALUE;
}
public SawLine(double a) // Parameteraized Constructor
{
modValue = a;
}
  
public void setModValue(double a)
{
modValue = a; // Mutator
}
  
public double getModValue() // Accesssor
{
return modValue;
}
  
@Override
public double getYpoint(double x)
{
return x % modValue;
}
}

class StaircaseLine implements Line
{
double width;
double height;
  
public StaircaseLine() // default Constructor
{
width = Double.MIN_VALUE;
height = Double.MIN_VALUE;
}
public StaircaseLine(double w, double h) // Parameteraized Constructor
{
width = w;
height = h;
}
  
public void setWidth(double w)
{
width = w; // Mutator
}
  
public void setHeight(double h)
{
height = h; // Mutator
}
  
public double getWidth() // Accesssor
{
return width;
}
public double getHeight() // Accesssor
{
return height;
}
  
@Override
public double getYpoint(double x)
{
return (x / width) * height;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote