?
Solved

how do i apply generic programming here

Posted on 2003-04-01
4
Medium Priority
?
382 Views
Last Modified: 2012-06-22
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
Comment
Question by:Bwaycct
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
jmfrazier1 earned 750 total points
ID: 8250343
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
 

Expert Comment

by:CleanupPing
ID: 9473900
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
 
LVL 1

Expert Comment

by:jmfrazier1
ID: 9476208
same as the other Q, i answered his question so the points should be awarded to me.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question