0

it's supposed to calculate the y axis then draw a small line segment each time in a different location and create the silhouette of a bowling pin. the x value is never changes so it doesnt draw anything. the black filled circle is just there to make sure it's actually drawing.

import java.util.Scanner;
public class test{
  public static void main(String args[])
  {
    double a0 = 1.27731344;
    double a1 = .85418707;
    double a2 = .032282353;
    double a3 = .127018447;
    double a4 = (-5.1957538)*(Math.pow(10,-2));
    double a5 = (6.718114)*(Math.pow(10,-3));
    double a6 = (-3.61828)*(Math.pow(10,-4));
    double a7 = (7.025)*(Math.pow(10,-6));  
    
    for(int i=0;i<=150;i++){
      for(double x=0;x<=1;x+=(1/150)){
      double x2 = x + (1/150);
      double y = Math.sqrt((a0)+(a1*x)+(a2*x)+(a3*x)+(a4*x)+(a5*x)+(a6*x)+(a7*x));
      StdDraw.line(x,y,x2,y);
      System.out.println(x+" "+y+" "+x2);
      }
     
  }StdDraw.filledCircle(.5,.5,.25);
 } 
}
graeme
  • 1

1 Answers1

0

The line: for(double x = 0; x <= 1; x += (1 / 150)) iterates starting from 0.0, ending at 1.0, and adding 0.0 to x after every iteration.

To increment x by 1 / 150, you have to change 1 / 150 to a double value 1.0 / 150

So the for loop is changed into for(double x = 0; x <= 1; x += (1.0 / 150))

Iterating doubles, however, is very bad practice because of how floating-point numbers may behave. For example the following code:

double var = 1.7;
var -= 0.1;
System.out.println(var);

may print something like 1.6000007. Read more about this in another post here.

You should find an alternative way of calculating your... calculations. The bottom line is; always iterate non-floating-point numbers.

Community
  • 1
  • 1
Olavi Mustanoja
  • 2,045
  • 2
  • 23
  • 34
  • Thanks, helped a lot and I have to calculate that way for the assignment. – graeme Oct 31 '14 at 00:15
  • @graeme If you think this answer solved your problem, then please mark this answer as the solution (the ok-sign next to the post) :) – Olavi Mustanoja Oct 31 '14 at 01:27
  • i have one more question. another part of the assignment. i need to create a loop that creates all possible 5 character passwords. heres what i have but i dont think its producing all passwords. char c1='0'; char c2='0'; char c3='0'; char c4='0'; char c5='0'; while(responce == null){ for(c1='0';c1<='Z';c1++){ for(c2='0';c2<='Z';c2++){ for(c3='0';c3<='Z';c3++){ for(c4='0';c4<='Z';c4++){ for(c5='0';c5<='Z';c5++){ }}}}} password = ""+c1+c2+c3+c4+c5; – graeme Oct 31 '14 at 21:40
  • sorry about the format. i cant add code snippets to the comment. – graeme Oct 31 '14 at 21:42
  • I think you should make a new question from that :) – Olavi Mustanoja Oct 31 '14 at 23:16