Creating Classes........?

I thought I was doing pretty well in Java, until I recieved this weeks assignment. It's an excersise on creating classes. An once again, it's as though I've hit a brick wall. I really have no idea on how to do this assignment. I've already e-mailed my prof for help (and it 2-3 days I might recieve a response, one of the bad parts about online school). I'm not asking anyone to DO my assignment, but I was hoping that some people could maybe talk me through the steps. Here's the exercise.

Classes and objects

Design a class named Triangle. The class must be able to determine the kind of triangle (isosceles, scalene, and equilateral) based on the three lengths. Optionally the class should calculate the area and perimeter of the triangle.

To determine the area, given the three lengths of the triangle you can use the Heron抯 formula.

The semi-perimeterof a triangle is defined as half its perimeter,

the area of a triangle can given by the formula:

Area =

To calculate the square root of the expression s * ( s ?a) * (s 朾) * (s - c), you can use the following Java statement

area = Math.sqrt(s * ( s ?a) * (s 朾) * (s - c) );

Do not use the Scanner class or the System.out within the Triangle class, define appropriate methods to set and get the value of the inner data members instead.

Write a test program for the class Triangle.

AND HERE"S WHAT I GOT SO FAR

CLASS: TRIANGLE

Data members: from what I understand it's supposed to be a constant, like a formula, maybe the area = Math.sqrt(s * ( s ?a) * (s 朾) * (s - c) ); can be applied here.

As you can see, I'm in need of some serious help.

[1696 byte] By [machoextremea] at [2007-10-2 9:12:59]
# 1
Is ypur problem about Java or Math? Do you know how to solve the Math aspect of this exercise? We only can give you ideas about the Java aspects.
Marcelo9a at 2007-7-16 23:20:01 > top of Java-index,Java Essentials,New To Java...
# 2

Your Triangle class needs to have 3 instance fields representing the 3 sides of a triangle. Your constructor should accept 3 double values and set the three instance fields. You probably will want "getters" for your instance fields. Once you have this setup, you can add the methods to calculate the area, perimeter, etc... I think that all the calculations can be made if you have the lengths of the 3 sides. If I'm wrong about that, you might have to add an instance field and modify your constructor. Determining if a triangle is equilateral is easy once you have the lengths (all the lengths are equal). Isosceles triangles have two equal sides and scalene triangles have sides with three different lengths.

Gita_Weinera at 2007-7-16 23:20:01 > top of Java-index,Java Essentials,New To Java...
# 3

Just to add to Gita's excellent and salient advice, you will need the following:

> Instance variables for the thee sides, say a, b, c. Make sure these variables are private. Also make them final. Your Triangle can easily be a read-only (or immutable) object. Coding immutable objects is easier.

> Initialize the instance variables via a constructor.

> Add accessors (aka: getters) to return the values of a, b, and c. This is not strictly required, but it may make your class more useful in the future. If you did, in fact, want your Triangle to be mutable you would also add mutators (aka: setters) for a, b, and c.

> Add your algorithm-based methods. These would be isIsosceles(), isEquilateral() and isScalene(). In addition, you add methods like getPerimeter() and getArea().

Then, you are basically done. You have a (rudimentary but fully-fledged) object!

- Saish

Saisha at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 4

Here's what I did so far. I'm really unsure about the methods, I made the isIsoceles, isScalene, isEquilateral into boolean based variables. and the perimeter, area methods into simple formulas designed to imput an integer of double type. Did I write the methods correctly. (if not, please correct the method(s) and explain what/why).And if so, how do I impliment this class into a program. Again, any help is greatly impreciated.

import java.util.*;

public class triangle

{

private static final double sideA, sideB, sideC;

public void isIsoceles()

{

if ((sideA = sideB) || (sideB = sideC) || (sideC = sideA))

isIsoceles = true

}

public void isScalene()

{

if (sideA != sideB) & (sideB != sideC)

isScalene= true

}

public void isEquilateral()

{

if (sideA = sideB) & (sideB = sideC)

isEquilateral = true

}

public void getArea()

{

getArea=Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC))

}

public void getPerimeter()

{

getPerimeter=(sideA + sideB + sideC)

}

machoextremea at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 5

Why are your methods declared as returning "void"?

Looks like they should be returning "boolean" (true/false) values.

