• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

how do i apply generic programming here

i was trying to write a general Stack program that woul be able to accept chars ,intergers and doubles.

the code consists of 3 empty Stacks namely p,q and r.elements are pushed in to p and q.the code compares the  two stacks and pushs the smaller elemnt in to r.i assume the rest would be self explainatory.


my problem is that the push method requires an object parameter,evev though i tried to supply the wrapper classes Integer,Double and Character.

pliz help me.

this is how far i went,please note that the aim is to implement this using the keyboard.










import java.io.*;


class Stacker implements Stackings
      
 
{  
      private Object[]array;      
      private  int top = 0;
      

 
  public Stacker()
 {
 top = 0;
 }

 public boolean emptyStacker()
{
 if (top==EMPTY)// if stack is Empty returns true
  return true;  // else return false
else
  return false;
}// End emptyStack()

 public boolean fullStacker()
{
if(top ==FULL)//if stack is full returns true
return true;// else return false
else
return false;
}//End fullStack()


public void push(Object object)

{
 if (!fullStacker())//if not full push element
 
array[top++]= object;
}//End push()


public Object pop()
{
if(!emptyStacker())//if not empty return object
 
      return array[--top];//else return empty

 
else
 
   return null;
}//End pop()

public Object peek()
{
if(!emptyStacker())//if not empty return the top object
return array[top];//else return empty

else return null;



 
}//End peek()
 

{
      
      
}

}
      
main method


import java.io.*;
                    

class StackerTester
{
 public static void main(String[]args)throws IOException
                                                                  
 {  
      
      String inData;
       int count = 0;
       Stacker r  =new Stacker();
     Stacker p = new Stacker();
 
      
       BufferedReader userin =new  BufferedReader(new InputStreamReader(System.in));    
             
      System.out.println("Which data type would tou like to use???");
      inData = userin.readLine();
      
      if (inData.equalsIgnoreCase("Integers"))
            
        {
            System.out.println("Enter the size of your Stack");
             int size = Integer.parseInt(userin.readLine());
             while (count < size)
            try        
        {
         ++count;
        System.out.println("Enter an element in to stack P");
        int element= Integer.parseInt(userin.readLine());
        p.push(object);
         System.out.println("Enter an element in to stack q");
        int elem= Integer.parseInt(userin.readLine());
        q.push(new    Integer()   );
        }
   
       catch(NumberFormatException nf)
        {
         System.out.println("you entered bad data");
        }
     }

else
if (inData.equalsIgnoreCase("Double"))
{


System.out.println("Enter the size of your Stack");
             int size = Integer.parseInt(userin.readLine());
             while (count < size)
            try
        {
         ++count;
        System.out.println("Enter an element in to stack P");
        double element=Double.parseDouble(userin.readLine());
        p.push(Double);
       
         System.out.println("Enter an element in to stack q");
         double elemn = Double.parseDouble(userin.readLine());
         q.push( new Double());

         }
       
       catch(NumberFormatException nf)
        {
         System.out.println("you entered bad data");
        }
     }

else
 if(inData.equalsIgnoreCase("Characters"))
{


System.out.println("Enter the size of your Stack");
             int size = Integer.parseInt(userin.readLine());
             while (count < size)
            try
        {
         ++count;
         System.out.println("Enter an element in to stack P");
         Character element = Character.parseCharacter(userin.readLine());
          p.push(Character);
         System.out.println("Enter an element in to stack q");
         Character elemen= Character.parseCharacter(userin.readLine());
          q.push(new Character());
         }

catch(NumberFormatException nf)
        {
         System.out.println("you entered bad data");
        }
     }

else
if (inData.equalsIgnoreCase("exit"))
{
 System.out.println("press any key to terminate");
}


 Object topq ,topr;
Object topp;            
 while(!p.emptyStacker()&&!q.emptyStacker())
 {
       topp = p.pop();
       topq = q.pop();
      
      if(topp <= topq)
      {
       r.push(topp);
       q.push(topq);
      }
      else
      {
            r.push(topq);
            q.push(topp);
      }
      
 }
 
 while(!p.emptyStacker())
 {
       topq = q.pop();
 r.push(topq);
 }
 
 //while(!r.emptyStack())
 //{
      // topr = r.pop();
      // p.push(topr);
            // }
 
 System.out.println("the contents of the Stack r are:\t");
while(!r.emptyStacker())
                    
      {
            System.out.println(r.pop());
          System.out.println();
            
 }


 
 //System.out.println("the contents of the Stack p are:\t");
//while(!p.emptyStack())            
//      {
//            System.out.println(p.pop());
//          System.out.println();
//            System.out.println(q.pop());
 
//}
 
 
 
 }

}            
 
      
































0
Bwaycct
Asked:
Bwaycct
  • 2
1 Solution
 
jmfrazier1Commented:
Bwaycct,

The problem that you are having with needing an object for each of the types can be accomplished by the proper use of the wrapper classes.  for instance, if you want to take a char and make it into an Object, you must wrap it with this:

char mychar = 'x';
new Character(mychar);

int and double can be done this way:

int myint = 420;
Integer myIntegerObject = new Integer(myint);
double mydouble = 0.0d;
Double myDoubleObject = new Double(mydouble);

However, I have seen other problems in how you are trying to solve this problem. See my answer to your other problem and it may give you some hints on how to solve this problem.  as always, i will be here if you need any other assistance.

jmfrazier1
0
 
CleanupPingCommented:
Bwaycct:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
jmfrazier1Commented:
same as the other Q, i answered his question so the points should be awarded to me.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now