[java] Help with methods and classes

Started by
2 comments, last by cordel 15 years, 3 months ago
Hello everyone , I have 2 files , one is Triangle.java and the other is its Main file. The triangle.java contains methods (functions) , the code

public class Triangle {
	 
	Point p1,p2,p3;

	public double perimeter()     {           //    Computes the triangle perimeter
		                                      //    distance between each 2 dots
		
		double d1=Math.sqrt(Math.pow(this.p1.xval-this.p2.xval,2)+Math.pow(this.p1.yval-this.p2.xval,2));
		double d2=Math.sqrt(Math.pow(this.p1.xval-this.p3.xval,2)+Math.pow(this.p1.yval-this.p3.yval,2));
		double d3=Math.sqrt(Math.pow(this.p3.xval-this.p2.xval,2)+Math.pow(this.p3.yval-this.p2.yval,2));
		return (d1+d2+d3);
	}
}



Now I'm in the Main file , and I want to calculate the triangle's perimeter with a given point ,but I don't know how to write it :

public class Main{
Point p1=new Point(1.0,3.5),p2=new Point (2.0,7.3),p3=new Point(1.4,3.9);	
    /**
        System.out.println("The Triangle's perimeter with the points " 
        	+ "(" + p1.getX() + "," + p1.getY() + ")" 
            + "(" + p2.getX() + "," + p2.getY() + ")"
            + "(" + p3.getX() + "," + p3.getY() + ")" + " is:" );
..........



since the method "perimeter" doesn't has any given parameters (the lecturer's instructions).any hints ? thanks in advance !
Advertisement
cordel,

First of all, keep in mind that whenever anyone is learning how to program, it doesn't do any good to just get the answer to your problem from someone else. It is really hard to learn this stuff on your own without any help at all, but part of programming is learning to figure this stuff out on your own. Read the documentation and tutorials, write code examples and run them, step through your code with the debugger, and don't give up.

Everyone who knows how to program has gone through this.

Having said that, when you start learning with a language with Java, there are so many new things that it can seem impossible.

To point you in the right direction without giving it away, try using a constructor in the Triangle class.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Well I do agree with the person right above me. You need to create a constructor so that you can actually make the triangle/rectangle.
As well you need some things called getter methods which simply said get your values.
Well people ,you were right .All that was needed was one sipmle line :

Triangle t=new Triangle (p1,p2,p3); in order to use the constructor...

and : double per=t.perimeter();

Thanks for the encouragement :)

[Edited by - cordel on January 10, 2009 6:33:51 AM]

This topic is closed to new replies.

Advertisement