Hint:

public boolean isIsoceles()

{

//instead of trying to assign something to isIsoceles (which is a syntax error), do this:

return (sideA == sideB) || (sideB == sideC) ...;

}

Also, use the == operator, not the assignment (=) operator when comparing values.

warnerjaa at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 6

> import java.util.*;

>

> public class triangle

> {

> private static final double sideA, sideB, sideC;

>

You don't want to make sideA, sideB, and sideC static. Doing that means all Triangle objects you ever create will have the same side lengths, since they will be properties of the class itself, not of the individual instances of the class.

happy_hippoa at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 7

First of all. Thank you guys for the incredibaly quick response. It's nice when you're new to something to get help so quickly. And it's alot easier having someone explain something to you instead of trying to decipher a text . So I modified my code according to your instructions. Again I appreciate all input (you have no idea just how much). So, is my class/methods written correctly this time, and if not what do I need to do to correct it (and please explain how/why this is).

import java.util.*;

public class triangle

{

private double sideA, sideB, sideC;

public boolean isIsoceles()

{

return ((sideA == sideB) || (sideB == sideC) || (sideC == sideA))

}

public boolean isScalene()

{

return (sideA != sideB) & (sideB != sideC)

}

public boolean isEquilateral()

{

return (sideA == sideB) & (sideB == sideC)

}

public void getArea()

{

return Math.sqrt(s*(s-sideA)*(s-sideB)*(s- sideC))

}

public void getPerimeter()

{

return (sideA + sideB + sideC)

}

machoextremea at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 8

> import java.util.*;

You're not using anything from the java.util package, so no need to import it.

> public class triangle

> {

> private double sideA, sideB, sideC;

How are you going to assign values to these members? I see no constructor, no setter methods

> public boolean isIsoceles()

> {

> return ((sideA == sideB) || (sideB == sideC) ||

> (sideC == sideA)); <-- note semicolon

> }

> public boolean isScalene()

> {

> return (sideA != sideB) && (sideB != sideC); <-- note &&, and semicolon. I don't remember exactly what a "scalene" triangle is, but it looks like no two sides can be the same length? If so, this implementation is incomplete. What if sideA and sideC are equal, but sideB isn't? Your implementation would still return true, when it should be false.

> }

Same semicolon and && problem with the rest of these kinds of methods.

> public void getArea()

> {

> return Math.sqrt(s*(s-sideA)*(s-sideB)*(s- sideC))

> }

What is this "s" variable? Also, the area is a numeric value, right? So again why are you returning "void"? Should be "double".

> public void getPerimeter()

> {

> return (sideA + sideB + sideC)

> }

Ditto here

warnerjaa at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 9

> public class triangle

In Java the names of classes are generally written with capital first letters so that you won't confuse them with other things such as variables, so rather write Triangle instead

> public boolean isIsoceles()

Typo: it's "isosceles"

> public boolean isScalene()

> {

> return (sideA != sideB) & (sideB != sideC)

> }

What if sideA == sideC ?

> return Math.sqrt(s*(s-sideA)*(s-sideB)*(s- sideC))

You need to define the variable "s" and calculate a value for it before you use it

jsalonena at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 10

Okay again I revised my code. By the way I know that s=sideA + sideB + sideC/2, but am not sure where to declare it.

public class triangle

{

private double sideA, sideB, sideC;

public Triangle (double s1, double s2, doubles3) {

sideA= s1;

sideB = s2;

sideC = s3;

}

public boolean isIsoceles()

{

if (((sideA == sideB) && (sideA != sideC)) ||

((sideA == sideC) && (sideA != sideB)) ||

((sideB == sideC) && (sideB != sideA)))

return true;

else

return false;

}

public boolean isScalene()

{

if ((sideA != sideB) && (sideA != sideC) && (sideB != sideC))

return true;

else

return false;

}

public boolean isEquilateral()

{

if ((sideA == sideB) & (sideB == sideC))

return true;

else

return false;

}

public void getArea()

{

return Math.sqrt(s*(s-sideA)*(s-sideB)*(s- sideC))

}

public void getPerimeter()

{

return (sideA + sideB + sideC)

}

machoextremea at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 11

If s is only used in the getArea method, you could make it a local variable. If it was being used a lot, you could make it an instance field and initialize it in the constructor. Either way will work.

By the way, your "is" methods would look neater if you eliminate the "if". For example:

return ((sideA != sideB) && (sideA != sideC) && (sideB != sideC));

Gita_Weinera at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 12
You're getting there. But it looks to me like your "isIsosceles" method will return "false" for equilateral triangles. That's incorrect. An isosceles triangle is a triangle with (at least) two equal sides.
DrClapa at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 13

Okay, here's my new code. First question: It was state that my Isosceles method was incorrect because it would prove false for an equilateral triangle. However (maybe I'm seeing it wrong), according to my code it would prove true as long as one side is equal to any other side. Also assuming that my class is correct (finally), how can I implement it into a test program (or any other program for that matter) Do I do something like:

Scanner keyb = new Scanner(System.in);

System.out.println("Enter the value of SideA");

sideA = keyb.nextDouble();

System.out.println("Enter the value of SideB");

sideB = keyb.nextDouble();

System.out.println("Enter the value of SideC");

sideC = keyb.nextDouble();

And then use the entered variables in if statements in order to compare them to the method? Like:

if isIsoceles

System.out.println("You have an isosceles triangle");

Well anyways, here's my revised class/method code. Please give me your input

public class triangle

{

private double sideA, sideB, sideC;

public Triangle (double s1, double s2, doubles3) {

sideA= s1;

sideB = s2;

sideC = s3;

}

public boolean isIsosceles()

{

return (((sideA == sideB) && (sideA != sideC)) ||((sideA == sideC) && (sideA != sideB)) ||((sideB == sideC) && (sideB != sideA)));

}

public boolean isScalene()

{

return ((sideA != sideB) && (sideA != sideC) && (sideB != sideC));

}

public boolean isEquilateral()

{

return ((sideA == sideB) & (sideB == sideC));

}

public void getArea()

{

double s;

s= ((sideA + sideB + sideC)/2);

return Math.sqrt(s*(s-sideA)*(s-sideB)*(s- sideC));

}

public void getPerimeter()

{

return (sideA + sideB + sideC);

}

machoextremea at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 14

> First question: It was state that my Isosceles method was incorrect

> because it would prove false for an equilateral triangle. However

> (maybe I'm seeing it wrong), according to my code it would prove true

> as long as one side is equal to any other side.

Not quite: your code says that to be isosceles one side is equal to

any other and not equal to the third

It's a bit nitpicky, but if all three are equal the triangle will be isosceles

and equilateral.

pbrockway2a at 2007-7-16 23:20:02 > top of Java-index,Java Essentials,New To Java...
# 15

Your test program outline looks OK. But you have to create a

new Triangle() before you can do an if-test.

(That triangle with a capital T, I think someone mentioned before -

keep the classes with an initial capital, everything else starts with

lower case).

There's another little problem that you may want to put aside until

your test program is up and running. To see it place your hands in

front of you with the tips of your thumbs touching to make the base

of a triangle. Now form the other two sides of the triangle with your

index fingers ... Keep your thumbs straight!

pbrockway2a at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 16

Okay, corrected my isoceles method, and here's my new code(again). Also assuming that my class is correct (finally), how can I implement it into a test program (or any other program for that matter) Do I do something like:

Scanner keyb = new Scanner(System.in);

System.out.println("Enter the value of SideA");

sideA = keyb.nextDouble();

System.out.println("Enter the value of SideB");

sideB = keyb.nextDouble();

System.out.println("Enter the value of SideC");

sideC = keyb.nextDouble();

And then use the entered variables in if statements in order to compare them to the method? Like:

if isIsoceles

System.out.println("You have an isosceles triangle");

Well anyways, here's my revised class/method code. Please give me your input

public class triangle

{

private double sideA, sideB, sideC;

public Triangle (double s1, double s2, doubles3) {

sideA= s1;

sideB = s2;

sideC = s3;

}

public boolean isIsosceles()

{

return (((sideA == sideB) && (sideA != sideC)) ||((sideA == sideC) && (sideA != sideB)) ||((sideB == sideC) && (sideB != sideA)))||((sideA == sideC)) & ((sideA==sideB)) & ((sideB == sideC));

}

public boolean isScalene()

{

return ((sideA != sideB) && (sideA != sideC) && (sideB != sideC));

}

public boolean isEquilateral()

{

return ((sideA == sideB) & (sideB == sideC));

}

public void getArea()

{

double s;

s= ((sideA + sideB + sideC)/2);

return Math.sqrt(s*(s-sideA)*(s-sideB)*(s- sideC));

}

public void getPerimeter()

{

return (sideA + sideB + sideC);

}

machoextremea at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 17

so after all the methods would be my test program, and you say I need to create a new Triangle()

Since this in longer part of the class/method , which would normally be started with

public static void main (String args[])

should I follow suit when employing a program using a class. Pretty much what I'm asking is what heading would I use to start out this test program. You said I need a new triangle, so would it be

public static void Triangle()?

machoextremea at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 18

> return (((sideA == sideB) && (sideA != sideC)) ||((sideA == sideC) && > (sideA != sideB)) ||((sideB == sideC) && (sideB != sideA)))||((sideA == > sideC)) & ((sideA==sideB)) & ((sideB == sideC));

Ouch! This makes my head ache! ;)

So, you've got an isosceles triangle. 2 sides equal (maybe three, but

at least 2).

return (sideA == sideB) || (sideB == sideC) || (sideC == sideA);

(By the way note the use of code tags: [code]put your code here[/code].

There is a code button for this above the message pane).

You've still got a small 't' triangle right at the start of the code. Are you

compiling this as you go along? It helps catch typos like this.

pbrockway2a at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 19

Personally I see nothing wrong with putting the test program in as part

of the Triangle class. After all wouldn't you want the Trangle class and

the code that tests it in the same place? However, in your case this

is ruled out by a technicality: you aren't allowed to use Scanner in your

triangle class.

(1)So go ahead and make another class (public class Whatever {)

that will be used to test the Triangle class.

(2)Give it a public static main method as usual.

(3) Put your Scanner stuff in the main method, along with code to

create a new Traingle() and a call to its isIsosceles() method. (or

other tests and output to taste)

Compile, and see what happens. Post back if you can't understand the

compiler error messages (there will be some, there always are).

pbrockway2a at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 20
Personally I see nothing wrong with putting the test program in as partof the Triangle class.There's lots wrong with it.
itchyscratchya at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 21

> (That triangle with a capital T, I think someone

> mentioned before -

> keep the classes with an initial capital, everything

> else starts with

> lower case).

<nitpicking>

what about class constants ?

</nitpicking>

;-)

Torajiroua at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 22
Okay, just got back on. After a couple of corrections, my triangle class compiled perfectly. Now in a bit I'm going to tackle the main method. Wish me luck (I'll probably be posting again later tonight)
machoextremea at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 23

> > return (((sideA == sideB) && (sideA != sideC))

> ||((sideA == sideC) && > (sideA != sideB)) ||((sideB

> == sideC) && (sideB != sideA)))||((sideA == > sideC))

> & ((sideA==sideB)) & ((sideB == sideC));

>

> Ouch! This makes my head ache! ;)

>

> So, you've got an isosceles triangle. 2 sides equal

> (maybe three, but

> at least 2).

> return (sideA == sideB) || (sideB == sideC) ||

> (sideC == sideA);

... which is the way the OP had coded it the first time. I'm not sure why he changed it and thereby goofed it up.

warnerjaa at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 24

Well people told me it was wrong, so I tried to conform. I'll change it back

Well I'm going to ask a question that probably seems really easy, but isn't really clearly explained in my text. When you create a class to use for future programs, you create two files, one with a .java extention and .class. Now supposed I want to use this class in a new program (in my case my class is triangle, and my program is tritest, which is in it's own .java file) how exactly does this program load the class definitions. The book said something about as long as they are in the same directory, but I thought that I would have to at least reference to the .class file in the main method. Can anyone clear this up for me?

machoextremea at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 25

public Triangle (double s1, double s2, doubles3) {

sideA= s1;

sideB = s2;

sideC = s3;

}

I would check the params and throw an exception if they do not make sense like being negative or not satisfying the obvious constraint a + b >= c.

BTW s a "triangle" (1,1,2) allowed? Or (0,0,0)?

BIJ001a at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 26

Both the java and javac commands look for the classes that are

required on the classpath. You specify the classpath when you run

the commands.

For example, suppose you have two .java files/* File: Triangle.java */

public class Triangle {

// rest of the class

}

// End file Triangle.java

and/* File: TriTest.java */

public class TriTest {

public static void main(String args[]) {

// scanner stuff to get some sides

Traingle tri = new Triangle(s1, s2, s3);

if(/* some test*/) {

// some output

}

}

}

/*End file TriTest.java*/

Then, if they were in the same directory, you could issue the following

command from the same directory:

javac -cp . *.java

*.java means "compile everything in this directory"

and -cp . (that's dash cee pee space dot) means "you will find the class

files you want in this directory".

When the compiler hits the Triangle class mentioned in TriTest it will

then know where to find it.

Likewise with the java command:

java -cp . TriTest

When the Triangle is encountered in TriTest, java will have a classpath

that tells it where to find the relevant Triangle.class file.

The classpath switch (-cp) can take other arguments besides dot (the

current directory). This is useful is your classes are in lots of places.

javac -cp .;graphics.jar;bin SomeClass.java

will compile SomeClass looking for any classes it needs in the current

directory (dot), the graphics.jar file and in the bin subdirectory.

You should also note that you don't have to have all your files in the

same directory; and won't do so for any serious work. There is a

mechanism - packages - for dealing with this. Packages are dealt with

in the Tutorial [urlhttp://java.sun.com/docs/books/tutorial/java/index.html]here[/url].

pbrockway2a at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 27

Thanks for the response, it was really helpful in explaing the whole class relationship. Here's another question; Does the classpath go before the main method like follows?

javac -cp . *.java

public class TriTest {

public static void main(String args[]) {

// scanner stuff to get some sides

Traingle tri = new Triangle(s1, s2, s3);

if(/* some test*/) {

// some output

}

}

}

machoextremea at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 28

> Does the classpath go before the main method like follows?

I'm not sure I understand what you're getting at...

javac -cp . *.java is a command. It doesn't go in the

.java file at all. It is typed at the command prompt.

The .java file will contain the "public class TriTest {" bit, the main()

method, and that's about all. Exactly as in your last post: but

remembering that "javac -cp . *.java" is not part of the .java file.

pbrockway2a at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 29

> Well people told me it was wrong, so I tried to

> conform. I'll change it back

I see nothing from any of the repliers in this thread that implied you had your isosceles implementation wrong (the way you had it originally implemented), other than misspelling the word "isosceles" and having it return "void".

warnerjaa at 2007-7-20 20:07:12 > top of Java-index,Java Essentials,New To Java...
# 30

I'm compiling this project using JCreator. I've compiled the project, and it still has errors. The errors consist of the variables in TriTest not being found. Is there something wrong with my class code, or is there some option I need to check in the compiler. With this project I simply created a new project call TriTest. Created a file named TriTest.java. Added a new class (didn't hit any options) and pasted the class code into the new file triangle.java . Any ideas on what to do?

Okay, here's my class

public class triangle

{

private double sideA, sideB, sideC;

public triangle (double s1, double s2, double s3) {

sideA= s1;

sideB = s2;

sideC = s3;

}

public boolean isIsosceles()

{

return ((sideA == sideB) || (sideB == sideC) || (sideC == sideA));

}

public boolean isScalene()

{

return ((sideA != sideB) && (sideA != sideC) && (sideB != sideC));

}

public boolean isEquilateral()

{

return ((sideA == sideB) & (sideB == sideC));

}

public double getArea()

{

double s;

s= ((sideA + sideB + sideC)/2);

return Math.sqrt(s*(s-sideA)*(s-sideB)*(s- sideC));

}

public double getPerimeter()

{

return (sideA + sideB + sideC);

}

}

Here's my main method

import java.util.*;

/* File: TriTest.java */

public class TriTest {

public static void main(String args[]) {

// scanner stuff to get some sides

Scanner keyb = new Scanner(System.in);

System.out.println("Enter the value of SideA");

sideA = keyb.nextDouble();

System.out.println("Enter the value of SideB");

sideB = keyb.nextDouble();

System.out.println("Enter the value of SideC");

sideC = keyb.nextDouble();

triangle tri = new triangle(s1, s2, s3);

if(isIsosceles) {

System.out.println("You have an Isosceles triangle");

if (isScalene)

System.out.println("You have a Scalen triangle");

if (isEquilateral)

System.out.println("You have an Equilateral triangle");

}

}

}

/*End file TriTest.java*/

machoextremea at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 31

In TriTest.java the three if statements should be like

if(tri.isIsosceles())

^^^^^

^- add tri^- add parentheses

You have to say which triangle you are talking about - there is only

one in your program, but there could be lots of them. Also notice

the parentheses at the end of isIsosceles() - these are needed

because isIsosceles() is a method.

I don't have JCreator so, (and for other reasons) you sould say exactly

what errors you are getting. Copy and paste them into your post if

possible, so they are word perfect. I am guessing that the compiler

grumbled about not being able to find variable isIsosceles, but that's

only a guess.

Please rename triangle as Triangle. If you get compiler errors when

you do this, try and understand the errors and fix them. If you get stuck

ask here. But don't leave it as triangle.

Remember [code]put code in code tags to format it[/code]. (Highlight

the code, and click the "code" button). There's more information

about variables/methods etc, and how the if-statement works in the

[urlhttp://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html]Tutorial - Nuts and Bolts[/url]

pbrockway2a at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 32

Okay, I renamed the triangle to Triangle, and corrected the if statements as per your instructions. I'm still getting error messages when I try to compile the project however. They are as follows

cannot find symbol variable sideA

sideB

sideC

s1

s2

s3

How to I initialize these variable from the Triangle.java class?

machoextremea at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 33

> How to I initialize these variable from the

> Triangle.java class?

You don't. sideA, sideB, and sideC are local variables. They are local to the main method, and you need to declare them there. Before you ask how to do that, look at how you declare the local variable keyb.

Once you have sideA, sideB, and sideC, you pass those variables to the TRiangle constructor. There is no need for s1, s2, and s3.

happy_hippoa at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 34

> How to I initialize these variable from the Triangle.java class?

You don't. (Initialisation of the Triangle's data occurs within the

Triangle() constructor and that's fine).

I bet the compiler said something along the lines of "Cannot find

symbol sideA in class TriTest" and gave a reference to a line

number within TriTest.java

In any case, that's where the trouble is. You have a line in TriTest.java

that sayssideA = keyb.nextDouble();

But you haven't declared sideA - that is you haven't said what sort

of variable it is. The compiler has no way of knowing that you mean it

to be a double so it can be used as a side for Triangle. The line should

really be:double sideA = keyb.nextDouble(); //<-- note declared as double

The same with sideB and sideC.

And now those s1, s2, s3 variables (still in TriTest.java, leave

Triangle.java alone)... They haven't been declared, nor have they been

initialised (given a value) - in fact they shouldn't be there at all!

Delete them.

Change the line where you create the Triangle to:Triangle tri = new Triangle(/*?*/, /*?*/, /*?*/);

What variables should go in the place of /*?*/? They're called

the arguments of the Triangle constructor. Figure out what

they should be, put them in and compile.

pbrockway2a at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 35

It Works!!!........ Almost

I have one problem. It doesn't print out anything at all for a Scalene Triangle. For Isosceles and Equilateral triangle it works perfectly, but nothing for Scalene. Here's the line on it in Triangle.java

public boolean isScalene()

{

return ((sideA != sideB) && (sideA != sideC) && (sideB != sideC));

}

And here's my main method, again

import java.util.*;

/* File: TriTest.java */

public class TriTest {

public static void main(String args[]) {

// scanner stuff to get some sides

Scanner keyb = new Scanner(System.in);

System.out.println("Enter the value of SideA");

double sideA = keyb.nextDouble();

System.out.println("Enter the value of SideB");

double sideB = keyb.nextDouble();

System.out.println("Enter the value of SideC");

double sideC = keyb.nextDouble();

Triangle tri = new Triangle(sideA, sideB, sideC);

if(tri.isIsosceles()) {

System.out.println("You have an Isosceles triangle");

if (tri.isScalene())

System.out.println("You have a Scalen triangle");

if (tri.isEquilateral())

System.out.println("You have an Equilateral triangle");

}

}

}

/*End file TriTest.java*/

machoextremea at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 36

> It Works!!!........ Almost

Well, it compiles and that's a huge start. But you make an excellent

observation: compiled != correct.

I take it you input values for a scalene triangle (6, 7, 8 for instance).

So your problem could be precisely stated as: "I input 6, 7, 8 expecting

isScalene() to return true, but it does not".

So we look at isScalene(). It's a nice, simple, one line method. And -

yes - that is exactly what "scalene" means! So, why doesn't it return

true (and hence have the message print)?

Your task is not simply to make the program "work", it is to be able

to give a simple answer to the question in the previous paragraph.

Here is your code from the end of main (but better indented, tabs got in the way):if(tri.isIsosceles()) {

System.out.println("You have an Isosceles triangle");

if (tri.isScalene())

System.out.println("You have a Scalen triangle");

if (tri.isEquilateral())

System.out.println("You have an Equilateral triangle");

}

Now remember that when java strikes an if(condition) { statement and

the condition is false it will skip all the way to the closing }. Figure out

step by step what happens when you enter a 6-7-8 triangle. Do not

try and get fancy with the geometry (eg if it's equilateral, then it's...)

because you want to test each of these three properties.

pbrockway2a at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 37

Well, as you said, if the if statement proves false, then the code will just jump to the end of the program.

my code is :

public boolean isScalene()

{

return ((sideA != sideB) && (sideA != sideC) && (sideB != sideC));

}

or return true if sideAi s not equal to sideB and sideA is not equal sideC and sideB is not equal to sideC

so inputing the 6,7,8 triangle, or three unequal sides (while mainting the integrity of a triangle) it should meet these conditions

6 is not equal to 7, 6 is not equal to 8, and 7 is not equal to 8

I tried looking at this problem many different ways and I still can't figure out what's wrong. It should meet these conditions, so what's the problem?

machoextremea at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 38
I'll give you a slightly less subtle hint than the one pbrockway2 gave you:The problem is in your test program, not in the Triangle class.
happy_hippoa at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 39

Sorry, I didn't notice your post ... I think the "watch list" thing isn't. (either

watching or listing).

> if the if statement proves false, then the code will just jump to the end

> of the program

Yes. But which if-statement is causing the jump? As HH says,

the problem is with TriTest. Think about what happens when a 6-7-8

triangle hits those if statements. Think about it one line of code at a

time.

pbrockway2a at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 40

One technique for tracing the flow of a program is to throw in some

println statements. Like this:System.out.println("About to test isosceles");

if(tri.isIsosceles()) {

System.out.println("You have an Isosceles triangle");

System.out.println("About to test scalene");

if (tri.isScalene())

System.out.println("You have a Scalen triangle");

System.out.println("About to test equilateral");

if (tri.isEquilateral())

System.out.println("You have an Equilateral triangle");

}

System.out.println("I have completed *all* of the tests you asked for");

pbrockway2a at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 41

Okay, I finally figured it out. I feel kinda dumb though, everyone else could see it, but I couldn't for a while. It was kinda like the whole elephant standing in the room. I want to thank everybody who helped me with this. I really learned alot with you guys talking me through this project. Again, thank you.

machoextremea at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 42

> Okay, I finally figured it out. I feel kinda dumb though...

I'm happy you've figured it out. And there's no reason to feel dumb -

the dumb ones are the "do-my-homework" posters.

As with any intellectual task that's new, it's the concepts that are

hard to grasp. Understanding requires practice, but practice can't even

begin without some understanding. Others can help and encourage,

but you have to break through the vicious circle: no pain, no gain.

Quite apart from the specific problem, you have mastered

the "mechanics" of class/variable naming conventions and using the

code button. You've got the -classpath switch working properly where

two classes are involved in a program (and in a way that allows

extension to more complex cases). And you've experienced the value

of checking what the code does line by line. Alas, you will experience

this again. These are significant achievements.

I mentioned the Tutorial earlier with respect to packages. The section

on the language basics is well worth reading.

pbrockway2a at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...
# 43

> I'm compiling this project using JCreator.

String justMyTwoCents = "IMHO, new Java users should stick to emacs or notepad or whatever plaintext editor you prefer until you're comfortable with the mechanics of it all. Otherwise, you'll be learning on somewhat of a crutch. When you're comfortable with the basics of Java (say, when you've gone through the Java tutorial and have understood it), then you're ready for an IDE like Eclipse or JCreator."

- elAmericano

theAmericana at 2007-7-20 20:07:17 > top of Java-index,Java Essentials,New To Java...