Jump to content
  • Advertisement
Sign in to follow this  

[java] Help with methods and classes

This topic is 3621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 !

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!