Смекни!
smekni.com

Створення калькулятора множення (стр. 2 из 2)

x1 = x1 + "3";

jTextField1.setText(x1);

}

public void jButton4_actionPerformed(ActionEvent e) {

x1 = x1 + "4";

jTextField1.setText(x1);

}

public void jButton5_actionPerformed(ActionEvent e) {

x1 = x1 + "5";

jTextField1.setText(x1);

}

public void jButton6_actionPerformed(ActionEvent e) {

x1 = x1 + "6";

jTextField1.setText(x1);

}

public void jButton7_actionPerformed(ActionEvent e) {

x1 = x1 + "7";

jTextField1.setText(x1);

}

public void jButton8_actionPerformed(ActionEvent e) {

x1 = x1 + "8";

jTextField1.setText(x1);

}

public void jButton9_actionPerformed(ActionEvent e) {

x1 = x1 + "9";

jTextField1.setText(x1);

}

public void jButton10_actionPerformed(ActionEvent e) {

x1 = x1 + ".";

jTextField1.setText(x1);

}

public void jButton11_actionPerformed(ActionEvent e) {

x1 = x1 + "0";

jTextField1.setText(x1);

}

public void jButton12_actionPerformed(ActionEvent e) {

if (Double.parseDouble(x1)>0)

{

x1 = "-" + x1;

jTextField1.setText(x1);

}

else

{

x1 = ""+(Double.parseDouble(x1)*(-1))+"";

jTextField1.setText(x1);

}

}

public void jButton13_actionPerformed(ActionEvent e) {

x2 = x1;

x1 = "";

}

public void jButton14_actionPerformed(ActionEvent e) {

x3 = x1;

d1 = roundDouble(Double.parseDouble(x2), 10);

d2 = roundDouble(Double.parseDouble(x3), 10);

String str1 = BOp.Double_to_Binary(b1);

String str2 = BOp.Double_to_Binary(b2);

String ress = BOp.Bin_Multiplication(str1,str2);

res = BOp.Binary_to_Double(ress);

res = roundDouble(d1*d2, 10);

str = ""+res+"";

jTextField1.setText(str);

x1 = jTextField1.getText();

}

public void jButton15_actionPerformed(ActionEvent e) {

jTextField1.setText("0");

x1 = "";

x2 = "";

x3 = "";

str = "";

d1 = 0;

d2 = 0;

}

class Rozr_BinCalc_Multiplication_jButton1_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton1_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton1_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton2_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton2_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton2_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton3_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton3_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton3_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton4_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton4_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton4_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton5_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton5_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton5_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton6_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton6_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton6_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton7_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton7_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton7_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton8_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton8_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton8_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton9_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton9_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton9_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton10_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton10_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton10_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton11_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton11_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton11_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton12_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton12_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton12_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton13_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton13_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton13_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton14_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton14_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton14_actionPerformed(e);

}

}

class Rozr_BinCalc_Multiplication_jButton15_actionAdapter implements ActionListener {

private Rozr_BinCalc_Multiplication adaptee;

Rozr_BinCalc_Multiplication_jButton15_actionAdapter(Rozr_BinCalc_Multiplication adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton15_actionPerformed(e);

}

}

class Binary_Operations

