/** * Problem: Computing all Subset Sums * Given an array containing some integers (there may be duplicates), * write a routine that returns all the possible sums that can be formed * by using numbers in the array. For, if the array contains 4 and 6, * the possible sums are 0, 4, 6, and 10. If the array contains 1, 3, 5, and 7, * the possible sums are 0, 1, 3, 5, 7, 4, 6, 8, 8, 10, 12, 9, 11, 13, 15, and 16. * Notice that 8 appears twice in the answer (1+7 and 3+5). * * @author (Vilchor G. Perdido) * @date (April 14, 2010) */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class SumOfSubsetNumbers extends Applet implements ActionListener { Button bPrintSum,bReset; TextField txtSize,txtInput; List listElements = new List(); List listSums = new List(); Label lblElements,lblSize; TextArea tProblem; int arraySize; int []arrayElements; int elements = 0; int temp = 0; int index = 0; int num, data, ctr, possibleSum, subIndex, valueOfSum; Font myFont,otherFont,buttonFont; public void init() { showStatus("Developed by: Vilchor G. Perdido - NVSU Bayombong Campus (MIT-1)"); setLayout(null); setSize(550,760); myFont = new Font("Arial",Font.BOLD,14); otherFont = new Font("Arial",Font.BOLD,18); buttonFont = new Font("Arial",Font.BOLD,16); setBackground(Color.darkGray); listElements.add("==============================="); listElements.add("SIZE AND ELEMENTS OF THE ARRAY"); listElements.add("==============================="); listElements.setBounds(10,130,260,200); listElements.setFont(myFont); listElements.setBackground(Color.yellow); add(listElements); listSums.add("==============================="); listSums.add(" Possible SUMS of the ELEMENTS"); listSums.add("==============================="); listSums.setBounds(10,340,260,400); listSums.setFont(myFont); listSums.setBackground(Color.yellow); add(listSums); String myString = "Problem: Computing all Subset Sums\nGiven an array containing some integers (there may be duplicates), write a"; myString += "\nroutine that returns all the possible sums that can be formed by using numbers in the array."; myString += "\nFor instance, if the array contains 4 and 6, the possible sums are n0, 4, 6, and 10."; myString += "\nIf the array contains 1, 3, 5, and 7, the possible sums are 0, 1, 3, 5, 7, 4, 6, 8, 8, 10,"; myString += "\n 12, 9, 11, 13, 15, and 16. Notice that 8 appears twice in the answer (1+7 and 3+5)."; tProblem = new TextArea(myString); tProblem.setBounds(10,10,530,112); tProblem.setEditable(false); tProblem.setForeground(Color.black); add(tProblem); lblSize = new Label("WHAT IS THE SIZE OF THE ARRAY?"); lblSize.setBounds(290,160,500,30); lblSize.setForeground(Color.white); add(lblSize); txtSize = new TextField(); txtSize.setBounds(290,190,200,30); add(txtSize); lblElements = new Label("ENTER THE ELEMENTS OF THE ARRAY"); lblElements.setBounds(290,230,500,30); lblElements.setForeground(Color.white); add(lblElements); txtInput = new TextField(); txtInput.setBounds(290,260,200,30); add(txtInput); bPrintSum = new Button("PRINT all possible SUMS"); bPrintSum.setBounds(300,600,210,30); bPrintSum.setEnabled(false); bPrintSum.setFont(buttonFont); add(bPrintSum); bReset = new Button("RESET / CLEAR Array"); bReset.setBounds(300,640,210,30); bReset.setEnabled(false); bReset.setFont(buttonFont); add(bReset); txtSize.addActionListener(this); txtInput.addActionListener(this); bPrintSum.addActionListener(this); bReset.addActionListener(this); } public void paint(Graphics g) { showStatus("Developed by: Vilchor G. Perdido - NVSU Bayombong Campus (MIT-1)"); } public void actionPerformed(ActionEvent evt) { if(evt.getSource()==txtSize) { //this will assign the size of the array arraySize = Integer.parseInt(txtSize.getText()); listElements.add(" Size of the Array: " + arraySize); listElements.add("==============================="); arrayElements = new int [arraySize + 1]; listElements.add(" Elements of the Array"); listElements.add("==============================="); } elements = Integer.parseInt(txtInput.getText()); if(evt.getSource()==txtInput) { //this will determine if the array is full if(index==arrayElements.length-1) { listElements.add("No more space for new element."); bPrintSum.setEnabled(true); } else { //this line of code will store the elements in the array with their indeces index++; arrayElements[index] = elements; listElements.add("---> Element number " + index + ": " + arrayElements[index]); temp = temp + arrayElements[index]; } txtInput.selectAll(); txtInput.requestFocus(); bReset.setEnabled(true); } if(evt.getSource()==bPrintSum) { for (valueOfSum=0;valueOfSum<=temp;valueOfSum++) { num = arraySize * arraySize; for (index = 0; index < num; index++) { subIndex = index; possibleSum = 0; ctr = 1; while (subIndex > 0) { data = subIndex % 2; if (data > 0) possibleSum = possibleSum + arrayElements[ctr]; subIndex = subIndex / 2; ctr++; } //this code will display the possible sums of all the elements in the array if (possibleSum == valueOfSum) listSums.add(" " + valueOfSum); } } listSums.add("==============================="); listSums.add(" There are " + index + " possible sums."); listSums.add("==============================="); arraySize = 0; index = 0; bPrintSum.setEnabled(false); } if(evt.getSource()==bReset) { listElements.clear(); listSums.clear(); txtInput.setText(""); txtSize.setText(""); listElements.add("==============================="); listElements.add("SIZE AND ELEMENTS OF THE ARRAY"); listElements.add("==============================="); listSums.add("==============================="); listSums.add(" Possible SUMS of the ELEMENTS"); listSums.add("==============================="); bPrintSum.setEnabled(false); bReset.setEnabled(false); } } }