Search code examples
javajavascriptnetbeansbooleanhypotenuse

NEED HELP: Computer Science 101 HW/JAVA


I wrote the code for the problem in the title and seem to be having some problems. Can anyone give me some insight or tips on what I am doing wrong. Especially with the "if...else" portion of the code.

Here is the question #9. If you click the link it will show you a printout of the question. http://s21.postimg.org/nl2tmf5tj/Screen_Shot_2013_09_21_at_6_44_46_PM.png

Here is my code:

import java.util.*;
public class Ch3Ex9 {
/**
 * This method asks user for an x,y coordinates of a point, then returns the
 * distance to the origin and which quadrant the point is in.
 */
public static void main(String[] args)
{
    double xCoord; //x Coordinant initialized to 3
    double yCoord; //y Coordinant initalized to 4
    double hypo; //hypotenuse

    //declare an instance of Scanner to read the datastream from the keyboard
    Scanner keyboard = new Scanner(System.in);

    //get x Coordinant from the user
    System.out.print("Please enter the X coordinant: ");
    xCoord = keyboard.nextDouble();

    //get Y Coordinate from the user
    System.out.print("Please enter the Y coordinant: ");
    yCoord = keyboard.nextDouble();

    //calculate the hypotenuse which is the length to the origin
    hypo = Math.hypot(xCoord, yCoord);
            System.out.println("The hypotenuse is "+hypo);

    //determine which Quadrant the user-inputted point resides in
    if (xCoord>0) && (yCoord>0) //
        System.out.println("Point lies in Quadrant I.");
    else if (xCoord<0) && (yCoord>0)
        System.out.println("Point lies in Quadrant II.");
    else if (xCoord<0) && (yCoord<0)
        System.out.println("Point lies in Quadrant III.");
    else if (xCoord>0) && (yCoord<0)
        System.out.println("Point lies in Quadrant IV.")
    }
}

Solution

  • There are too many parentheses. Change

    if (xCoord>0) && (yCoord>0) 
    

    to

    if (xCoord>0 && yCoord>0) 
    

    and similarly with the others.