Kalpana Periasamy

Greenhorn
+ Follow
since Jan 05, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Kalpana Periasamy

Hi everyone,
The clone() method declared in Object class raises CloneNotSupportedException, if the class does not implement Cloneable interface. Then, why is the method present in Object class? Is it just to give a default implementation for clone method? Since Cloneable is an interface, it cannot have default implementation. If Cloneable is declared as a class, then it should be extended. But , Java does not support multiple inheritance. Am I right?
Still I wonder , how the JVM make sure that the calling class implements a Cloneable interface when it is trying to clone method? Is this check implemented in the clone method definition?
13 years ago
. . . for object reference variables, the called method can change the object the variable referred to. . . .

Let me rephrase this sentence for better understanding. For object reference variables, the called method can change the contents of the object(referred by the variable). This is what I understood.
13 years ago
Called method can't change the caller's variable

Ex: void bar() {
int i = 5;
foo(i);
System.out.println("Value of i in bar is" + i);
}

void foo(int i){
i++;
System.out.println("Value of i in foo is "+i);
}

Output :
Value of i in foo is 6.
Value of i in bar is 5.

for object reference variables, the called method can change the
object the variable referred to.


Ex : void bar() {
Foo f = new Foo("Foo");
doStuff(f);
System.out.println(f.getName());
}
Foo doStuff(Foo g) {
g.setName("Boo");
}

Output : Boo (its changed in doStuff but reflected in caller's method)

3) For object references, it means the called method can't
reassign the caller's original reference variable and make it refer to a different object,
or null."

Ex :
void bar() {
Foo f = new Foo();
f.setName("Bar");
doStuff(f);
}
void doStuff(Foo g) {
g.setName("Boo");
g = new Foo();
g.setName("New Foo");
}

Output : Boo (value is changed in doStuff. But it cannot be made to point to a new variable. So it wont print "New Foo".
13 years ago
Still I couldnt find out your mistake. Can you post the new pop code? (Just curious..)
13 years ago
Your sort logic is perfectly fine. If you change your stack logic, the code will work perfectly fine

Here is a sample Stack Code:
class Stack{
Node head;
public Stack(){
this.head = null;
}

void push(int value){
Node temp = new Node(value);
temp.next = head;
if(head == null)
head = temp;
else{
temp.next = head;
head = temp;
}
}

int pop(){
if(head != null){
int temp = head.value;
head = head.next;
return temp;
} else {
throw new EmptyStackException();
}
}

boolean isEmpty() {
return head == null;
}

int peek(){
if(!isEmpty())
return head.value;
else
throw new EmptyStackException();
}

void print(){
Node ref = head;
System.out.println("Contents of stack are:");
while(ref != null){
System.out.println(ref.value);
ref = ref.next;
}
}
}

class Node{
public int value;
public Node next;

public Node(int value){
this.value = value;
this.next = null;
}
}

Replace this stack code in your program. There you go.. Your input will be sorted
13 years ago
I think that you have pasted the same program again.
(There is no need of 2 references. Just keep 'head'. Always insert the new value to head and pop the value from the head)
13 years ago
You are right. It will work for 2 elements also.

If you use just one pointer, then the code will become way simpler, like

int pop(){
int element;
if(head==null)
throw Exception;
else {
element = head.value;
head = head.next;
return element;
}
13 years ago
I understand his reason. I think that I dint understand the basics of generics yet. Do you have any pointers to good generics tutorial?
13 years ago
Not sure about the Null Pointer Exception.

Though your logic is correct , why do you have 2 references ( head and last) in the Stack class? Is head alone not sufficient? Instead of pushing and popping the element from the end of the stack, if you push and pop from the head of the stack, only one reference will be enough. The code will look simple.

Another thing, in line 38, the verification should be while(ref1.next!=null). Your pop will work wrongly if the stack has 2 elements.


13 years ago
@All : Thanks for the explanation.
For my understanding, what was the method definition of add before Generics were added in Java 5. Was it void add(Object element)? If so, when add(Object element) is rewritten as add(E element), why not remove(Object element) is rewritten?
13 years ago
Hi Java Geeks,
This is my first post in Java Ranch. Happy new year to all.

Coming to the question, I was looking at the "Collections" interface. I saw these 2 methods.

void add(E element);
void remove(Object Element);

Why isnt the remove method declared as void remove(E Element)?

Thanks in advance,
Kalps.
13 years ago