|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Need help on a question, I just can't understand how to create a test program for my assignment. I created the main program named LineSegment but when trying to make a LineTest file I just stare blankly.
+ Show Spoiler +In a separate LineTest class, create a test driver that exercises each of the methods in your LineSegment class, including the accessors for the two endpoints, as well as both constructors.
Use the following line segments as test data: Line Segment 1: (0.0, 0.0) to (0.0, 5.0) Line Segment 2: (2.0, 5.0) to (2.0, 7.0) Line Segment 3: (1.0, 5.0) to (6.0, 5.0) Line Segment 4: (2.0, 5.0) to (7.3, 10.1) Line Segment 5: (0.0, -3.0) to (10.6, 7.2) Line Segment 6: (0.0, -3.0) to (10.6, 3.0) Line Segment 7: (2.0, 5.0) to (2.0, 5.0)
For the isParallelTo method, include test cases for each of the following: • Line segments 1 and 2. • Line segments 1 and 3. • Line segments 4 and 5. • Line segments 4 and 6. • Line segment 4 compared with itself (yes, this is considered parallel).
For each test case for the isParallelTo method, use an if-else statement in your test driver to print whether or not the two line segments were found to be parallel. All output should include enough information so you can identify which line segments are involved in each test case.
For both the midpoint and length methods, include test cases for each of the following: • Line segment 1 • Line segment 3 • Line segment 4 • Line segment 7
It is okay to use the CartesianPoint toString() method for displaying midpoint results.
Code for Cartesian Point
/** Represents a point in a 2D plane, defined as (x,y). */ public class CartesianPoint { private double x; private double y; public CartesianPoint(double xVal, double yVal) { x = xVal; y = yVal; } public double getX() { return x; }
public double getY() { return y; } public double distance (CartesianPoint other) { double dx = x - other.x; double dy = y - other.y; return Math.sqrt(dx*dx + dy*dy); } public String toString() { return "CartesianPoint[x=" + x + ", y=" + y + "]"; } }
Code for LineSegment
public class LineSegment { private CartesianPoint point1; private CartesianPoint point2;
public LineSegment(CartesianPoint pt1,CartesianPoint pt2) { point1 = new CartesianPoint(pt1.getX(), pt1.getY()); point2 = new CartesianPoint(pt2.getX(), pt2.getY()); } public LineSegment(double x1, double x2, double y1, double y2) { point1 = new CartesianPoint(x1, y1); point2 = new CartesianPoint(x2, y2); } public CartesianPoint getPoint1() { return point1; } public CartesianPoint getPoint2() { return point2; } public boolean isParallelTo(LineSegment line) { boolean isParallel = false; boolean undefined1 = false; boolean undefined2 = false; double slope1 = 0; double slope2 = 0; if(point1.getX() - point2.getX() == 0) undefined1 = true; else { slope1 = (point1.getY() - point2.getY() / point1.getX() - point2.getX()); } if(line.getPoint1().getX() - line.getPoint2().getY() == 0) undefined2 = true; else { slope2 = line.getPoint1().getY() - line.getPoint2().getY() / line.getPoint1().getX() - line.getPoint2().getY(); } if(slope1 == slope2 || undefined1 == undefined2) isParallel = true; return isParallel; } public double length() { return point1.distance(point2); } public CartesianPoint midpoint() { double xVar = point1.getX() + point2.getX() / 2; double yVar = point1.getY() + point2.getY() / 2; CartesianPoint midpoint = new CartesianPoint(xVar, yVar); return midpoint; } }
I've checked these over, the Cartesian was given (cannot be changed) and my Linesegment compiles and seems to meet all the criteria required I just don't understand how to put it into a test file... My minds like exploding.
EDIT: as a sidenote if you are going to help could you go over the steps etc? I need to learn and even if you just want to give me a start in the right direction that would be perfect, I definitely dont want anyone to do the work for me :D
|
On October 11 2012 06:46 NeMeSiS3 wrote:Need help on a question, I just can't understand how to create a test program for my assignment. I created the main program named LineSegment but when trying to make a LineTest file I just stare blankly. + Show Spoiler +In a separate LineTest class, create a test driver that exercises each of the methods in your LineSegment class, including the accessors for the two endpoints, as well as both constructors.
Use the following line segments as test data: Line Segment 1: (0.0, 0.0) to (0.0, 5.0) Line Segment 2: (2.0, 5.0) to (2.0, 7.0) Line Segment 3: (1.0, 5.0) to (6.0, 5.0) Line Segment 4: (2.0, 5.0) to (7.3, 10.1) Line Segment 5: (0.0, -3.0) to (10.6, 7.2) Line Segment 6: (0.0, -3.0) to (10.6, 3.0) Line Segment 7: (2.0, 5.0) to (2.0, 5.0)
For the isParallelTo method, include test cases for each of the following: • Line segments 1 and 2. • Line segments 1 and 3. • Line segments 4 and 5. • Line segments 4 and 6. • Line segment 4 compared with itself (yes, this is considered parallel).
For each test case for the isParallelTo method, use an if-else statement in your test driver to print whether or not the two line segments were found to be parallel. All output should include enough information so you can identify which line segments are involved in each test case.
For both the midpoint and length methods, include test cases for each of the following: • Line segment 1 • Line segment 3 • Line segment 4 • Line segment 7
It is okay to use the CartesianPoint toString() method for displaying midpoint results. Code for Cartesian Point Show nested quote +/** Represents a point in a 2D plane, defined as (x,y). */ public class CartesianPoint { private double x; private double y; public CartesianPoint(double xVal, double yVal) { x = xVal; y = yVal; } public double getX() { return x; }
public double getY() { return y; } public double distance (CartesianPoint other) { double dx = x - other.x; double dy = y - other.y; return Math.sqrt(dx*dx + dy*dy); } public String toString() { return "CartesianPoint[x=" + x + ", y=" + y + "]"; } }
Code for LineSegment Show nested quote +public class LineSegment { private CartesianPoint point1; private CartesianPoint point2;
public LineSegment(CartesianPoint pt1,CartesianPoint pt2) { point1 = new CartesianPoint(pt1.getX(), pt1.getY()); point2 = new CartesianPoint(pt2.getX(), pt2.getY()); } public LineSegment(double x1, double x2, double y1, double y2) { point1 = new CartesianPoint(x1, y1); point2 = new CartesianPoint(x2, y2); } public CartesianPoint getPoint1() { return point1; } public CartesianPoint getPoint2() { return point2; } public boolean isParallelTo(LineSegment line) { boolean isParallel = false; boolean undefined1 = false; boolean undefined2 = false; double slope1 = 0; double slope2 = 0; if(point1.getX() - point2.getX() == 0) undefined1 = true; else { slope1 = (point1.getY() - point2.getY() / point1.getX() - point2.getX()); } if(line.getPoint1().getX() - line.getPoint2().getY() == 0) undefined2 = true; else { slope2 = line.getPoint1().getY() - line.getPoint2().getY() / line.getPoint1().getX() - line.getPoint2().getY(); } if(slope1 == slope2 || undefined1 == undefined2) isParallel = true; return isParallel; } public double length() { return point1.distance(point2); } public CartesianPoint midpoint() { double xVar = point1.getX() + point2.getX() / 2; double yVar = point1.getY() + point2.getY() / 2; CartesianPoint midpoint = new CartesianPoint(xVar, yVar); return midpoint; } } I've checked these over, the Cartesian was given (cannot be changed) and my Linesegment compiles and seems to meet all the criteria required I just don't understand how to put it into a test file... My minds like exploding. I can't look at the problem right now, but just glancing. One important rule : Never compare doubles by equality, specifically in this case do not compare double to 0. Use double_var < epsilon, where epsilon is low enough for your needs.
EDIT: As for the test class, just create a class, that somehow acquires test data, then executes the test cases and prints results. Acquiring data can be done in constructor, specialized method or have data hardcoded into the class. Then have some execution method that sequentially calls appropriate methods from your LineSegment class on that data and prints out the result based on return value.
|
On October 11 2012 06:53 mcc wrote:Show nested quote +On October 11 2012 06:46 NeMeSiS3 wrote:Need help on a question, I just can't understand how to create a test program for my assignment. I created the main program named LineSegment but when trying to make a LineTest file I just stare blankly. + Show Spoiler +In a separate LineTest class, create a test driver that exercises each of the methods in your LineSegment class, including the accessors for the two endpoints, as well as both constructors.
Use the following line segments as test data: Line Segment 1: (0.0, 0.0) to (0.0, 5.0) Line Segment 2: (2.0, 5.0) to (2.0, 7.0) Line Segment 3: (1.0, 5.0) to (6.0, 5.0) Line Segment 4: (2.0, 5.0) to (7.3, 10.1) Line Segment 5: (0.0, -3.0) to (10.6, 7.2) Line Segment 6: (0.0, -3.0) to (10.6, 3.0) Line Segment 7: (2.0, 5.0) to (2.0, 5.0)
For the isParallelTo method, include test cases for each of the following: • Line segments 1 and 2. • Line segments 1 and 3. • Line segments 4 and 5. • Line segments 4 and 6. • Line segment 4 compared with itself (yes, this is considered parallel).
For each test case for the isParallelTo method, use an if-else statement in your test driver to print whether or not the two line segments were found to be parallel. All output should include enough information so you can identify which line segments are involved in each test case.
For both the midpoint and length methods, include test cases for each of the following: • Line segment 1 • Line segment 3 • Line segment 4 • Line segment 7
It is okay to use the CartesianPoint toString() method for displaying midpoint results. Code for Cartesian Point /** Represents a point in a 2D plane, defined as (x,y). */ public class CartesianPoint { private double x; private double y; public CartesianPoint(double xVal, double yVal) { x = xVal; y = yVal; } public double getX() { return x; }
public double getY() { return y; } public double distance (CartesianPoint other) { double dx = x - other.x; double dy = y - other.y; return Math.sqrt(dx*dx + dy*dy); } public String toString() { return "CartesianPoint[x=" + x + ", y=" + y + "]"; } }
Code for LineSegment public class LineSegment { private CartesianPoint point1; private CartesianPoint point2;
public LineSegment(CartesianPoint pt1,CartesianPoint pt2) { point1 = new CartesianPoint(pt1.getX(), pt1.getY()); point2 = new CartesianPoint(pt2.getX(), pt2.getY()); } public LineSegment(double x1, double x2, double y1, double y2) { point1 = new CartesianPoint(x1, y1); point2 = new CartesianPoint(x2, y2); } public CartesianPoint getPoint1() { return point1; } public CartesianPoint getPoint2() { return point2; } public boolean isParallelTo(LineSegment line) { boolean isParallel = false; boolean undefined1 = false; boolean undefined2 = false; double slope1 = 0; double slope2 = 0; if(point1.getX() - point2.getX() == 0) undefined1 = true; else { slope1 = (point1.getY() - point2.getY() / point1.getX() - point2.getX()); } if(line.getPoint1().getX() - line.getPoint2().getY() == 0) undefined2 = true; else { slope2 = line.getPoint1().getY() - line.getPoint2().getY() / line.getPoint1().getX() - line.getPoint2().getY(); } if(slope1 == slope2 || undefined1 == undefined2) isParallel = true; return isParallel; } public double length() { return point1.distance(point2); } public CartesianPoint midpoint() { double xVar = point1.getX() + point2.getX() / 2; double yVar = point1.getY() + point2.getY() / 2; CartesianPoint midpoint = new CartesianPoint(xVar, yVar); return midpoint; } } I've checked these over, the Cartesian was given (cannot be changed) and my Linesegment compiles and seems to meet all the criteria required I just don't understand how to put it into a test file... My minds like exploding. I can't look at the problem right now, but just glancing. One important rule : Never compare doubles by equality, specifically in this case do not compare double to 0. Use double_var < epsilon, where epsilon is low enough for your needs. I am a first year CS student and this is as much as they've taught us thus far (one month in) so I don't really know how that would work. Sorry I'm green as grass. Thank you though.
|
On October 11 2012 06:54 NeMeSiS3 wrote:Show nested quote +On October 11 2012 06:53 mcc wrote:On October 11 2012 06:46 NeMeSiS3 wrote:Need help on a question, I just can't understand how to create a test program for my assignment. I created the main program named LineSegment but when trying to make a LineTest file I just stare blankly. + Show Spoiler +In a separate LineTest class, create a test driver that exercises each of the methods in your LineSegment class, including the accessors for the two endpoints, as well as both constructors.
Use the following line segments as test data: Line Segment 1: (0.0, 0.0) to (0.0, 5.0) Line Segment 2: (2.0, 5.0) to (2.0, 7.0) Line Segment 3: (1.0, 5.0) to (6.0, 5.0) Line Segment 4: (2.0, 5.0) to (7.3, 10.1) Line Segment 5: (0.0, -3.0) to (10.6, 7.2) Line Segment 6: (0.0, -3.0) to (10.6, 3.0) Line Segment 7: (2.0, 5.0) to (2.0, 5.0)
For the isParallelTo method, include test cases for each of the following: • Line segments 1 and 2. • Line segments 1 and 3. • Line segments 4 and 5. • Line segments 4 and 6. • Line segment 4 compared with itself (yes, this is considered parallel).
For each test case for the isParallelTo method, use an if-else statement in your test driver to print whether or not the two line segments were found to be parallel. All output should include enough information so you can identify which line segments are involved in each test case.
For both the midpoint and length methods, include test cases for each of the following: • Line segment 1 • Line segment 3 • Line segment 4 • Line segment 7
It is okay to use the CartesianPoint toString() method for displaying midpoint results. Code for Cartesian Point /** Represents a point in a 2D plane, defined as (x,y). */ public class CartesianPoint { private double x; private double y; public CartesianPoint(double xVal, double yVal) { x = xVal; y = yVal; } public double getX() { return x; }
public double getY() { return y; } public double distance (CartesianPoint other) { double dx = x - other.x; double dy = y - other.y; return Math.sqrt(dx*dx + dy*dy); } public String toString() { return "CartesianPoint[x=" + x + ", y=" + y + "]"; } }
Code for LineSegment public class LineSegment { private CartesianPoint point1; private CartesianPoint point2;
public LineSegment(CartesianPoint pt1,CartesianPoint pt2) { point1 = new CartesianPoint(pt1.getX(), pt1.getY()); point2 = new CartesianPoint(pt2.getX(), pt2.getY()); } public LineSegment(double x1, double x2, double y1, double y2) { point1 = new CartesianPoint(x1, y1); point2 = new CartesianPoint(x2, y2); } public CartesianPoint getPoint1() { return point1; } public CartesianPoint getPoint2() { return point2; } public boolean isParallelTo(LineSegment line) { boolean isParallel = false; boolean undefined1 = false; boolean undefined2 = false; double slope1 = 0; double slope2 = 0; if(point1.getX() - point2.getX() == 0) undefined1 = true; else { slope1 = (point1.getY() - point2.getY() / point1.getX() - point2.getX()); } if(line.getPoint1().getX() - line.getPoint2().getY() == 0) undefined2 = true; else { slope2 = line.getPoint1().getY() - line.getPoint2().getY() / line.getPoint1().getX() - line.getPoint2().getY(); } if(slope1 == slope2 || undefined1 == undefined2) isParallel = true; return isParallel; } public double length() { return point1.distance(point2); } public CartesianPoint midpoint() { double xVar = point1.getX() + point2.getX() / 2; double yVar = point1.getY() + point2.getY() / 2; CartesianPoint midpoint = new CartesianPoint(xVar, yVar); return midpoint; } } I've checked these over, the Cartesian was given (cannot be changed) and my Linesegment compiles and seems to meet all the criteria required I just don't understand how to put it into a test file... My minds like exploding. I can't look at the problem right now, but just glancing. One important rule : Never compare doubles by equality, specifically in this case do not compare double to 0. Use double_var < epsilon, where epsilon is low enough for your needs. I am a first year CS student and this is as much as they've taught us thus far (one month in) so I don't really know how that would work. Sorry  I'm green as grass. Thank you though. The reason for that is that you can get screwed over by rounding errors and something that should be 0 will be something like 1e-15, which will fail the test even though it should not.
|
@NeMeSiS3
Try this : 0.3 == 0.2 + 0.1
Should be true right? Unfortunately Java will tell you it is false, which has to do with how float numbers are stored in computers. (IEEE 754 for google) You should instead use something like :
if (value-0.3<0.0001) return true; else return false;
Hope this clears it up
|
On October 11 2012 07:02 Kalkom wrote: @NeMeSiS3
Try this : 0.3 == 0.2 + 0.1
Should be true right? Unfortunately Java will tell you it is false, which has to do with how float numbers are stored in computers. (IEEE 754 for google) You should instead use something like :
if (value-0.3<0.0001) return true; else return false;
Hope this clears it up
My problem was more with writing a testdriver but I will get to editing my boolean statements to make them sharper.
Thanks btw guys
EDIT: For now I'm leaving in the double values as we will eventually learn what you are saying
Code so far (It could be COMPLETELY wrong, this is just how I am interpreting the question, someone feeds in data values (x1, y1, x2. y2) making up a line segment and then I will have to find a way to compare and see parallel, length and midpoint (not at those yet).
import java.util.*;
public class LineTest { public static void main(String[] args) { Scanner var = new Scanner(System.in); double x1, x2, y1, y2; System.out.println("Please enter a line segment" + "(in the form x1 (enter) x2 (enter) y1 " + "(enter) y2 (enter))"); x1 = var.nextDouble(); x2 = var.nextDouble(); y1 = var.nextDouble(); y2 = var.nextDouble(); LineSegment lineSeg1 = new LineSegment(x1, x2, y1, y2); } }
|
+ Show Spoiler +In a separate LineTest class, create a test driver that exercises each of the methods in your LineSegment class, including the accessors for the two endpoints, as well as both constructors.
Use the following line segments as test data: Line Segment 1: (0.0, 0.0) to (0.0, 5.0) Line Segment 2: (2.0, 5.0) to (2.0, 7.0) Line Segment 3: (1.0, 5.0) to (6.0, 5.0) Line Segment 4: (2.0, 5.0) to (7.3, 10.1) Line Segment 5: (0.0, -3.0) to (10.6, 7.2) Line Segment 6: (0.0, -3.0) to (10.6, 3.0) Line Segment 7: (2.0, 5.0) to (2.0, 5.0)
For the isParallelTo method, include test cases for each of the following: • Line segments 1 and 2. • Line segments 1 and 3. • Line segments 4 and 5. • Line segments 4 and 6. • Line segment 4 compared with itself (yes, this is considered parallel).
For each test case for the isParallelTo method, use an if-else statement in your test driver to print whether or not the two line segments were found to be parallel. All output should include enough information so you can identify which line segments are involved in each test case.
For both the midpoint and length methods, include test cases for each of the following: • Line segment 1 • Line segment 3 • Line segment 4 • Line segment 7
It is okay to use the CartesianPoint toString() method for displaying midpoint results.
This is your data.
Line Segment 1: (0.0, 0.0) to (0.0, 5.0) Line Segment 2: (2.0, 5.0) to (2.0, 7.0) Line Segment 3: (1.0, 5.0) to (6.0, 5.0) Line Segment 4: (2.0, 5.0) to (7.3, 10.1) Line Segment 5: (0.0, -3.0) to (10.6, 7.2) Line Segment 6: (0.0, -3.0) to (10.6, 3.0) Line Segment 7: (2.0, 5.0) to (2.0, 5.0)
Run isParallelTo for
• Line segments 1 and 2. • Line segments 1 and 3. • Line segments 4 and 5. • Line segments 4 and 6. • Line segment 4 and 4.
Run midpoint and length for
• Line segment 1 • Line segment 3 • Line segment 4 • Line segment 7
Create the 7 instances of your LineSegment class with the data. Then run the functions for each of the test cases above.
For example,
import java.util.*;
public class LineTest { public static void main(String[] args) { // initialize data LineSegment line1 = new LineSegment(0.0, 0.0, 0.0, 5.0); LineSegment line2 = new LineSegment(2.0, 5.0, 2.0, 7.0);
// run isParallelTo for line1 and line2 system.out.println(line1.isParallelTo(line2));
// run midpoint and length for line1 system.out.println(line1.midpoint()); system.out.println(line1.length());
} }
The isParallelTo(LineSegment l) function is incorrect.
slope2 = line.getPoint1().getY() - line.getPoint2().getY() / line.getPoint1().getX() - line.getPoint2().getY();
Notice the problem?
I'm not a Java programmer so forgive me if I made a mistake somewhere in syntax. Other things you should work on though.
• Indenting style. Use Allman's or K&R's, never Horstman's. And don't mix and match them.
• Don't skip braces in if-elsif-else branches unless you only have an if branch. It makes the code annoying to read because if there are errors, you're going to have to check the braces too and this just makes it a hassle.
• Use brackets to strictly enforce your BEDMAS rules.
slope2 = line.getPoint1().getY() - line.getPoint2().getY() / line.getPoint1().getX() - line.getPoint2().getY(); Looks like you want to do (y2/x1) rather than (y1-y2)/(x1-x2). Here's the error in your code by the way. Actually, there's 2.
|
I'm wondering if anyone on TeamLiquid is involved in the Zero Robotics competition held by NASA and MIT. StarCraft players have to unite in the alliance stage! :D
Send me a PM if you are involved, doubting anyone responses though 
~Rock Rovers
(Zero Robotics is a competition in C to make a sphere - a little cube on the ISS - play a game, autonomously. ONLY HIGH SCHOOLS ARE ALLOWED. I'm a junior.)
|
Hey guys! My friend needs a bit of help. In his direct words, "What does a programmer usually want to be paid to program an app that is algorithmed up and ready to go?" Thanks.
|
On October 11 2012 16:29 PPTouch wrote: Hey guys! My friend needs a bit of help. In his direct words, "What does a programmer usually want to be paid to program an app that is algorithmed up and ready to go?" Thanks.
Answer: "It depends."
What does the app do? Who is the client? How many (free) post-launch changes can be expected?
|
On October 11 2012 16:31 Morfildur wrote:Show nested quote +On October 11 2012 16:29 PPTouch wrote: Hey guys! My friend needs a bit of help. In his direct words, "What does a programmer usually want to be paid to program an app that is algorithmed up and ready to go?" Thanks. Answer: "It depends." What does the app do? Who is the client? How many (free) post-launch changes can be expected? Thanks! Do you mind if I PM you once I attain this information?
|
hihi, i am trying to do something very simple. it is like my 2nd class so we havent covered much at all. + Show Spoiler + #include <cstdlib> #include <iostream>
using namespace std;
int main(int argc, char *argv[]) { float n; float n1; cout<<"Enter numbers. Press '0' to end."<<endl; do { cin>>n; n1=n1+n; } while (n!=0); cout<<"Okay! The total sum of your numbers is "<<n1<<endl; system("PAUSE"); }
however i want to make the button to quit the loop to be 'g' rather than '0' ie a char/long. i tried making it into If or Switch and that didnt seem to work, and i tried looking at making a Do..While or While inside the Do...While... that didnt seem to work. so i realised it was probably something u can do with validation which we touched on for a split second..
but i cant get anything to work and dont want to jump ahead doing stuff with copy paste im unable to understand
we have a worksheet with an example of atoi validation which would make it look something like..
+ Show Spoiler + #include <cstdlib> #include <iostream>
using namespace std;
int main(int argc, char *argv[]) { char exit[256]; double n1; double n; cout<<"Enter numbers. '0' to end."<<endl; do { gets (exit); n=atoi(exit); n1+=n; } while (exit!="0"); cout<<"ending"<<endl; cout<<n1; system("PAUSE"); }
//while (!((cin >> n).rdstate() & ifstream::failbit)) { n2 += n; }
someone online suggested trying strcmp and strtol but i dont know how to use those (was messing around and couldnt get much out of it at least) . really if there is a simpler way then i should stick to that because we will probably be covering it in class soonish...
+ Show Spoiler + #include <cstdlib> #include <iostream> #include <string.h>
using namespace std;
int main(int argc, char *argv[]) { int n; int n1; char exit[256];
cout<<"Enter numbers. 'g' to end."<<endl; //while(0 != strcmp(stringsx,"g")) do { cin>>exit; n1+=n; } while (1 != strcmp(exit,"g")); cout<<"ending"<<endl; cout<<n1; system("PAUSE"); }
//while (!((cin >> n).rdstate() & ifstream::failbit)) { n2 += n; }
the other thing in the bottom of the code is something someone suggested but i asked for something simpler so ignored that so far
|
On October 13 2012 06:35 FFGenerations wrote: however i want to make the button to quit the loop to be 'g' rather than '0' Then do something like this:
do { char input; cin >> input;
// optionally if you include <stdio.h> getchar( &input ); } while(input != 'g');
Floats take up 4 bytes of memory. If you don't know what a byte is, try looking it up. A character takes only a single byte. If you are trying to grab a float out of cin (your n variable is a float) it will be looking for something that it can interpret as a float, i.e. a number. You won't be able to convert a float to the character 'g' very easily. This is because the bit format for a float is very different from that of a character.
So, you use cin and extract a single character, which grabs the next 8 bits from the standard in stream (cin). If you aren't familiar with the difference between ' ' and " " in C and C++, then read the spoiler: + Show Spoiler +Using the ' ' will generate a character literal, as in a single byte long character. 'a' is actually an integer, as characters are unsigned integers ranging from 0 to 255. 'a' represents the number 96. You can actually do 10 + 'a' and have valid code. 10 + 'a' is 106, or 'j'. " " creates a string literal, as in an array of characters. " " takes whatever is inside the two brackets and creates an array of characters with the characters between the two quotes, and places the character representing the number 0 at the end. This represents a string in C. An array of characters with a null character ( 0 ) at the end is a string. So, using "sadf" returns a character array that represents a string, which looks like this: "sadf" // equivalent to the following: { 's', 'a', 'd', 'f', '/0' }
Edit: I also don't like seeing everyone do "using namespace std;". I guess it's okay if this is only your second class, but really you should be fully understanding what a namespace is, what using means, and what std is. When you write cin you should be manually accessing the std namespace with the :: operator. The using operator clutters your global namespace. I'm just not a fan of using things you don't understand.
do { char input; std::cin >> input;
// optionally if you include <stdio.h> getchar( &input ); } while(input != 'g');
|
Hey guys im creating a currency webpage with php and I have a question on how to do the conversion calculation This is my code so far
<?php
$aExchangeRates = array ( 'USD' => 1.00000 'AUD' => 0.95593 'BPL' => 0.49418 'CAD' => 0.97645 'CNY' => 6.30662 'EUR' => 0.77045 'GBP' => 0.61616 'JPY' => 78.1850 'INR' => 53.3650 'NZD' => 1.20642 'RUB' => 31.0310 );
$amount = $_POST['amount']; $from = $_POST['from']; $to = $_POST['to']; echo " " . $amount . " " . $from . " Converts to " . $to . " "; ?>
I get the amount, to and from, from a textfield, drop down and another dropdown, and ideas how can i can fit in the array to compute the currencies?
|
Hyrule18982 Posts
$amount = $amount; $from = $amount * $aExchangeRates['USD']; $to = $from * $aExchangeRates[$_POST['to']]; echo " $amount from {$_POST['from']} converts to \$$to {$_POST['to']} "; something like that I guess completely untested
|
On October 13 2012 12:18 dapierow wrote:Hey guys im creating a currency webpage with php and I have a question on how to do the conversion calculation This is my code so far <?php
$aExchangeRates = array ( 'USD' => 1.00000 'AUD' => 0.95593 'BPL' => 0.49418 'CAD' => 0.97645 'CNY' => 6.30662 'EUR' => 0.77045 'GBP' => 0.61616 'JPY' => 78.1850 'INR' => 53.3650 'NZD' => 1.20642 'RUB' => 31.0310 );
$amount = $_POST['amount']; $from = $_POST['from']; $to = $_POST['to']; echo " " . $amount . " " . $from . " Converts to " . $to . " "; ?>
I get the amount, to and from, from a textfield, drop down and another dropdown, and ideas how can i can fit in the array to compute the currencies?
Are you doing this as a proof of concept or for real work. If the latter why not use a service like google calculator?
Example way of using google calculator
If it is a proof of concept, I'm guessing all those exchange rates are in relation to USD ? So you would have to convert everything to USD and then convert that result to your target currency.
From currency X to currency Y with value V going through USD -> ( V / exchangeRate(X) ) * exchangeRate(Y)
|
I just started using Python 2.7.3
The package comes with an IDLE Python GUI (which comes with a run Module option in the shell) and a Python Command line.
Now I write my programs in the GUI, but how do I run them from the command line? Because some of the practice code I am writing have prompts such as 'if x < 1, print " "', meaning the user must type in a value for the program to continue.
But I can't get the thing to work.
Does the command line have a code editor or is there something I am missing?
|
http://cli.learncodethehardway.org/book/ http://learnpythonthehardway.org/book/ex0.html Here are 2 links for my info.
The cli doesn't have a code editor. You write your code in a separate text editor and save it. Then you open your cli (powershell assuming windows) and change folders (type "cd name" to do so) into where your code was saved through the cli. When you're at the right folder in the cli, run your program from it (type "python name.py"). I'll assume you know that name is the name of the folder and the file. One thing is that you might write "cd name" and the name you type is the folder your code is in itself. It won't work like that. First type "pwd" and that will print the current directory/folder you are in. Then type "ls" that will list the folders/files in the directory you are in. Then you change into the folder which contains the folder where your code is saved like "My Documents". And you keep changing till you reach it.
|
On October 11 2012 07:04 NeMeSiS3 wrote:My problem was more with writing a testdriver Blisse's post above will work, and it can definitely help you out. Pay attention to most of his points about style - it'll definitely help the next time you have to ask someone else for assistance (much easier to read well-formatted code).
However, I would suggest using a Unit Test instead of writing a binary class - JUnit is your friend. In fact, if you're using Eclipse, this could probably be built in.
Additionally, when you compare doubles, make sure to take the absolute value of the difference, instead of doing just (Expected - Actual) < epsilon. Even better, if you use a unit test, simply write
assertEquals(expected, actual, epsilon) - if your variables are doubles, TestCase.assertEquals will take care of the rest of it for you.
More info on java unit tests: http://www.vogella.com/articles/JUnit/article.html
|
So I just got my lineup for the midterm + Show Spoiler + 1. Write a "standard" java class.
//Instance variables //Constructors //Methods --Accessor (getters) --Mutator (setters)
2. "Has a" relationships
3. If Statements (easy pzy)
4. Understanding object reference variables EX: CP pt1 = new CP(1,2); CP pt2 = new CP(3,4); pt1 = pt2; pt1.setX(5); pt2.getX();
What is going to be presented? (ASSUME*** all previous code is in place to make such functions able to process)
My question is how should I study for this? It seems like a silly question but we never really got any questions all year, we just ran examples and did assignments and as this is my first year in CS I just dunno what it'll look like. Any advice or help on these, anyone got a simple way to remember "class" creation/use. I always have a bitch of a time writing multiple classes.
thanks!
|
|
|
|