Results 1 to 2 of 2
 03172010, 01:18 AM #1Member
 Join Date
 Mar 2010
 Posts
 1
 Rep Power
 0
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(((x2x1)*(x2x1)+(y2y1)*(y2y1)));
//Display distance
System.out.print("\nThe distance between (" + x1 + "," + y1 +") and (" + x2 + "," + y2 + ") is "+ distanceA);
double distanceB = Math.sqrt(((x3x2)*(x3x2)+(y3y2)*(y3y2)));
System.out.print("\n\nThe distance between (" + x2 + "," + y2 +") and (" + x3 + "," + y3 + ") is " + distanceB);
double distanceC = Math.sqrt(((x3x1)*(x3x1)+(y3y1)*(y3y1)));
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);
}
}
 03172010, 02:00 AM #2Member
 Join Date
 Mar 2010
 Posts
 22
 Rep Power
 0
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(((p1xp2x)*(p1xp2x)+(p1yp2y)*(p1yp2y))); return distance; }
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 coordinates 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

[SOLVED] Help Me with Printing Asterisk Triangular
By AFstudent in forum New To JavaReplies: 20Last Post: 11122008, 04:59 PM 
asterisk java
By abhi in forum New To JavaReplies: 4Last Post: 01222008, 08:09 AM 
AsteriskJava 0.3
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07022007, 08:30 AM
Bookmarks