- #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: