SonicPants Posted February 8, 2011 Report Share Posted February 8, 2011 I made this to express my feelings in a slightly humorous manner... yep. http://img546.imageshack.us/img546/2914/javayuno.jpg Link to comment Share on other sites More sharing options...
Zan Posted February 8, 2011 Report Share Posted February 8, 2011 you sir win a +1. Whatcha compiling? Link to comment Share on other sites More sharing options...
SonicPants Posted February 8, 2011 Author Report Share Posted February 8, 2011 I'm making just like a simple plotter and having issues with things not printing and the scale factor when plotting, if you can help, i'll love you forever. I should probably use some for loops to shorten this thing up but idk, i'm just bad with java atm, hell half the file is declaring and initializing variables. Also, if anyone can help, here's a list that I'm trying to do: Input and output a, b, & nCalculate and print dxFind and print the max & min on [a, b] Find and print the scale factor Plot the function x^3 on [a,b] public class Plotter { public static void main(String[] args) { int n = 0; int nSpaces = 0; int i=0; double a = 0; double b = 0; double x = 0; double dx = (b-a)/n; double sf = 70/(max-min); double y = 0; double max = 0; double min = 0; System.out.print("a = " + a + " "); System.out.print("b = " + b + " "); System.out.print("n = " + n + " "); System.out.print("dx = " + dx + " "); if (i<=n){ x=a+i*dx; y=x*x*x; } if (y>max){ max = y; i++; } if (y<min){ min = y; i++; } else {i++; } System.out.print("min = " + min); System.out.print("max = " + max); } } Link to comment Share on other sites More sharing options...
Zan Posted February 8, 2011 Report Share Posted February 8, 2011 ok, the input and calculate are easy and done, but i don't understand the other 3 items. Tell me how to calculate the max between [a;b], wtf is the scale factor and the last one and i will do it tomorrow while breakfast...ing. Link to comment Share on other sites More sharing options...
Solitaire Posted February 8, 2011 Report Share Posted February 8, 2011 What sort of errors is the compiler throwing? Link to comment Share on other sites More sharing options...
Scorch Posted February 8, 2011 Report Share Posted February 8, 2011 Post 1:I'm guessing the compiler is throwing a cannot find symbol error at line 12, cause you use min and max before you define them. Move the two lines where you define max and min up to before you use them, and it should be fine. Also, for formatting, try changing "min = " + min to "min = " + min + " " just to make it consistent the whole way through. Post 2:Now, to actually answer your question, here's what I've done so far. I've commented out the code of yours I haven't used so you can see what I abridged: public class Plotter { public static void main(String[] args) { int n = 0; //Number of divisions of [a,b] int nSpaces = 0; //No idea // int i=0; //Not actually necessary double a = 0; //Start of function domain double b = 0; //End of function domain // double x = 0; //x co-ordinate // double y = 0; //y co-ordinate double dx = (b-a)/n; //Step size in x double max = 0; //Largest value of y double min = 0; //Smallest value of y double sf = 70/(max-min); //No idea double[] x = new double[n]; //x co-ordinate array double[] y = new double[n]; //y co-ordinate array // System.out.print("a = " + a + " "); // System.out.print("b = " + b + " "); // System.out.print("n = " + n + " "); // System.out.print("dx = " + dx + " "); System.out.println("a = " + a + " b = " + b + " n = " + n + " dx = " + dx); for(int i = 0; i < n; i++) { x[i] = a + i*dx; y[i] = Math.pow(x[i],3); if(y[i] > max) max = y[i]; else if(y[i] < min) min = y[i]; } System.out.println("min = " + min + " max = " + max); // if (i<=n) // { // x=a+i*dx; // y=x*x*x; // } // if (y>max) // { // max = y; // i++; // } // if (y<min) // { // min = y; // i++; // } // else // { // i++; // } // System.out.print("min = " + min + " "); // System.out.print("max = " + max); } } I've added comments as to what I think variables do. Correct me if I'm wrong. I'm not sure how to proceed with the plotter, but creating arrays will give you a list of matched x and y values you can plot one at a time. The way you were doing it at the end of the code just gives you a max value, a min value, and the last x and y values. I don't know if there's some function already in the Java package to plot functions or plot values, but I'm guessing if you're learning Java from somewhere, you'll already have some method of doing it given to you. Good luck with the rest of the code! Post 3:I just noticed what the sf is meant to be. You can't put it there, it'll work out the scale factor with the initialised values of max and min, which are 0 and 0 giving a value of NaN. Put that line after you calculate the max and min. In my code, you'd put it after the for loop, and print it with the max and min. Link to comment Share on other sites More sharing options...
SuperHetero Posted February 8, 2011 Report Share Posted February 8, 2011 I made this to express my feelings in a slightly humorous manner... yep. http://img546.imageshack.us/img546/2914/javayuno.jpg http://images2.memegenerator.net/ImageMacro/5620873/YO-DAWG-I-HEARD-YOU-WAS-HAVING-TROUBLE-WITH-JAVA-SO-I-BROUGHT-YOU-A-CUP-OF-JAVAWHAT-EXPECTING-SOMETH.jpg?imageSize=Medium&generatorName=XZIBIT EDIT: Yo dawg, i had touble with manual . . . . Link to comment Share on other sites More sharing options...
Zan Posted February 8, 2011 Report Share Posted February 8, 2011 Ok, behold import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class main { public static void main(String[] args) throws IOException{ double a = consoleAdd("Input a: "); double b = consoleAdd("Input b: "); int n = (int) consoleAdd("Input n: "); double dx = (b-a)/n; System.out.println("a = " + a + " "); System.out.println("b = " + b + " "); System.out.println("n = " + n + " "); System.out.println("dx = " + dx + " "); plot(a,b,n,dx); } static double consoleAdd(String txt) throws IOException{ BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print(txt); String line = input.readLine(); double x = Integer.parseInt(line); return x; } static void plot(double a, double b,int n, double dx){ double max=0,min=0; for( int i = 0; i < n; i++){ double x = a+(1*dx); double y = Math.pow(x, 3); if(y>max) max = y; else if(y<min) min = y; System.out.println(x+" | "+y); a++; } System.out.println("Max: "+max); System.out.println("Min: "+min); System.out.println("Scale Factor: "+70/(max-min)); } } Thats what i understood from the code you put that you were trying to do. I don't know what b is for since it's never used but well... just see if that code does what you need to do and tell me what's missing if any. :shiftyninja: Link to comment Share on other sites More sharing options...
SonicPants Posted February 8, 2011 Author Report Share Posted February 8, 2011 Yea zan that's pretty close to what i'm looking for, here's an example of what I want the code to print, but also, you totally lost me when you got to the 2nd method called consoleAdd, but other than that I recognized everything else. Also, Scorch to answer some of your questions, b is the endpoint of the graph, nSpaces is the number of spaces before it prints a "*" (check example output), and the reason the scale factor is 70/min-max is because the cmd prompt has a max of 70 columns that the graph can be printed on. Example Output:http://people.uncw.edu/tompkinsj/121/1/xCubedOnNeg2To2inDos.JPG Edit: also forgot to mention, the 0's like a = 0; b = 0; are just place holders for the starting point and endpoint of the graph. Edit #2: Well, I think i've got most of it complete but i'm still having 1 problem, int nSpaces = (y-min)*sf is giving me a:Plotter.java: possible loss of precisionfound: doublerequired: int New Code:public class Plotter { public static void main(String[] args){ int n = 0; double a = 0; double b = 0; double dx = (b-a)/n; System.out.print("a = " + a + " "); System.out.print("b = " + b + " "); System.out.print("n = " + n + " "); System.out.print("dx = " + dx + " "); plot(a,b,n,dx); } static void plot(double a, double b, int n, double dx){ double max = 0, min = 0; for(int i = 0; i <= n; i++){ double x = a+(i*dx); double y = x*x*x; double sf = 70/(max-min); int nSpaces = (y-min) * sf; int j = 0; if (j<nSpaces){ System.out.print(" "); j++; } else { System.out.print("*"); System.out.println(); i++; } if (y>max){ max = y; } else if(y<min){ min = y; i++; } } System.out.print("min = " + min + " "); System.out.print("max = " + max + " "); System.out.print("sf = " +70/(max-min)); } } Sorry for the long post, but I really appreciate all of your help guys. Link to comment Share on other sites More sharing options...
Zan Posted February 8, 2011 Report Share Posted February 8, 2011 oic, when you said you were using java i thought you were using the console inside the environment, but now i see you use the DOS console, like when coding and running in C. Since you pass the values of a,b and n as an argument then yeah, the consoleAdd is kinda useless xD. It's just a mini method i created that acts like a scanf in C. For error #1, in line int nSpaces = (y-min) * sf; you need to cast it to int, since y and sf are double and nSpaces is an int, thats why it says you might lose precision. It should look like thisint nSpaces = (int) (y-min) * sf; or instead of an int nSpaces you could declare it as a double nSpaces. Try running it like that and see if it prints. BTW, when coding in java i recommend you 1000000% Eclipse, it's the best when using JAVA imhoBTW #2: What's the i++ supposed to do? o.o else if(y<min){ min = y; i++; } } Link to comment Share on other sites More sharing options...
SonicPants Posted February 8, 2011 Author Report Share Posted February 8, 2011 yea all i had to do was change it todouble nSpaces = (y-min) * sf;and so that works the i++; says that it will keep running that loop until it becomes false and then it moves on to the next statement and ya you're right, I probably should use Eclipse. Link to comment Share on other sites More sharing options...
Zan Posted February 8, 2011 Report Share Posted February 8, 2011 But the if isn't a loop D: is a conditional D: and the i++ is already on the "for" statement above. I think that i++ shouldn't be at all D: Link to comment Share on other sites More sharing options...
SonicPants Posted February 8, 2011 Author Report Share Posted February 8, 2011 Hmm, well I just ran it without the i++; in the if statements and nothing changed so idk :/overall, its still not working exactly right but i'm taking a break from it and i'll come back to it tomorrow. Link to comment Share on other sites More sharing options...
Zan Posted February 8, 2011 Report Share Posted February 8, 2011 Well, just a few pointers: a) If you need to use a variable, in this case i, to move forward a loop it's recommended, unless stated otherwise, that said variable isn't inside a conditional loop like "if". Why? Because if it doesn't access that "if" for some reason then you will be trapped in an eternal loop since it wont move forward / backwardsB) Only use i++ or whatever when dealing with a "while", not a "for", since the "for" has already integrated the i++. c) If you know how to debug then do eet! always!. It's the easiest way to see, step by step (uhh baby! ) what went wrong. If you don't know how then download Eclipse and i will teach you on xfire D: Link to comment Share on other sites More sharing options...
X-Ray Posted February 8, 2011 Report Share Posted February 8, 2011 Yea zan that's pretty close to what i'm looking for, here's an example of what I want the code to print, but also, you totally lost me when you got to the 2nd method called consoleAdd, but other than that I recognized everything else. Also, Scorch to answer some of your questions, b is the endpoint of the graph, nSpaces is the number of spaces before it prints a "*" (check example output), and the reason the scale factor is 70/min-max is because the cmd prompt has a max of 70 columns that the graph can be printed on. Example Output:http://people.uncw.edu/tompkinsj/121/1/xCubedOnNeg2To2inDos.JPG Edit: also forgot to mention, the 0's like a = 0; b = 0; are just place holders for the starting point and endpoint of the graph. Edit #2: Well, I think i've got most of it complete but i'm still having 1 problem, int nSpaces = (y-min)*sf is giving me a:Plotter.java: possible loss of precisionfound: doublerequired: int New Code:public class Plotter { public static void main(String[] args){ int n = 0; double a = 0; double b = 0; double dx = (b-a)/n; System.out.print("a = " + a + " "); System.out.print("b = " + b + " "); System.out.print("n = " + n + " "); System.out.print("dx = " + dx + " "); plot(a,b,n,dx); } static void plot(double a, double b, int n, double dx){ double max = 0, min = 0; for(int i = 0; i <= n; i++){ double x = a+(i*dx); double y = x*x*x; double sf = 70/(max-min); int nSpaces = (y-min) * sf; int j = 0; if (j<nSpaces){ System.out.print(" "); j++; } else { System.out.print("*"); System.out.println(); i++; } if (y>max){ max = y; } else if(y<min){ min = y; i++; } } System.out.print("min = " + min + " "); System.out.print("max = " + max + " "); System.out.print("sf = " +70/(max-min)); } } Sorry for the long post, but I really appreciate all of your help guys. What language is that in? Link to comment Share on other sites More sharing options...
SonicPants Posted February 8, 2011 Author Report Share Posted February 8, 2011 What language is that in?Lol, its Java, the title of this topic Also, I'm downloading eclipse right now so xfire me whenever zan :D Link to comment Share on other sites More sharing options...
Theos Sairin Posted February 9, 2011 Report Share Posted February 9, 2011 What language is that in?Lol, its Java, the title of this topic Also, I'm downloading eclipse right now so xfire me whenever zan :D This is an English public chat server. If you wish to speak in another language, please use private chat. Link to comment Share on other sites More sharing options...
SonicPants Posted February 9, 2011 Author Report Share Posted February 9, 2011 Problem solved, many thanks to zan +1 him if ya get the chance! Link to comment Share on other sites More sharing options...
Scorch Posted February 9, 2011 Report Share Posted February 9, 2011 Problem not quite solved yet, I think. The input line to the console is "java Plotter -2 2 40" or something like that, right? That's stored in args. So then you want a few lines like this: double a = args[0]; double b = args[1]; int n = args[3]; That should be everything you need. Unless you already did that and didn't tell anyone >=( Link to comment Share on other sites More sharing options...
SonicPants Posted February 9, 2011 Author Report Share Posted February 9, 2011 That should be everything you need. Unless you already did that and didn't tell anyone >=(Heh, yea we did that over xfire and TeamViewer Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.