Professional Documents
Culture Documents
Cqc1otmc Lab Manual
Cqc1otmc Lab Manual
THIM TRUST'S
THEEM COLLEGE OF ENGINEERING
DEPARTMENT OF INFORMATION TECHNOLOGY ENGINEERING
Mission
To commit ourselves for the high Standards of Technical and Professional Education
with student centered teaching learning process to nurture responsible technocrats
and professionals.
Vision
To become a center of excellence in the Information technology discipline and to
create technically capable and intellectual IT professionals.
Mission
1. To nurture an effective teaching-learning process to provide in-depth knowledge of
principles and its applications pertaining to Information technology and
interdisciplinary areas leading to new technology.
CERTIFICATE
Date: Date:
Date: Date:
10 Implementation of calculator.
NAME:
SUBJECT: MC LAB
EXPERIMENT NO: 01
DATE OF PERFORMANCE: / /
DATE OF SUBMISSION: / /
MAX.MARKS 4 4 2 10
What is Bluetooth?
Bluetooth is a wireless radio technology that allows many different devices to connect to each other and
work together. It was originally invented as an affordable wireless alternative to wired keyboards,
headphones, speakers, and other peripherals. Now, many kinds of devices use Bluetooth, including cell
phones, stereos, health monitors, and safety trackers. Almost any wireless device you encounter might
use Bluetooth technology.
Bluetooth is named after Danish king Harold Bluetooth. The Bluetooth logo is a combination of the two
Norse runes for Harold Bluetooth's initials.
After you have successfully connected to a Bluetooth device, each one has a connected Bluetooth
Socket. You can now share information between devices. Using the Bluetooth Socket, the general
procedure to transfer data is as follows:
1. Get the Input Stream and Output Stream that handle transmissions through the socket using get
Input Stream() and get Output Stream(), respectively.
2. Read and write data to the streams using read(byte[]) and write(byte[]).
There are, of course, implementation details to consider. In particular, you should use a dedicated thread
for reading from the stream and writing to it. This is important because both
the read(byte[]) and write(byte[]) methods are blocking calls. The read(byte[]) method blocks until
there is something to read from the stream. The write(byte[]) method doesn't usually block, but it can
block for flow control if the remote device isn't calling read(byte[]) quickly enough and the intermediate
buffers become full as a result. So, you should dedicate your main loop in the thread to reading from
the Input Stream. You can use a separate public method in the thread to initiate writes to the Output
Stream.
After the constructor acquires the necessary streams, the thread waits for data to come through the Input
Stream. When read(byte[]) returns with data from the stream, the data is sent to the main activity using
a member Handler from the parent class. The thread then waits for more bytes to be read from the Input
Stream.
7 | MC Lab, Class: TE COMP, Theem COE
To send outgoing data, you call the thread's write () method from the main activity and pass in the bytes
to be sent. This method calls write (byte []) to send the data to the remote device. If an IO Exception is
thrown when calling write(byte[]), the thread sends a toast to the main activity, explaining to the user
that the device couldn't send the given bytes to the other (connected) device.
The thread's cancel () method allows you to terminate the connection at any time by closing
the Bluetooth Socket. Always call this method when you're done using the Bluetooth connection.
private Handler handler; // handler that gets info from Bluetooth service
mmSocket = socket;
// Get the input and output streams; using temp objects because
try {
tmpIn = socket.getInputStream();
} catch (IOException e) {
try {
tmpOut = socket.getOutputStream();
} catch (IOException e) {
mmInStream = tmpIn;
mmOutStream = tmpOut;
while (true) {
try {
numBytes = mmInStream.read(mmBuffer);
mmBuffer);
readMsg.sendToTarget();
} catch (IOException e) {
break;
// Call this from the main activity to send data to the remote device.
try {
mmOutStream.write(bytes);
writtenMsg.sendToTarget();
} catch (IOException e) {
Message writeErrorMsg =
handler.obtainMessage(MessageConstants.MESSAGE_TOAST);
bundle.putString("toast",
writeErrorMsg.setData(bundle);
10 | MC Lab, Class: TE COMP, Theem COE
handler.sendMessage(writeErrorMsg);
// Call this method from the main activity to shut down the connection.
try {
mmSocket.close();
} catch (IOException e) {
Conclusion:
NAME:
SUBJECT: MC LAB
EXPERIMENT NO: 02
DATE OF PERFORMANCE: / /
DATE OF SUBMISSION: / /
MAX.MARKS 4 4 2 10
INTRODUCTION:-
CDMA is a channelization protocol for Multiple Access, where information can be sent simultaneously
through several transmitters over a single communication channel.
It is achieved in below steps:
A signal is generated which extends over a wide bandwidth.
The code which performs this action is called spreading code.
Later on, a specific signal can be selected with a given code even in the presence of many other
signals.
It is mainly used in mobile networks like 2G and 3G.
To see how CDMA works, we have to understand orthogonal sequences (also known as chips).
Let N be the number of stations establishing multiple access over a common channel.
1. An orthogonal sequence can be thought of as a 1xN matrix.
Eg: [+1 -1 +1 -1] for N = 4.
2. Scalar multiplication and matrix addition rules follow as usual.
Eg: 3.[+1 -1 +1 -1] = [+3 -3 +3 -3]
Eg: [+1 -1 +1 -1] + [-1 -1 -1 -1] = [0 -2 0 -2]
3. Inner Product: It is evaluated by multiplying two sequences element by element and then adding all
elements of the resulting list.
Inner Product of a sequence with itself is equal to N
[+1 -1 +1 -1].[+1 -1 +1 -1] = 1 + 1 + 1 + 1 = 4
Inner Product of two distinct sequences is zero
[+1 -1 +1 -1].[+1 +1 +1 +1] = 1-1+1-1 = 0
Procedure:
1. The station encodes its data bit as follows.
+1 if bit = 1
-1 if bit = 0
no signal(interpreted as 0) if station is idle
2. Each station is assigned a unique orthogonal sequence (code) which is N bit long for N stations
3. Each station does a scalar multiplication of its encoded data bit and code sequence.
4. The resulting sequence is then placed on the channel.
5. Since the channel is common, amplitudes add up and hence resultant channel sequence is sum of
sequences from all channels.
6. If station 1 wants to listen to station 2, it multiplies (inner product) the channel sequence with code of
station S2.
7. The inner product is then divided by N to get data bit transmitted from station 2.
Example: Assume 4 stations S1, S2, S3, S4. We’ll use 4×4 Walsh Table to assign codes to them.
C1 = [+1 +1 +1 +1]
C2 = [+1 -1 +1 -1]
C3 = [+1 +1 -1 -1]
C4 = [+1 -1 -1 +1]
Program:-
// Java code illustrating a simple implementation of CDMA
import java.util.*;
public class CDMA {
private int[][] wtable;
private int[][] copy;
private int[] channel_sequence;
public void setUp(int[] data, int num_stations)
{ wtable = new int[num_stations][num_stations];
copy = new int[num_stations][num_stations];
buildWalshTable(num_stations, 0, num_stations - 1, 0,num_stations - 1, false);
showWalshTable(num_stations);
for (int i = 0; i < num_stations; i++) {
for (int j = 0; j < num_stations; j++) {
// Making a copy of walsh table
// to be used later
copy[i][j] = wtable[i][j];
// each row in table is code for one station.
// So we multiply each row with station data
wtable[i][j] *= data[i];
} }
channel_sequence = new int[num_stations];
for (int i = 0; i < num_stations; i++) {
for (int j = 0; j < num_stations; j++) {
// Adding all sequences to get channel sequence
channel_sequence[i] += wtable[j][i];
} } }
public void listenTo(int sourceStation, int num_stations)
{ int innerProduct = 0;
for (int i = 0; i < num_stations; i++) {
// multiply channel sequence and source station code
innerProduct += copy[sourceStation][i] * channel_sequence[i];
} System.out.println("The data received is: " +(innerProduct / num_stations));
} public int buildWalshTable(int len, int i1, int i2, int j1,int j2, boolean isBar)
{ // len = size of matrix. (i1, j1), (i2, j2) are
// starting and ending indices of wtable.isBar represents whether we want to add simple
entry or complement(southeast submatrix) to wtable.
14 | MC Lab, Class: TE COMP, Theem COE
if (len == 2) {
if (!isBar) {
wtable[i1][j1] = 1;
wtable[i1][j2] = 1;
wtable[i2][j1] = 1;
wtable[i2][j2] = -1;
} else {
wtable[i1][j1] = -1;
wtable[i1][j2] = -1;
wtable[i2][j1] = -1;
wtable[i2][j2] = +1;
} return 0; }
int midi = (i1 + i2) / 2;
int midj = (j1 + j2) / 2;
buildWalshTable(len / 2, i1, midi, j1, midj, isBar);
buildWalshTable(len / 2, i1, midi, midj + 1, j2, isBar);
buildWalshTable(len / 2, midi + 1, i2, j1, midj, isBar);
buildWalshTable(len / 2, midi + 1, i2, midj + 1, j2, !isBar);
return 0; }
public void showWalshTable(int num_stations) {
System.out.print("\n");
for (int i = 0; i < num_stations; i++) {
for (int j = 0; j < num_stations; j++) {
System.out.print(wtable[i][j] + " ");
}System.out.print("\n");
}System.out.println("-------------------------");
System.out.print("\n"); }
// Driver Code
public static void main(String[] args)
{ int num_stations = 4;
int[] data = new int[num_stations];
//data bits corresponding to each station
data[0] = -1;
data[1] = -1;
data[2] = 0;
data[3] = 1;
CDMA channel = new CDMA();
channel.setUp(data, num_stations);
// station you want to listen to
int sourceStation = 3;
channel.listenTo(sourceStation, num_stations);
}}
Output:
1 1 1 1
1 -1 1 -1
1 1 -1 -1
1 -1 -1 1
NAME:
SUBJECT: MC LAB
EXPERIMENT NO: 03
DATE OF PERFORMANCE: / /
DATE OF SUBMISSION: / /
MAX.MARKS 4 4 2 10
Ki (128 bit)
A3
Ki (128 bit)
A8
KC (64 bit)
Figure 2: A8 algorithm
RAND (128
bit)
The above mentioned steps are described in the activity diagram and block diagram shown in Fig. 4&5
Encrypted data
Conclusion:
NAME:
SUBJECT: MC LAB
EXPERIMENT NO: 04
DATE OF PERFORMANCE: / /
DATE OF SUBMISSION: / /
MAX.MARKS 4 4 2 10
INTRODUCTION
Wireless networks are more convenient than wired networks and allow you to move from room to
room in your home. Further, with a advancement in wireless hardware, higher throughput and lower latency
support has become possible. But they can also be more vulnerable if not properly secured. If our wireless
network is ’unsecured’ or ‘open’ then an intruder can easily gain access to our internal network resources as
well as to the Internet, all without our consent. Once the intruder has access to our network, he/she can use it
for a variety of operations, such as:
To steal your Internet bandwidth.
To perform disruptive or illegal acts.
To steal your sensitive information.
Kismet to sniff data management traffics packets from wireless LAN, analyze them to see the
vulnerability of the different detected access points and compare this result with captured
sources using NETSH
Troubleshoot wireless connections by way of analyzing signal strength to noise ratio of captured
sources.
Launch Kismet GUI (Kismet_ui.conf) application for real-time visualization and monitoring.
Open TCP dumps with Wireshark software to examine and analyze management data packets
from captured sources in http files. This is to help me see the possibility of capturing sensitive
password or valuable information and a test of the vulnerability of the network.
Figure out the meaning, uses and differences between the different file dumps, for example; the
.dump, .csv, .network, .weak and .cisco
To perform Denial-of-Service (DoS) attacks to make the network unusable by sending out false
requests.
To infect the network with malicious threats Thus, wireless networking is inherently risky
because we are transmitting information via radio waves.
Data from your wireless network can be intercepted just like signals from our cellular or cordless
phones. Whenever we use a wireless connection, we might want to ensure that our communications and files
are private and protected. If our transmissions are not secure, it may be possible for others to intercept our e-
KISMET
Kismet is a network detector, packet sniffer, and intrusion detection system for 802.11 wireless LANs. Kismet
will work with any wireless card which supports raw monitoring mode, and can sniff 802.11a, 802.11b,
802.11g, and 802.11n traffic.
The program runs under Linux, FreeBSD, NetBSD, OpenBSD, and Mac OS X. The client can also run on
Microsoft Windows, although, aside from external drones , there’s only one supported wireless hardware
available as packet source. Distributed under the GNU General Public License, Kismet is free software.
A. Working of kismet
Kismet differs from other wireless network detectors in working passively. Namely, without sending any
loggable packets, it is able to detect the presence of both wireless access points and wireless clients, and to
associate them with each other. It is also the most widely used and up to date open source wireless monitoring
tool.Refer fig. 1 to view at explanation of the headings displayed in Kismet.
1. Kismet also includes basic wireless IDS features such as detecting active wireless sniffing programs
including NetStumbler, as well as a number of wireless network attacks.
2. Kismet also features the ability to detect default or ”not configured” networks, probe requests, and
determine what level of wireless encryption is used on a given access point.
Conclusion:
NAME:
SUBJECT: MC LAB
EXPERIMENT NO: 05
DATE OF PERFORMANCE: / /
DATE OF SUBMISSION: / /
MAX.MARKS 4 4 2 10
Then select the Minimum SDK as shown below and click Next.
Now click on Design and your application will look as given below.
Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
?
package com.example.exno1;
1
2
import android.graphics.Color;
3 import android.support.v7.app.AppCompatActivity;
4 import android.os.Bundle;
5 import android.view.View;
6 import android.widget.Button;
import android.widget.TextView;
7
8 public class MainActivity extends AppCompatActivity
9 {
10 int ch=1;
11 float font=30;
12 @Override
protected void onCreate(Bundle savedInstanceState)
13 {
14 super.onCreate(savedInstanceState);
15 setContentView(R.layout.activity_main);
16 final TextView t= (TextView) findViewById(R.id.textView);
Button b1= (Button) findViewById(R.id.button1);
17 b1.setOnClickListener(new View.OnClickListener() {
18 @Override
35 | MC Lab, Class: TE COMP, Theem COE
19 public void onClick(View v) {
20 t.setTextSize(font);
font = font + 5;
21 if (font == 50)
22 font = 30;
23 }
24 });
Button b2= (Button) findViewById(R.id.button2);
25 b2.setOnClickListener(new View.OnClickListener() {
26 @Override
27 public void onClick(View v) {
28 switch (ch) {
29 case 1:
t.setTextColor(Color.RED);
30 break;
31 case 2:
32 t.setTextColor(Color.GREEN);
33 break;
case 3:
34 t.setTextColor(Color.BLUE);
35 break;
36 case 4:
37 t.setTextColor(Color.CYAN);
38 break;
case 5:
39 t.setTextColor(Color.YELLOW);
40 break;
41 case 6:
42 t.setTextColor(Color.MAGENTA);
43 break;
}
44 ch++;
45 if (ch == 7)
46 ch = 1;
47 }
});
48 }
49 }
50
Output:
NAME:
SUBJECT: MC LAB
EXPERIMENT NO: 06
DATE OF PERFORMANCE: / /
DATE OF SUBMISSION: / /
MAX.MARKS 4 4 2 10
Title: Write an application that draws basic graphical primitive on the screen.
Date of Performance: Date of Submission:
Then select the Minimum SDK as shown below and click Next.
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
</RelativeLayout>
Now click on Design and your application will look as given below.
package com.example.exno4;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
//Creating a Bitmap
Bitmap bg = Bitmap.createBitmap(720, 1280, Bitmap.Config.ARGB_8888);
NAME:
SUBJECT:MC LAB
EXPERIMENT NO: 07
DATE OF PERFORMANCE: / /
DATE OF SUBMISSION: / /
MAX.MARKS 4 4 2 10
Then select the Minimum SDK as shown below and click Next.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="110dp"
android:text="Enter Rollno:"
android:textSize="20sp" />
<EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp" />
<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="150dp"
android:inputType="text"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="210dp"
android:text="Enter Marks:"
android:textSize="20sp" />
<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="200dp"
android:inputType="number"
android:textSize="20sp" />
<Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="300dp"
android:text="Insert"
android:textSize="30dp" />
51 | MC Lab, Class: TE COMP, Theem COE
<Button
android:id="@+id/Delete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="300dp"
android:text="Delete"
android:textSize="30dp" />
<Button
android:id="@+id/Update"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="400dp"
android:text="Update"
android:textSize="30dp" />
<Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="400dp"
android:text="View"
android:textSize="30dp" />
<Button
android:id="@+id/ViewAll"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="500dp"
android:text="View All"
android:textSize="30dp" />
</AbsoluteLayout>
Now click on Design and your application will look as given below.
Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
package com.example.exno5;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
Rollno=(EditText)findViewById(R.id.Rollno);
Name=(EditText)findViewById(R.id.Name);
Marks=(EditText)findViewById(R.id.Marks);
Insert=(Button)findViewById(R.id.Insert);
Delete=(Button)findViewById(R.id.Delete);
Update=(Button)findViewById(R.id.Update);
View=(Button)findViewById(R.id.View);
ViewAll=(Button)findViewById(R.id.ViewAll);
Insert.setOnClickListener(this);
Delete.setOnClickListener(this);
Update.setOnClickListener(this);
View.setOnClickListener(this);
ViewAll.setOnClickListener(this);
NAME:
SUBJECT:MC LAB
EXPERIMENT NO: 08
DATE OF PERFORMANCE: / /
DATE OF SUBMISSION: / /
MAX.MARKS 4 4 2 10
Then select the Minimum SDK as shown below and click Next.
06 >
07 <Button
08 android:id="@+id/retrieve_location_button"
android:text="Retrieve Location"
09
android:layout_width="wrap_content"
10
android:layout_height="wrap_content"
11
/>
12
</LinearLayout
13
Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
package com.javacodegeeks.android.lbs;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class LbsGeocodingActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in
Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in
Milliseconds
NAME:
SUBJECT:MC LAB
EXPERIMENT NO: 09
DATE OF PERFORMANCE: / /
DATE OF SUBMISSION: / /
MAX.MARKS 4 4 2 10
Then select the Minimum SDK as shown below and click Next.
Then delete the code which is there and type the code as given below.
Code for Activity_main.xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:layout_gravity="center"
android:text="Notify"
android:textSize="30sp"/>
</LinearLayout>
Now click on Design and your application will look as given below.
Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
package com.example.exno10;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
notify.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
PendingIntent pending =
PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Notification noti = new
Notification.Builder(MainActivity.this).setContentTitle("New
Message").setContentText(e.getText().toString()).setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pending).build();
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(0, noti);
}
});
}
}
NAME:
SUBJECT:MC LAB
EXPERIMENT NO: 10
DATE OF PERFORMANCE: / /
DATE OF SUBMISSION: / /
MAX.MARKS 4 4 2 10
Then select the Minimum SDK as shown below and click Next.
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<Button
android:id="@+id/Add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="30sp"/>
<Button
android:id="@+id/Sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="30sp"/>
<Button
android:id="@+id/Mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="30sp"/>
<Button
android:id="@+id/Div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="30sp"/>
</LinearLayout>
83 | MC Lab, Class: TE COMP, Theem COE
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Answer is"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
Now click on Design and your application will look as given below.
Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
package com.example.devang.exno3;
84 | MC Lab, Class: TE COMP, Theem COE
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set a listener
Add.setOnClickListener(this);
Sub.setOnClickListener(this);
Mul.setOnClickListener(this);
Div.setOnClickListener(this);
}
85 | MC Lab, Class: TE COMP, Theem COE
@Override
public void onClick (View v)
{
float num1 = 0;
float num2 = 0;
float result = 0;
String oper = "";
// defines the button that has been clicked and performs the corresponding
operation
// write operation into oper, we will use it later for output
switch (v.getId())
{
case R.id.Add:
oper = "+";
result = num1 + num2;
break;
case R.id.Sub:
oper = "-";
result = num1 - num2;
break;
case R.id.Mul:
oper = "*";
result = num1 * num2;
break;
case R.id.Div:
oper = "/";
result = num1 / num2;
break;
default:
break;
86 | MC Lab, Class: TE COMP, Theem COE
}
// form the output line
Result.setText(num1 + " " + oper + " " + num2 + " = " + result);
}
}
Conclusion: