-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSidePlot.java
More file actions
129 lines (120 loc) · 4.57 KB
/
SidePlot.java
File metadata and controls
129 lines (120 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
public class SidePlot{
final static char FILL_CHAR = (char)183;
final static char PLOT_CHAR = 'x';
final static char VERT_LINE = '|';
final static char DASH_CHAR = '-';
final static char PLUS = '+';
public static void main(String[] args) {
System.out.print('\u000c');
plotXSquaredPlus2(-5, 5);
plotAbsXTimes3(-6, 6);
plotNegXSquaredPlus25 (-5, 5);
plotSinWave(-11, 11);
}
public static void plotXSquaredPlus2(int min, int max){
System.out.print("Sideways Plot" + "\n" + "y =x*x+2 where -5<=x<=5" + "\n");
int area = 0;
for(int x = min;x <= max;x++){
int y = x * x + 2;
area += y;
int middle = (max - Math.abs(min))/2; //finding the middle
if (x == middle) {
System.out.print("--" + PLUS);
}else {
System.out.print(" " + VERT_LINE);
}
for(int col = 1;col <= y;col++){
if (x==middle){ //First 2 dashs
System.out.print(DASH_CHAR);
} else{
System.out.print(FILL_CHAR); //First two dots for every other line other than the middle
}
}
System.out.print(PLOT_CHAR);
y = max * max + 2;
if (x==middle){
for(int i = 0; i < y; i++){
System.out.print(DASH_CHAR);
}
}
System.out.println();
}
System.out.println("The area under the plot is " + area + "\n");
}
public static void plotAbsXTimes3(int min, int max){
System.out.print("Sideways Plot" + "\n" + "y = |x|*3, where -6<=x<=6" + "\n");
int area = 0;
for(int x = min; x <= max;x++){
int y = Math.abs(x) * 3;
area += y;
int middle = (max - Math.abs(min))/2; //finding the middle
if (x == middle) {
System.out.print("--" + PLUS);
}else {
System.out.print(" " + VERT_LINE);
}
for(int col = 1; col <= y; col++){
if (x==middle){ //First 2 dashs
System.out.print(DASH_CHAR);
} else{
System.out.print(FILL_CHAR); //First two dots for every other line other than the middle
}
}
System.out.print(PLOT_CHAR);
y = Math.abs(max) * 3;
if (x==middle){
for(int i = 0; i < y; i++){
System.out.print(DASH_CHAR);
}
}
System.out.println();
}
System.out.println("The area under the plot is " + area + "\n");
}
public static void plotNegXSquaredPlus25 (int min, int max) {
System.out.print("Sideways Plot" + "\n" + "y = -(x*x)+25 where -5<=x<=5" + "\n");
int area = 0;
for(int x = min; x <= max;x++){
int y = (-x * x) + 25;
area += y;
int middle = (max - Math.abs(min))/2; //finding the middle
if (x == middle) {
System.out.print("--" + PLUS);
}else {
System.out.print(" " + VERT_LINE);
}
for(int col = 1; col <= y; col++){
if (x==middle){ //First 2 dashs
System.out.print(DASH_CHAR);
} else{
System.out.print(FILL_CHAR); //First two dots for every other line other than the middle
}
}
System.out.println(PLOT_CHAR);
}
System.out.println("The area under the plot is " + area + "\n");
}
public static void plotSinWave(int min, int max){
System.out.print("Sideways Plot" + "\n" + "y = 20sin(.5x)+20 where -11<=x<=11" + "\n");
int area = 0;
for(int x = min; x<= max; x++){
int y = (int)(20 * Math.sin(x * .5) + 20);
area += y;
int middle = (max - Math.abs(min))/2;
if (x == middle) {
System.out.print("--" + PLUS);
}else {
System.out.print(" " + VERT_LINE);
}
for(int col = 1; col <= y; col++){
if (x==middle){ //First 2 dashs
System.out.print(DASH_CHAR);
} else{
System.out.print(FILL_CHAR); //First two dots for every other line other than the middle
}
}
System.out.println(PLOT_CHAR);
}
System.out.println("The area under the plot is " + area + "\n");
}
}