I'm a high school student that's learning Java now, and we have all these huge assignments to do, but school just started and it takes time and practice to remember a lot of codes and stuff, yet I think it's going way too fast. So we did one project about linear equation, this was what the teacher told me:
/* YOUR PROGRAM ASSUMES THAT THE SLOPES AND Y-INTERCEPTS ARE INTEGERS
* EVEN IF THEY WERE INTEGERS, THE COORDINATES OF THE POINT OF INTERSECTION ARE NOT NECESSARILY INTEGERS
* ALTHOUGH YOUR COORDINATES ARE DOULBES, THEY ARE CALCULATED FROM INTEGER.
* THEREFORE THEY ARE INTEGERS CAST INTO DOUBLES. THE DECIMAL PART IS DISCARDED
* SO YOUR ANSWER IS WRONG IF THE COORDINATES OF THE SOLUTION ARE NOT INTEGERS
*
* YOUR CODE IS HARD TO READ BECAUSE THERE ARE NO COMMENTS, ALMOST NO INDENTING, AND NO USE OF BRACES FOR if CODE
*/
publicclass Line
{
publicstaticvoid main(String args[])
{
int slope1=Jin.readInt("Type first slope here ");
int intercept1=Jin.readInt("Type first intercept here ");
int slope2=Jin.readInt("Type second slope here ");
int intercept2=Jin.readInt("Type second intercept here ");
int bslope1=slope1;
int bintercept1=intercept1;
/* YOU DID NOT TEST FOR PARALLEL OR IDENTICAL LINES
*
* WHEN POSSIBLE, IT IS CLEARER IF YOU COLLECT ALL THE DATA FOR A LINE OF OUTPUT
* AND DISPLAY IT ALL IN A println STATEMENT, RATHER THAN MULTIPLE SEPARATED print STATEMENTS
*/
System.out.print("The intersection point between the lines y = ");
if (slope1!=1)
System.out.print(slope1);
System.out.print("x + " +intercept1+ " and y = ");
if (slope2!=1)
System.out.print(slope2);
System.out.print("x + " +intercept2+ " is (");
intercept2=intercept2-intercept1;
intercept1=0;
slope1=slope1-slope2;
slope2=0;
intercept2=intercept2/slope1;
slope1=1;
double xcoordinate=intercept2;
double ycoordinate=bslope1*xcoordinate+bintercept1;
System.out.println(+xcoordinate+ "," +ycoordinate+ ")." );
}
}
I don't understand what he's trying to say...and I'm really dumb, am I suppose to change int to double because I can't really assume that the numbers are integers...or what?
Okay, I can anwser! From a math stand point, using a (int) data type assumes that every number calculated will result in a whole number (1, 123, 9999929938, etc). Well what about a calculation like 3/2? Hey, that equals 1.5. WOW! You program will present only "1." Okay now it's 15 years in the future and your working for NASA, doing space shuttle re-entry plans. Your program has just truncated some very important information for landing. The shuttle will land in Russia istead of Florida because the ".5" was lost. In short the (int) data type was not big enough to hold 1.5 but only 1. Using the (double) data type allows you to hold the fractional numbers. So by using (double) whenever doing a calculation is a safe bet (i.e look at changing the slopes for example from (int) to (double)). And, no YOU ARE NOT DUMB! You are learning and you will alway continue in this time of life. I hope that helps.