Soldato
- Joined
- 24 Apr 2011
- Posts
- 5,455
I'm having a small problem here. I'm doing a past paper question, as tomorrow I have an exam. We had written it in class on a paper, but I am programming it, just so I know how it will work etc.
Its a long-ish question, but I'm stuck on the last part. The question basically tells you to tell the user to enter 2 points, and see if they are equal. If not, calculate the distance between them and output distance as an int. Ok, fair enough.
The last part tells you to do a static method in the main class which accepts the points and returns distance between them.
I couldn't get it working. This is what I did:
main method:
That gives me a 'not a statement' error.
So I tried not putting it in the main method, like this:
Now the question specifically states, IN THE MAIN METHOD, so I am stumped.
I'm very shaky in Java, so don't be harsh. I am trying to wrap my head around this, so I do well in my exam tomo (Won't consist of java only btw. Java is 30%, I'm confident I can pass even if I get 0 in java, but still I wanna do well)
Its a long-ish question, but I'm stuck on the last part. The question basically tells you to tell the user to enter 2 points, and see if they are equal. If not, calculate the distance between them and output distance as an int. Ok, fair enough.
The last part tells you to do a static method in the main class which accepts the points and returns distance between them.
I couldn't get it working. This is what I did:
class ExamPastPaper2009 {
int x = 0;
int y = 0;
ExamPastPaper2009() {
x = 0;
y = 0;
}
ExamPastPaper2009 (int x, int y) {
this.x = x;
this.y = y;
}
void setx (int x) {
this.x=x;
}
void sety (int y) {
this.y = y;
}
int getx (){
return x;
}
int gety () {
return y;
}
void setValues (int x, int y) {
this.x = x;
this.y = y;
}
boolean equals (ExamPastPaper2009 p) {
if ((this.x == p.x) && (this.y == p.y))
return true;
else
return false;
}
public String toString () {
return ("(" + this.x + ") (" + this.y + ")");
}
}
main method:
import java.util.*;
class useExamPastpaper2009 {
public static void main (String args []) {
Scanner sc = new Scanner (System.in);
System.out.println ("Enter x1");
int x1 = sc.nextInt();
System.out.println("Enter y1");
int y1 = sc.nextInt();
ExamPastPaper2009 p1 = new ExamPastPaper2009 (x1,y1);
System.out.println ("Enter x2");
int x2 = sc.nextInt();
System.out.println("Enter y2");
int y2 = sc.nextInt();
ExamPastPaper2009 p2 = new ExamPastPaper2009 (x2,y2);
if (p1.equals(p2)) {
System.out.println("They are equal");
}
else {
System.out.println("They are not equal. The distance between them is: " + calculate_distance (p1, p2));
}
Static int calculate_distance (ExamPastPaper2009 p1, ExamPastPaper2009 p2) {
double temp1 = Math.pow((p2.x - p1.x),2);
double temp2 = Math.pow((p2.y - p1.y),2);
int result = (int) Math.sqrt(temp1 + temp2);
return result;
}
}
}
That gives me a 'not a statement' error.
So I tried not putting it in the main method, like this:
class ExamPastPaper2009 {
int x = 0;
int y = 0;
ExamPastPaper2009() {
x = 0;
y = 0;
}
ExamPastPaper2009 (int x, int y) {
this.x = x;
this.y = y;
}
void setx (int x) {
this.x=x;
}
void sety (int y) {
this.y = y;
}
int getx (){
return x;
}
int gety () {
return y;
}
void setValues (int x, int y) {
this.x = x;
this.y = y;
}
boolean equals (ExamPastPaper2009 p) {
if ((this.x == p.x) && (this.y == p.y))
return true;
else
return false;
}
public String toString () {
return ("(" + this.x + ") (" + this.y + ")");
}
static int calculate_distance (ExamPastPaper2009 p1, ExamPastPaper2009 p2) {
double temp1 = Math.pow((p2.x - p1.x),2);
double temp2 = Math.pow((p2.y - p1.y),2);
int result = (int) Math.sqrt(temp1 + temp2);
return result;
}
}
import java.util.*;
class useExamPastpaper2009 {
public static void main (String args []) {
Scanner sc = new Scanner (System.in);
System.out.println ("Enter x1");
int x1 = sc.nextInt();
System.out.println("Enter y1");
int y1 = sc.nextInt();
ExamPastPaper2009 p1 = new ExamPastPaper2009 (x1,y1);
System.out.println ("Enter x2");
int x2 = sc.nextInt();
System.out.println("Enter y2");
int y2 = sc.nextInt();
ExamPastPaper2009 p2 = new ExamPastPaper2009 (x2,y2);
if (p1.equals(p2)) {
System.out.println("They are equal");
}
else {
System.out.println("They are not equal. The distance between them is: " + ExamPastPaper2009.calculate_distance (p1, p2));
}
}
}
Now the question specifically states, IN THE MAIN METHOD, so I am stumped.
I'm very shaky in Java, so don't be harsh. I am trying to wrap my head around this, so I do well in my exam tomo (Won't consist of java only btw. Java is 30%, I'm confident I can pass even if I get 0 in java, but still I wanna do well)