The Artima Developer Community
Sponsored Link

Java Answers Forum
Please Help!

25 replies on 2 pages. Most recent reply: Sep 23, 2003 3:27 AM by Senthoorkumaran Punniamoorthy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 25 replies on 2 pages [ 1 2 | » ]
Kenny

Posts: 5
Nickname: ck890
Registered: Sep, 2003

Please Help! Posted: Sep 21, 2003 3:45 PM
Reply to this message Reply
Advertisement
I need to write a tree program, and I dont know where to put the methods.

In the c++, I can create a class with all its variables and methods in the header file, then implement all the methods in the implement file.
Example:
void BinarySearchTree::insertItem(<arg>);

How do I do that in java?

Thanks!


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Please Help! Posted: Sep 21, 2003 9:53 PM
Reply to this message Reply
Create an interface like
public interface Iinterface{
public static String MY_NAME="Kenny";
public void displayText(String name);
}
[java]
 
Then implement the interface.
 
[java]
public class ImpelementingClass implements Iinterface{
public void displayText(String text){
System.out.println(MY_NAME+" says:"+text);
}
}


The example in java is
java.awt.event.MouseListener is an Interface which is implemented by MouseAdapter.

Note in interface you can not define the body of methods.
if you want to use the methods of other class use 'extends'. e.g

public class TextField extends TextComponent

Kenny

Posts: 5
Nickname: ck890
Registered: Sep, 2003

Re: Please Help! Posted: Sep 22, 2003 12:26 AM
Reply to this message Reply
Thanks, but I have another problem now!
Please help me again!

class AvlNode
{
NodeData element;
AvlNode left;
AvlNode right;
int height;

AvlNode( NodeData theElement )
{
this( theElement, null, null );
}

AvlNode( NodeData theElement, AvlNode lt, AvlNode rt )
{
element = theElement;
left = lt;
right = rt;
height = 0;
}

public int getHeight( AvlNode t )
{
if ( t == null )
return -1;
else
return t.height;
}

public AvlNode rotateWithLeftChild( AvlNode k2 )
{
AvlNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;

int x = getHeight(k2.left);
int y = getHeight(k2.right);
k2.height = max( x, y ) + 1;
/*
k1.height = max(height(k1.height), k2.height) + 1;
return k1;
*/
}
}

I assume
int x = getHeight(k2.left);
will assign the height of k2.left in x and it will
be a integer, but the compiler said x is a AvlNode.
I dont know what happen, and how can I fix it so
that it will store the height of k2.left as integer in x.
Thanks!

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Please Help! Posted: Sep 22, 2003 12:56 AM
Reply to this message Reply
Writing an AVL tree would not have been my choice for writing my first Java program....but anyway....

The method (member function) "getHeight(...)" is not a static method:
int x = getHeight(k2.left); 

You must call it on an object. In this case, since you do not specify an object, the compiler assumes that you're referring to the current object. It is as if you wrote:
int x = this.getHeight(k2.left); 

The compiler then looks at the argument you're supplying "k2.left", which is of type "AvlNode" and that's why it is complaining - "getHeight(...)" accepts an int argument.

So, that's why you're getting an error. The solution is to define "getHeight(....)" as static:
public static int getHeight(AvlNode t) {
  if ( t == null )
    return -1;
  else
    return t.height;
}

However, this is rather nasty and not very Object-Oriented (but as you're a C++ programmer you're forgiven!). What you should do instead is define the method as:
public int getHeight() {
  return this.height;
}

...and then call the method thus:
int x = k2.left.getHeight(); 

(You might want to declare all of your instance variables - element, left, right and height all private)

In future, please post any error messages you receive.

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Please Help! Posted: Sep 22, 2003 1:55 AM
Reply to this message Reply
erghhhh confusion....

Do we always need to call this.method() to get a non static method....

I had a method
public int getHeight(JComponent comp)
{
	return(comp==null?-1:comp.getHeight());
	
}


and I call it from method another method init(); of the same class and it works fine......

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Please Help! Posted: Sep 22, 2003 2:39 AM
Reply to this message Reply
No. Generally speaking you don't need to use the this keyword. By default, when you call a non-static method from within an instance of a class, then the method called will also be from that instance. The this keyword just makes it explicit (which is no bad thing).

The this keyword is normally used in Java as follows:
public class Dog
{
    private String name;
 
    public void setName(String name)
    {
        // The follwing line does work...
        name = name; 
        // ...but it looks very odd.
        // The following usage is more common.
        this.name = name;
    }
}
Of course, that practice of using the same parameter and variable names within the same chunk of code has long been known to be bad (Steve McConnel - Code Complete - 1992), but Java now has a well established tradition of minimizing the length and meaningfulness of variable names. (I believe this is park of its convention of breaking with the past wherever possible.)