{

private double d2;

public String Double_to_Binary(double d_val){

String res = "", bin_fr = "";

double fract, res_fr;

long whole_part;

if (d_val >= 0)

{

d2 = Math.floor(d_val);

whole_part = Math.round(d2);

}

else

{

d2 = Math.floor(d_val)+1;

whole_part = Math.round(d2);

}

fract = Math.abs(roundDouble(d_val - whole_part, 10));

res_fr = fract;

if (res_fr == 0d) return ""+(Long.toBinaryString(whole_part))+".0";

while (roundDouble(res_fr,10) != 1d)

{

res_fr = res_fr * 2;

if (res_fr > 1)

{

bin_fr = bin_fr + "1";

res_fr--;

}

else

if (roundDouble(res_fr,10) != 1d)

bin_fr = bin_fr + "0";

}

bin_fr = bin_fr + "1";

res = ""+(Long.toBinaryString(whole_part))+"."+bin_fr;

return res;

}

public double Binary_to_Double(String res_s){

double d_res = 0, d_val, fract;

long whole_part;

String temp = "";

int fr_count = 0;

int [][] table;

d_val = Double.parseDouble(res_s);

if (d_val == 0d)

return d_val;

if (d_val > 0)

{

d2 = Math.floor(d_val);

whole_part = Math.round(d2);

table = new int[2][res_s.length()-1];

}

else

{

d2 = Math.floor(d_val)+1;

whole_part = Math.round(d2);

table = new int[2][res_s.length()-2];

}

fract = Math.abs(roundDouble(d_val - whole_part, 10));

temp = ""+fract+"";

fr_count = temp.length()-2;

int len = res_s.length() - 1;

if (d_val < 0) len = res_s.length() - 2;

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

table[1][i] = i - fr_count;

d_val = Math.abs(d_val);

res_s = ""+d_val+"";

int pos = res_s.length()-1;

for (int i = 0; i < len; i++, pos--)

{

if (res_s.charAt(pos) == '.')

pos--;

table[0][i] = Integer.parseInt(""+res_s.charAt(pos)+"");

}

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

d_res = d_res + table[0][i]*Math.pow(2.0,table[1][i]);

return d_res;

}

public String Bin_Multiplication(String str1, String str2)

{

String res_str = "", res_s = "";

String res1=str1, res2=str2;

int pos1=0, pos2=0;

int b=0;

for (int i=0; i<str1.length(); i++)

{

if (str1.charAt(i) == '.')

{

pos1 = i;

break;

}

}

for (int i=0; i<str2.length(); i++)

{

if (str2.charAt(i) == '.')

{

pos2 = i;

break;

}

}

if (pos1>pos2)

{

for (int i = 0; i < pos1 - pos2; i++)

res1 = '0' + res1;

}

else

if (pos2>pos1)

{

for (int i = 0; i < pos2 - pos1; i++)

res2 = '0' + res2;

}

if (res1.length()>res2.length())

{

for (int i = 0; i < res1.length() - res2.length(); i++)

res2 = res2 + '0';

}

else

if (res2.length()>res1.length())

{

for (int i = 0; i < res2.length() - res1.length(); i++)

res1 = res1 + '0';

}

for (int i=0; i<res1.length(); i++)

{

if (res1.charAt(i) == '.')

{

pos1 = i;

break;

}

}

str1 = "";

pos1 = Math.abs(res1.length()-pos1);

str2 = "";

for (int i=0; i<res1.length(); i++)

{

if (res1.charAt(i) != '.') str1 = str1+res1.charAt(i);

if (res2.charAt(i) != '.') str2 = str2+res2.charAt(i);

}

res_s = " ";

pos2 = 0;

for (int i=0; i<res1.length(); i++)

{

if (res2.charAt(i) == '1')

{

for (int j=0; j<res1.length(); j++)

{

res_s.replace(res_s.charAt(j+pos2),res1.charAt(j));

}

pos2++;

}

}

str1 = "0";

str1 = str1 +'.';

for (int i=pos1; i<res_s.length(); i++)

{

str1 = str1 + res_s.charAt(i);

}

return str1;

}

public double roundDouble(double val, int places) {

long factor = (long)Math.pow(10,places);

val = val * factor;

long tmp = Math.round(val);

return (double)tmp / factor;

}

}

}


Висновок

В моїй розрахунковій роботі було представлено приклад калькулятора, що перемножує числа. Було представлено побудову діаграм UML, розглянуто деякі типи діаграм та їх значення, структуру, на зворот ньому процесі до проектування було продемонстровано переваги попереднього проектування розроблюваного даного програмного продукту.


СПИСОК ВИКОРИСТАНОЇ ЛІТЕРАТУРИ

1. Программирование на Java 2 (2-е издание) ФОЛИО 2003

2. 11-12 лекції.