# Thread: Perimeter of a Triangle and Circle Program

1. Member
Join Date
Sep 2011
Posts
21
Rep Power
0

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

Java Code:
```import java.util.*;
public class Lab4
{
public static void main (String[] args)  {
int userOption =0;
// Sample code to test 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.");
}

}

//------------------------------
/** 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
*/

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 Zora; 10-12-2011 at 09:56 PM.

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

3. Member
Join Date
Sep 2011
Posts
21
Rep Power
0

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

4. Member
Join Date
Sep 2011
Posts
21
Rep Power
0

## Re: Perimeter of a Triangle and Circle Program

I don't know what happened to my original post, but here's my updated code.

Java Code:
```import java.util.*;
public class Lab4
{
public static void main (String[] args)  {
int userOption =0;
// Sample code to test 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.");
}

}

//------------------------------
/** 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
*/

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.

Java 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);
}```

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
16

## Re: Perimeter of a Triangle and Circle Program

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.

6. Member
Join Date
Sep 2011
Posts
21
Rep Power
0

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

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
16

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

Java 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.

8. Member
Join Date
Sep 2011
Posts
21
Rep Power
0

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•