Question

In: Computer Science

Write Java Program. (Plot the sine and cosine functions) Write a program that plots the sine...

Write Java Program.

(Plot the sine and cosine functions) Write a program that plots the sine function in red and cosine in blue, as shown in Figure 14.48c.

Hint: The Unicode for P is u03c0. To display -2p, use Text(x, y, "-2\u03c0"). For a trigonometric function like sin(x), x is in radians. Use the following loop to add the points to a polyline:

Polyline polyline = new polyline ();

ObservableList<Double> list = polyline.getPoints ();

double scaleFactor = 50;

for (int x = -170; x <= 170; x++){

list.add(x + 200.0);

list.add(100 - 50 * Math.sin((x / 100.0) * 2 * Math.PI));

}

Solutions

Expert Solution

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polyline;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class Exercise_19 extends Application{

private static double WIDTH = 500;
private static double HEIGHT = 200;
private static double CENTER_X = WIDTH / 2;
private static double CENTER_Y = HEIGHT / 2;

@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();


Polyline xAxis = new Polyline();
drawXAxis(pane, xAxis);
Polyline yAxis = new Polyline();
drawYAxis(pane, yAxis);

String[] pi = new String[] {"-3\u03c0", "-2\u03c0", "-\u03c0", "0" , "\u03c0", "2\u03c0", "3\u03c0"};

int index = 0;
Polyline polyline1 = new Polyline();
ObservableList<Double> list = polyline1.getPoints();
for (int x = -170; x <= 170; x++) {
list.add(x + CENTER_X);
double y = CENTER_Y - 50 * Math.sin((x / 100.0) * 2 * Math.PI);
list.add(y);

if (y >= 99.9 && y < 100.01) {
Text text1 = new Text(x + CENTER_X, CENTER_Y / 0.9, pi[index++]);
pane.getChildren().add(text1);
}

}
Polyline polyline2 = new Polyline();
ObservableList<Double> list2 = polyline2.getPoints();

for (int x = -170; x <= 170; x++) {
list2.add(x + CENTER_X);
list2.add(CENTER_Y - 50 * Math.cos((x / 100.0) * 2 * Math.PI));

}
pane.getChildren().addAll(xAxis, yAxis,polyline1,polyline2);
primaryStage.setScene(new Scene(pane, WIDTH, HEIGHT));
primaryStage.setTitle("Plot the sine and cosine functions");
primaryStage.show();

}

private static void drawXAxis(Pane pane, Polyline xAxis) {
ObservableList<Double> xAxisList = xAxis.getPoints();
double limit = WIDTH * 0.95;

for (double x = 0; x < limit; x++) {
xAxisList.add(x);
xAxisList.add(CENTER_Y);
}

Line line1 = new Line(limit, CENTER_Y, limit - WIDTH * 0.05, CENTER_Y * 0.875);
Line line2 = new Line(limit, CENTER_Y, limit - WIDTH * 0.05, CENTER_Y / 0.875);
Text text = new Text(limit + (WIDTH * 0.02), CENTER_Y, "X");
text.setFont(Font.font(22));
pane.getChildren().addAll(line1, line2, text);
}

private static void drawYAxis(Pane pane, Polyline yAxis) {

ObservableList<Double> yAxisList = yAxis.getPoints();
double limit = HEIGHT * 0.95;

for (double y = 0; y < limit; y++) {
yAxisList.add(CENTER_X);
yAxisList.add(y + HEIGHT * 0.1);

}
Line line1 = new Line(CENTER_X, HEIGHT * 0.1, CENTER_X - WIDTH * 0.05, HEIGHT * 0.2);
Line line2 = new Line(CENTER_X, HEIGHT * 0.1, CENTER_X + WIDTH * 0.05, HEIGHT * 0.2);
Text text = new Text(limit + (WIDTH * 0.2), HEIGHT * 0.1, "Y");
text.setFont(Font.font(22));
pane.getChildren().addAll(line1, line2,text);
}


public static void main(String[] args) {

Application.launch(args);

}

}


Related Solutions

The solution for the finite potential well is a combination of sine and cosine functions a...
The solution for the finite potential well is a combination of sine and cosine functions a combination of exponential functions a combination of exponential functions and sine and/or cosine functions a third degree polynomial
In a single Matlab script, plot a sine wave and cosine wave over 2 periods (0...
In a single Matlab script, plot a sine wave and cosine wave over 2 periods (0 to 4π). Checkpoints of the requirements: Generate your independent variables first; the step size should be no larger than 0.1 so that it has enough samples to get smooth lines. Plot the two waves on the same graph.   Specify the line color and styles: red dashed line for sine & blue solid line with dot markers for cosine. Include axis labels and a descriptive...
Gui calculator in Java with additon, subtraction, multiplication, division, sine, cosine, tangent, square, cube, raise to...
Gui calculator in Java with additon, subtraction, multiplication, division, sine, cosine, tangent, square, cube, raise to x to the power of y functionality. It should also have memory button, memory recall button and clear.
Determine the equation of a sine or cosine function for the vertical position, in metres, of...
Determine the equation of a sine or cosine function for the vertical position, in metres, of a rider on a Ferris Wheel, after a certain amount of time, in seconds. The maximum height above the ground is 26 metres and the minimum height is 2 metres. The wheel completes one turn in 60 seconds. Assume that the lowest point is at time = 0 seconds.
Find the exact values of the sine, cosine, and tangent of the angle. −195° = 30°...
Find the exact values of the sine, cosine, and tangent of the angle. −195° = 30° − 225° sin(−195°) = cos(−195°) = tan(−195°) =
Create a script that calculates the sine, cosine, and tangent of a vector from 0 to2?...
Create a script that calculates the sine, cosine, and tangent of a vector from 0 to2? with steps of ?/16. The numbers should have three digits to the right of the decimal. Determine an appropriate field width. Use fprintf to display the output in tabular format with the following headers. x sin(x) cos(x) tan(x) Also, plot the three equations in the same figure window, but on separate axes. Plot all thee equations from 0 to 2? on the x-axis. Plot...
essay regarding the applications of the Fourier series (or fourier sine or fourier cosine series) in...
essay regarding the applications of the Fourier series (or fourier sine or fourier cosine series) in other disciplines, or in the real world
MATLAB Question Write a script to plot the function 'x times sine-squared of x' over a...
MATLAB Question Write a script to plot the function 'x times sine-squared of x' over a user-specified interval. It should prompt the user to enter the starting and stopping values of x, perform the calculation, and plot the result. Test the script using start/stop values 28 and 42.
Use both cosine law/sine law and the component method to answer the following: A missile of...
Use both cosine law/sine law and the component method to answer the following: A missile of mass 1000 kg is fired down a test range at 500 m/s. It explodes into five pieces. A 300 kg piece moves west at 300 m/s, a 50 kg piece moves east at 700 m/s, 100 kg piece moves northeast at 700 m/s and a 200 kg mass moves Southwest at 100 m/s. Calculate the velocity of the remaining piece after the explosion.
java In one program, write 3 separate functions, and use them. 1) Write the function body...
java In one program, write 3 separate functions, and use them. 1) Write the function body for each: int squareInteger s( int x) double squareDouble( double d) float squareFloat ( float f) They basically do the same thing, square a number, but use different argument datatypes. 2) Test Each function with: Using int 4, double 4.9, float 9.4 What happened when you use the correct numerical data type as input ? What happened when you use the incorrect numerical data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT