Could not use non-static method from a static location- Compiler Error

Hello everyone, I am new to this site and I just needed a little help with my code to finish it. Im not sure why I get the error stated above, but here is my code(s) from both classes.

public class Triangle

{

private int base;

private int height;

private int numOfTriangles = 0;

double calc;

public void zeroReturn()

{

}

public void twoReturn(String base, String height)

{

System.out.println("Triangle base: " + base + " inches.");

System.out.println("Triangle height: " + height + " inches.");

}

public void areaReturn()

{

calc = base*height/2;

System.out.println("\nThe area of this triangle is: "+ calc);

numOfTriangles++;

System.out.println("\nThere are a total of " + numOfTriangles + " triangles entered.");

}

}

And the second prgram class:

public class TriangleProgram

{

public static void main(String[] args)

{

Triangle firstTriangle = new Triangle();

TrianglesecondTriangle = new Triangle();

Triangle thirdTriangle = new Triangle();

System.out.println("Triangle One\n");

firstTriangle.twoReturn("23","32");

System.out.println("\n");

System.out.println("Triangle Two\n");

secondTriangle.twoReturn("12","21");

System.out.println("\n");

System.out.println("Triangle Three\n");

thirdTriangle.twoReturn("34", "43");

System.out.println("\n");

Triangle.areaReturn();

}

}

Basically what I am trying to do is Write an object file named Triangle.java to use in conjunction with a program file named TriangleProgram.java. The instance variables base and height should have to be private modifiers and be non static. The Triangle class should also contain an instance variable named numOfTriangles to count the number of Triangleobjects instantiated. The Triangleclass should have two constructors, a constructor to handle zero parameter triangles and a constructor with two parameters to handle triangle objects that receive base and height arguments. The Triangle class should use as many methods as needed to calculate and output the dimensions of each triangle object, the area of each triangle object, the sum of the area of all triangle objects and the number of triangle objects instantiated.

Any help or guidance would be greatly appreciated. Thank you.

Message was edited by:

beatjunkie

[2477 byte] By [beatjunkiea] at [2007-11-27 10:43:46]
# 1

Methods are applied to objects:

//wrong:

Triangle.areaReturn();

//right:

firstTriangle.areaReturn();

Did you notice that you were calling twoReturn correctly?

You need to rethink your Triangle class, as you never set the base and height

fields, but you'll see that when you call areaReturn :-)

BigDaddyLoveHandlesa at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 2

Didn't I set the base and height in the TrianglProgram class?

System.out.println("Triangle One\n");

firstTriangle.two("23","32");

?

Or am I missing something here?

beatjunkiea at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 3

By the way I love the name, its halarious!

beatjunkiea at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 4

> Didn't I set the base and height in the

> TrianglProgram class?

>

> System.out.println("Triangle One\n");

> firstTriangle.two("23","32");

> ?

>

> Or am I missing something here?

Yes, you are. Consider the code:

public void twoReturn(String base, String height)

{

System.out.println("Triangle base: " + base + " inches.");

System.out.println("Triangle height: " + height + " inches.");

}

That is just displaying the given arguments. It does not change the fields in Triangle. This does:

public void setBase(int b) {

base = b;

}

BigDaddyLoveHandlesa at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 5

> By the way I love the name, its halarious!

I am morbidly obese.

Well, I guess that's mildly amusing....

BigDaddyLoveHandlesa at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 6

so I would have to use accessor or mutator methods? or both?

beatjunkiea at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 7

errrr mutator meds use set so Im assuming the latter. And are your seriously obese?

beatjunkiea at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 8

> And are your seriously obese?

Isn't everyone who develops software?

BigDaddyLoveHandlesa at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 9

> > And are your seriously obese?

>

> Isn't everyone who develops software?

They must also have some stubble on (one of) their chin(s).

Djaunla at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 10

not that I knew, and I am new to this so I wouldn't really know anyway. That aside If I offended you I apologize dearly, and if it counts any, your comment made me depressed.

beatjunkiea at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 11

> > > And are your seriously obese?

> >

> > Isn't everyone who develops software?

>

> They must also have some stubble on (one of) their

> chin(s).

Check.

BigDaddyLoveHandlesa at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 12

all that aside I assume I will be using accessor method in my TriangleProgram class correct?

beatjunkiea at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 13

> not that I knew, and I am new to this so I wouldn't

> really know anyway. That aside If I offended you I

> apologize dearly, and if it counts any, your comment

> made me depressed.

Cheer up, we're a happy, joking, bunch here. I'm

actually dieting and not that fat any more (BMI = 25.0),

but my user name spurs me on to lose a little more weight.

I also use the username ParvatiDevi, but let's not over analyze that one!

BigDaddyLoveHandlesa at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 14

> all that aside I assume I will be using accessor method in my TriangleProgram class correct?

In a simple class like Triangle, getters and setters are useful.

Try rewriting your code to use them.

BigDaddyLoveHandlesa at 2007-7-28 20:02:21 > top of Java-index,Java Essentials,New To Java...
# 15

Also, a method that computes and returns the area would be more useful than one that just writes it to System.out:

public int getArea() {

return base * height / 2;

}

With this, you code can go on and use the area in further calculations, or write it out to System.out.

BigDaddyLoveHandlesa at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 16

Hopefully I finally finish this code, once I do the Dukes are yours. (not sure if you care, but I do appreciate the help immensely)

On another note, is my code way off for achieving the objective written in the first post at the bottom?

beatjunkiea at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 17

> On another note, is my code way off for achieving the

> objective written in the first post at the bottom?

* Basically what I am trying to do is Write an object file named Triangle.java to use in conjunction with a program file named TriangleProgram.java.

--done

*The instance variables base and height should have to be private modifiers and be non static.

--done. Are you using them, now, though?

*The Triangle class should also contain an instance variable named numOfTriangles to count the number of Triangleobjects instantiated.

--That doesn't make much sense. Shouldn't that be static variable,

not an instance variable?

*The Triangleclass should have two constructors, a constructor to handle zero parameter triangles and a constructor with two parameters to handle triangle objects that receive base and height arguments.

-- You didn't write any constructors!

* The Triangle class should use as many methods as needed to calculate and output the dimensions of each triangle object, the area of each triangle object, the sum of the area of all triangle objects and the number of triangle objects instantiated.

-- Easy enough to say! I would take this as a vague statement; for example, as I wrote before, let the client code in TriangleProgram output to System.out.

And what about "sum of the area of all triangle objects" -- is this summing the job of the client or the Triangle class?

BigDaddyLoveHandlesa at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 18

>>And what about "sum of the area of all triangle objects" -- is this summing the job of the client or the Triangle class?

Basically, it counts the number of triangles outputted, I guess for use If I was to allow a user to enter values for base and height and numbers of triangles.

As far as the constructors, I thought creating a copy of the object class was a constructor, at least that is what I was taught (about the only thing I was taught). ie.

Triangle firstTriangle = new Triangle ?

beatjunkiea at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 19

> As far as the constructors, I thought creating a copy

> of the object class was a constructor, at least that

> is what I was taught (about the only thing I was

> taught). ie.

>

> Triangle firstTriangle = new Triangle ?

Yes, if you don't write any constructors, a no-argument one is automatically

supplied. That is why this line is not a syntax error:

Triangle firstTriangle = new Triangle();

But that could be described by "Triangle has one constructor", but "I *wrote* one

constructor for Triangle" is a stretch, and your requirements state:

"The Triangleclass should have two constructors", so get cracking!

BigDaddyLoveHandlesa at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 20

ugh, can you please explain to me how an assessor method is supposed to access a mutator method in another class, I am returning alot of errors.

beatjunkiea at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 21

Accessor accesses a mutator? I don;t understand. Post a little code, please.

BigDaddyLoveHandlesa at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 22

public class TriangleAPW2

{

private int base;

private int height;

private int numOfTriangles = 3;

double area;

double num1 = .5;

public void zero()

{

}

public void two(String base, String height)

{

System.out.println("Triangle base: " + base + " inches.");

System.out.println("Triangle height: " + height + " inches.");

}

public void area()

{

area = (num1*base*height);

System.out.println("\nThe area of this triangle is: "+ area);

System.out.println("\nThere are a total of " + numOfTriangles + " triangles entered.");

}

public void setBase(int b, int h)

{

base = b;

height = h;

}

public int getB()

{

return base;

}

public int getH()

{

return height;

}

}

**Compiles with no errors**

When I compile the other class to run this class, I still cannot get the area of each triangle to display.

Message was edited by:

beatjunkie

beatjunkiea at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 23

> When I compile the other class to run this class, I still cannot get the area of each triangle to display.

