Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.43 KB | None | 0 0
  1. /*
  2.  * realizarea poliformizmului pentru clasele date cu crearea metodelor
  3.  * conform specificului claselor.
  4.  * Polynom <- ComplexPolynom
  5.  *
  6.  *
  7.  *
  8.  */
  9. import java.util.*;
  10. import javax.swing.*;
  11. import java.awt.event.*;
  12. import java.awt.Color;
  13.  
  14. class Monom {
  15.     private double coef;
  16.     private double power;
  17.  
  18.  
  19.     public Monom( double coef, double power) {
  20.         this.coef = coef;
  21.         this.power = power;
  22.     }
  23.    
  24.     public double getCoef() {
  25.         return coef;
  26.     }
  27.  
  28.     public double getPower() {
  29.         return power;
  30.     }
  31.    
  32.     public void setCoef(double val) {
  33.         this.coef = val;
  34.     }
  35.  
  36.     public void setPower(double val) {
  37.         this.power = val;
  38.     }
  39.  
  40. }
  41.  
  42. abstract class Operations {
  43.     abstract public Polynom Add(Polynom obj);
  44.     abstract public Polynom Subtract(Polynom obj);
  45.     abstract public Polynom Multiply(Polynom obj);
  46. }
  47.  
  48.  
  49. class Polynom extends Operations {  
  50.     public Monom[] poly;
  51.  
  52.     public char variable;
  53.    
  54.     Polynom(Monom[] array, char var) {
  55.         poly = array.clone();
  56.         variable = var;
  57.         this.Simplify();
  58.     }
  59.  
  60.     public void Simplify() {
  61.         HashSet<Double> powers = new HashSet<Double>();
  62.         int k = 0;
  63.         Monom[] result = new Monom[this.poly.length];
  64.  
  65.         for (int i=0; i<this.poly.length; ++i) {
  66.           if (!powers.contains(this.poly[i].getPower()))
  67.             powers.add(this.poly[i].getPower());
  68.         }
  69.         double sum = 0;
  70.         for (Double j: powers) {
  71.           for (int i=0; i<this.poly.length; ++i) {
  72.             if ( j == this.poly[i].getPower()) {
  73.                 sum += this.poly[i].getCoef();
  74.             }
  75.           }
  76.  
  77.           if (sum != 0) {
  78.             result[k] = new Monom(sum,j);
  79.             k++;
  80.           }
  81.           sum = 0;
  82.         }
  83.  
  84.         Monom[] tmp = new Monom[k];
  85.  
  86.         for (int i=0; i<k; ++i) {
  87.           tmp[i] = result[i];
  88.         }
  89.  
  90.         poly = tmp;
  91.     }
  92.  
  93.     public Polynom Add(Polynom obj) {
  94.         double sum = 0;
  95.         int k = 0;
  96.         Monom[] result = new Monom[this.poly.length + obj.poly.length];
  97.         HashSet<Integer> indexes = new HashSet<Integer>();
  98.         HashSet<Integer> allIndexes = new HashSet<Integer>();
  99.  
  100.  
  101.         for (int i = 0; i<this.poly.length; ++i) {
  102.             for (int j = 0; j<obj.poly.length; ++j) {
  103.               if (this.poly[i].getPower() == obj.poly[j].getPower()){
  104.                 sum += obj.poly[j].getCoef();
  105.                 indexes.add(j);
  106.               }
  107.               allIndexes.add(j);
  108.             }
  109.             sum += this.poly[i].getCoef();
  110.             if (sum != 0) {
  111.               result[k] = new Monom(sum,this.poly[i].getPower());
  112.               k++;
  113.             }
  114.             sum = 0;
  115.         }
  116.  
  117.  
  118.         allIndexes.removeAll(indexes);
  119.  
  120.         for (Integer i : allIndexes) {
  121.             result[k] = new Monom(obj.poly[i].getCoef(),obj.poly[i].getPower());
  122.             k++;
  123.         }
  124.  
  125.         Monom[] tmp = new Monom[k];
  126.  
  127.         for (int i=0; i<k; ++i) {
  128.           tmp[i] = result[i];
  129.         }
  130.  
  131.         result = tmp;
  132.  
  133.         return new Polynom(result,'x');
  134.  
  135.  
  136.  
  137.     }
  138.  
  139.     public Polynom Subtract(Polynom obj) {
  140.         double sum = 0;
  141.         int k = 0;
  142.         Monom[] result = new Monom[this.poly.length + obj.poly.length];
  143.         HashSet<Integer> indexes = new HashSet<Integer>();
  144.         HashSet<Integer> allIndexes = new HashSet<Integer>();
  145.  
  146.  
  147.         for (int i = 0; i<this.poly.length; ++i) {
  148.             for (int j = 0; j<obj.poly.length; ++j) {
  149.               if (this.poly[i].getPower() == obj.poly[j].getPower()){
  150.                 sum -= obj.poly[j].getCoef();
  151.                 indexes.add(j);
  152.               }
  153.               allIndexes.add(j);
  154.             }
  155.             sum += this.poly[i].getCoef();
  156.             if (sum != 0) {
  157.               result[k] = new Monom(sum,this.poly[i].getPower());
  158.               k++;
  159.             }
  160.             sum = 0;
  161.         }
  162.  
  163.  
  164.         allIndexes.removeAll(indexes);
  165.  
  166.         for (Integer i : allIndexes) {
  167.             result[k] = new Monom(-obj.poly[i].getCoef(),obj.poly[i].getPower());
  168.             k++;
  169.         }
  170.  
  171.         Monom[] tmp = new Monom[k];
  172.  
  173.         for (int i=0; i<k; ++i) {
  174.           tmp[i] = result[i];
  175.         }
  176.  
  177.         result = tmp;
  178.  
  179.         return new Polynom(result,'x');
  180.     }
  181.  
  182.     public Polynom Multiply(Polynom obj) {
  183.  
  184.         Monom[] result = new Monom[this.poly.length * obj.poly.length];
  185.         int k = 0;
  186.         for (int i = 0; i<this.poly.length; ++i) {
  187.             for (int j = 0; j<obj.poly.length; ++j) {
  188.               result[k] = new Monom(this.poly[i].getCoef() * obj.poly[j].getCoef(),
  189.                                   this.poly[i].getPower() + obj.poly[j].getPower());
  190.               k++;
  191.             }
  192.         }
  193.  
  194.         Monom[] tmp = new Monom[k];
  195.  
  196.         for (int i=0; i<k; ++i) {
  197.           tmp[i] = result[i];
  198.         }
  199.  
  200.         result = tmp;
  201.  
  202.         return new Polynom(result,'x');
  203.  
  204.     }
  205.  
  206.     public String toString() {
  207.         StringBuilder str = new StringBuilder();
  208.         str.append("[ ");
  209.         for (int i = 0; i < poly.length; ++i) {
  210.             if (poly[i].getCoef() > 0) str.append("(+");
  211.             str.append(poly[i].getCoef());
  212.             str.append("*");
  213.             str.append(variable);
  214.             str.append("^");
  215.             str.append(poly[i].getPower());
  216.             str.append(")");
  217.         }
  218.  
  219.         str.append(" ]");
  220.         return str.toString();
  221.  
  222.     }
  223.  
  224. }
  225.  
  226. final class ComplexPolynom extends Polynom {
  227.  
  228.    ComplexPolynom(Monom[] array, char var) {
  229.        super(array,var);
  230.    }
  231.  
  232.   public String toString() {
  233.       StringBuilder str = new StringBuilder();
  234.       str.append("[ ");
  235.       for (int i = 0; i < poly.length; ++i) {
  236.           if (poly[i].getCoef() > 0) str.append("+");
  237.           str.append(poly[i].getCoef());
  238.           str.append("*");
  239.           str.append("(x+iy)");
  240.           str.append("^");
  241.           str.append(poly[i].getPower());
  242.       }
  243.  
  244.       str.append(" ]");
  245.       return str.toString();
  246.  
  247.   }
  248.    
  249.    public ComplexPolynom Add(Polynom obj) {
  250.         System.out.println("Add operation for ComplexPolynom");
  251.         return null;
  252.    }
  253.  
  254.    public ComplexPolynom Subtract(Polynom obj) {
  255.         System.out.println("Subtract operation for ComplexPolynom");
  256.         return null;
  257.    }
  258.  
  259.    public ComplexPolynom Multiply(Polynom obj) {
  260.         System.out.println("Subtract operation for ComplexPolynom");
  261.         return null;
  262.    }
  263.  
  264. }
  265.  
  266. class App extends JFrame  {
  267.    
  268.     private int pos = 20;
  269.     private Integer polySize = 2;
  270.     private JTextField[] txtFields1;
  271.     private JTextField[] txtFields2;
  272.     private JRadioButton simple;
  273.     private JRadioButton complex;
  274.  
  275.     private Polynom poly1;
  276.     private Polynom poly2;
  277.     private Polynom result;
  278.  
  279.     private JTextArea log;
  280.     private int logPos = 300;
  281.  
  282.  
  283.     App() {
  284.         setSize(800,600);
  285.         setLayout(null);
  286.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  287.  
  288.         JButton btnSet  = new JButton("Set Size");
  289.         btnSet.setBounds(20,20,200,50);
  290.         final JTextField txtSet = new JTextField();
  291.         txtSet.setBounds(20,80,200,50);
  292.  
  293.         simple = new JRadioButton("Polynom class");
  294.         simple.setSelected(true);
  295.         simple.setBounds(230,80,200,50);
  296.         complex = new JRadioButton("Complex class");
  297.         complex.setBounds(230,110,200,50);
  298.  
  299.         ButtonGroup group = new ButtonGroup();
  300.         group.add(simple);
  301.         group.add(complex);
  302.  
  303.         add(simple);
  304.         add(complex);
  305.        
  306.        
  307.        
  308.  
  309.  
  310.         ActionListener listenerSet = new ActionListener()
  311.         {
  312.             public void actionPerformed(ActionEvent e) {
  313.                
  314.                
  315.                try {
  316.                    polySize = new Integer(txtSet.getText());
  317.                } catch (NumberFormatException ev) {
  318.                    System.out.println(ev.getMessage());
  319.                }
  320.                
  321.                
  322.  
  323.                JLabel poly1 = new JLabel("Poly 1");
  324.                poly1.setBounds(20,160,100,30);
  325.                add(poly1);
  326.  
  327.                JLabel poly2 = new JLabel("Poly 2");
  328.                poly2.setBounds(20,210,100,30);
  329.                add(poly2);
  330.  
  331.                
  332.  
  333.                txtFields1 = new JTextField[2*polySize];
  334.                txtFields2 = new JTextField[2*polySize];
  335.  
  336.                for (int i = 0; i< 2*polySize; i+= 2) {
  337.  
  338.                    txtFields1[i] = new JTextField();
  339.                    txtFields1[i].setBounds(pos,190,30,30);
  340.                    txtFields1[i+1] = new JTextField();
  341.                    txtFields1[i+1].setBounds(pos+30,190,30,30);
  342.                    add(txtFields1[i]);
  343.                    add(txtFields1[i+1]);
  344.                    
  345.                    txtFields2[i] = new JTextField();
  346.                    txtFields2[i].setBounds(pos,240,30,30);
  347.                    txtFields2[i+1] = new JTextField();
  348.                    txtFields2[i+1].setBounds(pos+30,240,30,30);
  349.                    add(txtFields2[i]);
  350.                    add(txtFields2[i+1]);
  351.  
  352.                    pos += 80;
  353.                }
  354.                
  355.                
  356.             }
  357.  
  358.         };
  359.  
  360.         ActionListener listenerAdd = new ActionListener()
  361.         {
  362.             public void actionPerformed(ActionEvent e) {
  363.  
  364.               SetPoly();
  365.  
  366.               result = poly1.Add(poly2);
  367.  
  368.               //log = new JTextArea(result.toString());
  369.               //log.setBounds(20,logPos,400,100);
  370.               //logPos += 70;
  371.               //add(log);
  372.               if (result != null)
  373.               System.out.println(result.toString());
  374.  
  375.             }
  376.         };
  377.  
  378.         ActionListener listenerSubtract = new ActionListener()
  379.         {
  380.             public void actionPerformed(ActionEvent e) {
  381.                 SetPoly();
  382.  
  383.                 result = poly1.Subtract(poly2);
  384.                 if (result != null)
  385.                 System.out.println(result.toString());
  386.                 //log = new JTextArea(result.toString());
  387.                 //log.setBounds(20,logPos,400,100);
  388.                 //logPos += 70;
  389.                 //add(log);
  390.             }
  391.         };
  392.  
  393.         ActionListener listenerMultiply =  new ActionListener()
  394.         {
  395.             public void actionPerformed(ActionEvent e) {
  396.                 SetPoly();
  397.  
  398.                 result = poly1.Multiply(poly2);
  399.                 if (result != null)
  400.                 System.out.println(result.toString());
  401.             }
  402.         };
  403.  
  404.         ActionListener listenerDivide = new ActionListener()
  405.         {
  406.             public void actionPerformed(ActionEvent e) {
  407.                 SetPoly();
  408.             }
  409.         };
  410.        
  411.  
  412.        
  413.        
  414.         btnSet.addActionListener(listenerSet);
  415.        
  416.         JButton addButton = new JButton("+");
  417.         addButton.setBounds(250,20,50,50);
  418.         addButton.addActionListener(listenerAdd);
  419.  
  420.         JButton substractButton = new JButton("-");
  421.         substractButton.setBounds(300,20,50,50);
  422.         substractButton.addActionListener(listenerSubtract);
  423.  
  424.         JButton multiplyButton = new JButton("*");
  425.         multiplyButton.setBounds(350,20,50,50);
  426.         multiplyButton.addActionListener(listenerMultiply);
  427.  
  428.         JButton divideButton = new JButton("/");
  429.         divideButton.setBounds(400,20,50,50);
  430.         divideButton.addActionListener(listenerDivide);
  431.  
  432.         add(btnSet);
  433.         add(addButton);
  434.         add(substractButton);
  435.         add(multiplyButton);
  436.         add(divideButton);
  437.         add(txtSet);
  438.  
  439.  
  440.         setVisible(true);
  441.     }
  442.  
  443.     private void SetPoly() {
  444.       Monom[] monoms1 = createMonoms(txtFields1);
  445.       Monom[] monoms2 = createMonoms(txtFields2);
  446.              
  447.       if (simple.isSelected()) {
  448.           poly1 = new Polynom(monoms1,'x');
  449.           poly2 = new Polynom(monoms2,'x');
  450.       }
  451.       else {
  452.           poly1 = new ComplexPolynom(monoms1,'z');
  453.           poly2 = new ComplexPolynom(monoms2,'z');
  454.       }
  455.  
  456.     }
  457.  
  458.     private Monom[] createMonoms(JTextField[] t) {
  459.           Monom[] monoms = new Monom[polySize];
  460.           int k = 0;
  461.  
  462.           for (int i=0; i<2*polySize; i+= 2) {
  463.               monoms[k++] = new Monom(Double.parseDouble(t[i].getText()),
  464.                                       Double.parseDouble(t[i+1].getText()));
  465.           }
  466.           return monoms;
  467.     }
  468.  
  469.  
  470. }
  471.  
  472. public class Lab5 {
  473.     public static void main(String[] args) {
  474.         new App();
  475.     }
  476. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement