Using methods in java programming

  • #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.
 

Similar threads

Replies
7
Views
2K
Replies
1
Views
2K
Replies
7
Views
2K
Replies
12
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
37
Views
4K
Back
Top