That code has no main method, so you can't run it

Are zero and two supposed to be constructors? Recall that constructors must be named after the class! And in any case, "two" is just displaying arguments, not setting fields.

BigDaddyLoveHandlesa at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 24

import java.util.*;

public class TriangleProgramAPW2

{

public static void main(String[] args)

{

TriangleAPW2 firstTriangle = new TriangleAPW2();

TriangleAPW2 secondTriangle = new TriangleAPW2();

TriangleAPW2 thirdTriangle = new TriangleAPW2();

System.out.println("Triangle One\n");

firstTriangle.two("23","32");

System.out.println("\n");

System.out.println("Triangle Two\n");

secondTriangle.two("12","21");

System.out.println("\n");

System.out.println("Triangle Three\n");

thirdTriangle.two("34", "43");

System.out.println("\n");

firstTriangle.getB();

firstTriangle.getH();

firstTriangle.area();

}

}

beatjunkiea at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 25

The above is the main method, the class prior to that was the object class. zero and two are supposed to be constructors yes, but they look like methods (im confused), how would I make the two work with eachother by using constructors instead of methods in the TriangleAPW class?

beatjunkiea at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 26

> firstTriangle.getB();

This method call returns the base value. And your code ignores it. Woops!

int base = firstTriangle.getB();

System.out.println("traingle 1 has a base of " + base);

BigDaddyLoveHandlesa at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 27

public class TriangleAPW2

{

private int base;

private int height;

private int numOfTriangles = 3;

double area;

double num1 = .5;

public void TriangleAPW2()

{

}

public void TriangleAPW2(String base, String height)

{

System.out.println("Triangle base: " + base + " inches.");

System.out.println("Triangle height: " + height + " inches.");

}

public void area()

{

area = (num1*base*height);

System.out.println("\nThe area of this triangle is: "+ area);

System.out.println("\nThere are a total of " + numOfTriangles + " triangles entered.");

}

public void setBase(int b, int h)

{

base = b;

height = h;

}

public int getB()

{

return base;

}

public int getH()

{

return height;

}

}

They are constructors now? And would I put the get and set methods within the area method to display the area odf all triangles?

beatjunkiea at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 28

Constructor example:

public class Person {

private String name;

private int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

}

}

Recall that the constructor must be named after the class.

BigDaddyLoveHandlesa at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 29

> Accessor accesses a mutator? I don;t understand. Post

> a little code, please.

getXXX() calls setXXX()

filestreama at 2007-7-28 20:02:26 > top of Java-index,Java Essentials,New To Java...
# 30

>>int base = firstTriangle.getB();

System.out.println("traingle 1 has a base of " + base);

This returns a base of zero, which means my values for the int base are being overlooked.

beatjunkiea at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 31

> This returns a base of zero, which means my values for the int base are being overlooked.

Your method two does nothing and you didn't call method setB, which is misnamed, by the way, since it sets both base and height!

BigDaddyLoveHandlesa at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 32

I am going to redo both codes, I believe I am going the wrong way with trying to do this.

beatjunkiea at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 33

I dont mean to frustrate you but my instructor doesn't help the class any and it is very difficult to teach yourself Java when you dont get help from somewhere.

I really dont know what you mean by saying that the method two doesn't do anything, and when you state that I didn't set the base, I thought I did this in the code below no?

beatjunkiea at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 34

public class TriangleAPW2

{

private int base;

private int height;

private int numOfTriangles = 3;

double area;

double num1 = .5;

public void TriangleAPW2()

{

}

public void TriangleAPW2(String base, String height)

{

System.out.println("Triangle base: " + base + " inches.");

System.out.println("Triangle height: " + height + " inches.");

}

public void area()

{

area = (num1*base*height);

System.out.println("\nThe area of this triangle is: "+ area);

System.out.println("\nThere are a total of " + numOfTriangles + " triangles entered.");

}

public void setBase(int b)

{

base = b;

}

public void setHeight(int h)

{

height = h;

}

public int getB()

{

return base;

}

public int getH()

{

return height;

}

public int setB()

{

return base;

}

public int setH()

{

return height;

}

}

beatjunkiea at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 35

> I dont mean to frustrate you but my instructor

> doesn't help the class any and it is very difficult

> to teach yourself Java when you dont get help from

> somewhere.

What textbook are you using? It must have a chapter on constructors.

