Using methods in java programming

In summary: Finally, the triangle perimeter method and the circle circumference method would each call the findDistance method, passing in the appropriate arguments, and then the perimeter or circumference would be calculated and displayed. This would be a more modular and organized approach.
  • #1
preceptor1919
35
0
We are asked to make a program to prompt the user to choose between calculating the perimeter of a circle or triangle. We are supposed to use at least 5 methods. I believe I have created the appropriate methods but my real problem is with method calling.

Homework Equations

Java:
import java.util.*;
public class Lab4
{
  public static void main (String[] args)  {
  int userOption =0;
  // Sample code to test myMenu
  userOption = myMenu();
  //System.out.println("User selected Option"+userOption);
int x1=0;
int y2=0;
int x2=0;
int y2=0;
int x3=0;
int y3=0;
Scanner myInput = new Scanner(System.in);
if (userOption==1){
System.out.print("This program calculates the perimeter of a Triangle.");
System.out.print("Please enter the x-coordinate of point1:");
point1=myInput.nextInt();
System.out.print("Please enter the y-coordinate of point1:");
point2=myInput.nextInt();
System.out.print("Please enter the x-coordinate of point2:");
point3=myInput.nextInt();
System.out.print("Please enter the y-coordinate of point3:");
point4=myInput.nextInt();
System.out.print("Please enter the x-coordinate of point4:");
point5=myInput.nextInt();
System.out.print("Please enter the y-coordinate of point4:");
point6=myInput.nextInt();
}
}
public static double findDistance (int x1, int x2, int y1, int y2) {
  double length = Math.sqrt(Math.pow(x1 - x2),2 + Math.pow((y1 - y2), 2));
  return length;
  }
  public static double sideOne() {
  return findDistance (x1, x2, y1, y2);
  }

  public static double sideTwo() {
  return findDistance(x1, x3, y1, y3);
  }
  public static double sideThree() {
  return findDistance(x2, x3, y2, y3);
  }

public static double perimeter {
  System.out.println("The perimeter of a Triangle with point 1 (" + x1 + ", " + y1 + "), point 2 (" + x2 + ", " + y2 ") and point 3 (" + x3 + ", " + y3 + ") is " + Math.round(perimeter));
}  //------------------------------
  /** Method myMenu displays three options to be selected by keyboard,
  and it returns user selection as an integer number.
  @param  none
  [USER=124540]@Return[/USER] selected option: integer 1 or 2
  */
  public static int myMenu(){
  int userOption;
  Scanner myInput=new Scanner(System.in);
  do {
  System.out.println("Select one of the following options:");
  System.out.println(" 1. Triangle");
  System.out.println(" 2. Circle");
  System.out.println(" 3. Exit");
  userOption= myInput.nextInt();
  // To read a number of type float, use : myInput.nextFloat();
  // To read a character use : (myInput.next()).charAt(0);
  if (userOption==3){
  System.out.println("Bye");
  System.exit(0);
  }
  } while (userOption !=1 && userOption !=2);
  return userOption;
  }
}
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
preceptor1919 said:
We are asked to make a program to prompt the user to choose between calculating the perimeter of a circle or triangle. We are supposed to use at least 5 methods. I believe I have created the appropriate methods but my real problem is with method calling.

Homework Equations

Code:
import java.util.*;
public class Lab4
{
  public static void main (String[] args)  {
  int userOption =0;
  // Sample code to test myMenu
  userOption = myMenu();
  //System.out.println("User selected Option"+userOption);
int x1=0;
int y2=0;
int x2=0;
int y2=0;
int x3=0;
int y3=0;
Scanner myInput = new Scanner(System.in);
if (userOption==1){
System.out.print("This program calculates the perimeter of a Triangle.");
System.out.print("Please enter the x-coordinate of point1:");
point1=myInput.nextInt();
System.out.print("Please enter the y-coordinate of point1:");
point2=myInput.nextInt();
System.out.print("Please enter the x-coordinate of point2:");
point3=myInput.nextInt();
System.out.print("Please enter the y-coordinate of point3:");
point4=myInput.nextInt();
System.out.print("Please enter the x-coordinate of point4:");
point5=myInput.nextInt();
System.out.print("Please enter the y-coordinate of point4:");
point6=myInput.nextInt();
}
}
public static double findDistance (int x1, int x2, int y1, int y2) {
  double length = Math.sqrt(Math.pow(x1 - x2),2 + Math.pow((y1 - y2), 2));
  return length;
  }
  public static double sideOne() {
  return findDistance (x1, x2, y1, y2);
  }