I believe the following is much better altogether (but I'm in the minority)::
public class Dog
{
    private String name;
 
    public void setName(String newName)
    {
        // No ambiguity.  No need for extra keywords
        // to clarify things.
        name = newName; 
    }
}
Vince.

PS. If you stick "java" and "/java" tags (in square brackets) around java code, it will be more readable when posted.

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Please Help! Posted: Sep 22, 2003 2:40 AM
Reply to this message Reply
Agreeed but bot usin "this" can not be the cause of problem.

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Please Help! Posted: Sep 22, 2003 2:41 AM
Reply to this message Reply
Sorry the sentence read as " Agreeed but not using "this" can not be the cause of problem"

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Please Help! Posted: Sep 22, 2003 3:04 AM
Reply to this message Reply
err....I have to pick up a point here Vincent...The following code WON'T work:

public class Dog{
private String name;

public void setName(String name) {
name = name;
}

public String getName() {
return name;
}

public static void main(String[] args) {
Dog d1 = new Dog();
d1.setName("Rover");
System.out.println("Name is :'" + d1.getName() + "'");
}
}

Run the code if you don't believe me! ...and this is the point - unless qualified with the 'this' keyword, variables will resolve to the closest scope - in this case the parameter 'name', rather than the instance variable 'name'.

Mausum, I'm not sure what you're talking about.

With regard to parameters with the same name as the instance variables (as you might have guessed), I'm a bit of a fan. In my programming at work I started getting very frustrated that with every method I wrote I either had to come up with new names for each parameter, or always prefix them with "param" or "a" or something else...

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Please Help! Posted: Sep 22, 2003 3:26 AM
Reply to this message Reply
class HSBColor {
    int hue, saturation, brightness;
    HSBColor (int hue, int saturation, int brightness) {
        this.hue = hue;
        this.saturation = saturation;
        this.brightness = brightness;
    }


You must use the this keyword in this constructor because you have to disambiguate the argument hue from the member variable hue (and so on with the other arguments). Writing hue = hue; makes no sense. Argument names take precedence and hide member variables with the same name. So to refer to the member variable you must do so through the current object--using the this keyword to refer to the current object--explicitly.

http://java.sun.com/docs/books/tutorial/java/javaOO/methodbody.html

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Please Help! Posted: Sep 22, 2003 3:34 AM
Reply to this message Reply
That I know that if a variable is passed as a parameter of a method (or constructor) we must use this to diffrenciate between class level variable and the method/constructor level variable.My point is that

in the example posted

int x = getHeight(k2.left);
 
and int x = this.getHeight(k2.left);
 


must not throw erro only because of "this" identifier.

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Please Help! Posted: Sep 22, 2003 3:40 AM
Reply to this message Reply
My point is that

import javax.swing.*;
public class ThisExample
{
 
	void checkTry()
	{
			JTextField tf = new JTextField();
			System.out.println("Print without this :"+getHeight((JComponent)(tf)));
			System.out.println("Print with this:"+getHeight((JComponent)(tf)));
	}
	public int getHeight(JComponent comp)
	{
		return(comp==null?-1:comp.getHeight());
		
	}
	public static void main(String args [])
	{
	
		new ThisExample).checkTry();
	}
}
 


Here in this example either use this or dont use this.

It will compile.
Same was the case posted by Kenny
He was calling

int x = getHeight(k2.left);

from
public AvlNode rotateWithLeftChild( AvlNode k2 )


and so this will certainly not make any diffrence here.

Here there is no reference to method level varaible[local variabl] that use of this is MUST

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Please Help! Posted: Sep 22, 2003 3:42 AM
Reply to this message Reply
replace the line
System.out.println("Print with this:"+getHeight((JComponent)(tf)));

with

System.out.println("Print with this:"+this.getHeight((JComponent)(tf)));


Tell me...using this.getHeight and simple getHeight will make any differece.

No talks on CODING standard...here compilation is in question

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Please Help! Posted: Sep 22, 2003 4:05 AM
Reply to this message Reply
mausam,

I am not quite sure at which point you disagree with Davids answer? To quote his answer
-------------------------------------------------------
int x = getHeight(k2.left);

You must call it on an object. In this case, since you do not specify an object, the compiler assumes that you're referring to the current object. It is as if you wrote:

int x = this.getHeight(k2.left);
-------------------------------------------------------

Are you arguing this point? Or something else?

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Please Help! Posted: Sep 22, 2003 4:12 AM
Reply to this message Reply
yes i am not sure of this point only.

I am sure that

int x = getHeight(k2.left);

and

int x = this.getHeight(k2.left);

both should work.

Flat View: This topic has 25 replies on 2 pages [ 1  2 | » ]
Topic: Sample Code Required Using jPOS libraries Previous Topic   Next Topic Topic: URL Rewriting?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use