>

> I really dont know what you mean by saying that the

> method two doesn't do anything,

Here's the code:

public void two(String base, String height) {

System.out.println("Triangle base: " + base + " inches.");

System.out.println("Triangle height: " + height + " inches.");

}

I overstated the situation. This method does do something -- it displays

the given arguments, but it doesn't assign values to Triangle's fields.

Note that the parameter base is not the field base. That is why one has to write:

public void setHeight(int height) {

this.height = height; //this is need!

}

> and when you state

> that I didn't set the base, I thought I did this in

> the code below no?

No, in the code you posted, the only method that set Triangle fields was "setB", and you didn't invoke it.

BigDaddyLoveHandlesa at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 36

>>public void two(String base, String height) {

System.out.println("Triangle base: " + base + " inches.");

System.out.println("Triangle height: " + height + " inches.");

}

public void setHeight(int height) {

this.height = height; //this is need!

}

> See, this is where you lose me, is the "set" above in the Triangleclass or TriangleProgram ? And if so, where are all these values?

public void setBase(int b)

{

base = b;

}

public void setHeight(int h)

{

height = h;

}

public int getB()

{

return base;

}

public int getH()

{

return height;

}

public int setB()

{

return base;

}

public int setH()

{

return height;

}

beatjunkiea at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 37

> See, this is where you lose me, is the "set" above in the Triangleclass or TriangleProgram ? And if so, where are all these values?

In the Triangle class.

BigDaddyLoveHandlesa at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 38

>public void TriangleAPW2() {

>}

>

>public void TriangleAPW2(String base, String height) {

>System.out.println("Triangle base: " + base + " inches.");

>System.out.println("Triangle height: " + height + " inches.");

>}

There's a common mistake in syntax here: constructors don't have a return type. The compiler will misinterpret this code as methods instead of constructors.

Solution: remove the "voids"

BigDaddyLoveHandlesa at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 39

Maybe you need to step back. Here's a simple example, to demonstrate Java syntax:

public class Box {

private int width;

private int height;

private int depth;

public Box() {

//0x0x0

}

public Box(int width, int height, int depth) {

this.width = width;

this.height = height;

this.depth = depth;

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

this.width = width;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

public int getDepth() {

return depth;

}

public void setDepth(int depth) {

this.depth = depth;

}

public int getVolume() {

return width * height * depth;

//or

//return getWidth() * getHeight() * getDepth();

}

}

BigDaddyLoveHandlesa at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 40

when I take out the voids this is what I get.

**invalid method declaration; return type required**

even when i did this

public TriangleAPW3(int base, int height)

{

this.base = Base;

this.height = Height;

System.out.println("Base: " + base + " inches");

System.out.println("Height: " + height + " inches");

}

beatjunkiea at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 41

> when I take out the voids this is what I get.

>

> **invalid method declaration; return type required**

>

> even when i did this

>

> public TriangleAPW3(int base, int height)

> {

>

> this.base = Base;

> this.height = Height;

> System.out.println("Base: " + base + " inches");

> System.out.println("Height: " + height + " inches");

>

Be sure the name of the case matches the name used for the constructor.

> this.base = Base;

> this.height = Height;

Be careful. Java is case-sensitive.

BigDaddyLoveHandlesa at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 42

and do I add a main method before the constructors or no?

public static void main(String[] args)

beatjunkiea at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 43

> and do I add a main method before the constructors or

> no?

>

> public static void main(String[] args)

It doesn't matter what order you list methods and constructors in a class.

BigDaddyLoveHandlesa at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 44

this is torture.

beatjunkiea at 2007-7-28 20:02:31 > top of Java-index,Java Essentials,New To Java...
# 45

> this is torture.

Tell me about it!

Please copy & paste your current code and the error messages you are receiving.

tsitha at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 46

sorry for the late reply, I had to put my kids to bed.

here is the code ( I cleared all errors)

Now where?

public class TriangleAPW3

{

private int base;

private int height;

private int area;

private int numOfTriangles = 3;

double num1 = .5;

public static void main(String[] args)

{

}

public TriangleAPW3()

{

//No arguments here.

}

public TriangleAPW3(int base, int height)

{

System.out.println("Base: " + base + " inches");

System.out.println("Height: " + height + " inches");

this.base = base;

this.height = height;

}

public int getBase()

{

return base;

}

public void setBase(int base) {

this.base = base;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

public int getArea() {

return base * height / 2;

}

}

beatjunkiea at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 47

what about the complete error messages?

Also, when posting code, please use code tags. Read about them here:

url http://forum.java.sun.com/help.jspa?sec=formatting

petes1234a at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 48

> sorry for the late reply, I had to put my kids to

> bed.

>

> here is the code ( I cleared all errors)

>

> Now where?

now you fill in that empty main method with what you want this thing to do.

tsitha at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 49

> what about the complete error messages?

>

> Also, when posting code, please use code tags. Read

> about them here:

>

> url

> http://forum.java.sun.com/help.jspa?sec=formatting

import java.util.*;

public class TriangleProgramAPW3

{

public static void main(String[] args)

{

TriangleAPW3 firstTriangle = new TriangleAPW3();

TriangleAPW3 secondTriangle = new TriangleAPW3();

TriangleAPW3 thirdTriangle = new TriangleAPW3();

// Information used in output display for all triangles.

System.out.println("Triangle One\n");

firstTriangle.TriangleAPW3("23","32");

System.out.println("\n");

System.out.println("Triangle Two\n");

secondTriangle.TriangleAPW3("12","21");

System.out.println("\n");

System.out.println("Triangle Three\n");

thirdTriangle.TriangleAPW3("34", "43");

System.out.println("\n");

}

}

This is the program class using the previous object class.

beatjunkiea at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 50

Go back to the requirements stated in the original post and see what needs to be done.

BigDaddyLoveHandlesa at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 51

> > what about the complete error messages?

> >

> > Also, when posting code, please use code tags.

> Read

> about them here:

>

> url

> http://forum.java.sun.com/help.jspa?sec=formatting[/b

>

>

>

> import java.util.*;

>

> public class TriangleProgramAPW3

> {

>

> public static void main(String[] args)

> {

>

>

>

>

> TriangleAPW3 firstTriangle = new TriangleAPW3();

>

> TriangleAPW3 secondTriangle = new TriangleAPW3();

>

> TriangleAPW3 thirdTriangle = new TriangleAPW3();

>

>

> // Information used in output display for all

> triangles.

>

> System.out.println("Triangle One\n");

> firstTriangle.TriangleAPW3("23","32");

> System.out.println("\n");

> System.out.println("Triangle Two\n");

> secondTriangle.TriangleAPW3("12","21");

> System.out.println("\n");

> System.out.println("Triangle Three\n");

> thirdTriangle.TriangleAPW3("34", "43");

> System.out.println("\n");

>

> }

> }

>

>

> This is the program class using the previous object

> class.

public class TriangleAPW3

{

private int base;

private int height;

private int area;

private int numOfTriangles = 3;

double num1 = .5;

public static void main(String[] args)

{

}

public TriangleAPW3()

{

//No arguments here.

}

public TriangleAPW3(int base, int height)

{

this.base = base;

this.height = height;

System.out.println("Base: " + base + " inches");

System.out.println("Height: " + height + " inches");

}

public int getBase()

{

return base;

}

public void setBase(int base) {

this.base = base;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

public int getArea() {

return base * height / 2;

}

}

And this is the object class that is used by TriangleProgram class.

beatjunkiea at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 52

Now this is the new compiler error I am getting with TrianglProgram

java:22: cannot find symbol

symbol : method TriangleAPW3(java.lang.String,java.lang.String)

location: class TriangleAPW3

firstTriangle.TriangleAPW3("23","32");

beatjunkiea at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 53

> > what about the complete error messages?

> >

> > Also, when posting code, please use code tags.

> Read

> about them here:

>

> url

> http://forum.java.sun.com/help.jspa?sec=formatting[/b

>

>

>

> import java.util.*;

>

> public class TriangleProgramAPW3

> {

>

> public static void main(String[] args)

> {

>

>

>

>

> TriangleAPW3 firstTriangle = new TriangleAPW3();

>

> TriangleAPW3 secondTriangle = new TriangleAPW3();

>

> TriangleAPW3 thirdTriangle = new TriangleAPW3();

>

>

> // Information used in output display for all

> triangles.

>

> System.out.println("Triangle One\n");

> firstTriangle.TriangleAPW3("23","32");

> System.out.println("\n");

> System.out.println("Triangle Two\n");

> secondTriangle.TriangleAPW3("12","21");

> System.out.println("\n");

> System.out.println("Triangle Three\n");

> thirdTriangle.TriangleAPW3("34", "43");

> System.out.println("\n");

>

> }

> }

>

>

> This is the program class using the previous object

> class.

ThisTriangleAPW3 firstTriangle = new TriangleAPW3();

makes sense. You create a new TriangleAPW3 (whatever that is) via a call to its constructor. ThisfirstTriangle.TriangleAPW3("23","32");

doesn't make sense - you're trying to call the constructor as if it were a method. Don't do that.

tsitha at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 54

> > > what about the complete error messages?

> > >

> > > Also, when posting code, please use code tags.

> > Read

> > about them here:

> >

> > url

> >

> http://forum.java.sun.com/help.jspa?sec=formatting[/b

> >

> >

> >

> > import java.util.*;

> >

> > public class TriangleProgramAPW3

> > {

> >

> > public static void main(String[] args)

> > {

> >

> >

> >

> >

> > TriangleAPW3 firstTriangle = new TriangleAPW3();

> >

> > TriangleAPW3 secondTriangle = new TriangleAPW3();

> >

> > TriangleAPW3 thirdTriangle = new TriangleAPW3();

> >

> >

> > // Information used in output display for all

> > triangles.

> >

> > System.out.println("Triangle One\n");

> > firstTriangle.TriangleAPW3("23","32");

> > System.out.println("\n");

> > System.out.println("Triangle Two\n");

> > secondTriangle.TriangleAPW3("12","21");

> > System.out.println("\n");

> > System.out.println("Triangle Three\n");

> > thirdTriangle.TriangleAPW3("34", "43");

> > System.out.println("\n");

> >

> > }

> > }

> >

> >

> > This is the program class using the previous

> object

> > class.

>

>

> ThisTriangleAPW3 firstTriangle = new

> TriangleAPW3();

makes sense. You create a new

> TriangleAPW3 (whatever that is) via a call to its

> constructor.

> ThisfirstTriangle.TriangleAPW3("23","32");[/cod

> ]doesn't make sense - you're trying to call the

> constructor as if it were a method. Don't do that.

Ok i get it now, these are true constructors

[code]

TriangleAPW3 firstTriangle = new TriangleAPW3(23,32);

TriangleAPW3 secondTriangle = new TriangleAPW3(12,21);

TriangleAPW3 thirdTriangle = new TriangleAPW3(34,43);

beatjunkiea at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 55

I cheated an entered the value of the area manually, but I am not fufilling the requirements. How do I get the area to run the calculation like I wrote in TriangleAPW class?

TriangleProgramAPW class :

import java.util.*;

public class TriangleProgramAPW3

{

public static void main(String[] args)

{

System.out.println("Triangle One\n");

TriangleAPW3 firstTriangle = new TriangleAPW3(23,32,368);

System.out.println("\nTriangle Two\n");

TriangleAPW3 secondTriangle = new TriangleAPW3(12,21,126);

System.out.println("\nTriangle Three\n");

TriangleAPW3 thirdTriangle = new TriangleAPW3(34,43,731);

}

}

And here is the TriangleAPW class

public class TriangleAPW3

{

private int base;

private int height;

private int area;

private int numOfTriangles = 3;

double num1 = .5;

public static void main(String[] args)

{

}

public TriangleAPW3()

{

//No arguments here.

}

public TriangleAPW3(int base, int height, int area)

{

this.base = base;

this.height = height;

System.out.println("Base: " + base + " inches");

System.out.println("Height: " + height + " inches");

System.out.println("Area: " + area + " inches");

}

public int getBase()

{

return base;

}

public void setBase(int base) {

this.base = base;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

public int getArea() {

return base * height / 2;

}

public void setArea(int area) {

this.area = area;

}

}

Please dont give up on me now.

beatjunkiea at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 56

What happens when you use getArea?

You shouldn't have a setArea -- think of area as a read-only

property determined by base and height.

(Read-only means a getter with no matching setter.)

You also shouldn't have a field area -- there's no need to

store the area since it can be calculated from the other fields.

By the way, your constructor should not output anything to System.out,

unless you are doing that to debug your code.

BigDaddyLoveHandlesa at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 57

> What happens when you use getArea?

>

> You shouldn't have a setArea -- think of area as a

> read-only

> property determined by base and height.

> (Read-only means a getter with no matching setter.)

>

> By the way, your constructor should not output

> anything to System.out,

> unless you are doing that to debug your code.

Nothing happens when I use getArea.

I deleted setArea and it compiled.

And if I dont use SYstem.out in my constructor the triangles dimesnsion are not shown in the output.

beatjunkiea at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 58

> Nothing happens when I use getArea.

Remember how to use methods that return values?

TriangleAPW3 firstTriangle = new TriangleAPW3(23,32);

int firstArea = firstTriangle.getArea();

System.out.println("firstTriangle's area=" + area);

BigDaddyLoveHandlesa at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 59

> And if I dont use SYstem.out in my constructor the triangles dimesnsion are not shown in the output.

That's not a reason to do something backwards!

BigDaddyLoveHandlesa at 2007-7-28 20:02:36 > top of Java-index,Java Essentials,New To Java...
# 60

> > Nothing happens when I use getArea.

>

> Remember how to use methods that return values?

> > TriangleAPW3 firstTriangle = new

> TriangleAPW3(23,32);

>

> int firstArea = firstTriangle.getArea();

> System.out.println("firstTriangle's area=" + area);

>

It says it cannot find the variable +area

cannot find symbol

symbol : variable Area

location: class TriangleProgramAPW3

System.out.println("firstTriangle's area=" + Area);

beatjunkiea at 2007-7-28 20:02:41 > top of Java-index,Java Essentials,New To Java...
# 61

My mistake:

int firstArea = firstTriangle.getArea();

System.out.println("firstTriangle's area=" + firstArea );

BigDaddyLoveHandlesa at 2007-7-28 20:02:41 > top of Java-index,Java Essentials,New To Java...
# 62

> My mistake:

> > int firstArea = firstTriangle.getArea();

> System.out.println("firstTriangle's area=" +

> firstArea );

>

Actually its the other way around =)

int area = firstTriangle.getArea();

System.out.println("firstTriangle's area=" + area);

Okay awesome, so I have the area able to run. Now I need to go about getting

the value of the base of each Triangle object.

the value of the height of each Triangle object.

a method to set (change) the value of the base of each Triangle object.

and

a method to set (change) the value of the height of each Triangle object.

beatjunkiea at 2007-7-28 20:02:41 > top of Java-index,Java Essentials,New To Java...
# 63

> Okay awesome, so I have the area able to run. Now I

> need to go about getting

> the value of the base of each Triangle object.

getBase?

> the value of the height of each Triangle object.

getHeight?

> method to set (change) the value of the base of each

> Triangle object.

setBase?

> and

> a method to set (change) the value of the height of

> each Triangle object.

setHeight?

BigDaddyLoveHandlesa at 2007-7-28 20:02:41 > top of Java-index,Java Essentials,New To Java...
# 64

I lied you win. I guess you cant use just area because it will already be defined for the other two =P

beatjunkiea at 2007-7-28 20:02:41 > top of Java-index,Java Essentials,New To Java...
# 65

> I lied you win. I guess you cant use just area

> because it will already be defined for the other two

> =P

You could always write a method in your program class:

public static void displayProperties(Triangle triangle) {

System.out.println("base = " + triangle.getBase());

...//ditto height and area

}

And just invoke it with different triangles. No need to duplicate code.

BigDaddyLoveHandlesa at 2007-7-28 20:02:41 > top of Java-index,Java Essentials,New To Java...
# 66

You rock Big Daddy, the dukes are in your account.

I wish I knew you when the class first started, I only have one week left, do you mind helping me understand one last project?

beatjunkiea at 2007-7-28 20:02:41 > top of Java-index,Java Essentials,New To Java...
# 67

not tonight but maybe tommorrow? I live in Germany and it is 10:30 P.M here

beatjunkiea at 2007-7-28 20:02:41 > top of Java-index,Java Essentials,New To Java...
# 68

> not tonight but maybe tommorrow? I live in Germany

> and it is 10:30 P.M here

I am on the west coast of Canada, so I can't promise I'm be awake!

But there are many forum regulars who could help you.

Just don't use the word "urgent" in your topic; that makes them see red :-)

BigDaddyLoveHandlesa at 2007-7-28 20:02:41 > top of Java-index,Java Essentials,New To Java...
# 69

lol. got it. thanks again man!

beatjunkiea at 2007-7-28 20:02:41 > top of Java-index,Java Essentials,New To Java...