  public static double sideTwo() {
  return findDistance(x1, x3, y1, y3);
  }
  public static double sideThree() {
  return findDistance(x2, x3, y2, y3);
  }

public static double perimeter {
  System.out.println("The perimeter of a Triangle with point 1 (" + x1 + ", " + y1 + "), point 2 (" + x2 + ", " + y2 ") and point 3 (" + x3 + ", " + y3 + ") is " + Math.round(perimeter));
}  //------------------------------
  /** Method myMenu displays three options to be selected by keyboard,
  and it returns user selection as an integer number.
  @param  none
  [USER=124540]@Return[/USER] selected option: integer 1 or 2
  */
  public static int myMenu(){
  int userOption;
  Scanner myInput=new Scanner(System.in);
  do {
  System.out.println("Select one of the following options:");
  System.out.println(" 1. Triangle");
  System.out.println(" 2. Circle");
  System.out.println(" 3. Exit");
  userOption= myInput.nextInt();
  // To read a number of type float, use : myInput.nextFloat();
  // To read a character use : (myInput.next()).charAt(0);
  if (userOption==3){
  System.out.println("Bye");
  System.exit(0);
  }
  } while (userOption !=1 && userOption !=2);
  return userOption;
  }
}
When you call a method, the number of actual arguments (the arguments used in the call) must agree with the number of formal arguments (the number of arguments in the method definition), the types of arguments in the call should agree with the types of arguments in the method definition, and finally, if the method returns a value, when the method is called, the return value should be stored in a variable or the return value should be otherwise used (such as in a print statement). You didn't provide any details on the problem you're having, so I can't get any more specific than that.

Some comments on your code:
1. This statement in findDistance is wrong:
Java:
double length = Math.sqrt(Math.pow(x1 - x2),2 + Math.pow((y1 - y2), 2));
The first call to pow() is not formed correctly. It should be Math.pow(x1 - x2, 2) - you're missing a right parenthesis.
2. You don't need these three methods: sideOne, sideTwo, and sideThree. Just use the findDistance method that you already have.
3. I would organize things differently. I would take all of the point input stuff out of main, and use it to call myMenu(). Depending on whether myMenu returns 1, 2, or 3, I would then call a method to calculate the perimeter of a triangle or another method to calculate the circumference of a circle. These two methods would each call a method to input the appropriate information for calculating the triangle perimeter or circle circumference.
 

1. What is the purpose of using methods in Java programming?

The main purpose of using methods in Java programming is to break down a large and complex problem into smaller, more manageable pieces. This allows for easier understanding, organization, and maintenance of code. Additionally, using methods helps to avoid repetition of code and promotes code reusability.

2. How do you define a method in Java?

To define a method in Java, you must first declare it using the keyword "void" (if the method does not return a value) or the data type of the value it will return. Then, give the method a name followed by parentheses (). Inside the parentheses, you can specify any parameters that the method will take in. Finally, use curly braces {} to enclose the code that will be executed when the method is called.

3. Can a method in Java take in multiple parameters?

Yes, a method in Java can take in multiple parameters. Each parameter should be separated by a comma within the parentheses when the method is declared. The parameters can then be used within the method's code to perform specific tasks.

4. How do you call a method in Java?

To call a method in Java, you must use the method's name followed by parentheses () and any required arguments (if the method has parameters). The method will then be executed, and any returned value can be stored in a variable or used in other parts of the code.

5. What is the difference between a method and a function in Java?

In Java, the terms "method" and "function" are often used interchangeably. However, some developers may make a distinction between the two. A method is typically associated with an object or a class and is used to define behavior or actions that can be performed on that object. On the other hand, a function is a standalone piece of code that can be called from anywhere in the program and does not require an object or a class to be defined.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
Back
Top