coordinate finder syntax, if else statement help

import java.util.*;

publicclass Cartesian

{

publicstaticvoid main (String[] args)

{

double locale;

double origin;

double xaxis;

double yaxis;

double quad1;

double quad2;

double quad3;

double quad4;

double first;

double second;

double third;

double fourth;

Scanner console =new Scanner(System.in);

System.out.println("Enter x-coordinate: ");

double x = console.nextDouble();

System.out.println("Enter y-coordinate: ");

double y = console.nextDouble();

if (x == 0 && y == 0)

locale = origin;

if (x > 0 && y == 0)

locale = xaxis;

if (x < 0 && y == 0)

locale = xaxis;

if (x == 0 && y > 0)

locale = yaxis;

if (x == 0 && y < 0)

locale = yaxis;

if (x > 0 && y > 0)

locale = quad1;

if (x < 0 && y > 0)

locale = quad2;

if (x < 0 && y < 0)

locale = quad3;

if (x > 0 && y < 0)

locale = quad4;

quad1 = first;

quad2 = second;

quad3 = third;

quad4 = fourth;

System.out.println("(" + x +"," + y") " +"is on the " + locale"quadrant"));// ")" expected?

}

}

Question:

1.) How do I make an if statement that subtracts quadrant if it is the origin, otherwise it'd print (0,0) is on the origin quadrant

-Same goes with xaxis and yaxis

[2841 byte] By [nsfoura] at [2007-11-27 2:56:53]
# 1

1) You code won't compile

System.out.println("(" + x + "," + y ") " + "is on the " + locale "quadrant"));

should be

System.out.println("(" + x + "," + y + ") " + "is on the " + locale + "quadrant");

2) What is the meaning of the following code ?

quad1 = first;

quad2 = second;

quad3 = third;

quad4 = fourth;

3) How do I make an if statement that subtracts quadrant if it is the origin, otherwise it'd print (0,0) is on the origin quadrant

I don't understand your question, but the logic should look like this

if (origin) {

// subtract quadrant

}

rym82a at 2007-7-12 3:35:01 > top of Java-index,Java Essentials,Java Programming...
# 2
When I subtract that last ")" it still brings up error message ')' expectedThe quad1-4 code is converting the identifiers into proper names instead of:(x,y) is on the quad1 quadrantI want,(x,y) is on the first quadrantI couldn't think of any other way
nsfoura at 2007-7-12 3:35:02 > top of Java-index,Java Essentials,Java Programming...
# 3

You don't need all these local variables.public static void main (String[] args) {

Scanner console = new Scanner(System.in);

System.out.println("Enter x-coordinate: ");

double x = console.nextDouble();

System.out.println("Enter y-coordinate: ");

double y = console.nextDouble();

String location = "?";

if (x == 0 && y == 0) {

location = "origin";

} else if (y == 0) {

location = "x axis";

} else if (x == 0) {

location = "y axis";

} else if (x > 0) {

if( y > 0) {

//...

} else {

//...

}

} else {

if( y > 0) {

//...

} else {

//...

}

}

System.out.print("(" + x + "," + y ") is on the "+location);

}

TimTheEnchantora at 2007-7-12 3:35:02 > top of Java-index,Java Essentials,Java Programming...
# 4

> When I subtract that last ")" it still brings up

> error message ')' expected

You should check my previous post, you have some "+" sign missing.

> The quad1-4 code is converting the identifiers into

> proper names instead of:

>

> (x,y) is on the quad1 quadrant

>

> I want,

>

> (x,y) is on the first quadrant

>

>

> I couldn't think of any other way =\

You code is wrong. You need something like this

String myLocation = "origin";

if ( x,y = 1,1) {

myLocation = "first";

} else ( x,y = 1,-1) {

myLocation = "second";

}

System.out.println("It is in " + myLocation + " quadrant");

rym82a at 2007-7-12 3:35:02 > top of Java-index,Java Essentials,Java Programming...
# 5

before trying your method which doesn't seem to fit because (1,-1) is not on the origin, i simplified mine..

all i want to do is assign the location a [lack of technical terms] the corresponding word.

import java.util.*;

public class Cartesian

{

public static void main (String[] args)

{

double locale;

double origin;

double xaxis;

double yaxis;

double first;

double second;

double third;

double fourth;

Scanner console = new Scanner(System.in);

System.out.println("Enter x-coordinate: ");

double x = console.nextDouble();

System.out.println("Enter y-coordinate: ");

double y = console.nextDouble();

if (x == 0 && y == 0)

locale = origin;

if (x > 0 && y == 0)

locale = xaxis;

if (x < 0 && y == 0)

locale = xaxis;

if (x == 0 && y > 0)

locale = yaxis;

if (x == 0 && y < 0)

locale = yaxis;

if (x > 0 && y > 0)

locale = first;

if (x < 0 && y > 0)

locale = second;

if (x < 0 && y < 0)

locale = third;

if (x > 0 && y < 0)

locale = fourth;

System.out.println("(" + x + "," + y + ") " + "is on the " + locale + "quadrant");

}

}

nsfoura at 2007-7-12 3:35:02 > top of Java-index,Java Essentials,Java Programming...
# 6
wait, Tim..I'm going to try yours, however, I can already see where I'm going to get confused..hold on and let me try to figure this out
nsfoura at 2007-7-12 3:35:02 > top of Java-index,Java Essentials,Java Programming...
# 7

> before trying your method which doesn't seem to fit

> because (1,-1) is not on the origin, i simplified

> mine..

