Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * realizarea poliformizmului pentru clasele date cu crearea metodelor
- * conform specificului claselor.
- * Polynom <- ComplexPolynom
- *
- *
- *
- */
- import java.util.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.Color;
- class Monom {
- private double coef;
- private double power;
- public Monom( double coef, double power) {
- this.coef = coef;
- this.power = power;
- }
- public double getCoef() {
- return coef;
- }
- public double getPower() {
- return power;
- }
- public void setCoef(double val) {
- this.coef = val;
- }
- public void setPower(double val) {
- this.power = val;
- }
- }
- abstract class Operations {
- abstract public Polynom Add(Polynom obj);
- abstract public Polynom Subtract(Polynom obj);
- abstract public Polynom Multiply(Polynom obj);
- }
- class Polynom extends Operations {
- public Monom[] poly;
- public char variable;
- Polynom(Monom[] array, char var) {
- poly = array.clone();
- variable = var;
- this.Simplify();
- }
- public void Simplify() {
- HashSet<Double> powers = new HashSet<Double>();
- int k = 0;
- Monom[] result = new Monom[this.poly.length];
- for (int i=0; i<this.poly.length; ++i) {
- if (!powers.contains(this.poly[i].getPower()))
- powers.add(this.poly[i].getPower());
- }
- double sum = 0;
- for (Double j: powers) {
- for (int i=0; i<this.poly.length; ++i) {
- if ( j == this.poly[i].getPower()) {
- sum += this.poly[i].getCoef();
- }
- }
- if (sum != 0) {
- result[k] = new Monom(sum,j);
- k++;
- }
- sum = 0;
- }
- Monom[] tmp = new Monom[k];
- for (int i=0; i<k; ++i) {
- tmp[i] = result[i];
- }
- poly = tmp;
- }
- public Polynom Add(Polynom obj) {
- double sum = 0;
- int k = 0;
- Monom[] result = new Monom[this.poly.length + obj.poly.length];
- HashSet<Integer> indexes = new HashSet<Integer>();
- HashSet<Integer> allIndexes = new HashSet<Integer>();
- for (int i = 0; i<this.poly.length; ++i) {
- for (int j = 0; j<obj.poly.length; ++j) {
- if (this.poly[i].getPower() == obj.poly[j].getPower()){
- sum += obj.poly[j].getCoef();
- indexes.add(j);
- }
- allIndexes.add(j);
- }
- sum += this.poly[i].getCoef();
- if (sum != 0) {
- result[k] = new Monom(sum,this.poly[i].getPower());
- k++;
- }
- sum = 0;
- }
- allIndexes.removeAll(indexes);
- for (Integer i : allIndexes) {
- result[k] = new Monom(obj.poly[i].getCoef(),obj.poly[i].getPower());
- k++;
- }
- Monom[] tmp = new Monom[k];
- for (int i=0; i<k; ++i) {
- tmp[i] = result[i];
- }
- result = tmp;
- return new Polynom(result,'x');
- }
- public Polynom Subtract(Polynom obj) {
- double sum = 0;
- int k = 0;
- Monom[] result = new Monom[this.poly.length + obj.poly.length];
- HashSet<Integer> indexes = new HashSet<Integer>();
- HashSet<Integer> allIndexes = new HashSet<Integer>();
- for (int i = 0; i<this.poly.length; ++i) {
- for (int j = 0; j<obj.poly.length; ++j) {
- if (this.poly[i].getPower() == obj.poly[j].getPower()){
- sum -= obj.poly[j].getCoef();
- indexes.add(j);
- }
- allIndexes.add(j);
- }
- sum += this.poly[i].getCoef();
- if (sum != 0) {
- result[k] = new Monom(sum,this.poly[i].getPower());
- k++;
- }
- sum = 0;
- }
- allIndexes.removeAll(indexes);
- for (Integer i : allIndexes) {
- result[k] = new Monom(-obj.poly[i].getCoef(),obj.poly[i].getPower());
- k++;
- }
- Monom[] tmp = new Monom[k];
- for (int i=0; i<k; ++i) {
- tmp[i] = result[i];
- }
- result = tmp;
- return new Polynom(result,'x');
- }
- public Polynom Multiply(Polynom obj) {
- Monom[] result = new Monom[this.poly.length * obj.poly.length];
- int k = 0;
- for (int i = 0; i<this.poly.length; ++i) {
- for (int j = 0; j<obj.poly.length; ++j) {
- result[k] = new Monom(this.poly[i].getCoef() * obj.poly[j].getCoef(),
- this.poly[i].getPower() + obj.poly[j].getPower());
- k++;
- }
- }
- Monom[] tmp = new Monom[k];
- for (int i=0; i<k; ++i) {
- tmp[i] = result[i];
- }
- result = tmp;
- return new Polynom(result,'x');
- }
- public String toString() {
- StringBuilder str = new StringBuilder();
- str.append("[ ");
- for (int i = 0; i < poly.length; ++i) {
- if (poly[i].getCoef() > 0) str.append("(+");
- str.append(poly[i].getCoef());
- str.append("*");
- str.append(variable);
- str.append("^");
- str.append(poly[i].getPower());
- str.append(")");
- }
- str.append(" ]");
- return str.toString();
- }
- }
- final class ComplexPolynom extends Polynom {
- ComplexPolynom(Monom[] array, char var) {
- super(array,var);
- }
- public String toString() {
- StringBuilder str = new StringBuilder();
- str.append("[ ");
- for (int i = 0; i < poly.length; ++i) {
- if (poly[i].getCoef() > 0) str.append("+");
- str.append(poly[i].getCoef());
- str.append("*");
- str.append("(x+iy)");
- str.append("^");
- str.append(poly[i].getPower());
- }
- str.append(" ]");
- return str.toString();
- }
- public ComplexPolynom Add(Polynom obj) {
- System.out.println("Add operation for ComplexPolynom");
- return null;
- }
- public ComplexPolynom Subtract(Polynom obj) {
- System.out.println("Subtract operation for ComplexPolynom");
- return null;
- }
- public ComplexPolynom Multiply(Polynom obj) {
- System.out.println("Subtract operation for ComplexPolynom");
- return null;
- }
- }
- class App extends JFrame {
- private int pos = 20;
- private Integer polySize = 2;
- private JTextField[] txtFields1;
- private JTextField[] txtFields2;
- private JRadioButton simple;
- private JRadioButton complex;
- private Polynom poly1;
- private Polynom poly2;
- private Polynom result;
- private JTextArea log;
- private int logPos = 300;
- App() {
- setSize(800,600);
- setLayout(null);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- JButton btnSet = new JButton("Set Size");
- btnSet.setBounds(20,20,200,50);
- final JTextField txtSet = new JTextField();
- txtSet.setBounds(20,80,200,50);
- simple = new JRadioButton("Polynom class");
- simple.setSelected(true);
- simple.setBounds(230,80,200,50);
- complex = new JRadioButton("Complex class");
- complex.setBounds(230,110,200,50);
- ButtonGroup group = new ButtonGroup();
- group.add(simple);
- group.add(complex);
- add(simple);
- add(complex);
- ActionListener listenerSet = new ActionListener()
- {
- public void actionPerformed(ActionEvent e) {
- try {
- polySize = new Integer(txtSet.getText());
- } catch (NumberFormatException ev) {
- System.out.println(ev.getMessage());
- }
- JLabel poly1 = new JLabel("Poly 1");
- poly1.setBounds(20,160,100,30);
- add(poly1);
- JLabel poly2 = new JLabel("Poly 2");
- poly2.setBounds(20,210,100,30);
- add(poly2);
- txtFields1 = new JTextField[2*polySize];
- txtFields2 = new JTextField[2*polySize];
- for (int i = 0; i< 2*polySize; i+= 2) {
- txtFields1[i] = new JTextField();
- txtFields1[i].setBounds(pos,190,30,30);
- txtFields1[i+1] = new JTextField();
- txtFields1[i+1].setBounds(pos+30,190,30,30);
- add(txtFields1[i]);
- add(txtFields1[i+1]);
- txtFields2[i] = new JTextField();
- txtFields2[i].setBounds(pos,240,30,30);
- txtFields2[i+1] = new JTextField();
- txtFields2[i+1].setBounds(pos+30,240,30,30);
- add(txtFields2[i]);
- add(txtFields2[i+1]);
- pos += 80;
- }
- }
- };
- ActionListener listenerAdd = new ActionListener()
- {
- public void actionPerformed(ActionEvent e) {
- SetPoly();
- result = poly1.Add(poly2);
- //log = new JTextArea(result.toString());
- //log.setBounds(20,logPos,400,100);
- //logPos += 70;
- //add(log);
- if (result != null)
- System.out.println(result.toString());
- }
- };
- ActionListener listenerSubtract = new ActionListener()
- {
- public void actionPerformed(ActionEvent e) {
- SetPoly();
- result = poly1.Subtract(poly2);
- if (result != null)
- System.out.println(result.toString());
- //log = new JTextArea(result.toString());
- //log.setBounds(20,logPos,400,100);
- //logPos += 70;
- //add(log);
- }
- };
- ActionListener listenerMultiply = new ActionListener()
- {
- public void actionPerformed(ActionEvent e) {
- SetPoly();
- result = poly1.Multiply(poly2);
- if (result != null)
- System.out.println(result.toString());
- }
- };
- ActionListener listenerDivide = new ActionListener()
- {
- public void actionPerformed(ActionEvent e) {
- SetPoly();
- }
- };
- btnSet.addActionListener(listenerSet);
- JButton addButton = new JButton("+");
- addButton.setBounds(250,20,50,50);
- addButton.addActionListener(listenerAdd);
- JButton substractButton = new JButton("-");
- substractButton.setBounds(300,20,50,50);
- substractButton.addActionListener(listenerSubtract);
- JButton multiplyButton = new JButton("*");
- multiplyButton.setBounds(350,20,50,50);
- multiplyButton.addActionListener(listenerMultiply);
- JButton divideButton = new JButton("/");
- divideButton.setBounds(400,20,50,50);
- divideButton.addActionListener(listenerDivide);
- add(btnSet);
- add(addButton);
- add(substractButton);
- add(multiplyButton);
- add(divideButton);
- add(txtSet);
- setVisible(true);
- }
- private void SetPoly() {
- Monom[] monoms1 = createMonoms(txtFields1);
- Monom[] monoms2 = createMonoms(txtFields2);
- if (simple.isSelected()) {
- poly1 = new Polynom(monoms1,'x');
- poly2 = new Polynom(monoms2,'x');
- }
- else {
- poly1 = new ComplexPolynom(monoms1,'z');
- poly2 = new ComplexPolynom(monoms2,'z');
- }
- }
- private Monom[] createMonoms(JTextField[] t) {
- Monom[] monoms = new Monom[polySize];
- int k = 0;
- for (int i=0; i<2*polySize; i+= 2) {
- monoms[k++] = new Monom(Double.parseDouble(t[i].getText()),
- Double.parseDouble(t[i+1].getText()));
- }
- return monoms;
- }
- }
- public class Lab5 {
- public static void main(String[] args) {
- new App();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement