Results 1 to 8 of 8
  1. #1
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default 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 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;
      }      
    }
    Last edited by Zora; 10-12-2011 at 08:56 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default 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. #4
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

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

    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. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #6
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default 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. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #8
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

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

Similar Threads

  1. Trying to write a circle program
    By paulmmj in forum New To Java
    Replies: 7
    Last Post: 10-12-2010, 06:06 PM
  2. Replies: 3
    Last Post: 09-26-2010, 04:21 AM
  3. triangle program
    By computerbum in forum New To Java
    Replies: 8
    Last Post: 03-02-2010, 04:55 AM
  4. java program help. find the radius of a circle
    By megironi in forum New To Java
    Replies: 1
    Last Post: 03-27-2009, 07:09 AM

Posting Permissions

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