In this program how can i overload the pop() method to use two pop() methods (int and float) in a program as i used two push() methods ?

class Stack{
int StackArrayI[] = new int[3]; 
float StackArrayF[] = new float[3]; 
int tos=-1;

public void push(int value){
	if (tos==2)
	System.out.print ("Stack already Full..!\n");
	else
	StackArrayI[++tos] = value;
	}

public void push(float fvalue){
	if (tos==2)
	System.out.print ("Stack already Full..!\n");
	else
	StackArrayF[++tos] = fvalue;
	}
	
float pop(){
	if (tos==-1){
	System.out.print ("Stack is Empty");
	return 0;
	}
	else
	return StackArrayF[tos--]; 
	}
}

I want to create another pop() method of type integer but i have to pass the parameter to create another pop() method !! what paramter should i pass since there is no need to pass the parameter in pop() method ?

our teacher advised us to overload both pop() and push()..!

You already asked the same question in your previous thread. Please do not double-post.
You teacher is being silly. You cannot overload pop() with no parameters, and there is no parameter needed for a pop.
Serena5 already gave you good advice

have two methods: int popInt() and float popFloat()

Have a look at my previous thread again..! i didn't asked this question !!
i had solved the problem with Sarena5's advice but my teacher again told me to use two pop() methods...!
previous thread was solved that is why I've started this thread..!

OK, sorry.

Xufyan, you may have two control indexes, such as int tosInt=-1,tosFloat=-1 to manage these two indipendent arrays of different data type which store different data. It is a common sense.

can you please elaborate your answer ?? how can i overload the pop()method if i declare to variable for two arrays ??

I don't understand what tong1 was trying to say, but you cannot overload a method in java by changing the return type alone. Overloaded methods must have different parameter lists (and it is the datatypes that count, not their names). So for example,
public void f1(int n) and
public void f1(int x)
is not a valid example of overloading since java will not be able to tell which one you mean if you call
f1(5);

On the other hand, as you can see in your push methods, it is valid to say
public void g1(int x) and
public void g1(float x)
since java can tell which one to call if you say
g1(5); versus g1(5.0);

i've already understand overloading i want to know what tong1 is saying

Xufyan has declared two arrays but only one controlling index tos (normally written as top, the stack top indicator), in the class Stack. How can one use only one indicator to indicate the tops of two different arrays since the tops of the two arrays could be different? Hence, we should declare two tifferent tops: int topInt and float topFloat. The class Stack is defined as follows:

class Stack{
int StackArrayI[] = new int[3]; 
float StackArrayF[] = new float[3]; 
int topInt=-1;
int topFlat=-1;

public void push(int value){
	if (topInt==2)
	System.out.print ("Stack already Full..!\n");
	else
	StackArrayI[++topInt] = value;
	}

public void push(float fvalue){
	if (topFloat==2)
	System.out.print ("Stack already Full..!\n");
	else
	StackArrayF[++topFloat] = fvalue;
	}
	
float pop(){
	if (topFloat==-1){
	System.out.print ("Stack is Empty");
	return 0;
	}
	else
	return StackArrayF[topFloat--]; 
	}
} 
…

Xufyan has declared two arrays but only one controlling index tos (normally written as top, the stack top indicator), in the class Stack. How can one use only one indicator to indicate the tops of two different arrays since the tops of the two arrays could be different? Hence, we should declare two tifferent tops: int topInt and float topFloat. The class Stack is defined as follows:

class Stack{
int StackArrayI[] = new int[3]; 
float StackArrayF[] = new float[3]; 
int topInt=-1;
int topFlat=-1;

public void push(int value){
	if (topInt==2)
	System.out.print ("Stack already Full..!\n");
	else
	StackArrayI[++topInt] = value;
	}

public void push(float fvalue){
	if (topFloat==2)
	System.out.print ("Stack already Full..!\n");
	else
	StackArrayF[++topFloat] = fvalue;
	}
	
float pop(){
	if (topFloat==-1){
	System.out.print ("Stack is Empty");
	return 0;
	}
	else
	return StackArrayF[topFloat--]; 
	}
} 
…

when i'm using one index variable then program is working but when i am using two index variable why it is giving an error ???

Error in Line no 58..!

class Stack{
int StackArrayI[] = new int[3]; 
float StackArrayF[] = new float[3]; 
int tosInt;
int tosFloat;

Stack(){
    tosInt=-1;
    tosFloat=-1;
}

public void push(int value){
    if (tosInt==2)
    System.out.print ("Stack already Full..!\n");
    else
    StackArrayI[++tosInt] = value;
}

public void push(float fvalue){
    if (tosFloat==2)
    System.out.print ("Stack already Full..!\n");
    else
    StackArrayF[++tosFloat] = fvalue;
}

float pop(){
    if (tosFloat==-1){
    System.out.print ("Stack is Empty");
    return 0;
    }
    else
    return StackArrayF[tosFloat--]; 
 }

}
 
class Stack1{
public static void main (String args[]){
    Stack first = new Stack();
    
    // Push values in Floating Point Array...!
    for (float i=11;i<=13;i++){ 
        first.push(i);
        System.out.println(i + " pushed at StackArrayF[" + first.tosFloat + "]" );
        }
        
    // Pop values from Floating Point Array...!
    for (float i=1;i<=3;i++)
        System.out.println("StackArrayF[" + first.tosFloat + "] : " + first.pop());
    
    // Push values in Integer Array...!
    for (int i=15;i<=17;i++){ 
        first.push(i);
        System.out.println(i + " pushed at StackArrayI[" + first.tosInt + "]" );
        }
        
    for (int i=1;i<=3;i++){
    first.StackArrayF[first.tosFloat]=first.StackArrayI[first.tosInt];
    System.out.println("StackArrayI[" + first.tosFloat + "] : " + (int)first.pop());
    }
 }
}

Output:

11.0 pushed at StackArrayF[0]
12.0 pushed at StackArrayF[1]
13.0 pushed at StackArrayF[2]
StackArrayF[2] : 13.0
StackArrayF[1] : 12.0
StackArrayF[0] : 11.0
15 pushed at StackArrayI[0]
16 pushed at StackArrayI[1]
17 pushed at StackArrayI[2]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Stack1.main(Stack1.java:58)

(1)Insert the line of code before line 58 to check the two indexes' values: tosFloat and TosInt:

System.out.println("tosFloat: " + first.tosFloat + " ,tosInt: " + first.tosInt);

(2) the values of the two indexes before the run time error message is issued thus are found:
tosFloat: -1 ,tosInt: 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Stack1.main(Stack1.java:59)
(3) Therefore, it is easy to realize that
the first for each loop pushes 3 float values into the Stack first so that the first.tosFloat becomes 2; then the second for each loop pops them all out so that the first.tosFloat becomes -1; the third for each loop has nothing to do with first.tosFloat; and the fourth for loop has finally started to execute the code:

first.StackArrayF[first.tosFloat]=first.StackArrayI[first.tosInt];

where the first.tosFloat is -1, causing an ArrayIndexOutOfBoundsException.

hey thanks..! i solved this issue :)

that is what i've done ! first.StackArrayF[++first.tosFloat]=first.StackArrayI[first.tosInt--];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.