# Perimeter of a Triangle and Circle Program

• 10-12-2011, 08:52 PM
Zora
Perimeter of a Triangle and Circle Program
Hey guys,

I have to develop a program to prompt a user to select 1. Triangle 2. Circle or 3.Exit and get them to enter 6 points for the Triangle, and the radius for the Circle. Then calculate the perimeter using methods.

I seem to have many errors on the methods, that I don't know how to fix. Here's my code:

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 point1=0; int point2=0; int point3=0; int point4=0; int point5=0; int point6=0; Scanner scn = 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=scn.nextInt(); System.out.print("Please enter the y-coordinate of point1:"); point2=scn.nextInt(); System.out.print("Please enter the x-coordinate of point2:"); point3=scn.nextInt(); System.out.print("Please enter the y-coordinate of point3:"); point4=scn.nextInt(); System.out.print("Please enter the x-coordinate of point4:"); point5=scn.nextInt(); System.out.print("Please enter the y-coordinate of point4:"); point6=scn.nextInt(); } public static double findDistance (int x1, int x2, int y1, int y2) {       return double (Math.sqrt(x1 - x2) * 2 + (y1 - y2) * 2);     }   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);     }     double perimeter = sideOne + sideTwo + sideThree {     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)); } if (userOption==2){ System.out.print("This program calculates the perimeter of a Circle."); System.out.print("Please enter the radius: "); } }   //------------------------------   /** Method myMenu displays three options to be selected by keyboard,       and it returns user selection as an integer number.              @param  none       @return 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;   }      }```
• 10-12-2011, 09:01 PM
JosAH
Re: Perimeter of a Triangle and Circle Program
Wouldn't it be nice if you'd tell us what those errors are? You don't want us to guess, do you?

kind regards,

Jos
• 10-12-2011, 09:37 PM
Zora
Re: Perimeter of a Triangle and Circle Program
Oh sorry about that. I'm making some changes to the code right now, so I'll update it in a bit.
• 10-12-2011, 10:11 PM
Zora
Re: Perimeter of a Triangle and Circle Program
I don't know what happened to my original post, but here's my updated code.

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 point1=0; int point2=0; int point3=0; int point4=0; int point5=0; int point6=0; Scanner scn = 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=scn.nextInt(); System.out.print("Please enter the y-coordinate of point1:"); point2=scn.nextInt(); System.out.print("Please enter the x-coordinate of point2:"); point3=scn.nextInt(); System.out.print("Please enter the y-coordinate of point3:"); point4=scn.nextInt(); System.out.print("Please enter the x-coordinate of point4:"); point5=scn.nextInt(); System.out.print("Please enter the y-coordinate of point4:"); point6=scn.nextInt(); } public static double findDistance (int x1, int x2, int y1, int y2) {       return double (Math.sqrt(x1 - x2) * 2 + (y1 - y2) * 2);     }   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);     }     double perimeter = sideOne + sideTwo + sideThree {     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)); } if (userOption==2){ System.out.print("This program calculates the perimeter of a Circle."); System.out.print("Please enter the radius: "); } }   //------------------------------   /** Method myMenu displays three options to be selected by keyboard,       and it returns user selection as an integer number.              @param  none       @return 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;   }      }```
The errors occur mostly in the methods that I've used. After a user inputs the points, I have to calculate the distance between 3 lines of the triangle using a formula, and then add them together to find the perimeter.

Code:

```public static double findDistance (int x1, int x2, int y1, int y2) {       return double (Math.sqrt(x1 - x2) * 2 + (y1 - y2) * 2);     }   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);     }```
• 10-13-2011, 02:42 AM
pbrockway2
Re: Perimeter of a Triangle and Circle Program
Quote:

The errors occur mostly in the methods that I've used.
Jos' point still stands: we don't know what these errors are and you do. Are these compiler messages? Or runtime errors?

Either way, post the output exactly as it appears. And indicate which lines of your code the messages refer to.
• 10-13-2011, 03:58 AM
Zora
Re: Perimeter of a Triangle and Circle Program
Nevermind. I fixed all my errors later today and it's working as expected, but I have a question. How would you go about reducing decimal places? I don't know how to use Math.round properly.
• 10-13-2011, 04:29 AM
pbrockway2
Re: Perimeter of a Triangle and Circle Program
It depends on what you mean by "reducing decimal places".

The most common thing people want to do is not to round a quantity (or otherwise change it into something that will, of necessity, be less accurate). Rather they want to change the way the quantity is displayed: ie to format a string representing the quantity. For that you can use the String format() method. Print streams (like System.out) etc also allow such formatting:

Code:

```double val = 3.14159; String str = String.format("%.3f", val); System.out.println("val is " + str); System.out.printf("You owe 10pi dollars: \$%.2fc", 10 * val);```
There is also a DecimalFormat class that is designed to do a similar quantity -> readable string operation.
• 10-13-2011, 04:43 AM
Zora
Re: Perimeter of a Triangle and Circle Program
For example you have a large number 8.9320840203258124 and you want to reduce it to 4 decimal places to 8.9320 without changing the value. Hmm I'll take a look at the links you provided.