Mitali Basak

foreheadsobstinacyΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 1 μήνα)

402 εμφανίσεις


1

Code Document and JUnit Code Execution Screenshot


Exercise 6.5


The code includes the following components:


1.

Stack
Class
.java (The stack class implementing the stack interface)

2.

S
tackgui.java

3.

Stack
Class
Test.java (Junit Test Program to give a demo of the sta
ck class)

4.

Stackpackage
Suite.java (Junit Test Suite class)



1.

Stack
Class
.java


/*


* To change this template, choose Tools | Templates


* and open the template in the editor.


*/


package Stackpackage;


/**


*


* @author manisha


*/


public class StackClass

implements StackInterface {


public Object[] objstack;


private int inttop;


public StackClass(int size)


{


inttop=
-
1;


objstack = new Object[size];


}




public boolean isEmpty() {


return (inttop ==
-
1);



}



public boolean isFull() {


return (inttop == (objstack.length
-

1));


}



public Object pop() {


if (isEmpty()) return false;


2


return objstack[inttop
--
];


}



public boolean push(Object x) {


if (isFull())
return false;


objstack[++inttop]=x;


return true;


}





public int Stacksize() {


return inttop + 1;


}



public Object top() {


if (isEmpty()) return false;


return objstack[inttop];


}

}

2.

Stackgui.java


/*


* stackgui.java


*


* Created on October 17, 2008, 11:53 PM


*/


package Stackpackage;


import java.awt.Graphics;


/**


*


* @author manisha


*/

public class S
tackgui extends java.applet.Applet {



StackClass stc= new StackClass(10);




@Over
ride


public void init() {


try {


java.awt.EventQueue.invokeAndWait(new Runnable() {


public void run() {


initComponents();




3




}


});


} catch (Exception ex) {


ex.printStackTrace();


}


}



/** This method is called from within the init() method to


* initialize the form.


* WARNING: Do NOT modify this code. The content of this method is


* alw
ays regenerated by the Form Editor.


*/


// <editor
-
fold defaultstate="collapsed" desc="Generated Code">


private void initComponents() {



panel1 = new java.awt.Panel();


label1 = new java.awt.Label();



btnPush = new java.awt.Button();


textField1 = new java.awt.TextField();


btnPop = new java.awt.Button();


btnSize = new java.awt.Button();


btnTop = new java.awt.Button();


label2 = new java.awt.Label();


tex
tField2 = new java.awt.TextField();


label3 = new java.awt.Label();


textaStack = new java.awt.TextArea();



setLayout(new java.awt.BorderLayout());



panel1.setBackground(new java.awt.Color(204, 255, 204));


panel1.setNa
me("STACK GUI
\
n"); // NOI18N


panel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());



label1.setAlignment(java.awt.Label.CENTER);


label1.setBackground(new java.awt.Color(153, 153, 153));


label1.setForeground(new ja
va.awt.Color(255, 255, 255));


label1.setText("INPUT ");


panel1.add(label1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 50,
-
1));



btnPush.setBackground(new java.awt.Color(153, 153, 153));


btnPush.setForeground(
new java.awt.Color(255, 255, 255));


btnPush.setLabel("PUSH");


btnPush.addActionListener(new java.awt.event.ActionListener() {


public void actionPerformed(java.awt.event.ActionEvent evt) {


4


btnPushActionPerformed(e
vt);


}


});


panel1.add(btnPush, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40,
-
1,
-
1));



textField1.addActionListener(new java.awt.event.ActionListener() {


public void actionPerformed(java.awt.even
t.ActionEvent evt) {


textField1ActionPerformed(evt);


}


});


panel1.add(textField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 10,
160,
-
1));



btnPop.setBackground(new java.awt.Color(153, 153, 15
3));


btnPop.setForeground(new java.awt.Color(255, 255, 255));


btnPop.setLabel("POP");


btnPop.addActionListener(new java.awt.event.ActionListener() {


public void actionPerformed(java.awt.event.ActionEvent evt) {



btnPopActionPerformed(evt);


}


});


panel1.add(btnPop, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 40,
-
1,
-
1));



btnSize.setBackground(new java.awt.Color(153, 153, 153));


btnSize.setForeground(new

java.awt.Color(255, 255, 255));


btnSize.setLabel("SIZE");


btnSize.addActionListener(new java.awt.event.ActionListener() {


public void actionPerformed(java.awt.event.ActionEvent evt) {


btnSizeActionPerformed(evt)
;


}


});


panel1.add(btnSize, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 40,
-
1,
-
1));



btnTop.setBackground(new java.awt.Color(153, 153, 153));


btnTop.setForeground(new java.awt.Color(255, 255, 255));


btnTop.setLabel("TOP");


btnTop.addActionListener(new java.awt.event.ActionListener() {


public void actionPerformed(java.awt.event.ActionEvent evt) {


btnTopActionPerformed(evt);


}


});


p
anel1.add(btnTop, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 40,
-
1,
-
1));


5



label2.setAlignment(java.awt.Label.CENTER);


label2.setBackground(new java.awt.Color(153, 153, 153));


label2.setForeground(new java.awt.Color(255
, 255, 255));


label2.setText("OUTPUT");


panel1.add(label2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100,
-
1,
-
1));



textField2.addActionListener(new java.awt.event.ActionListener() {


public void actionPerfor
med(java.awt.event.ActionEvent evt) {


textField2ActionPerformed(evt);


}


});


panel1.add(textField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 100,
160,
-
1));



label3.setText(" Stack Contents");


panel1.add(label3, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 10,
-
1,
-
1));


panel1.add(textaStack, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 30,
-
1, 260));



add(panel1, java.awt.BorderLayout.CENTER);


}/
/ </editor
-
fold>


private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}


private void tex
tField2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:



}


private void btnPushActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:




if(textField1.getText().equals(""))


{


}


else


{


stc.push(textField1.getText());


}

refreshTextArea();

}


6


private void btnPopActionPerformed(java.awt.event.ActionEvent evt) {



textField2.setText("");


if(stc.Stacksize()>0)


{


Object retTypr=stc.pop();



textField2.setText(retTypr.toString());


refresh
TextArea();


}



}


private void btnSizeActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:


textField2.setText("");


int sz=stc.Stack
size();


if(sz>0)


{


textField2.setText(String.valueOf(sz));


}

}


private void btnTopActionPerformed(java.awt.event.ActionEvent evt) {

textField2.setText("");



t
extField2.setText("");


if(stc.Stacksize()>0)


{

Object rettype1= stc.top();



textField2.setText(rettype1.toString());

}




}

private void refreshTextArea()

{

if(stc.Stacksize()>
-
1)

{


// Clear text area


for (int i=0;i<=textaStack.getRows();i++)


textaStack.setText("");




7


for(int i=stc.Stacksize()
-
1;i>=0;i
--
)


{


if(i==stc.Stacksize()
-
1)


textaStack.setText(stc.objstack[i].toString());


else


textaStack.append
("
\
n" + stc.objstack[i].toString());


}

}

}



// Variables declaration
-

do not modify


private java.awt.Button btnPop;


private java.awt.Button btnPush;


private java.awt.Button btnSize;


private java.awt.Button btnTo
p;


private java.awt.Label label1;


private java.awt.Label label2;


private java.awt.Label label3;


private java.awt.Panel panel1;


private java.awt.TextField textField1;


private java.awt.TextField textField2;


private java.awt.TextAr
ea textaStack;


// End of variables declaration


}


3.

Stack
Class
Test.java

/*


* To change this template, choose Tools | Templates


* and open the template in the editor.


*/


package Stackpackage;


import junit.framework.TestCase;



imp
ort org.junit.After;


import org.junit.Before;


import org.junit.Test;

import static org.junit.Assert.*;


/**


*


8


* @author manisha


*/

public class StackClassTest extends TestCase {


StackClass instance;



public StackClassTest() {




}




@Before


@Override


public void setUp() {


instance = new StackClass(10);





}



@After


@Override


public void tearDown() {


}



/**


* Test of isEmpty method, of class StackClass.


*/


@Test


public
void testIsEmpty() {




System.out.println("isEmpty");


// StackClass instance = null;


// boolean expResult = true;


boolean result = instance.isEmpty();


assertTrue(result);//, result);


// TODO review the
generated test code and remove the default call to fail.


// fail("The test case is a prototype.");


}



/**


* Test of isFull method, of class StackClass.


*/


@Test


public void testIsFull() {




for(int i=0;i<10;i
++)


instance.push(i);


9






System.out.println("isFull");


//instance = null;


// boolean expResult = true;


boolean result = instance.isFull();


assertTrue(result);


// TODO review the ge
nerated test code and remove the default call to fail.


// fail("The test case is a prototype.");


}



/**


* Test of pop method, of class StackClass.


*/


@Test


public void testPop() {




System.out.println("pop");


// StackClass instance = null;


instance.push(20);


Object expResult = 20;


Object result = instance.pop();


assertEquals(expResult, result);


// TODO review the generated test code and remove the default call to
fail.


// fail("The test case is a prototype.");


}



/**


* Test of push method, of class StackClass.


*/


@Test


public void testPush() {


System.out.println("push");


int x = 20;


instance.push(x);


/
/ StackClass instance = null;


boolean expResult = true;


boolean result = instance.push(x);


assertTrue( result);


// TODO review the generated test code and remove the default call to fail.


// fail("The test case is a p
rototype.");


}



/**


* Test of Stacksize method, of class StackClass.


*/


10


@Test


public void testStacksize() {


System.out.println("Stacksize");


// StackClass instance = null;


instance.push(10);


instanc
e.push(20);


int expResult = 2;


int result = instance.Stacksize();


assertEquals(expResult, result);


// TODO review the generated test code and remove the default call to fail.


// fail("The test case is a prototype.");


}



/**


* Test of top method, of class StackClass.


*/


@Test


public void testTop() {


System.out.println("top");


// StackClass instance = null;


instance.push(20);


Object expResult = 20;


Object r
esult = instance.top();


assertEquals(expResult, result);


// TODO review the generated test code and remove the default call to fail.




// fail("The test case is a prototype.");


}





}

4.

StackpackageSuite
.java


/*


* To cha
nge this template, choose Tools | Templates


* and open the template in the editor.


*/


package Stackpackage;


import org.junit.runner.RunWith;

import org.junit.runners.Suite;


/**


*


11


* @author manisha


*/

@RunWith(Suite.class)

@Suite.SuiteClasses({Stack
package.StackClassTest.class})

public class StackpackageSuite {


}