***IN JAVAA**** Write a bunch of classes that will draw a bunch of lines! Don’t
ID: 3701081 • Letter: #
Question
***IN JAVAA****
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
Explanation / Answer
//----- Line.java
public interface Line {
int getYPoint(int x);
}
//----- SloppedLine.java
public class SloppedLine implements Line {
private int slope;
public SloppedLine() {
}
public SloppedLine(int slope) {
this.slope = slope;
}
public int getSlope() {
return slope;
}
public void setSlope(int slope) {
this.slope = slope;
}
@Override
public int getYPoint(int x) {
return slope*x;
}
}
//----- ExponentialLine.java
public class ExponentialLine implements Line {
private int exponent;
public ExponentialLine() {
}
public ExponentialLine(int exponent) {
this.exponent = exponent;
}
public int getExponent() {
return exponent;
}
public void setExponent(int exponent) {
this.exponent = exponent;
}
@Override
public int getYPoint(int x) {
return (int) Math.pow(x, exponent);
}
}
//----- SineLine.java
public class SineLine implements Line {
private int amplitude;
private int frequency;
public SineLine() {
}
public SineLine(int amplitude, int frequency) {
this.amplitude = amplitude;
this.frequency = frequency;
}
public int getAmplitude() {
return amplitude;
}
public void setAmplitude(int amplitude) {
this.amplitude = amplitude;
}
public int getFrequency() {
return frequency;
}
public void setFrequency(int frequency) {
this.frequency = frequency;
}
@Override
public int getYPoint(int x) {
int y = amplitude* (int) Math.sin(Math.toRadians(x*frequency));
return y;
}
}
//----- SawLine.java
public class SawLine implements Line {
private int modValue;
public SawLine() {
}
public SawLine(int modValue) {
this.modValue = modValue;
}
public int getModValue() {
return modValue;
}
public void setModValue(int modValue) {
this.modValue = modValue;
}
@Override
public int getYPoint(int x) {
return x % modValue;
}
}
//----- StaircaseLine.java
public class StaircaseLine implements Line {
private int width;
private int height;
public StaircaseLine(){
}
public StaircaseLine(int width, int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public int getYPoint(int x) {
int y = x / width * height;
return y;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.