Results 1 to 2 of 2
  1. #1
    kaurpower is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default Create an Asterisk Pattern in Java

    Hi, I'm very new to Java and I have an assignment where the user needs to input 3 sets of x,y coordinates...create a 2d graph of the triangle with asterisks to represent the corners...determine the distance between the points...and finally the perimeter. I have been able to do all of that, but I'm now stuck on how to actually create the asterisk pattern. I have to call a method to actually draw the triangle...and i completely fail at that point. I can only use Loops, Selection Statements, Methods - nothing beyond that point. I believe I have to use a Nested Loop...but i'm not sure. I have put the code below of what i have SO far...i would appreciate the help!

    Also, on a side note - is there a more efficient way to do the calculation of the distance in Java...rather than me having to repeat the equation each time?

    import java.util.Scanner;

    public class DistancePerimeterAndDrawingOfTriangle {
    //Main method
    public static void main(String[] args) {

    //Create a scanner to input the x and y coordinates for the triangle
    Scanner input = new Scanner(System.in);

    //Prompt the user to enter in the x1,y1 coordinate
    System.out.print("Enter first positive x coordinate, between 0 & 10 [x1]: ");
    double x1 = input.nextDouble();
    System.out.print("Enter first positive y coordinate, between 0 & 10 [y1]: ");
    double y1 = input.nextDouble();

    //Prompt the user to enter in the x2,y2 coordinates
    System.out.print("Enter second positive x coordinate, between 0 & 10 [x2]: ");
    double x2 = input.nextDouble();
    System.out.print("Enter second positive y coordinate, between 0 & 10 [y2]: ");
    double y2 = input.nextDouble();

    //Prompt the user to enter in the final x3,y3 coordinates
    System.out.print("Enter final positive x coordinate, between 0 & 10 [x3]: ");
    double x3 = input.nextDouble();
    System.out.print("Enter final positive y coordinate, between 0 & 10 [y3]: ");
    double y3 = input.nextDouble();

    //Display the coordinates
    System.out.println("\nYour coordinates are (" + x1 + "," + y1 + "); (" + x2 + "," + y2 + "); (" + x3 + "," + y3 + ").");

    //Compute the distance between each point and assign letters: a, b, c
    double distanceA = Math.sqrt(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
    //Display distance
    System.out.print("\nThe distance between (" + x1 + "," + y1 +") and (" + x2 + "," + y2 + ") is "+ distanceA);

    double distanceB = Math.sqrt(((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)));
    System.out.print("\n\nThe distance between (" + x2 + "," + y2 +") and (" + x3 + "," + y3 + ") is " + distanceB);

    double distanceC = Math.sqrt(((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)));
    System.out.print("\n\nThe distance between (" + x3 + "," + y3 +") and (" + x1 + "," + y1 + ") is " + distanceC);

    //Now compute the perimeter of the triange
    double perimeter = Math.round(distanceA + distanceB + distanceC);
    System.out.print("\n\nThe perimeter of the triangle is " + perimeter);
    }
    }

  2. #2
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    Hiya, it seems you would be better off creating a seperate class for points and making methods that work on those. However for now, if you want to make the distance calculation more efficient you could make it a seperate method and call it 3 times passing in the point combinations.

    Java Code:
    public static double findDistance(double p1x, double p1y, double p2x, double p2y)
    {
        double distance = Math.sqrt(((p1x-p2x)*(p1x-p2x)+(p1y-p2y)*(p1y-p2y)));
        return distance;
    }
    Place this seperate to your main method and you can remove the equations and simply put 'findDistance(x1, y1, x2, y2)' in place of 'DistanceA/B/C'.

    I was looking into your graph problem but in my tiredness I got it wrong. It is possible but quite complex. I think you would need to sort your y co-ordinates into order, then loop to add " " to a String x times for your highest y value, then loop to add as many "\n" as the difference between your highest and second highest y value, before adding the corresponding x number of " " again, then once more "\n" for the distance between your smallest and middle y and again " " for the corresponding x distance. If that makes sense? lol.

Similar Threads

  1. [SOLVED] Help Me with Printing Asterisk Triangular
    By AFstudent in forum New To Java
    Replies: 20
    Last Post: 11-12-2008, 03:59 PM
  2. asterisk java
    By abhi in forum New To Java
    Replies: 4
    Last Post: 01-22-2008, 07:09 AM
  3. Asterisk-Java 0.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-02-2007, 08:30 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
  •