Saturday, March 28, 2009
CASE 2
Download Case 2
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.io.*;
import java.text.DecimalFormat;
import javax.swing.*;
import java.lang.*;
import java.util.*;
public class Conversion extends JFrame {
String text = "";
boolean enter = true;
JFrame h = new JFrame();
JPanel p6 = new JPanel();
JPanel p1 = new JPanel();
JLabel fnumber = new JLabel(" Number:");
JTextField num = new JTextField("", 20);
JLabel binary = new JLabel(" Conversion:");
JLabel bin = new JLabel("");
JButton add1 = new JButton("Binary");
JButton minus = new JButton("Decimal");
JButton multiply = new JButton("Octal");
JButton divide = new JButton("Hexadecimal");
public static void main(String args[]) {
Conversion Layout = new Conversion();
}
public Conversion( )
{
p1.setLayout(new GridLayout(2, 1));
p1.add(fnumber);
p1.add(num);
p1.add(binary);
p1.add(bin);
p1.add(add1);
add1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
solution();
}
}
);
p1.add(minus);
minus.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text = num.getText();
String p = num.getText();
int number;
for(int i = 0; i < let =" p.charAt(i);" enter =" false;" enter ="=" number =" hexdecsolution();" enter =" true;" numbers =" Integer.parseInt(num.getText());" binaryconvert = "" i =" 0;">= array[i])
{
numbers = numbers - array[i];
binaryconvert = binaryconvert + "1";
}
else
binaryconvert = binaryconvert + "0";
}
bin.setText(binaryconvert);
}
public void decimalsolution()
{
String numbers = num.getText();
int arrays[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16383 };
int binaryconvert = 0;
int len = numbers.length()-1;
num.setText(text);
for(int i = len, j = 0; i >= 0; i--)
{
if(numbers.charAt(j) == '1')
binaryconvert = arrays[i] + binaryconvert ;
j++;
}
bin.setText(String.valueOf(binaryconvert));
}
public void octalsolution()
{
int numbers = Integer.parseInt(num.getText());
int octal = 0, t = numbers, a, b;
String concat = "";
while(numbers!=0)
{
a = numbers / 8;
b = a * 8;
t = numbers - b;
numbers = a;
concat = String.valueOf(t) + concat;
}
bin.setText(concat);
}
public void hexasolution()
{
int numbers = Integer.parseInt(num.getText());
int octal = 0, t = numbers, a, b;
String concat = "";
while(numbers!=0)
{
a = numbers / 16;
b = a * 16;
t = numbers - b;
numbers = a;
if(t == 10)
concat = "A" + concat;
else if(t == 11)
concat = "B" + concat;
else if(t == 12)
concat = "C" + concat;
else if(t == 13)
concat = "D" + concat;
else if(t == 14)
concat = "E" + concat;
else if(t == 15)
concat = "F" + concat;
else
concat = String.valueOf(t) + concat;
}
bin.setText(concat);
}
public int hexdecsolution()
{
String concat = "", get = num.getText();
int num;
for(int i = 0; i < get.length(); i++)
{
if(get.charAt(i) == 'A')
concat = concat + "10";
else if(get.charAt(i) == 'B')
concat = concat + "11";
else if(get.charAt(i) == 'C')
concat = concat + "12";
else if(get.charAt(i) == 'D')
concat = concat + "13";
else if(get.charAt(i) == 'E')
concat = concat + "14";
else if(get.charAt(i) == 'F')
concat = concat + "15";
else
concat = concat + String.valueOf(get.charAt(i));
}
return Integer.valueOf(concat);
}
}
decimal to binary
Private Sub Command1_Click()
Dim i As Integer
Dim res As String
Dim Done As Boolean
Text5.Text = Val(Text1.Text) + Val(Text3.Text)
Temp = Text1.Text
Temp1 = Text1.Text
Do Until Temp \ 2 = 1
Temp = Temp \ 2
i = i + 1
Loop
If res = "" Then
For j = i + 1 To 0 Step -1
If Temp1 - 2 ^ j > 0 And Done = False Then
Temp1 = Temp1 - 2 ^ j
res = res & "1"
ElseIf Temp1 - 2 ^ j <> 0 And Done = False Then
res = res & "0"
ElseIf Temp1 - 2 ^ j = 0 And Done = False Then
res = res & "1"
Done = True
ElseIf Done = True Then
res = res & "0"
End If
Next
Text2.Text = res
End If
Dim Temp3, Temp4 As Long
Dim i34 As Integer
Dim res34 As String
Dim Done34 As Boolean
Temp3 = Text3.Text
Temp4 = Text3.Text
Do Until Temp3 \ 2 = 1
Temp3 = Temp3 \ 2
i34 = i34 + 1
Loop
If res34 = "" Then
For j = i34 + 1 To 0 Step -1
If Temp4 - 2 ^ j > 0 And Done34 = False Then
Temp4 = Temp4 - 2 ^ j
res34 = res34 & "1"
ElseIf Temp4 - 2 ^ j <> 0 And Done34 = False Then
res34 = res34 & "0"
ElseIf Temp4 - 2 ^ j = 0 And Done34 = False Then
res34 = res34 & "1"
Done34 = True
ElseIf Done34 = True Then
res34 = res34 & "0"
End If
Next
Text4.Text = res34
End If
Dim Temp5, Temp6 As Long
Dim i56 As Integer
Dim res56 As String
Dim Done56 As Boolean
Temp5 = Text5.Text
Temp6 = Text5.Text
Do Until Temp5 \ 2 = 1
Temp5 = Temp5 \ 2
i56 = i56 + 1
Loop
If res56 = "" Then
For j = i56 + 1 To 0 Step -1
If Temp6 - 2 ^ j > 0 And Done56 = False Then
Temp6 = Temp6 - 2 ^ j
res56 = res56 & "1"
ElseIf Temp6 - 2 ^ j <> 0 And Done56 = False Then
res56 = res56 & "0"
ElseIf Temp6 - 2 ^ j = 0 And Done56 = False Then
res56 = res56 & "1"
Done56 = True
ElseIf Done56 = True Then
res56 = res56 & "0"
End If
Next
Text6.Text = res56
End If
End Sub
Private Sub Command2_Click()
Dim Temp, Temp1 As Long
Dim i As Integer
Dim res As String
Dim Done As Boolean
Text5.Text = Val(Text1.Text) - Val(Text3.Text)
Temp = Text1.Text
Temp1 = Text1.Text
Do Until Temp \ 2 = 1
Temp = Temp \ 2
i = i + 1
Loop
If res = "" Then
For j = i + 1 To 0 Step -1
If Temp1 - 2 ^ j > 0 And Done = False Then
Temp1 = Temp1 - 2 ^ j
res = res & "1"
ElseIf Temp1 - 2 ^ j <> 0 And Done = False Then
res = res & "0"
ElseIf Temp1 - 2 ^ j = 0 And Done = False Then
res = res & "1"
Done = True
ElseIf Done = True Then
res = res & "0"
End If
Next
Text2.Text = res
End If
Dim Temp3, Temp4 As Long
Dim i34 As Integer
Dim res34 As String
Dim Done34 As Boolean
Temp3 = Text3.Text
Temp4 = Text3.Text
Do Until Temp3 \ 2 = 1
Temp3 = Temp3 \ 2
i34 = i34 + 1
Loop
If res34 = "" Then
For j = i34 + 1 To 0 Step -1
If Temp4 - 2 ^ j > 0 And Done34 = False Then
Temp4 = Temp4 - 2 ^ j
res34 = res34 & "1"
ElseIf Temp4 - 2 ^ j <> 0 And Done34 = False Then
res34 = res34 & "0"
ElseIf Temp4 - 2 ^ j = 0 And Done34 = False Then
res34 = res34 & "1"
Done34 = True
ElseIf Done34 = True Then
res34 = res34 & "0"
End If
Next
Text4.Text = res34
End If
Dim Temp5, Temp6 As Long
Dim i56 As Integer
Dim res56 As String
Dim Done56 As Boolean
Temp5 = Text5.Text
Temp6 = Text5.Text
Do Until Temp5 \ 2 = 1
Temp5 = Temp5 \ 2
i56 = i56 + 1
Loop
If res56 = "" Then
For j = i56 + 1 To 0 Step -1
If Temp6 - 2 ^ j > 0 And Done56 = False Then
Temp6 = Temp6 - 2 ^ j
res56 = res56 & "1"
ElseIf Temp6 - 2 ^ j <> 0 And Done56 = False Then
res56 = res56 & "0"
ElseIf Temp6 - 2 ^ j = 0 And Done56 = False Then
res56 = res56 & "1"
Done56 = True
ElseIf Done56 = True Then
res56 = res56 & "0"
End If
Next
Text6.Text = res56
End If
End Sub
Private Sub Command3_Click()
Dim Temp, Temp1 As Long
Dim i As Integer
Dim res As String
Dim Done As Boolean
Text5.Text = Val(Text1.Text) * Val(Text3.Text)
Temp = Text1.Text
Temp1 = Text1.Text
Do Until Temp \ 2 = 1
Temp = Temp \ 2
i = i + 1
Loop
If res = "" Then
For j = i + 1 To 0 Step -1
If Temp1 - 2 ^ j > 0 And Done = False Then
Temp1 = Temp1 - 2 ^ j
res = res & "1"
ElseIf Temp1 - 2 ^ j <> 0 And Done = False Then
res = res & "0"
ElseIf Temp1 - 2 ^ j = 0 And Done = False Then
res = res & "1"
Done = True
ElseIf Done = True Then
res = res & "0"
End If
Next
Text2.Text = res
End If
Dim Temp3, Temp4 As Long
Dim i34 As Integer
Dim res34 As String
Dim Done34 As Boolean
Temp3 = Text3.Text
Temp4 = Text3.Text
Do Until Temp3 \ 2 = 1
Temp3 = Temp3 \ 2
i34 = i34 + 1
Loop
If res34 = "" Then
For j = i34 + 1 To 0 Step -1
If Temp4 - 2 ^ j > 0 And Done34 = False Then
Temp4 = Temp4 - 2 ^ j
res34 = res34 & "1"
ElseIf Temp4 - 2 ^ j <> 0 And Done34 = False Then
res34 = res34 & "0"
ElseIf Temp4 - 2 ^ j = 0 And Done34 = False Then
res34 = res34 & "1"
Done34 = True
ElseIf Done34 = True Then
res34 = res34 & "0"
End If
Next
Text4.Text = res34
End If
Dim Temp5, Temp6 As Long
Dim i56 As Integer
Dim res56 As String
Dim Done56 As Boolean
Temp5 = Text5.Text
Temp6 = Text5.Text
Do Until Temp5 \ 2 = 1
Temp5 = Temp5 \ 2
i56 = i56 + 1
Loop
If res56 = "" Then
For j = i56 + 1 To 0 Step -1
If Temp6 - 2 ^ j > 0 And Done56 = False Then
Temp6 = Temp6 - 2 ^ j
res56 = res56 & "1"
ElseIf Temp6 - 2 ^ j <> 0 And Done56 = False Then
res56 = res56 & "0"
ElseIf Temp6 - 2 ^ j = 0 And Done56 = False Then
res56 = res56 & "1"
Done56 = True
ElseIf Done56 = True Then
res56 = res56 & "0"
End If
Next
Text6.Text = res56
End If
End Sub
Private Sub Command4_Click()
Dim Temp, Temp1 As Long
Dim i As Integer
Dim res As String
Dim Done As Boolean
Text5.Text = Val(Text1.Text) / Val(Text3.Text)
Temp = Text1.Text
Temp1 = Text1.Text
Do Until Temp \ 2 = 1
Temp = Temp \ 2
i = i + 1
Loop
If res = "" Then
For j = i + 1 To 0 Step -1
If Temp1 - 2 ^ j > 0 And Done = False Then
Temp1 = Temp1 - 2 ^ j
res = res & "1"
ElseIf Temp1 - 2 ^ j <> 0 And Done = False Then
res = res & "0"
ElseIf Temp1 - 2 ^ j = 0 And Done = False Then
res = res & "1"
Done = True
ElseIf Done = True Then
res = res & "0"
End If
Next
Text2.Text = res
End If
Dim Temp3, Temp4 As Long
Dim i34 As Integer
Dim res34 As String
Dim Done34 As Boolean
Temp3 = Text3.Text
Temp4 = Text3.Text
Do Until Temp3 \ 2 = 1
Temp3 = Temp3 \ 2
i34 = i34 + 1
Loop
If res34 = "" Then
For j = i34 + 1 To 0 Step -1
If Temp4 - 2 ^ j > 0 And Done34 = False Then
Temp4 = Temp4 - 2 ^ j
res34 = res34 & "1"
ElseIf Temp4 - 2 ^ j <> 0 And Done34 = False Then
res34 = res34 & "0"
ElseIf Temp4 - 2 ^ j = 0 And Done34 = False Then
res34 = res34 & "1"
Done34 = True
ElseIf Done34 = True Then
res34 = res34 & "0"
End If
Next
Text4.Text = res34
End If
Dim Temp5, Temp6 As Long
Dim i56 As Integer
Dim res56 As String
Dim Done56 As Boolean
Temp5 = Text5.Text
Temp6 = Text5.Text
Do Until Temp5 \ 2 = 1
Temp5 = Temp5 \ 2
i56 = i56 + 1
Loop
If res56 = "" Then
For j = i56 + 1 To 0 Step -1
If Temp6 - 2 ^ j > 0 And Done56 = False Then
Temp6 = Temp6 - 2 ^ j
res56 = res56 & "1"
ElseIf Temp6 - 2 ^ j <> 0 And Done56 = False Then
res56 = res56 & "0"
ElseIf Temp6 - 2 ^ j = 0 And Done56 = False Then
res56 = res56 & "1"
Done56 = True
ElseIf Done56 = True Then
res56 = res56 & "0"
End If
Next
Text6.Text = res56
End If
End Sub
Private Sub Command5_Click()
End
End Sub
'Private Sub Text1_KeyPress(KeyAscii As Integer)
'If (KeyAscii >= 48 And KeyAscii <= 57) Then
' ElseIf (KeyAscii = vbKeyReturn Or KeyAscii = vbKeyDelete) Then
' KeyAscii = 0
'ElseIf KeyAscii = vbKeyBack Then
'Else
'KeyAscii = 0
'MsgBox "You can only input numbers.", 64, "Error"
'Exit Sub
'End If
'End Sub
Subscribe to:
Posts (Atom)