简体   繁体   中英

reciving memory location instead of string output

When I run my program instead of receiving the string from the toString method in my RightTriangle class that I should get normally I instead receive the memory location of the string when I print the object from my driver. How do I fix this problem?

this is the RightTriangleDriver class

import java.util.*;
public class RightTriangleDriver
{
    public static void main ( String [] args )
        {
        Scanner reader = new Scanner ( System.in );

        System.out.println ( "Enter the length of the first leg " );
        double leg1 = reader.nextDouble ();

        System.out.println ( "Enter the length of the second leg " );
        double leg2 = reader.nextDouble ();

        RightTriangle f1 = new RightTriangle ( leg1, leg2 );
        System.out.println ( f1 );
        }
}

this is the RightTriangle class.

public class RightTriangle
{
private double leg1;
private double leg2;
private double hyp;

public RightTriangle (double one , double two)
    {
    leg1 = one;
    leg2 = two;
    hyp = Math.sqrt ( Math.pow ( leg1 , 2 ) + Math.pow (leg2 , 2 ) );   
    }

public double perimiter ()
    {
    double perimiter = 0;
    perimiter = leg1 + leg2 + hyp;
    return perimiter;
    }

public double area ()
    {
    double area = 0;
    area = ( leg1*leg2 )/2;
    return area;
    }

public String toSting ()
    {
     String str;
     str = "Leg 1 is " + leg1 + " units long. Leg 2 is " + leg2 + " units long." + "\n" + "the  Hypotenuse is " + hyp +
             " units long." + "\n" + " The perimiter is " + perimiter () + " units and the area is " + area () +
             " units squared.";
     return str;
    }
}

The method name is toString. You have not written correctly.

Whenever you override method of super class use @Override annotation (toString is method of Object class which every class inherit by default). This will help you to know if you are really overriding the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM