# Create an Asterisk Pattern in Java

• 03-17-2010, 01:18 AM
kaurpower
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);
}
}
• 03-17-2010, 02:00 AM
```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; }```