>

> all i want to do is assign the location a [lack of

> technical terms] the corresponding word.

>

Yes, you are right. The code I given even don't compile.

I was just giving you the idea how to implement this.

I am not giving you the answer.

So, you have to digest the sample code (pseudocode).

And apply it in your program.

You need a String and assign the content based on some conditions.

rym82a at 2007-7-12 3:35:02 > top of Java-index,Java Essentials,Java Programming...
# 8

I always hated those cascaded if-then-else thingies and I like squeezing

code to the max (read: minimum) so I would've done it this way:

0: zero

1: positive

2: negative

This little method can do it:public int value(double v) {

return (v == 0)?0:(v > 0)?1:2;

}

And this number:int i= value(x)+3*value(y);

Tells your all:

0: origin

1: positive x axis

2: negative x axis

3: positive y axis

4: 1st quadrant

5: 2nd quadrant

6: negative y axis

7: 3rd quadrant

8: 4th quadrant

kind regards,

Jos

JosAHa at 2007-7-12 3:35:02 > top of Java-index,Java Essentials,Java Programming...
# 9

true rym82.

as for the code, i got it up to this and reached a syntax error.

import java.util.*;

public class CartesianAlter

{

public static void main (String[] args); //missing method body, or declare abstract

{

Scanner console = new Scanner(System.in);

System.out.println("Enter x-coordinate:");

double x = console.nextDouble();

System.out.println("Enter y-coordinate:");

double y = console.nextDouble();

String location = "?";

if (x == 0 && y == 0) {

location = "origin";

} else if (y == 0) {

location = "x axis";

} else if (x == 0) {

location = "y axis";

} else if (x > 0 && y == 0) {

location = "first quadrant";

} else if (x < 0 && y > 0) {

location = "second quadrant";

} else if (x < 0 && y < 0) {

location = "third quadrant";

} else if (x > 0 && y < 0) {

location = "fourth quadrant";

}

System.out.print(x + "," + y + "is on the " +location);

}

}

nsfoura at 2007-7-12 3:35:02 > top of Java-index,Java Essentials,Java Programming...
# 10

> I always hated those cascaded if-then-else thingies

> and I like squeezing

> code to the max (read: minimum) so I would've done it

> this way:

> 0: zero

> 1: positive

> 2: negative

> This little method can do it:> public int value(double v) {

>return (v == 0)?0:(v > 0)?1:2;

> code]

> And this number:[code]

> int i= value(x)+3*value(y);

> Tells your all:

> 0: origin

> 1: positive x axis

> 2: negative x axis

> 3: positive y axis

> 4: 1st quadrant

> 5: 2nd quadrant

> 6: negative y axis

> 7: 3rd quadrant

> 8: 4th quadrant

> kind regards,

>

> Jos

i really do appreciate your alternative method, however, i think im going to rough it out on the if else statements.

thanks again! =]

nsfoura at 2007-7-12 3:35:03 > top of Java-index,Java Essentials,Java Programming...
# 11

public static void main (String[] args); //missing method body, or declare abstract

to

public static void main (String[] args) //missing method body, or declare abstract

You would see the magic that the code compiles (hopefully) when the semi-colon gone.

The error message always tell you what and where the errors are.

It would be helpful for yourself to understand it.

rym82a at 2007-7-12 3:35:03 > top of Java-index,Java Essentials,Java Programming...
# 12

import java.util.*;

public class CartesianAlter

{

public static void main (String[] args)

{

Scanner console = new Scanner(System.in);

System.out.println("Enter x-coordinate:");

double x = console.nextDouble();

System.out.println("Enter y-coordinate:");

double y = console.nextDouble();

String location = "origin";

String location = "x axis";//location is already defined in main(java.lang.String[])

String location = "y axis";

String location = "first quadrant";

String location = "second quadrant";

String location = "third quadrant";

String location = "fourth quadrant";

if (x == 0 && y == 0) {

location = "origin";

} else if (y == 0) {

location = "x axis";

} else if (x == 0) {

location = "y axis";

} else if (x > 0 && y == 0) {

location = "first quadrant";

} else if (x < 0 && y > 0) {

location = "second quadrant";

} else if (x < 0 && y < 0) {

location = "third quadrant";

} else if (x > 0 && y < 0) {

location = "fourth quadrant";

}

System.out.print(x + "," + y + " is on the " + location);

}

}

i already tried mixing up the strings like location1, location2.. but it keeps printing origin.

this is probably the last question.

nsfoura at 2007-7-12 3:35:03 > top of Java-index,Java Essentials,Java Programming...
# 13

Your reply #9 should work, why do you move to #12 ?

Use should not declare a variable so many times.

You can assign value to a variable as many time as you like.

String myLocation = "a";

myLocation = "b";

myLocation = "c";

myLocation = "d";

I would suggest you to start by reading the basic tutorial.

rym82a at 2007-7-12 3:35:03 > top of Java-index,Java Essentials,Java Programming...
# 14

You can't declare several variables with the same name.

And you dont need to have several variables here. You need one variable, and assign it a value depending on x and y (that's the job of the if/else statement.)

By the way, your condition for the first quadrant is probably wrong...

TimTheEnchantora at 2007-7-12 3:35:03 > top of Java-index,Java Essentials,Java Programming...
# 15
dudes..thank you so much..i love you all.now i have to crank out another program before 11:45..so beware of my controlled spamming..muahahahaha
nsfoura at 2007-7-21 20:37:27 > top of Java-index,Java Essentials,Java Programming...