Kyle H
Kyle H

Reputation: 101

Writing a program to find Length C of a triangle

I am trying to write a program to find side C of a right triangle, given the length of sides A and B. Here is what I have so far:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Math
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // Scanner scan = new scanner(system.in);
        int a, b, c;
        int length c; c = sqrt(a^(2)+b^(2))

        System.out.println("Length of side a?");
        a = scan.nextdouble();

        System.out.println("Length of side b?");
        b = scan.nextdouble();

        c = math.sqrt(a^(2)+b^(2));

        System.out.print("The length of side C is");
        System.out.println(c+ "units.");

        scan.close();

    }
}

Eclipse tells me I have an argument error but I don't know what to suspect. Any advice on fixing this? I know it's simple, and I feel like the solution is right in front of me.

Upvotes: 0

Views: 1378

Answers (5)

Xitcod13
Xitcod13

Reputation: 6079

I dont know how you managed to make so many mistakes..

this is a working code

import java.util.Scanner;


public class Lame {

    /**
     * @param args
     */
    public static void main(String[] args) {
        double a, b, c;

        Scanner scan = new Scanner(System.in);

        System.out.println("Length of side a?");
        a = scan.nextDouble();

        System.out.println("Length of side b?");
        b = scan.nextDouble();

        c = Math.sqrt((a*a + b*b));

        System.out.print("The length of side C is");
        System.out.println(c+ "units.");
    }

}

Some hints. Pay attention to your Capitalization. Java is case sensitive. scanner and Scanner are not the same thing in java! (Classes are capitalized while functions are not)

Java does not automatically convert types you cannot set a= scan.nextDouble(): because it will return a double not an int. (if you want to cast a double to int you must do it yourself)

there is no such thing as a power sign ^ in Java. You need to either use a pow() function or just do a variable times itself like a*a.

Change your class name from Math.

Try google things and do a lot of exercises from the book just coping the code. After a while you will start understanding things.

Upvotes: 0

jpw
jpw

Reputation: 44881

There were a lot of errors... this is the working version:

import java.util.*;
import java.lang.*;
import java.io.*;

class Math
{
    public static void main (String[] args) throws java.lang.Exception {
        Scanner scan = new Scanner(System.in); // this had errors
        double a, b, c; // doubles are better for division, 
                        // unless you want imprecise results

        System.out.println("Length of side a?");
        a = scan.nextDouble(); // note the case in the method name, this was wrong

        System.out.println("Length of side b?");
        b = scan.nextDouble(); // same here

        c = java.lang.Math.sqrt(java.lang.Math.pow(a,2) + java.lang.Math.pow(b,2));

        System.out.print("The length of side C is ");
        System.out.println(c + "units.");

        scan.close();
    }
}

Since you named your class Math, you have to use the full java.lang.Math prefix for sqrt and pow.

Upvotes: 1

Alaeddine
Alaeddine

Reputation: 1687

remove int length c; first length is not used and c is already declared.

Upvotes: 0

18107
18107

Reputation: 734

The ^ symbol is the same as XOR. It does not mean "to the power of". You will need to use

Math.pow(a, 2) + Math.pow(b, 2);

or

a*a + b*b

Upvotes: 1

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

change this int length c; c = sqrt(a^(2)+b^(2)) to int length;

Upvotes: 1

Related Questions