Results 1 to 8 of 8
 10122011, 07:52 PM #1Member
 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 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 xcoordinate of point1:"); point1=scn.nextInt(); System.out.print("Please enter the ycoordinate of point1:"); point2=scn.nextInt(); System.out.print("Please enter the xcoordinate of point2:"); point3=scn.nextInt(); System.out.print("Please enter the ycoordinate of point3:"); point4=scn.nextInt(); System.out.print("Please enter the xcoordinate of point4:"); point5=scn.nextInt(); System.out.print("Please enter the ycoordinate 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; } }
Last edited by Zora; 10122011 at 08:56 PM.
 10122011, 08:01 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 10122011, 08:37 PM #3Member
 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.
 10122011, 09:11 PM #4Member
 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 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 xcoordinate of point1:"); point1=scn.nextInt(); System.out.print("Please enter the ycoordinate of point1:"); point2=scn.nextInt(); System.out.print("Please enter the xcoordinate of point2:"); point3=scn.nextInt(); System.out.print("Please enter the ycoordinate of point3:"); point4=scn.nextInt(); System.out.print("Please enter the xcoordinate of point4:"); point5=scn.nextInt(); System.out.print("Please enter the ycoordinate 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; } }
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); }
 10132011, 01:42 AM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 15
Re: Perimeter of a Triangle and Circle Program
The errors occur mostly in the methods that I've used.
Either way, post the output exactly as it appears. And indicate which lines of your code the messages refer to.
 10132011, 02:58 AM #6Member
 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.
 10132011, 03:29 AM #7Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 15
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);
 10132011, 03:43 AM #8Member
 Join Date
 Sep 2011
 Posts
 21
 Rep Power
 0
Similar Threads

Trying to write a circle program
By paulmmj in forum New To JavaReplies: 7Last Post: 10122010, 06:06 PM 
Help with simple area/perimeter program for school
By icarus in forum New To JavaReplies: 3Last Post: 09262010, 04:21 AM 
triangle program
By computerbum in forum New To JavaReplies: 8Last Post: 03022010, 05:55 AM 
java program help. find the radius of a circle
By megironi in forum New To JavaReplies: 1Last Post: 03272009, 08:09 AM
Bookmarks