简体   繁体   中英

Calling one method into another

Here's an assignment I have to do for school:

  • Create a new project named AreaCircle
  • Copy the distance method from section 5.2
  • Copy the area method for circles from the exercise 4.2. Change it to return the area, rather than printing it.
  • Create another area method for circles that takes two points (four integer parameters: x1, x2, y1, y2) and returns a double. Be sure both methods have the same name. Since the parameters will be different, the one that is executed will depend on the parameters passed. In this new area method, call the distance method to obtain the radius of the circle.
    Then call the OTHER (original) area method to obtain the area of the circle.
  • You will need to make a couple of changes so that you don't get a loss of precison compiler error.
  • In main, prompt the user for four integers that represent a point on the outside of the circle and the center point of the circle (these will be used to find the length of the radius). Call the appropriate area method. Display the result.

I'm not sure if my code is right but obviously there is something wrong because it isn't working...

public static void main(String[] args) {
    Scanner reader;
    reader = new Scanner (System.in);
    System.out.println("Please enter the coordinates of a circle:");
    newLine();
    System.out.println("Outside point:");
    newLine();
    System.out.println("x1:");
    int x1 = reader.nextInt();
    newLine();
    System.out.println("y1:");
    int y1 = reader.nextInt();
    newLine();
    System.out.println("Center Point:");
    newLine();
    System.out.println("x2:");
    int x2 = reader.nextInt();
    newLine();
    System.out.println("y2:");
    int y2 = reader.nextInt();
}

public static void area(double radius, int x1, int x2, int y1, int y2)
{
    double areaCircle = (Math.PI * area(x1, x2, y1, y2) * area(x1, x2, y1, y2));
}

public static double area(int x1, int x2, int y1, int y2) {
   double radius = distance (x1, y1, x2, y2);
   return radius;
}

public static double distance(int x1, int y1, int x2, int y2) 
{
    double dx = x2 - x1;
    double dy = y2 - y1;
    double dsquared = dx*dx + dy*dy;
    double result = Math.sqrt (dsquared);
    return result;
}

//NewLine Method
public static void newLine () {
    System.out.println ("");
}

As previously mentioned, your code never calls any of the calculation methods, meaning that they're never run and area etc. is never calculated. There isn't any code for displaying the result either.

In addition, the area method doesn't even return anything, meaning that it calculates the value and then discards it.

Your main didn't call any of the other methods!. Complete the code first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM