· 6 years ago · Nov 08, 2019, 06:22 PM
1bit.ly/codeperfectmcc
2
3https://pastebin.com/raw/3ura0CKN
4
5
6
7
8Online editor: sandbox
9
10https://shift.infinite.red/getting-started-with-react-native-development-on-windows-90d85a72ae65
11react-native init AwesomeProject
12react-native run-android
13react-native start
14
15
16How to start React Experiment
17● Open Terminal
18● Write command expo init Navigation
19● Choose template Blank
20● Write the name of your app
21● cd Navigation
22● npm start
23● Now go to the Navigation folder , App.js file would already be there
24● Edit App.js using any text editor
25
26REACT NATIVE : GUI COMPONENTS / LOGIN SCREEN
27App.js
28import React from 'react';
29import { StyleSheet, Text, View , TextInput, TouchableOpacity} from 'react-native';
30export default function App() {
31return (
32<View style={styles.container}>
33<Text style={styles.textLogin}>Login to My App</Text>
34<TextInput
35style={styles.input}
36placeholder="Username"/>
37<TextInput
38style={styles.input}
39placeholder="Password"
40secureTextEntry/>
41<View style={styles.btnContainer}>
42<TouchableOpacity style={styles.usrBtn}
43onPress={()=>alert("Login works")}>
44<Text style={styles.btnText}>Login</Text>
45</TouchableOpacity>
46<TouchableOpacity style={styles.usrBtn}>
47<Text style={styles.btnText}
48onPress={()=>alert("SignUp works")}>SignUp</Text>
49</TouchableOpacity>
50</View>
51</View>
52);
53}
54const styles = StyleSheet.create({
55container: {
56flex: 1,
57backgroundColor: '#bfe',
58alignItems: 'center',
59justifyContent: 'center',
60marginBottom:20,
61fontSize:20,
62},
63input:{
64width:"90%",
65backgroundColor:"#fff",
66marginBottom:10,
67padding:15,
68},
69textLogin:{
70marginBottom:10,
71fontSize:15,
72},
73usrBtn:{
74backgroundColor:"#55D7",
75justifyContent:"center",
76width:"45%",
77padding:15,
78},
79btnContainer:{
80flexDirection:"row",
81justifyContent:"space-between",
82width:"90%"
83},
84btnText:{
85textAlign:"center",
86fontSize:15,
87}
88});
89
90
91
92REACT NATIVE:NAVIGATION
93App.js
94import { createAppContainer } from "react-navigation";
95import { createStackNavigator } from "react-navigation-stack";
96import HomeScreen from "./HomeScreen";
97import Page1 from "./Page1";
98import Page2 from "./Page2";
99const navigator = createStackNavigator(
100{
101Home: HomeScreen,
102Page1: Page1,
103Page2: Page2
104},
105{
106initialRouteName: "Home",
107defaultNavigationOptions: {
108title: "App"
109}
110}
111);
112export default createAppContainer(navigator);
113
114
115Page1.js
116import React from "react";
117import { Text, StyleSheet, View } from "react-native";
118function Page1() {
119return (
120<View style={styles.viewStyle}>
121<Text style={styles.textStyle}>Welcome to Page 1</Text>
122</View>
123);
124}
125const styles = StyleSheet.create({
126viewStyle: {
127alignItems: "center",
128flex: 1,
129justifyContent: "center"
130},
131textStyle: {
132fontSize: 30
133}
134});
135export default Page1;
136
137Page2.js
138import React from "react";
139import { Text, StyleSheet, View } from "react-native";
140function Page2() {
141return (
142<View style={styles.viewStyle}>
143<Text style={styles.textStyle}>Welcome to Page 2</Text>
144</View>
145);
146}
147const styles = StyleSheet.create({
148viewStyle: {
149alignItems: "center",
150flex: 1,
151justifyContent: "center"
152},
153textStyle: {
154fontSize: 30
155}
156});
157export default Page2;
158
159
160ANDROID 1. : READ AND WRITE FROM EXTERNAL STORAGE:
161Give permissions after installing apk in your phone
162Mainactivity.xml
163<?xml version= "1.0" encoding= "utf-8" ?>
164<LinearLayout
165xmlns:android = "http://schemas.android.com/apk/res/android"
166xmlns:app="http://schemas.android.com/apk/res-auto"
167xmlns:tools = "http://schemas.android.com/tools"
168android:layout_width= "match_parent"
169android:layout_height= "match_parent"
170android:orientation= "vertical"
171tools:context= ".MainActivity" >
172<EditText
173 android:id= "@+id/etData"
174 android:layout_width= "match_parent"
175 android:layout_height= "wrap_content"
176 android:ems= "10"
177 android:inputType= "textPersonName"
178 android:text= ""
179 android:hint= "Enter Data" />
180<Button
181 android:id= "@+id/btnWrite"
182 android:layout_width= "match_parent"
183 android:layout_height= "wrap_content"
184 android:text= "Write" />
185<Button
186 android:id= "@+id/btnRead"
187 android:layout_width= "match_parent"
188 android:layout_height= "wrap_content"
189 android:text= "Read" />
190<TextView
191 android:id= "@+id/tvData"
192 android:layout_width= "match_parent"
193 android:layout_height= "wrap_content"
194 android:text= "" />
195</LinearLayout >
196
197
198MainActivity.java:(Note only highlighted part)
199package com.example.a1extstorage;
200import android.support.v7.app.AppCompatActivity;
201import android.os.Bundle;
202import android.view.View;
203import android.widget.Button;
204import android.widget.EditText;
205import android.widget.TextView;
206import android.widget.Toast;
207import java.io.BufferedReader;
208import java.io.File;
209import java.io.FileInputStream;
210import java.io.FileNotFoundException;
211import java.io.FileOutputStream;
212import java.io.IOException;
213import java.io.InputStreamReader;
214public class MainActivity extends AppCompatActivity {
215 EditText etData ;
216 Button btnRead , btnWrite ;
217 TextView tvData ;
218
219 @Override
220 protected void onCreate(Bundle savedInstanceState) {
221 super .onCreate(savedInstanceState);
222 setContentView(R.layout. activity_main );
223 etData =(EditText)findViewById(R.id. etData );
224 btnRead =(Button)findViewById(R.id. btnRead );
225 btnWrite =(Button)findViewById(R.id. btnWrite );
226 tvData =(TextView)findViewById(R.id. tvData );
227 btnWrite .setOnClickListener( new View.OnClickListener() {
228 @Override
229 public void onClick(View v) {
230 String msg= etData .getText().toString();
231 try {
232 File f= new File( "/sdcard/myfile.txt" );
233 f.createNewFile();
234 FileOutputStream fout= new FileOutputStream(f);
235 fout.write(msg.getBytes());
236 fout.close();
237
238 Toast. makeText (getBaseContext(), "DATA INSERTED" ,Toast. LENGTH_SHORT ).show();
239 etData .setText( "" );
240 } catch (IOException e) {
241 e.printStackTrace();
242 }
243 }
244 });
245 btnRead .setOnClickListener( new View.OnClickListener() {
246 @Override
247 public void onClick(View v) {
248 String line;
249 StringBuffer sb= new StringBuffer();
250 try {
251 FileInputStream fis= new FileInputStream( "/sdcard/myfile.txt" );
252 InputStreamReader isr= new InputStreamReader(fis);
253 BufferedReader br= new BufferedReader(isr);
254 while ((line=br.readLine())!= null ){
255 sb.append(line+ "\n" );
256 }
257 tvData .setText( "The text in the file is: \n " +sb.toString());
258 br.close();
259 } catch (FileNotFoundException e) {
260 e.printStackTrace();
261 } catch (IOException e) {
262 e.printStackTrace();
263 }
264 }
265 });
266 }
267}
268
269AndroidManifest.xml:(Add these 2 lines)
270<uses-permission android:name= "android.permission.READ_EXTERNAL_STORAGE" />
271<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
272
273
274ANDROID : GPS
275Give permissions after installing apk in your phone
276MainActivity.xml:
277<?xml version= "1.0" encoding= "utf-8" ?>
278<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
279xmlns:app = "http://schemas.android.com/apk/res-auto"
280xmlns:tools = "http://schemas.android.com/tools"
281android:layout_width= "match_parent"
282android:layout_height= "match_parent"
283tools:context= ".MainActivity"
284android:orientation= "vertical"
285 >
286<TextView
287 android:id= "@+id/tvInfo"
288 android:layout_width= "match_parent"
289 android:layout_height= "wrap_content"
290 android:text= "" />
291<Button
292 android:id= "@+id/btnShare"
293 android:layout_width= "match_parent"
294 android:layout_height= "wrap_content"
295 android:text= "Share" />
296</LinearLayout >
297
298
299
300
301MainActivity.java:
302package com.example.locationmcc;
303import android.Manifest;
304import android.content.Intent;
305import android.content.pm.PackageManager;
306import android.location.Address;
307import android.location.Geocoder;
308import android.location.Location;
309import android.support.annotation. NonNull ;
310import android.support.annotation. Nullable ;
311import android.support.v4.app.ActivityCompat;
312import android.support.v7.app.AppCompatActivity;
313import android.os.Bundle;
314import android.view.View;
315import android.widget.Button;
316import android.widget.TextView;
317import android.widget.Toast;
318import com.google.android.gms.common.ConnectionResult;
319import com.google.android.gms.common.api.GoogleApiClient;
320import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
321import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
322import com.google.android.gms.location.LocationServices;
323import java.io.IOException;
324import java.util.List;
325import java.util.Locale;
326public class MainActivity extends AppCompatActivity implements
327GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
328TextView tvInfo ;
329Button btnShare ;
330GoogleApiClient gac ;
331Location loc ;
332@Override
333protected void onCreate(Bundle savedInstanceState) {
334super .onCreate(savedInstanceState);
335setContentView(R.layout. activity_main );
336tvInfo = findViewById(R.id. tvInfo );
337btnShare = findViewById(R.id. btnShare );
338GoogleApiClient.Builder builder = new GoogleApiClient.Builder( this );
339builder.addApi(LocationServices. API );
340builder.addConnectionCallbacks( this );
341builder.addOnConnectionFailedListener( this );
342gac = builder.build();
343btnShare .setOnClickListener( new View.OnClickListener() {
344@Override
345public void onClick(View view) {
346Intent i = new Intent(Intent. ACTION_SEND );
347i.setType( "text/plain" );
348i.putExtra(Intent. EXTRA_TEXT , "My address is: " +
349tvInfo .getText().toString());
350startActivity(i);
351}
352});
353}
354@Override
355protected void onStart() {
356super .onStart();
357if ( gac != null )
358gac .connect();
359}
360@Override
361protected void onStop() {
362super .onStop();
363if ( gac != null ) {
364gac .disconnect();
365//btnShare.setEnabled(false);
366}
367}
368@Override
369public void onPointerCaptureChanged( boolean hasCapture) {
370}
371@Override
372public void onConnectionFailed( @NonNull ConnectionResult connectionResult) {
373Toast. makeText ( this , "Connection Failed" , Toast. LENGTH_SHORT ).show();
374}
375@Override
376public void onConnected( @Nullable Bundle bundle) {
377if (ActivityCompat. checkSelfPermission ( this ,
378android.Manifest.permission. ACCESS_FINE_LOCATION ) != PackageManager. PERMISSION_GRANTED
379&& ActivityCompat. checkSelfPermission ( this ,
380android.Manifest.permission. ACCESS_COARSE_LOCATION ) !=
381PackageManager. PERMISSION_GRANTED ) {
382return ;
383}
384loc = LocationServices. FusedLocationApi .getLastLocation( gac );
385if ( loc != null ){
386double lat = loc .getLatitude();
387double lon = loc .getLongitude();
388tvInfo .setText( "Latitude: " + lat+ " Longitude: " + lon);
389Geocoder g = new Geocoder( this , Locale. ENGLISH );
390try {
391List<android.location.Address> la = g.getFromLocation(lat, lon, 1 );
392//number of results = 1
393android.location.Address add = la.get( 0 );
394String msg = add.getCountryName() + " " +
395add.getAdminArea() + " " +
396add.getSubAdminArea() + " " +
397add.getLocality() + " " +
398add.getSubLocality() + " " +
399add.getThoroughfare() + " " +
400add.getSubThoroughfare() + " " +
401add.getPostalCode();
402tvInfo .setText(msg);
403} catch (IOException e) {
404e.printStackTrace();
405}
406}
407else {
408tvInfo .setText( "Please enable GPS" );
409}
410}
411@Override
412public void onConnectionSuspended( int i) {
413Toast. makeText ( this , "Connection Suspended " + CAUSE_NETWORK_LOST ,
414Toast. LENGTH_SHORT ).show();
415}
416}
417
418
419AndroidManifest.xml:
420<?xml version="1.0" encoding="utf-8"?>
421<manifest xmlns:android="http://schemas.android.com/apk/res/android"
422package="com.example.locationmcc">
423<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
424<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
425<uses-permission android:name="android.permission.INTERNET"/>
426<application
427android:allowBackup="true"
428android:icon="@mipmap/ic_launcher"
429android:label="@string/app_name"
430android:roundIcon="@mipmap/ic_launcher_round"
431android:supportsRtl="true"
432android:theme="@style/AppTheme">
433<activity android:name=".MainActivity">
434<intent-filter>
435<action android:name="android.intent.action.MAIN" />
436<category android:name="android.intent.category.LAUNCHER" />
437</intent-filter>
438</activity>
439<meta-data
440android:name="com.google.android.geo.API_KEY"
441android:value=" YOUR KEY HERE "/>
442</application>
443</manifest>
444
445
446
447Keys:
448AIzaSyAO3_hKnEITOoGmMb67sCA5d-CDp4Ad80I
449
450
451Gradle:
452(Add this line)
453implementation 'com.google.android.gms:play-services:12.0.1'
454
455
456ANDROID : CALCULATOR
457
458Activity_main.xml
459<?xml version= "1.0" encoding= "utf-8" ?>
460<android.support.constraint.ConstraintLayout
461xmlns:android = "http://schemas.android.com/apk/res/android"
462xmlns:app = "http://schemas.android.com/apk/res-auto"
463xmlns:tools = "http://schemas.android.com/tools"
464android:layout_width= "match_parent"
465android:layout_height= "match_parent"
466tools:context= ".MainActivity" >
467
468 <TextView
469 android:id="@+id/operand1"
470 android:layout_width="wrap_content"
471 android:layout_height="wrap_content"
472 android:layout_marginStart="8dp"
473 android:layout_marginLeft="8dp"
474 android:layout_marginTop="76dp"
475 android:layout_marginEnd="8dp"
476 android:layout_marginRight="8dp"
477 android:text="Operand 1"
478 app:layout_constraintEnd_toEndOf="parent"
479 app:layout_constraintHorizontal_bias="0.084"
480 app:layout_constraintStart_toStartOf="parent"
481 app:layout_constraintTop_toTopOf="parent" />
482
483 <TextView
484 android:id="@+id/operand2"
485 android:layout_width="wrap_content"
486 android:layout_height="wrap_content"
487 android:layout_marginStart="8dp"
488 android:layout_marginLeft="8dp"
489 android:layout_marginTop="52dp"
490 android:layout_marginEnd="8dp"
491 android:layout_marginRight="8dp"
492 android:text="Operand 2"
493 app:layout_constraintEnd_toEndOf="parent"
494 app:layout_constraintHorizontal_bias="0.084"
495 app:layout_constraintStart_toStartOf="parent"
496 app:layout_constraintTop_toBottomOf="@+id/operand1" />
497
498 <TextView
499 android:id="@+id/operator"
500 android:layout_width="wrap_content"
501 android:layout_height="wrap_content"
502 android:layout_marginStart="8dp"
503 android:layout_marginLeft="8dp"
504 android:layout_marginTop="60dp"
505 android:layout_marginEnd="8dp"
506 android:layout_marginRight="8dp"
507 android:text="Operator"
508 app:layout_constraintEnd_toEndOf="parent"
509 app:layout_constraintHorizontal_bias="0.088"
510 app:layout_constraintStart_toStartOf="parent"
511 app:layout_constraintTop_toBottomOf="@+id/operand2" />
512
513 <EditText
514 android:id="@+id/op1"
515 android:layout_width="wrap_content"
516 android:layout_height="wrap_content"
517 android:layout_marginStart="8dp"
518 android:layout_marginLeft="8dp"
519 android:layout_marginTop="64dp"
520 android:layout_marginEnd="8dp"
521 android:layout_marginRight="8dp"
522 android:ems="10"
523 android:inputType="number"
524 app:layout_constraintEnd_toEndOf="parent"
525 app:layout_constraintHorizontal_bias="0.283"
526 app:layout_constraintStart_toEndOf="@+id/operand1"
527 app:layout_constraintTop_toTopOf="parent" />
528
529 <EditText
530 android:id="@+id/op2"
531 android:layout_width="wrap_content"
532 android:layout_height="wrap_content"
533 android:layout_marginStart="8dp"
534 android:layout_marginLeft="8dp"
535 android:layout_marginTop="24dp"
536 android:layout_marginEnd="8dp"
537 android:layout_marginRight="8dp"
538 android:ems="10"
539 android:inputType="number"
540 app:layout_constraintEnd_toEndOf="parent"
541 app:layout_constraintHorizontal_bias="0.283"
542 app:layout_constraintStart_toEndOf="@+id/operand2"
543 app:layout_constraintTop_toBottomOf="@+id/op1" />
544
545 <Spinner
546 android:id="@+id/op"
547 android:layout_width="wrap_content"
548 android:layout_height="wrap_content"
549 android:layout_marginTop="44dp"
550 android:layout_marginEnd="8dp"
551 android:layout_marginRight="8dp"
552 app:layout_constraintEnd_toEndOf="parent"
553 app:layout_constraintHorizontal_bias="0.467"
554 app:layout_constraintStart_toEndOf="@+id/operator"
555 app:layout_constraintTop_toBottomOf="@+id/op2" />
556
557 <Button
558 android:id="@+id/calc"
559 android:layout_width="wrap_content"
560 android:layout_height="wrap_content"
561 android:layout_marginStart="8dp"
562 android:layout_marginLeft="8dp"
563 android:layout_marginTop="372dp"
564 android:layout_marginEnd="8dp"
565 android:layout_marginRight="8dp"
566 android:text="Calculate"
567 app:layout_constraintEnd_toEndOf="parent"
568 app:layout_constraintHorizontal_bias="0.467"
569 app:layout_constraintStart_toStartOf="parent"
570 app:layout_constraintTop_toTopOf="parent" />
571
572 <TextView
573 android:id="@+id/txtResult"
574 android:layout_width="wrap_content"
575 android:layout_height="wrap_content"
576 android:layout_marginStart="8dp"
577 android:layout_marginLeft="8dp"
578 android:layout_marginTop="64dp"
579 android:layout_marginEnd="8dp"
580 android:layout_marginRight="8dp"
581 android:text="Result"
582 app:layout_constraintEnd_toEndOf="parent"
583 app:layout_constraintHorizontal_bias="0.109"
584 app:layout_constraintStart_toStartOf="parent"
585 app:layout_constraintTop_toBottomOf="@+id/operator" />
586
587 <TextView
588 android:id="@+id/result"
589 android:layout_width="wrap_content"
590 android:layout_height="wrap_content"
591 android:layout_marginTop="64dp"
592 android:layout_marginEnd="8dp"
593 android:layout_marginRight="8dp"
594 android:text="-"
595 app:layout_constraintEnd_toEndOf="parent"
596 app:layout_constraintStart_toEndOf="@+id/txtResult"
597 app:layout_constraintTop_toBottomOf="@+id/op" />
598
599</android.support.constraint.ConstraintLayout>
600
601MainActivty.java
602package com.example.a3_calculator;
603
604import android.support.v7.app.AppCompatActivity;
605import android.os.Bundle;
606import android.util.Log;
607import android.view.View;
608import android.widget.ArrayAdapter;
609import android.widget.Button;
610import android.widget.EditText;
611import android.widget.Spinner;
612import android.widget.TextView;
613import android.widget.Toast;
614public class MainActivity extends AppCompatActivity {
615 Button calc;
616 EditText op1, op2;
617 TextView res;
618 Spinner op;
619 int result;
620 @Override
621 protected void onCreate(Bundle savedInstanceState) {
622 super .onCreate(savedInstanceState);
623 setContentView(R.layout. activity_main );
624 op1 = findViewById(R.id.op1);
625 op2 = findViewById(R.id.op2);
626
627 calc = findViewById(R.id.calc);
628 op = findViewById(R.id.op);
629 res = findViewById(R.id.result);
630
631 String[] operators = new String[]{"+", "-", "*", "/"};
632 ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, operators);
633 op.setAdapter(adapter);
634
635 calc.setOnClickListener(new View.OnClickListener() {
636 @Override
637 public void onClick(View v) {
638 String text = op.getSelectedItem().toString();
639
640
641 if (op1.getText() == null || op1.getText() == null) {
642 Toast.makeText(MainActivity.this, "Please enter the operands", Toast.LENGTH_SHORT).show();
643 } else {
644 int a = Integer.parseInt(op1.getText().toString());
645 int b = Integer.parseInt(op2.getText().toString());
646 if(b==0 && text=="/"){
647 res.setText("Cannot divide by zero");
648 }
649 else{
650 if (text == "+") {
651 result = a + b;
652 } else if (text == "-") {
653 result = a - b;
654 } else if (text == "*") {
655 result = a * b;
656 } else if (text == "/") {
657 result = a / b;
658 }
659 Log.i("valcheck",a+"");
660 Log.i("valcheck",b+"");
661 Log.i("valcheck",result+"");
662 res.setText(String.valueOf(result));
663 }
664
665
666 }
667 }
668 });
669
670 }
671}
672
673
674
675
676
677ANDROID : CALCULATOR (This code works for one digit numbers only)
678MainActivity.xml:
679<?xml version= "1.0" encoding= "utf-8" ?>
680<android.support.constraint.ConstraintLayout
681xmlns:android = "http://schemas.android.com/apk/res/android"
682xmlns:app = "http://schemas.android.com/apk/res-auto"
683xmlns:tools = "http://schemas.android.com/tools"
684android:layout_width= "match_parent"
685android:layout_height= "match_parent"
686tools:context= ".MainActivity" >
687<EditText
688 android:id= "@+id/output"
689 android:layout_width= "wrap_content"
690 android:layout_height= "wrap_content"
691 android:layout_marginTop= "28dp"
692 android:ems= "10"
693 android:inputType= "textPersonName"
694 android:text= ""
695 app:layout_constraintEnd_toEndOf= "parent"
696 app:layout_constraintStart_toStartOf= "parent"
697 app:layout_constraintTop_toTopOf= "parent" />
698<Button
699 android:id= "@+id/plus"
700 android:layout_width= "0dp"
701 android:layout_height= "44dp"
702 android:layout_marginStart= "16dp"
703 android:layout_marginLeft= "16dp"
704 android:layout_marginEnd= "28dp"
705 android:layout_marginRight= "28dp"
706 android:text= "+"
707 app:layout_constraintBaseline_toBaselineOf= "@+id/minus"
708 app:layout_constraintEnd_toStartOf= "@+id/minus"
709 app:layout_constraintStart_toStartOf= "parent" />
710<Button
711 android:id= "@+id/minus"
712 android:layout_width= "0dp"
713 android:layout_height= "49dp"
714 android:layout_marginTop= "21dp"
715 android:layout_marginEnd= "32dp"
716 android:layout_marginRight= "32dp"
717 android:layout_marginBottom= "36dp"
718 android:text= "-"
719 app:layout_constraintBottom_toTopOf= "@+id/num2"
720 app:layout_constraintEnd_toStartOf= "@+id/mul"
721 app:layout_constraintStart_toEndOf= "@+id/plus"
722 app:layout_constraintTop_toBottomOf= "@+id/output" />
723<Button
724 android:id= "@+id/mul"
725 android:layout_width= "0dp"
726 android:layout_height= "45dp"
727 android:layout_marginEnd= "37dp"
728 android:layout_marginRight= "37dp"
729 android:text= "*"
730 app:layout_constraintBaseline_toBaselineOf= "@+id/minus"
731 app:layout_constraintEnd_toStartOf= "@+id/div"
732 app:layout_constraintStart_toEndOf= "@+id/minus" />
733<Button
734 android:id= "@+id/div"
735 android:layout_width= "0dp"
736 android:layout_height= "0dp"
737 android:layout_marginEnd= "30dp"
738 android:layout_marginRight= "30dp"
739 android:text= "/"
740 app:layout_constraintBottom_toBottomOf= "@+id/mul"
741 app:layout_constraintEnd_toEndOf= "parent"
742 app:layout_constraintStart_toEndOf= "@+id/mul"
743 app:layout_constraintTop_toTopOf= "@+id/mul" />
744<Button
745 android:id= "@+id/num1"
746 android:layout_width= "wrap_content"
747 android:layout_height= "wrap_content"
748 android:layout_marginStart= "16dp"
749 android:layout_marginLeft= "16dp"
750 android:layout_marginTop= "36dp"
751 android:text= "1"
752 app:layout_constraintStart_toStartOf= "parent"
753 app:layout_constraintTop_toBottomOf= "@+id/minus" />
754<Button
755 android:id= "@+id/equal"
756 android:layout_width= "wrap_content"
757 android:layout_height= "wrap_content"
758 android:layout_marginTop= "31dp"
759 android:layout_marginEnd= "30dp"
760 android:layout_marginRight= "30dp"
761 android:text= "="
762 app:layout_constraintEnd_toEndOf= "parent"
763 app:layout_constraintTop_toBottomOf= "@+id/num9" />
764
765<Button
766 android:id= "@+id/num0"
767 android:layout_width= "wrap_content"
768 android:layout_height= "wrap_content"
769 android:layout_marginTop= "24dp"
770 android:text= "0"
771 app:layout_constraintStart_toStartOf= "@+id/num8"
772 app:layout_constraintTop_toBottomOf= "@+id/num8" />
773<Button
774 android:id= "@+id/num8"
775 android:layout_width= "wrap_content"
776 android:layout_height= "wrap_content"
777 android:layout_marginTop= "40dp"
778 android:text= "8"
779 app:layout_constraintStart_toStartOf= "@+id/num5"
780 app:layout_constraintTop_toBottomOf= "@+id/num5" />
781<Button
782 android:id= "@+id/clear"
783 android:layout_width= "wrap_content"
784 android:layout_height= "wrap_content"
785 android:layout_marginStart= "16dp"
786 android:layout_marginLeft= "16dp"
787 android:layout_marginTop= "22dp"
788 android:text= "C"
789 app:layout_constraintStart_toStartOf= "parent"
790 app:layout_constraintTop_toBottomOf= "@+id/num7" />
791<Button
792 android:id= "@+id/num9"
793 android:layout_width= "wrap_content"
794 android:layout_height= "wrap_content"
795 android:layout_marginTop= "33dp"
796 android:layout_marginEnd= "30dp"
797 android:layout_marginRight= "30dp"
798 android:text= "9"
799 app:layout_constraintEnd_toEndOf= "parent"
800 app:layout_constraintTop_toBottomOf= "@+id/num6" />
801<Button
802 android:id= "@+id/num4"
803 android:layout_width= "wrap_content"
804 android:layout_height= "wrap_content"
805 android:layout_marginStart= "16dp"
806 android:layout_marginLeft= "16dp"
807 android:layout_marginTop= "26dp"
808 android:text= "4"
809 app:layout_constraintStart_toStartOf= "parent"
810 app:layout_constraintTop_toBottomOf= "@+id/num1" />
811<Button
812 android:id= "@+id/num7"
813 android:layout_width= "wrap_content"
814 android:layout_height= "wrap_content"
815 android:layout_marginStart= "16dp"
816 android:layout_marginLeft= "16dp"
817 android:layout_marginTop= "40dp"
818 android:text= "7"
819 app:layout_constraintStart_toStartOf= "parent"
820 app:layout_constraintTop_toBottomOf= "@+id/num4" />
821<Button
822 android:id= "@+id/num6"
823 android:layout_width= "wrap_content"
824 android:layout_height= "wrap_content"
825 android:layout_marginTop= "33dp"
826 android:layout_marginEnd= "30dp"
827 android:layout_marginRight= "30dp"
828 android:text= "6"
829 app:layout_constraintEnd_toEndOf= "parent"
830 app:layout_constraintTop_toBottomOf= "@+id/num3" />
831<Button
832 android:id= "@+id/num5"
833 android:layout_width= "wrap_content"
834 android:layout_height= "wrap_content"
835 android:layout_marginTop= "26dp"
836 android:text= "5"
837 app:layout_constraintStart_toStartOf= "@+id/num2"
838 app:layout_constraintTop_toBottomOf= "@+id/num2" />
839<Button
840 android:id= "@+id/num2"
841 android:layout_width= "wrap_content"
842 android:layout_height= "wrap_content"
843 android:layout_marginStart= "37dp"
844 android:layout_marginLeft= "37dp"
845 android:layout_marginTop= "106dp"
846 android:text= "2"
847 app:layout_constraintStart_toStartOf= "@+id/minus"
848 app:layout_constraintTop_toBottomOf= "@+id/output" />
849<Button
850 android:id= "@+id/num3"
851 android:layout_width= "wrap_content"
852 android:layout_height= "wrap_content"
853 android:layout_marginTop= "42dp"
854 android:layout_marginEnd= "30dp"
855 android:layout_marginRight= "30dp"
856 android:text= "3"
857 app:layout_constraintEnd_toEndOf= "parent"
858 app:layout_constraintTop_toBottomOf= "@+id/div" />
859</android.support.constraint.ConstraintLayout >
860
861
862
863MainActivity.java:
864package com.example.calculatormcc;
865import android.support.v7.app.AppCompatActivity;
866import android.os.Bundle;
867import android.view.View;
868import android.widget.Button;
869import android.widget.EditText;
870import android.widget.Toast;
871public class MainActivity extends AppCompatActivity {
872Button num1 , num2 , num3 , num4 , num5 , num6 , num7 , num8 , num9 , num0 , plus , minus ,
873mul , div , equal , clear ;
874EditText output ;
875String curr ;
876int ip1 , ip2 ;
877String op ;
878@Override
879protected void onCreate(Bundle savedInstanceState) {
880super .onCreate(savedInstanceState);
881setContentView(R.layout. activity_main );
882num0 = findViewById(R.id. num0 );
883num1 = findViewById(R.id. num1 );
884num2 = findViewById(R.id. num2 );
885num3 = findViewById(R.id. num3 );
886num4 = findViewById(R.id. num4 );
887num5 = findViewById(R.id. num5 );
888num6 = findViewById(R.id. num6 );
889num7 = findViewById(R.id. num7 );
890num8 = findViewById(R.id. num8 );
891num9 = findViewById(R.id. num9 );
892equal = findViewById(R.id. equal );
893plus = findViewById(R.id. plus );
894minus = findViewById(R.id. minus );
895mul = findViewById(R.id. mul );
896div = findViewById(R.id. div );
897clear = findViewById(R.id. clear );
898output = findViewById(R.id. output );
899//similar function for all
900num0 .setOnClickListener( new View.OnClickListener() {
901@Override
902public void onClick(View v) {
903curr = output .getText().toString();
904curr = curr + '0' ;
905output .setText( curr );
906}
907});
908num1 .setOnClickListener( new View.OnClickListener() {
909@Override
910public void onClick(View view) {
911curr = output .getText().toString();
912curr = curr + '1' ;
913output .setText( curr );
914} });
915num2 .setOnClickListener( new View.OnClickListener() {
916@Override
917public void onClick(View view) {
918curr = output .getText().toString();
919curr = curr + '2' ;
920output .setText( curr );
921}
922});
923num3 .setOnClickListener( new View.OnClickListener() {
924@Override
925public void onClick(View view) {
926curr = output .getText().toString();
927curr = curr + '3' ;
928output .setText( curr );
929}
930});
931num4 .setOnClickListener( new View.OnClickListener() {
932@Override
933public void onClick(View view) {
934curr = output .getText().toString();
935curr = curr + '4' ;
936output .setText( curr );
937}
938});
939num5 .setOnClickListener( new View.OnClickListener() {
940@Override
941public void onClick(View view) {
942curr = output .getText().toString();
943curr = curr + '5' ;
944output .setText( curr );
945}
946});
947num6 .setOnClickListener( new View.OnClickListener() {
948@Override
949public void onClick(View view) {
950curr = output .getText().toString();
951curr = curr + '6' ;
952output .setText( curr );
953}
954});
955num7 .setOnClickListener( new View.OnClickListener() {
956@Override
957public void onClick(View view) {
958curr = output .getText().toString();
959curr = curr + '7' ;
960output .setText( curr );
961}
962});
963num8 .setOnClickListener( new View.OnClickListener() {
964@Override
965public void onClick(View view) {
966curr = output .getText().toString();
967curr = curr + '8' ;
968output .setText( curr );
969}
970});
971num9 .setOnClickListener( new View.OnClickListener() {
972@Override
973public void onClick(View view) {
974curr = output .getText().toString();
975curr = curr + '9' ;
976output .setText( curr );
977}
978});
979plus .setOnClickListener( new View.OnClickListener() {
980@Override
981public void onClick(View view) {
982curr = output .getText().toString();
983curr = curr + '+' ;
984output .setText( curr );
985}
986});
987minus .setOnClickListener( new View.OnClickListener() {
988@Override
989public void onClick(View view) {
990curr = output .getText().toString();
991curr = curr + '-' ;
992output .setText( curr );
993}
994});
995mul .setOnClickListener( new View.OnClickListener() {
996@Override
997public void onClick(View view) {
998curr = output .getText().toString();
999curr = curr + '*' ;
1000output .setText( curr );
1001}
1002});
1003div .setOnClickListener( new View.OnClickListener() {
1004@Override
1005public void onClick(View view) {
1006curr = output .getText().toString();
1007curr = curr + '/' ;
1008output .setText( curr );
1009}
1010});
1011clear .setOnClickListener( new View.OnClickListener() {
1012@Override
1013public void onClick(View view) {
1014output .setText( "" );
1015}
1016});
1017equal .setOnClickListener( new View.OnClickListener() {
1018@Override
1019public void onClick(View view) {
1020curr = output .getText().toString();
1021if ( curr .length()> 3 )
1022{
1023Toast. makeText (getApplicationContext(), "Please enter two
1024operands only!" , Toast. LENGTH_LONG ).show();
1025}
1026else
1027{
1028ip1 = Integer. parseInt ( curr .substring( 0 , 1 ));
1029ip2 = Integer. parseInt ( curr .substring( 2 , 3 ));
1030op = curr .substring( 1 , 2 );
1031switch ( op )
1032{
1033case "+" : output .setText( ip1 + ip2 + "" ); break ;
1034case "-" : output .setText( ip1 - ip2 + "" ); break ;
1035case "*" : output .setText( ip1 * ip2 + "" ); break ;
1036case "/" : output .setText( ip1 / ip2 + "" ); break ;
1037}
1038}
1039}
1040});
1041}
1042}
1043
1044
1045ANDROID : PHONEBOOK
1046ActivityMain.xml:
1047<?xml version="1.0" encoding="utf-8"?>
1048<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
1049 xmlns:app="http://schemas.android.com/apk/res-auto"
1050 xmlns:tools="http://schemas.android.com/tools"
1051 android:layout_width="match_parent"
1052 android:layout_height="match_parent"
1053 android:orientation="vertical"
1054 tools:context=".MainActivity" >
1055
1056 <EditText
1057 android:id="@+id/etName"
1058 android:layout_width="match_parent"
1059 android:layout_height="wrap_content"
1060 android:ems="10"
1061 android:inputType="textPersonName"
1062 android:hint="Name" />
1063
1064 <EditText
1065 android:id="@+id/etPhone"
1066 android:layout_width="match_parent"
1067 android:layout_height="wrap_content"
1068 android:ems="10"
1069 android:inputType="phone"
1070 android:hint="Phone Number"/>
1071
1072 <EditText
1073 android:id="@+id/etEmail"
1074 android:layout_width="match_parent"
1075 android:layout_height="wrap_content"
1076 android:ems="10"
1077 android:inputType="textEmailAddress"
1078 android:hint="Email"/>
1079
1080 <EditText
1081 android:id="@+id/etAddress"
1082 android:layout_width="match_parent"
1083 android:layout_height="wrap_content"
1084 android:ems="10"
1085 android:inputType="textPostalAddress"
1086 android:hint="Address"/>
1087
1088 <Button
1089 android:id="@+id/btnAdd"
1090 android:layout_width="match_parent"
1091 android:layout_height="wrap_content"
1092 android:text="Add" />
1093
1094 <Button
1095 android:id="@+id/btnView"
1096 android:layout_width="match_parent"
1097 android:layout_height="wrap_content"
1098 android:text="View Contacts" />
1099
1100</LinearLayout>
1101
1102
1103
1104ActivityView.xml:
1105<?xml version="1.0" encoding="utf-8"?>
1106<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
1107xmlns:app="http://schemas.android.com/apk/res-auto"
1108xmlns:tools="http://schemas.android.com/tools"
1109android:layout_width="match_parent"
1110android:layout_height="match_parent"
1111tools:context=".ViewActivity"
1112android:orientation="vertical">
1113
1114<TextView
1115 android:id="@+id/tvData"
1116 android:layout_width="match_parent"
1117 android:layout_height="wrap_content"
1118 android:text=""
1119 android:scrollbars="vertical"/>
1120</LinearLayout>
1121
1122
1123
1124MainActivity.java:
1125package com.example.a3_phonebook;
1126
1127import android.content.Intent;
1128import android.support.v7.app.AppCompatActivity;
1129import android.os.Bundle;
1130import android.view.View;
1131import android.widget.Button;
1132import android.widget.EditText;
1133import android.widget.Toast;
1134import static android.widget.Toast. makeText ;
1135public class MainActivity extends AppCompatActivity {
1136 static DatabaseHandler dbH ;
1137 EditText etName , etPhone , etAddress , etEmail ;
1138 Button btnAdd , btnView ;
1139 @Override
1140 protected void onCreate(Bundle savedInstanceState) {
1141 super .onCreate(savedInstanceState);
1142 setContentView(R.layout. activity_main );
1143 etAddress =(EditText)findViewById(R.id. etAddress );
1144 etEmail =(EditText)findViewById(R.id. etEmail );
1145 etName =(EditText)findViewById(R.id. etName );
1146 etPhone =(EditText)findViewById(R.id. etPhone );
1147 btnAdd =(Button)findViewById(R.id. btnAdd );
1148 btnView =(Button)findViewById(R.id. btnView );
1149 dbH = new DatabaseHandler( this );
1150 btnAdd .setOnClickListener( new View.OnClickListener() {
1151 @Override
1152 public void onClick(View v) {
1153 String name= etName .getText().toString();
1154 String phone= etPhone .getText().toString();
1155 String email= etEmail .getText().toString();
1156 String address= etAddress .getText().toString();
1157 int check= dbH .addContact(name,phone,email,address);
1158 if (check== 1 ){
1159 Toast. makeText (MainActivity. this , "CONTACT ADDED" ,Toast. LENGTH_SHORT ).show();
1160 etAddress .setText( "" );
1161 etPhone .setText( "" );
1162 etEmail .setText( "" );
1163 etName .setText( "" );
1164 etName .requestFocus();
1165 }
1166 else {
1167 Toast. makeText (MainActivity. this , "Issues" ,Toast. LENGTH_SHORT ).show();
1168 }
1169 }
1170 });
1171 btnView .setOnClickListener( new View.OnClickListener() {
1172 @Override
1173 public void onClick(View v) {
1174 Intent i= new Intent(MainActivity. this ,ViewActivity. class );
1175 startActivity(i);
1176 }
1177 });
1178 }
1179}
1180
1181
1182
1183ViewActivity.java:
1184package com.example.a3_phonebook;
1185
1186import android.support.v7.app.AppCompatActivity;
1187 import android.os.Bundle;
1188 import android.text.method.ScrollingMovementMethod;
1189 import android.widget.TextView;
1190public class ViewActivity extends AppCompatActivity {
1191 TextView tvData ;
1192 @Override
1193 protected void onCreate(Bundle savedInstanceState) {
1194 super .onCreate(savedInstanceState);
1195 setContentView(R.layout. activity_view );
1196 tvData =(TextView)findViewById(R.id. tvData );
1197 String data=MainActivity. dbH.viewContact();
1198 tvData .setMovementMethod( new ScrollingMovementMethod());
1199 if (data.length()== 0 )
1200 tvData .setText( "No records to show" );
1201 else
1202 tvData .setText(data);
1203 }
1204}
1205
1206
1207
1208
1209
1210DatabaseHandler.java:
1211package com.example.a3_phonebook;
1212
1213import android.content.ContentValues;
1214import android.content.Context;
1215import android.database.Cursor;
1216import android.database.sqlite.SQLiteDatabase;
1217import android.database.sqlite.SQLiteOpenHelper;
1218public class DatabaseHandler extends SQLiteOpenHelper {
1219 SQLiteDatabase db ;
1220 Context context ;
1221 public DatabaseHandler(Context context) {
1222 super (context, "contacts" , null , 1 );
1223 this . context =context;
1224 db = this .getWritableDatabase();
1225 }
1226 @Override
1227 public void onCreate(SQLiteDatabase db) {
1228 db.execSQL( "create table phone(name TEXT,phoneNo text,email text,address text)" );
1229 }
1230 @Override
1231 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
1232 }
1233 public int addContact(String name,String phone,String email,String address){
1234 ContentValues values= new ContentValues();
1235 values.put( "name" ,name);
1236 values.put( "phoneNo" ,phone);
1237 values.put( "email" ,email);
1238 values.put( "address" ,address);
1239 long rid= db .insert( "phone" , null ,values);
1240 if (rid< 0 )
1241 return 0 ;
1242 else
1243 return 1 ;
1244 }
1245 public String viewContact(){
1246 Cursor cursor= db .query( "phone" , new
1247 String[]{ "name" , "phoneNo" , "email" , "address" }, null , null , null , null , null );
1248 StringBuffer sb= new StringBuffer();
1249 cursor.moveToFirst();
1250 if (cursor.getCount()> 0 )
1251 do
1252 {
1253 sb.append( "Name: " +cursor.getString( 0 )+ " \n Phone No: " +cursor.getString( 1 )+ " \n Email: " +cursor.getString( 2 )+ " \n Address: " +cursor.getString( 3 )+ " \n -------------------------------------------- \n " );
1254 } while (cursor.moveToNext());
1255 return sb.toString();
1256 }
1257}
1258
1259ANDROID : TIC TAC TOE
1260Activity_main.xml:
1261<?xml version="1.0" encoding="utf-8"?>
1262<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
1263xmlns:app="http://schemas.android.com/apk/res-auto"
1264xmlns:tools="http://schemas.android.com/tools"
1265android:layout_width="match_parent"
1266android:layout_height="match_parent"
1267android:orientation="vertical"
1268tools:context=".MainActivity">
1269
1270<RelativeLayout
1271 android:layout_width="match_parent"
1272 android:layout_height="wrap_content">
1273
1274 <TextView
1275 android:id="@+id/p1"
1276 android:layout_width="wrap_content"
1277 android:layout_height="wrap_content"
1278 android:text="Player 1:0"
1279 android:textSize="30sp" />
1280
1281 <TextView
1282 android:id="@+id/p2"
1283 android:layout_width="wrap_content"
1284 android:layout_height="wrap_content"
1285 android:layout_below="@+id/p1"
1286 android:text="Player 2:0"
1287 android:textSize="30sp"/>
1288 <Button
1289 android:id="@+id/breset"
1290 android:layout_width="wrap_content"
1291 android:layout_height="wrap_content"
1292 android:text="Reset"
1293 android:layout_alignParentEnd="true"
1294 android:layout_centerVertical="true"
1295 android:layout_marginEnd="33dp"/>
1296</RelativeLayout>
1297<LinearLayout
1298 android:layout_width="match_parent"
1299 android:layout_height="0dp"
1300 android:layout_weight="1">
1301
1302 <Button
1303 android:id="@+id/button_00"
1304 android:layout_width="0dp"
1305 android:layout_height="match_parent"
1306 android:layout_weight="1"
1307 android:textSize="60sp"/>
1308
1309 <Button
1310 android:id="@+id/button_01"
1311 android:layout_width="0dp"
1312 android:layout_height="match_parent"
1313 android:layout_weight="1"
1314 android:textSize="60sp"/>
1315
1316 <Button
1317 android:id="@+id/button_02"
1318 android:layout_width="0dp"
1319 android:layout_height="match_parent"
1320 android:layout_weight="1"
1321 android:textSize="60sp"/>
1322
1323</LinearLayout>
1324
1325<LinearLayout
1326 android:layout_width="match_parent"
1327 android:layout_height="0dp"
1328 android:layout_weight="1">
1329
1330 <Button
1331 android:id="@+id/button_10"
1332 android:layout_width="0dp"
1333 android:layout_height="match_parent"
1334 android:layout_weight="1"
1335 android:textSize="60sp"/>
1336
1337 <Button
1338 android:id="@+id/button_11"
1339 android:layout_width="0dp"
1340 android:layout_height="match_parent"
1341 android:layout_weight="1"
1342 android:textSize="60sp"/>
1343
1344 <Button
1345 android:id="@+id/button_12"
1346 android:layout_width="0dp"
1347 android:layout_height="match_parent"
1348 android:layout_weight="1"
1349 android:textSize="60sp"/>
1350
1351</LinearLayout>
1352
1353<LinearLayout
1354 android:layout_width="match_parent"
1355 android:layout_height="0dp"
1356 android:layout_weight="1">
1357
1358 <Button
1359 android:id="@+id/button_20"
1360 android:layout_width="0dp"
1361 android:layout_height="match_parent"
1362 android:layout_weight="1"
1363 android:textSize="60sp"/>
1364
1365 <Button
1366 android:id="@+id/button_21"
1367 android:layout_width="0dp"
1368 android:layout_height="match_parent"
1369 android:layout_weight="1"
1370 android:textSize="60sp"/>
1371
1372 <Button
1373 android:id="@+id/button_22"
1374 android:layout_width="0dp"
1375 android:layout_height="match_parent"
1376 android:layout_weight="1"
1377 android:textSize="60sp"/>
1378
1379</LinearLayout>
1380
1381</LinearLayout>
1382
1383
1384MainActivity.java
1385package com.example.a4_tictactoe;
1386
1387
1388import android.support.v7.app.AppCompatActivity;
1389import android.os.Bundle;
1390import android.view.View;
1391import android.view.View.OnClickListener;
1392import android.widget.Button;
1393import android.widget.TextView;
1394import android.widget.Toast;
1395
1396public class MainActivity extends AppCompatActivity implements OnClickListener {
1397 private Button[][] buttons = new Button[3][3];
1398
1399 private boolean player1Turn = true;
1400
1401 private int roundCount;
1402
1403 private int player1Points;
1404 private int player2Points;
1405
1406 private TextView textViewPlayer1;
1407 private TextView textViewPlayer2;
1408
1409 @Override
1410 protected void onCreate(Bundle savedInstanceState) {
1411 super.onCreate(savedInstanceState);
1412 setContentView(R.layout.activity_main);
1413 textViewPlayer1 = findViewById(R.id.p1);
1414 textViewPlayer2 = findViewById(R.id.p2);
1415
1416 for (int i = 0; i < 3; i++) {
1417 for (int j = 0; j < 3; j++) {
1418 String buttonID = "button_" + i + j;
1419 int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
1420 buttons[i][j] = findViewById(resID);
1421 buttons[i][j].setOnClickListener(this);
1422 }
1423 }
1424
1425 Button buttonReset = findViewById(R.id.breset);
1426 buttonReset.setOnClickListener(new OnClickListener() {
1427 @Override
1428 public void onClick(View v) {
1429 resetGame();
1430 }
1431 });
1432 }
1433
1434 @Override
1435 public void onClick(View v) {
1436 if (!((Button) v).getText().toString().equals("")) {
1437 return;
1438 }
1439
1440 if (player1Turn) {
1441 ((Button) v).setText("X");
1442 } else {
1443 ((Button) v).setText("O");
1444 }
1445
1446 roundCount++;
1447
1448 if (checkForWin()) {
1449 if (player1Turn) {
1450 player1Wins();
1451 } else {
1452 player2Wins();
1453 }
1454 } else if (roundCount == 9) {
1455 draw();
1456 } else {
1457 player1Turn = !player1Turn;
1458 }
1459
1460 }
1461
1462 private boolean checkForWin() {
1463 String[][] field = new String[3][3];
1464
1465 for (int i = 0; i < 3; i++) {
1466 for (int j = 0; j < 3; j++) {
1467 field[i][j] = buttons[i][j].getText().toString();
1468 }
1469 }
1470
1471 for (int i = 0; i < 3; i++) {
1472 if (field[i][0].equals(field[i][1])
1473 && field[i][0].equals(field[i][2])
1474 && !field[i][0].equals("")) {
1475 return true;
1476 }
1477 }
1478
1479 for (int i = 0; i < 3; i++) {
1480 if (field[0][i].equals(field[1][i])
1481 && field[0][i].equals(field[2][i])
1482 && !field[0][i].equals("")) {
1483 return true;
1484 }
1485 }
1486
1487 if (field[0][0].equals(field[1][1])
1488 && field[0][0].equals(field[2][2])
1489 && !field[0][0].equals("")) {
1490 return true;
1491 }
1492
1493 if (field[0][2].equals(field[1][1])
1494 && field[0][2].equals(field[2][0])
1495 && !field[0][2].equals("")) {
1496 return true;
1497 }
1498
1499 return false;
1500 }
1501
1502 private void player1Wins() {
1503 player1Points++;
1504 Toast.makeText(this, "Player 1 wins!", Toast.LENGTH_SHORT).show();
1505 updatePointsText();
1506 resetBoard();
1507 }
1508
1509 private void player2Wins() {
1510 player2Points++;
1511 Toast.makeText(this, "Player 2 wins!", Toast.LENGTH_SHORT).show();
1512 updatePointsText();
1513 resetBoard();
1514 }
1515
1516 private void draw() {
1517 Toast.makeText(this, "Draw!", Toast.LENGTH_SHORT).show();
1518 resetBoard();
1519 }
1520
1521 private void updatePointsText() {
1522 textViewPlayer1.setText("Player 1: " + player1Points);
1523 textViewPlayer2.setText("Player 2: " + player2Points);
1524 }
1525
1526 private void resetBoard() {
1527 for (int i = 0; i < 3; i++) {
1528 for (int j = 0; j < 3; j++) {
1529 buttons[i][j].setText("");
1530 }
1531 }
1532
1533 roundCount = 0;
1534 player1Turn = true;
1535 }
1536
1537 private void resetGame() {
1538 player1Points = 0;
1539 player2Points = 0;
1540 updatePointsText();
1541 resetBoard();
1542 }
1543
1544 @Override
1545 protected void onSaveInstanceState(Bundle outState) {
1546 super.onSaveInstanceState(outState);
1547
1548 outState.putInt("roundCount", roundCount);
1549 outState.putInt("player1Points", player1Points);
1550 outState.putInt("player2Points", player2Points);
1551 outState.putBoolean("player1Turn", player1Turn);
1552 }
1553
1554 @Override
1555 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1556 super.onRestoreInstanceState(savedInstanceState);
1557
1558 roundCount = savedInstanceState.getInt("roundCount");
1559 player1Points = savedInstanceState.getInt("player1Points");
1560 player2Points = savedInstanceState.getInt("player2Points");
1561 player1Turn = savedInstanceState.getBoolean("player1Turn");
1562 }
1563}
1564
1565
1566
1567
1568
1569ANDROID: TIMER
1570activity_main.xml
1571<?xml version="1.0" encoding="utf-8"?>
1572<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
1573 android:layout_width="match_parent"
1574 android:layout_height="match_parent"
1575 android:orientation="vertical"
1576 android:padding="10dp">
1577
1578 <TextView
1579 android:id="@+id/tvCounter"
1580 android:layout_marginTop="150dp"
1581 android:layout_marginBottom="150dp"
1582 android:layout_width="wrap_content"
1583 android:layout_height="wrap_content"
1584 android:gravity="center"
1585 android:layout_gravity="center_horizontal"
1586 android:textSize="60sp"
1587 android:text="00:00:00"
1588 android:textStyle="bold"
1589 android:textColor="#4CAF50"/>
1590
1591 <EditText
1592 android:id="@+id/etMinutes"
1593 android:layout_width="wrap_content"
1594 android:layout_gravity="center"
1595 android:layout_height="wrap_content"
1596 android:layout_marginLeft="10dp"
1597 android:layout_marginRight="10dp"
1598 android:hint="Enter Minutes!"
1599 android:padding="10dp"
1600 android:maxLength="4"
1601 android:inputType="number"
1602 android:textSize="26sp"
1603 android:gravity="center"
1604 android:textColor="#000000"
1605 android:textColorHint="#9E9E9E" />
1606
1607 <LinearLayout
1608 android:layout_marginTop="30dp"
1609 android:orientation="horizontal"
1610 android:layout_width="fill_parent"
1611 android:gravity="center"
1612 android:padding="10dp"
1613 android:layout_gravity="center"
1614 android:layout_height="wrap_content">
1615
1616 <Button
1617 android:id="@+id/btnStartStopTimer"
1618 android:layout_width="wrap_content"
1619 android:layout_height="wrap_content"
1620 android:layout_marginRight="10dp"
1621 android:padding="10dp"
1622 android:background="#009688"
1623 android:textColor="#ffffff"
1624 android:textSize="16sp"
1625 android:text="Start Timer" />
1626
1627 <Button
1628 android:id="@+id/btnResetTimer"
1629 android:layout_width="wrap_content"
1630 android:layout_height="wrap_content"
1631 android:background="#009688"
1632 android:textColor="#ffffff"
1633 android:textSize="16sp"
1634 android:padding="10dp"
1635 android:layout_marginLeft="10dp"
1636 android:text="Reset Timer" />
1637
1638 </LinearLayout>
1639
1640</LinearLayout>
1641
1642
1643MainActivity.java:
1644package com.example.a5_androidtimer;
1645
1646import android.support.v7.app.AppCompatActivity;
1647import android.os.Bundle;
1648import android.os.CountDownTimer;
1649import android.view.View;
1650import android.view.WindowManager;
1651import android.widget.Button;
1652import android.widget.EditText;
1653import android.widget.TextView;
1654import android.widget.Toast;
1655
1656import java.util.concurrent.TimeUnit;
1657
1658public class MainActivity extends AppCompatActivity {
1659
1660 TextView tvCounter;
1661 EditText etMinutes;
1662 Button btnStartStopTimer, btnResetTimer;
1663 private static CountDownTimer countDownTimer;
1664
1665 @Override
1666 protected void onCreate(Bundle savedInstanceState) {
1667 super.onCreate(savedInstanceState);
1668 setContentView(R.layout.activity_main);
1669
1670 // Hide status bar
1671 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
1672
1673 // Hide action bar
1674 getSupportActionBar().hide();
1675
1676 tvCounter = findViewById(R.id.tvCounter);
1677 etMinutes = findViewById(R.id.etMinutes);
1678 btnStartStopTimer = findViewById(R.id.btnStartStopTimer);
1679 btnResetTimer = findViewById(R.id.btnResetTimer);
1680
1681 btnStartStopTimer.setOnClickListener(new View.OnClickListener() {
1682 @Override
1683 public void onClick(View view) {
1684 if (countDownTimer == null) {
1685 String getMinutes = etMinutes.getText().toString(); //Get minutes from editText
1686 //Check validation over editText
1687 if (!getMinutes.equals("") && getMinutes.length() > 0) {
1688 int noOfMinutes = Integer.parseInt(getMinutes);
1689 int milliseconds = noOfMinutes * 60 * 1000; //Convert minutes into milliseconds
1690
1691 countDownTimer = new CountDownTimer(milliseconds, 1000) {
1692 public void onTick(long millisUntilFinished) {
1693 long millis = millisUntilFinished;
1694 //Convert milliseconds into hour, minute and seconds
1695 long hours = TimeUnit.MILLISECONDS.toHours(millis);
1696 long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) -
1697 TimeUnit.HOURS.toMinutes(hours);
1698 long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) -
1699 TimeUnit.MINUTES.toSeconds(minutes);
1700 String hms = String.format("%02d:%02d:%02d", hours, minutes, seconds);
1701 tvCounter.setText(hms);
1702 }
1703
1704 public void onFinish() {
1705
1706 tvCounter.setText("TIME'S UP!!");
1707 countDownTimer = null;
1708 btnStartStopTimer.setText("Start Timer");
1709 }
1710 }.start();
1711 btnStartStopTimer.setText("Stop Timer");//Change Text
1712
1713 } else
1714 Toast.makeText(getApplicationContext(),
1715 "Please enter no. of Minutes.",
1716 Toast.LENGTH_SHORT).show();
1717 }
1718 else {
1719 countDownTimer.cancel();
1720 countDownTimer = null;
1721 btnStartStopTimer.setText("Start Timer");
1722 }
1723 }
1724 });
1725
1726 btnResetTimer.setOnClickListener(new View.OnClickListener() {
1727 @Override
1728 public void onClick(View view) {
1729 if (countDownTimer != null) {
1730 countDownTimer.cancel();
1731 countDownTimer = null;
1732 }
1733 btnStartStopTimer.setText("Start Timer");
1734 tvCounter.setText("00:00:00");
1735 }
1736 });
1737 }
1738}
1739
1740
1741
1742
1743
1744
1745
1746ANDROID : ALARM CLOCK
1747AndroidManifest.xml
1748<?xml version="1.0" encoding="utf-8"?>
1749<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1750 package="com.example.alarmclockmcc">
1751
1752 <application
1753 android:allowBackup="true"
1754 android:icon="@mipmap/ic_launcher"
1755 android:label="@string/app_name"
1756 android:roundIcon="@mipmap/ic_launcher_round"
1757 android:supportsRtl="true"
1758 android:theme="@style/AppTheme">
1759 <activity android:name=".MainActivity">
1760 <intent-filter>
1761 <action android:name="android.intent.action.MAIN" />
1762
1763 <category android:name="android.intent.category.LAUNCHER" />
1764 </intent-filter>
1765 </activity>
1766 <receiver android:name=".AlarmReceiver" >
1767 </receiver>
1768
1769 </application>
1770
1771</manifest>
1772
1773
1774activity_main.xml
1775<?xml version="1.0" encoding="utf-8"?>
1776<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
1777 android:layout_width="match_parent"
1778 android:layout_height="match_parent"
1779 android:orientation="vertical">
1780
1781 <TimePicker
1782 android:id="@+id/tpClock"
1783 android:layout_width="wrap_content"
1784 android:layout_height="wrap_content"
1785 android:layout_gravity="center" />
1786
1787 <ToggleButton
1788 android:id="@+id/togbtnSwitch"
1789 android:layout_width="wrap_content"
1790 android:layout_height="wrap_content"
1791 android:layout_gravity="center"
1792 android:layout_margin="20dp"
1793 android:checked="false"
1794 />
1795
1796</LinearLayout>
1797MainActivity.java
1798package com.karanmadhu.alarm;
1799
1800import android.support.v7.app.AppCompatActivity;
1801import android.os.Bundle;
1802import android.app.AlarmManager;
1803import android.app.PendingIntent;
1804import android.content.Intent;
1805import android.os.Bundle;
1806import android.view.View;
1807import android.view.WindowManager;
1808import android.widget.TimePicker;
1809import android.widget.Toast;
1810import android.widget.ToggleButton;
1811
1812import java.util.Calendar;
1813
1814public class MainActivity extends AppCompatActivity {
1815
1816 TimePicker alarmTimePicker;
1817 PendingIntent pendingIntent;
1818 AlarmManager alarmManager;
1819 ToggleButton togbtnSwitch;
1820
1821 @Override
1822 protected void onCreate(Bundle savedInstanceState) {
1823 super.onCreate(savedInstanceState);
1824 setContentView(R.layout.activity_main);
1825
1826 // Hide status bar
1827 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
1828
1829 // Hide action bar
1830 getSupportActionBar().hide();
1831
1832 alarmTimePicker = findViewById(R.id.tpClock);
1833 alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
1834 togbtnSwitch = findViewById(R.id.togbtnSwitch);
1835
1836 togbtnSwitch.setOnClickListener(new View.OnClickListener() {
1837 @Override
1838 public void onClick(View view) {
1839 long time;
1840 if (((ToggleButton) view).isChecked())
1841 {
1842 Toast.makeText(getApplicationContext(), "ALARM ON", Toast.LENGTH_SHORT).show();
1843 Calendar calendar = Calendar.getInstance();
1844 calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
1845 calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
1846 Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
1847 pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
1848
1849 time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
1850 if(System.currentTimeMillis()>time)
1851 {
1852 if (calendar.AM_PM == 0)
1853 time = time + (1000*60*60*12);
1854 else
1855 time = time + (1000*60*60*24);
1856 }
1857 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);
1858 }
1859 else
1860 {
1861 alarmManager.cancel(pendingIntent);
1862 Toast.makeText(getApplicationContext(), "ALARM OFF", Toast.LENGTH_SHORT).show();
1863 }
1864 }
1865 });
1866 }
1867}
1868
1869
1870
1871AlarmReceiver.java
1872package com.karanmadhu.alarm;
1873
1874import android.content.BroadcastReceiver;
1875import android.content.Context;
1876import android.content.Intent;
1877import android.media.Ringtone;
1878import android.media.RingtoneManager;
1879import android.net.Uri;
1880import android.widget.Toast;
1881
1882public class AlarmReceiver extends BroadcastReceiver
1883{
1884 @Override
1885 public void onReceive(Context context, Intent intent)
1886 {
1887 Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();
1888 Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
1889 if (alarmUri == null)
1890 {
1891 alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
1892 }
1893 Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
1894 ringtone.play();
1895 }
1896}
1897
1898
1899
1900
1901ANDROID : TRAFFIC LIGHT
1902Activity_main.xml
1903<?xml version="1.0" encoding="utf-8"?>
1904<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
1905 xmlns:app="http://schemas.android.com/apk/res-auto"
1906 android:orientation="vertical"
1907 xmlns:tools="http://schemas.android.com/tools"
1908 android:layout_width="match_parent"
1909 android:layout_height="match_parent"
1910 tools:context=".MainActivity">
1911 <TextView
1912 android:id="@+id/tvWhattodo"
1913 android:layout_width="match_parent"
1914 android:layout_height="wrap_content"
1915 android:textAlignment="center"
1916 android:text="" />
1917 <RadioGroup
1918 android:id="@+id/rgTrafficLight"
1919 android:orientation="vertical"
1920 android:layout_width="match_parent"
1921 android:layout_height="wrap_content" >
1922 <RadioButton
1923 android:id="@+id/rbGreen"
1924 android:layout_width="match_parent"
1925 android:layout_height="wrap_content"
1926 android:text="GREEN" />
1927 <RadioButton
1928 android:id="@+id/rgYellow"
1929 android:layout_width="match_parent"
1930 android:layout_height="wrap_content"
1931 android:text="YELLOW" />
1932 <RadioButton
1933 android:id="@+id/rgRed"
1934 android:layout_width="match_parent"
1935 android:layout_height="wrap_content"
1936 android:text="RED" />
1937 </RadioGroup>
1938 <Button
1939 android:id="@+id/btnGetResults"
1940 android:layout_width="match_parent"
1941 android:layout_height="wrap_content"
1942 android:text="Get Results" />
1943</LinearLayout>
1944
1945
1946
1947
1948MainActivity.java
1949package com.example.trafficlight;
1950import androidx.appcompat.app.AppCompatActivity;
1951import android.graphics.Color;
1952import android.os.Bundle;
1953import android.view.View;
1954import android.widget.Button;
1955import android.widget.RadioButton;
1956import android.widget.RadioGroup;
1957import android.widget.TextView;
1958import android.widget.Toast;
1959public class MainActivity extends AppCompatActivity {
1960 TextView tvWhattodo;
1961 RadioGroup rgTrafficLight;
1962 Button btnGetResults;
1963 @Override
1964 protected void onCreate(Bundle savedInstanceState) {
1965 super.onCreate(savedInstanceState);
1966 setContentView(R.layout.activity_main);
1967 tvWhattodo = findViewById(R.id.tvWhattodo);
1968 rgTrafficLight = findViewById(R.id.rgTrafficLight);
1969 btnGetResults = findViewById(R.id.btnGetResults);
1970 btnGetResults.setOnClickListener(new View.OnClickListener() {
1971 @Override
1972 public void onClick(View view) {
1973 int id = rgTrafficLight.getCheckedRadioButtonId();
1974 RadioButton rb = rgTrafficLight.findViewById(id);
1975 String signal = rb.getText().toString().trim();
1976 if(signal.equals("GREEN")){
1977 tvWhattodo.setText("GO");
1978 tvWhattodo.setTextColor(Color.GREEN);
1979 tvWhattodo.setTextSize(40);
1980 }else if(signal.equals("YELLOW")){
1981 tvWhattodo.setText("READY");
1982 tvWhattodo.setTextColor(Color.YELLOW);
1983 tvWhattodo.setTextSize(30);
1984 }else if(signal.startsWith("RED")){
1985 tvWhattodo.setText("STOP");
1986 tvWhattodo.setTextColor(Color.RED);
1987 tvWhattodo.setTextSize(50);
1988 }
1989 }
1990 });
1991 }
1992}
1993
1994
1995
1996ANDROID : TICTACTOE-METHOD 2
1997activity_main.xml
1998<?xml version="1.0" encoding="utf-8"?>
1999<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2000 xmlns:app="http://schemas.android.com/apk/res-auto"
2001 xmlns:tools="http://schemas.android.com/tools"
2002 android:layout_width="match_parent"
2003 android:layout_height="match_parent"
2004 android:orientation="vertical"
2005 tools:context=".MainActivity">
2006
2007 <TextView
2008 android:id="@+id/tv1"
2009 android:layout_width="match_parent"
2010 android:textSize="20dp"
2011 android:layout_height="wrap_content"
2012 android:text="Player 1 (X)" />
2013
2014 <TextView
2015 android:id="@+id/tv2"
2016 android:layout_width="match_parent"
2017 android:textSize="20dp"
2018 android:layout_height="wrap_content"
2019 android:text="Player 2 (O)" />
2020
2021 <LinearLayout
2022 android:layout_width="wrap_content"
2023 android:layout_marginTop="50dp"
2024 android:layout_height="wrap_content">
2025
2026
2027 <Button
2028 android:id="@+id/btn00"
2029 android:layout_marginLeft="30dp"
2030 android:layout_width="wrap_content"
2031 android:layout_height="wrap_content"
2032 android:layout_weight="1"
2033 android:text="" />
2034
2035 <Button
2036 android:id="@+id/btn01"
2037 android:layout_marginLeft="30dp"
2038 android:layout_width="wrap_content"
2039 android:layout_height="wrap_content"
2040 android:layout_weight="1"
2041 android:text="" />
2042
2043 <Button
2044 android:id="@+id/btn02"
2045 android:layout_width="wrap_content"
2046 android:layout_marginLeft="30dp"
2047 android:layout_height="wrap_content"
2048 android:layout_weight="1"
2049 android:text="" />
2050 </LinearLayout>
2051 <LinearLayout
2052 android:layout_width="wrap_content"
2053 android:layout_marginTop="50dp"
2054 android:layout_height="wrap_content">
2055
2056
2057 <Button
2058 android:id="@+id/btn10"
2059 android:layout_marginLeft="30dp"
2060 android:layout_width="wrap_content"
2061 android:layout_height="wrap_content"
2062 android:layout_weight="1"
2063 android:text="" />
2064
2065 <Button
2066 android:id="@+id/btn11"
2067 android:layout_marginLeft="30dp"
2068 android:layout_width="wrap_content"
2069 android:layout_height="wrap_content"
2070 android:layout_weight="1"
2071 android:text="" />
2072
2073 <Button
2074 android:id="@+id/btn12"
2075 android:layout_width="wrap_content"
2076 android:layout_marginLeft="30dp"
2077 android:layout_height="wrap_content"
2078 android:layout_weight="1"
2079 android:text="" />
2080 </LinearLayout>
2081 <LinearLayout
2082 android:layout_width="wrap_content"
2083 android:layout_marginTop="50dp"
2084 android:layout_height="wrap_content">
2085
2086
2087 <Button
2088 android:id="@+id/btn20"
2089 android:layout_marginLeft="30dp"
2090 android:layout_width="wrap_content"
2091 android:layout_height="wrap_content"
2092 android:layout_weight="1"
2093 android:text="" />
2094
2095 <Button
2096 android:id="@+id/btn21"
2097 android:layout_marginLeft="30dp"
2098 android:layout_width="wrap_content"
2099 android:layout_height="wrap_content"
2100 android:layout_weight="1"
2101 android:text="" />
2102
2103 <Button
2104 android:id="@+id/btn22"
2105 android:layout_width="wrap_content"
2106 android:layout_marginLeft="30dp"
2107 android:layout_height="wrap_content"
2108 android:layout_weight="1"
2109 android:text="" />
2110 </LinearLayout>
2111</LinearLayout>
2112
2113
2114MainActivity.java
2115package com.example.tictactoeee;
2116
2117import androidx.appcompat.app.AppCompatActivity;
2118
2119import android.os.Bundle;
2120import android.view.View;
2121import android.widget.Button;
2122import android.widget.TextView;
2123import android.widget.Toast;
2124
2125public class MainActivity extends AppCompatActivity implements View.OnClickListener {
2126 TextView tv1,tv2;
2127 private Button [][] btn=new Button[3][3];
2128 private int roundCount=0;
2129 private Boolean player1Tern=true;
2130
2131 @Override
2132 protected void onCreate(Bundle savedInstanceState) {
2133 super.onCreate(savedInstanceState);
2134 setContentView(R.layout.activity_main);
2135 tv1=findViewById(R.id.tv1);
2136 tv2=findViewById(R.id.tv2);
2137 for(int i=0;i<3;i++)
2138 {
2139 for(int j=0;j<3;j++)
2140 {
2141 String id_name="btn"+i+j; //btn_00 ,btn_01,btn_02
2142 int btnId=this.getResources().getIdentifier(id_name,"id",getPackageName());
2143 btn[i][j]=findViewById(btnId); //findViewById(R.id.btn_00);
2144 btn[i][j].setOnClickListener(MainActivity.this);
2145 }
2146 }
2147 }//end of protected
2148 @Override
2149 public void onClick(View v){
2150 if(player1Tern){
2151 ((Button)v).setText("X");
2152 ((Button)v).setEnabled(false);
2153 }
2154 else {
2155 ((Button) v).setText("O");
2156 ((Button) v).setEnabled(false);
2157 }
2158 roundCount++;
2159 if(chechForWin()){
2160 if(player1Tern){
2161 Toast.makeText(this, "Player 1 wins", Toast.LENGTH_SHORT).show();
2162 resertBoard();
2163 }
2164 else{
2165 Toast.makeText(this, "Player 2 wins", Toast.LENGTH_SHORT).show();
2166 resertBoard();
2167 }
2168 } else if(roundCount==9){
2169 Toast.makeText(this, "Draw", Toast.LENGTH_SHORT).show();
2170 resertBoard();
2171 }else {
2172 player1Tern = !player1Tern;
2173 }
2174 }//end of onClick
2175 private void resertBoard(){
2176 for(int i=0;i<3;i++){
2177 for (int j=0;j<3;j++){
2178 btn[i][j].setText("");
2179 btn[i][j].setEnabled(true);
2180 }
2181 }
2182 roundCount=0;
2183 player1Tern=true;
2184 }//end of resertBoard
2185 private boolean chechForWin() {
2186 String field[][] = new String[3][3];
2187 for (int i = 0; i < 3; i++) {
2188 for (int j = 0; j < 3; j++) {
2189 field[i][j] = btn[i][j].getText().toString();
2190 }
2191 }
2192 //for rows
2193 for (int i = 0; i < 3; i++) {
2194 if (field[i][0].equals(field[i][1]) && field[i][0].equals(field[i][2]) && !(field[i][0].equals(""))) {
2195 return true;
2196 }
2197 }
2198 //for column
2199 for (int i = 0; i < 3; i++) {
2200 if (field[0][i].equals(field[1][i]) && field[0][i].equals(field[2][i]) && !(field[0][i].equals(""))) {
2201 return true; }
2202 }
2203 //for diagonal
2204 if(field[0][0].equals(field[1][1])&&field[0][0].equals(field[2][2])&&!(field[0][0].equals(""))) {
2205 return true; }
2206 //reverse diagonal
2207 if(field[0][2].equals(field[1][1])&&field[0][2].equals(field[2][0])&&!(field[0][2].equals(""))){
2208 return true; }
2209 return false;
2210 }//end for checkForWin
2211}//end of public main
2212
2213
2214
2215ANDROID : GUI COMPONENTS
2216Activity_main.xml:
2217<?xml version="1.0" encoding="utf-8"?>
2218<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2219 xmlns:app="http://schemas.android.com/apk/res-auto"
2220 xmlns:tools="http://schemas.android.com/tools"
2221 android:layout_width="match_parent"
2222 android:layout_height="match_parent"
2223 android:orientation="vertical"
2224 android:background="#bfefff"
2225 tools:context=".MainActivity"
2226 android:layout_gravity="center">
2227
2228 <TextView
2229 android:id="@+id/tvWelcome"
2230 android:layout_width="match_parent"
2231 android:layout_height="wrap_content"
2232 android:text="Welcome to Android"
2233 android:gravity="center"
2234 android:color="#fa1207"
2235 android:typeface="monospace"
2236 />
2237
2238 <EditText
2239 android:id="@+id/etMessage"
2240 android:layout_width="match_parent"
2241 android:layout_height="wrap_content"
2242 android:ems="10"
2243 android:inputType="textPersonName"
2244 android:hint="Enter message"/>
2245
2246 <Button
2247 android:id="@+id/btnSubmit"
2248 android:layout_width="wrap_content"
2249 android:layout_height="wrap_content"
2250 android:background="#71C412"
2251 android:layout_marginLeft="150dp"
2252 android:text="Submit"
2253 android:typeface="serif"
2254 android:textColor="#ffffff" />
2255</LinearLayout>
2256
2257
2258
2259MainActivity.java:
2260package com.example.guimcc2;
2261
2262import android.support.v7.app.AppCompatActivity;
2263import android.os.Bundle;
2264import android.view.View;
2265import android.widget.Button;
2266import android.widget.EditText;
2267import android.widget.TextView;
2268import android.widget.Toast;
2269
2270public class MainActivity extends AppCompatActivity {
2271 TextView tvWelcome;
2272 EditText etMessage;
2273 Button btnSubmit;
2274
2275 @Override
2276 protected void onCreate(Bundle savedInstanceState) {
2277 super.onCreate(savedInstanceState);
2278 setContentView(R.layout.activity_main);
2279 tvWelcome=(TextView)findViewById(R.id.tvWelcome);
2280 etMessage=(EditText)findViewById(R.id.etMessage);
2281 btnSubmit=(Button)findViewById(R.id.btnSubmit);
2282 btnSubmit.setOnClickListener(new View.OnClickListener() {
2283 @Override
2284 public void onClick(View v) {
2285 String msg="Entered message is:\n"+etMessage.getText().toString();
2286 Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
2287 etMessage.setText("");
2288 etMessage.requestFocus();
2289
2290 }
2291 });
2292 }
2293}
2294
2295
2296ANDROID : GRAPHICAL PRIMITIVES (CIRCLE,RECTANGLE,LINE)
2297Activity_main.xml:
2298<?xml version="1.0" encoding="utf-8"?>
2299<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2300 xmlns:app="http://schemas.android.com/apk/res-auto"
2301 xmlns:tools="http://schemas.android.com/tools"
2302 android:layout_width="match_parent"
2303 android:layout_height="match_parent"
2304 tools:context=".MainActivity">
2305
2306
2307 <ImageView
2308 android:id="@+id/image"
2309 android:layout_width="match_parent"
2310 android:layout_height="match_parent"
2311 />
2312</RelativeLayout>
2313
2314
2315
2316MainActivity.java:
2317package com.example.basicprimitivesmcc;
2318
2319import android.graphics.Bitmap;
2320import android.graphics.Canvas;
2321import android.graphics.Color;
2322import android.graphics.Paint;
2323import android.graphics.drawable.BitmapDrawable;
2324import android.support.v7.app.AppCompatActivity;
2325import android.os.Bundle;
2326import android.widget.ImageView;
2327
2328public class MainActivity extends AppCompatActivity {
2329
2330 @Override
2331 protected void onCreate(Bundle savedInstanceState) {
2332 super.onCreate(savedInstanceState);
2333 setContentView(R.layout.activity_main);
2334
2335 Bitmap bm = Bitmap.createBitmap(720,1280,Bitmap.Config.ARGB_8888);
2336 ImageView i = (ImageView)findViewById(R.id.image);
2337 i.setBackgroundDrawable(new BitmapDrawable(bm));
2338 Canvas canvas=new Canvas(bm);
2339 Paint paint=new Paint();
2340 paint.setColor(Color.RED);
2341 paint.setTextSize(50);
2342 canvas.drawRect(400,200,600,700,paint);//left top right bottom
2343 canvas.drawCircle(200,200,100,paint);//cx,cy,radius
2344 canvas.drawRect(50,800,300,1100,paint);
2345 canvas.drawLine(500,800,500,1100,paint);//startx,starty,stopx,stopy
2346
2347 }
2348}
2349
2350
2351
2352
2353ANDROID : ALERT ON RECEIVING MESSAGE
2354Give permissions after installing apk in your phone
2355Here, it shows toast on submitting message
2356AndroidManifest.xml
2357<?xml version="1.0" encoding="utf-8"?>
2358<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2359 package="com.example.smsalert">
2360
2361
2362 <uses-permission android:name="android.permission.WRITE_SMS" />
2363 <uses-permission android:name="android.permission.READ_SMS" />
2364 <uses-permission android:name="android.permission.RECEIVE_SMS" />
2365
2366
2367 <application
2368 android:allowBackup="true"
2369 android:icon="@mipmap/ic_launcher"
2370 android:label="@string/app_name"
2371 android:roundIcon="@mipmap/ic_launcher_round"
2372 android:supportsRtl="true"
2373 android:theme="@style/AppTheme">
2374
2375 <receiver
2376 android:name=".SmsBroadcast" android:exported="true" >
2377 <intent-filter android:priority="999" >
2378 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
2379 </intent-filter>
2380 </receiver>
2381
2382 <activity android:name=".MainActivity">
2383 <intent-filter>
2384 <action android:name="android.intent.action.MAIN" />
2385
2386 <category android:name="android.intent.category.LAUNCHER" />
2387 </intent-filter>
2388 </activity>
2389 </application>
2390
2391</manifest>
2392
2393
2394Activity_main.xml:
2395<?xml version="1.0" encoding="utf-8"?>
2396<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2397 xmlns:app="http://schemas.android.com/apk/res-auto"
2398 xmlns:tools="http://schemas.android.com/tools"
2399 android:layout_width="match_parent"
2400 android:layout_height="match_parent"
2401 tools:context=".MainActivity">
2402
2403</LinearLayout>
2404
2405
2406
2407MainActivity.java
2408package com.example.smsalert;
2409
2410import androidx.appcompat.app.AppCompatActivity;
2411
2412import android.os.Bundle;
2413
2414public class MainActivity extends AppCompatActivity {
2415 @Override
2416 protected void onCreate(Bundle savedInstanceState) {
2417 super.onCreate(savedInstanceState);
2418 setContentView(R.layout.activity_main);
2419 }
2420}
2421
2422
2423SmsBroadcast.java:
2424package com.example.smsalert;
2425
2426import android.content.BroadcastReceiver;
2427import android.content.Context;
2428import android.content.Intent;
2429import android.os.Bundle;
2430import android.telephony.SmsMessage;
2431import android.widget.Toast;
2432
2433public class SmsBroadcast extends BroadcastReceiver {
2434 public static final String SMS_BUNDLE = "pdus";
2435
2436 public void onReceive(Context context, Intent intent) {
2437 Bundle intentExtras = intent.getExtras();
2438 if (intentExtras != null) {
2439 Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
2440 String smsMessageStr = "";
2441 for (int i = 0; i < sms.length; ++i) {
2442 SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
2443
2444 String smsBody = smsMessage.getMessageBody().toString();
2445 String address = smsMessage.getOriginatingAddress();
2446
2447 smsMessageStr += "SMS From: " + address + "\n";
2448 smsMessageStr += smsBody + "\n";
2449 }
2450 Toast.makeText(context, smsMessageStr, Toast.LENGTH_LONG).show();
2451
2452 }
2453 }
2454}
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467JAVA : CDMA
2468import java.util.*;
2469public class CDMATry{
2470 private int[][] walshTable;
2471 private int[][] copy;
2472 private int[] channelSeq;
2473
2474 public void setUp(int data[],int numStations){
2475 walshTable=new int[numStations][numStations];
2476 copy=new int[numStations][numStations];
2477
2478 buildWalshTable(numStations, 0, numStations-1, 0, numStations-1, false);
2479 showWalshTable(numStations);
2480
2481 for(int i=0;i<numStations;i++)
2482 for(int j=0;j<numStations;j++){
2483 copy[i][j]=walshTable[i][j];
2484 walshTable[i][j]*=data[i];
2485 }
2486 channelSeq=new int [numStations];
2487 for (int i=0;i<numStations;i++)
2488 for(int j=0;j<numStations;j++){
2489 channelSeq[i]+=walshTable[j][i];
2490 }
2491
2492 }
2493
2494 public void buildWalshTable(int len,int i1,int i2,int j1,int j2,boolean val){
2495 if(len==2){
2496 if(!val){
2497 walshTable[i1][j1]=1;
2498 walshTable[i1][j2]=1;
2499 walshTable[i2][j1]=1;
2500 walshTable[i2][j2]=-1;
2501 }
2502 else{
2503 walshTable[i1][j1]=-1;
2504 walshTable[i1][j2]=-1;
2505 walshTable[i2][j1]=-1;
2506 walshTable[i2][j2]=1;
2507 }
2508
2509 }
2510 else{
2511 int midi=(i1+i2)/2;
2512 int midj=(j1+j2)/2;
2513
2514 buildWalshTable(len/2,i1,midi,j1,midj,val);
2515 buildWalshTable(len/2,midi+1,i2,j1,midj,val);
2516 buildWalshTable(len/2,i1,midi,midj+1,j2,val);
2517 buildWalshTable(len/2,midi+1,i2,midj+1,j2,!val);
2518 }
2519
2520 }
2521 public void showWalshTable(int numStations){
2522 System.out.println("Walsh Table");
2523 for(int i=0;i<numStations;i++)
2524 {
2525 for(int j=0;j<numStations;j++){
2526 System.out.print(walshTable[i][j]+"\t");
2527 }
2528 System.out.println();
2529 }
2530
2531 }
2532 public void listenTo(int numStations,int sourceStation){
2533 int innerProduct=0;
2534 for (int i=0;i< numStations;i++)
2535 innerProduct+=copy[sourceStation][i]*channelSeq[i];
2536 System.out.println("Data received is : "+innerProduct/numStations);
2537 }
2538 public static void main(String args[]){
2539 CDMATry channel= new CDMATry();
2540 int numStations=4;
2541
2542 int[] data=new int[numStations];
2543 data[0]=-1;
2544 data[1]=-1;
2545 data[2]=0;
2546 data[3]=1;
2547 channel.setUp(data,numStations);
2548 int sourceStation=3;
2549 channel.listenTo(numStations,sourceStation);
2550 }
2551}
2552
2553Input:
25541 1 1 1
25551 -1 1 -1
25561 1 -1 -1
25571 -1 -1 1
2558
2559
2560JAVA: COFREQUENCY
2561import java.util.Scanner;
2562class Cofrequency{
2563 public static void main(String[] args) {
2564 int[][] arr = new int[11][11];
2565
2566 int i,j,k;
2567 Scanner sc=new Scanner(System.in);
2568 System.out.print("Enter i: ");
2569 i=sc.nextInt();
2570 System.out.print("Enter j: ");
2571 j=sc.nextInt();
2572
2573 int x,y;
2574 System.out.println("\nBefore");
2575 for(x=0;x<11;x++){
2576 for(y=0;y<11;y++){
2577 System.out.print(arr[x][y] + " ");
2578 }
2579 System.out.println();
2580 }
2581 arr[5][5]=2;
2582 arr[5+j][5+j+i]=1;
2583 arr[5-j][5+j+i]=1;
2584 arr[5+j][5-j-i]=1;
2585 arr[5-j][5-j-i]=1;
2586 arr[5-i-j-1][5]=1;
2587 arr[5+i+j+1][5]=1;
2588
2589 System.out.println("\nAfter");
2590 for(x=0;x<11;x++){
2591 for(y=0;y<11;y++){
2592 System.out.print(arr[x][y] + " ");
2593 }
2594 System.out.println();
2595 }
2596 for(i=2;i<=8;i++)
2597 for(j=3;j<8;j++)
2598 if (arr[i][j]==0)
2599 arr[i][j]=1;
2600 System.out.println("\nCluster Formation");
2601 for(x=0;x<11;x++){
2602 for(y=0;y<11;y++){
2603 System.out.print(arr[x][y] + " ");
2604 }
2605 System.out.println();
2606 }
2607
2608}
2609}
2610
2611
2612Input:
2613Enter i: 1
2614Enter j: 2
2615
2616
2617
2618CDMA : python
2619
2620def mult(c,d):
2621 return list(map(lambda x : x * d, c))
2622
2623c1=[1,1,1,1]
2624c2=[1,-1,1,-1]
2625c3=[1,1,-1,-1]
2626c4=[1,-1,-1,1]
2627C=[c1,c2,c3,c4]
2628
2629d=[int(x) for x in input("Enter data bits for 4 channels:").split()]
2630result=[]
2631for i in range(4):
2632 result.append(mult(C[i],d[i]))
2633
2634print(result)
2635channel=[]
2636for i in range(4):
2637 res=0
2638 for j in range(4):
2639 res+=result[j][i]
2640 channel.append(res)
2641
2642station=int(input("Enter station you want to listen:"))
2643
2644res2=0
2645for i in range(4):
2646 res2+=channel[i]*C[station-1][i]
2647
2648print("Data bit transmitted:",res2//4)
2649
2650Output:
2651Enter data bits for 4 channels:-1 -1 0 1
2652Enter station you want to listen:2
2653Data bit transmitted: -1
2654
2655
2656
2657
2658
2659A3 ALGORITHM:
2660import random
2661
2662print("A3 algo")
2663m = random.getrandbits(128)
2664print("RAND number provided is: ", m)
2665c,d = input("Enter key Ki present in SIM: ").split()
2666
2667# Any operation can be chosen below. Addition/Subtraction/Combination of them
2668n = int(c)**int(d)
2669ans = m + n
2670
2671y = 3 ** 100
2672z = m + y
2673
2674if (z==ans):
2675 print("Generated SRES has matched. User is authenticated.")
2676else:
2677 print("Generated SRES does not match. Please retry.")
2678
2679
2680
2681A3 algo (python 2)
2682table0=[197, 235, 60, 151, 98, 96, 3, 100, 248, 118, 42, 117, 172, 211, 181, 203, 61,
2683 126, 156, 87, 149, 224, 55, 132, 186, 63, 238, 255, 85, 83, 152, 33, 160,
2684 184, 210, 219, 159, 11, 180, 194, 130, 212, 147, 5, 215, 92, 27, 46, 113,
2685 187, 52, 25, 185, 79, 221, 48, 70, 31, 101, 15, 195, 201, 50, 222, 137,
2686 233, 229, 106, 122, 183, 178, 177, 144, 207, 234, 182, 37, 254, 227, 231, 54,
2687 209, 133, 65, 202, 69, 237, 220, 189, 146, 120, 68, 21, 125, 38, 30, 2,
2688 155, 53, 196, 174, 176, 51, 246, 167, 76, 110, 20, 82, 121, 103, 112, 56,
2689 173, 49, 217, 252, 0, 114, 228, 123, 12, 93, 161, 253, 232, 240, 175, 67,
2690 128, 22, 158, 89, 18, 77, 109, 190, 17, 62, 4, 153, 163, 59, 145, 138,
2691 7, 74, 205, 10, 162, 80, 45, 104, 111, 150, 214, 154, 28, 191, 169, 213,
2692 88, 193, 198, 200, 245, 39, 164, 124, 84, 78, 1, 188, 170, 23, 86, 226,
2693 141, 32, 6, 131, 127, 199, 40, 135, 16, 57, 71, 91, 225, 168, 242, 206,
2694 97, 166, 44, 14, 90, 236, 239, 230, 244, 223, 108, 102, 119, 148, 251, 29,
2695 216, 8, 9, 249, 208, 24, 105, 94, 34, 64, 95, 115, 72, 134, 204, 43,
2696 247, 243, 218, 47, 58, 73, 107, 241, 179, 116, 66, 36, 143, 81, 250, 139,
2697 19, 13, 142, 140, 129, 192, 99, 171, 157, 136, 41, 75, 35, 165, 26 ]
2698
2699table1=[170, 42, 95, 141, 109, 30, 71, 89, 26, 147, 231, 205, 239, 212, 124, 129, 216,
2700 79, 15, 185, 153, 14, 251, 162, 0, 241, 172, 197, 43, 10, 194, 235, 6,
2701 20, 72, 45, 143, 104, 161, 119, 41, 136, 38, 189, 135, 25, 93, 18, 224,
2702 171, 252, 195, 63, 19, 58, 165, 23, 55, 133, 254, 214, 144, 220, 178, 156,
2703 52, 110, 225, 97, 183, 140, 39, 53, 88, 219, 167, 16, 198, 62, 222, 76,
2704 139, 175, 94, 51, 134, 115, 22, 67, 1, 249, 217, 3, 5, 232, 138, 31,
2705 56, 116, 163, 70, 128, 234, 132, 229, 184, 244, 13, 34, 73, 233, 154, 179,
2706 131, 215, 236, 142, 223, 27, 57, 246, 108, 211, 8, 253, 85, 66, 245, 193,
2707 78, 190, 4, 17, 7, 150, 127, 152, 213, 37, 186, 2, 243, 46, 169, 68,
2708 101, 60, 174, 208, 158, 176, 69, 238, 191, 90, 83, 166, 125, 77, 59, 21,
2709 92, 49, 151, 168, 99, 9, 50, 146, 113, 117, 228, 65, 230, 40, 82, 54,
2710 237, 227, 102, 28, 36, 107, 24, 44, 126, 206, 201, 61, 114, 164, 207, 181,
2711 29, 91, 64, 221, 255, 48, 155, 192, 111, 180, 210, 182, 247, 203, 148, 209,
2712 98, 173, 11, 75, 123, 250, 118, 32, 47, 240, 202, 74, 177, 100, 80, 196,
2713 33, 248, 86, 157, 137, 120, 130, 84, 204, 122, 81, 242, 188, 200, 149, 226,
2714 218, 160, 187, 106, 35, 87, 105, 96, 145, 199, 159, 12, 121, 103, 112]
2715
2716
2717def comp128v23_internal(KXOR,RAND):
2718 """Internal part of the COMP128v23 algo, should not be called manually
2719 """
2720 temp = [0] * 16
2721 KM_RM = RAND + KXOR
2722
2723 for i in range(5):
2724 for z in range(16):
2725 temp[z] = table0[table1[KM_RM[16+z]] ^ KM_RM[z] ]
2726
2727 j = 0
2728 while ( (1 << i) > j):
2729 k = 0
2730 while ( (1 << (4 - i)) > k ):
2731 KM_RM[((2 * k + 1) << i )+j] = table0[table1[temp[(k << i) + j]] ^ (KM_RM[(k << i) + 16 + j])]
2732 KM_RM[ (k << (i + 1)) + j] = temp[(k << i) + j]
2733 k = k+1
2734 j = j + 1
2735
2736 output = [0]*16
2737
2738 for i in range(16):
2739 for j in range(8):
2740 output[i] = output[i] ^ (((KM_RM[(19 * (j + 8 * i) + 19) % 256 / 8] >> (3 * j + 3) % 8) & 1) << j)
2741
2742 return output
2743
2744def comp128v23(K, RAND, version = 2):
2745 """The entry point for COMP128v2 and COMP128v3 algorithm
2746 K = The secret Ki number (that should be inside of your SIM card) - Format: list of integers
2747 RAND = The random number generated by the tower - Format: list of integers
2748 version = Version selecting integer (can be 2 or 3) - Format: integer
2749 """
2750
2751 assert version in [2,3] , "This function only support COMP128 version 2 and 3!"
2752 assert len(K) == 16 , "Ki incorrect (length must be 16)"
2753 assert len(RAND) == 16 , "RAND incorrect (length must be 16)"
2754
2755 K_MIX = [0]*16
2756 RAND_MIX = [0]*16
2757 KATYVASZ = [0]*16
2758 output = [0]*16
2759
2760 for i in range(8):
2761 K_MIX[i] = K[15 - i]
2762 K_MIX[15 - i] = K[i]
2763
2764 for i in range(8):
2765 RAND_MIX[i] = RAND[15 - i]
2766 RAND_MIX[15 - i] = RAND[i]
2767
2768 for i in range(16):
2769 KATYVASZ[i] = K_MIX[i] ^ RAND_MIX[i]
2770
2771 for i in range(8):
2772 RAND_MIX = comp128v23_internal(KATYVASZ,RAND_MIX)
2773
2774 for i in range(16):
2775 output[i] = RAND_MIX[15-i]
2776
2777
2778 if version == 2:
2779 output[15] = 0
2780 output[14] = 4 * (output[14] >> 2)
2781
2782 s = 8
2783 i = 0
2784 while i < 4:
2785 output[s+i-4] = output[s+i]
2786 output[s+i] = output[s+i+4]
2787 i = i+1
2788
2789 #the algorithm uses 16 bytes until this point, but only 12 bytes are effective
2790 #also 12 bytes coming out from the SIM card
2791
2792 output_final = output[:12]
2793 return output_final
2794
2795
2796def hex2intarr(input):
2797 """converts hex string to an array of integers
2798 """
2799 return map(lambda a: int(a.encode('hex'),16), (a for a in input.decode('hex')))
2800
2801def intarr2hex(input):
2802 """converts array of integers to hex strings
2803 """
2804 return ''.join('{:02x}'.format(x) for x in input).upper()
2805
2806def rand_hex():
2807 arr=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E']
2808 return random.choice(arr)
2809
2810
2811if __name__ == '__main__':
2812 import argparse
2813 import random
2814
2815 sample_ki=""
2816 sample_rand=""
2817 for x in range(32):
2818 sample_ki = sample_ki+rand_hex()
2819 sample_rand = sample_rand+rand_hex()
2820 parser = argparse.ArgumentParser(description='Process some integers.')
2821 parser.add_argument('Ki', metavar='Ki', default = sample_ki ,nargs='?', help='The super secret Ki key')
2822 parser.add_argument('RAND', metavar='RAND', default = sample_rand, nargs='?', help='The RANDom number you recieve from the tower')
2823 parser.add_argument('version', metavar='version', default = 2, nargs='?', help='The version of the COMP128 algo you wish to use (options: 2 or 3)')
2824
2825 args = parser.parse_args()
2826
2827 Ki = hex2intarr(args.Ki)
2828 RAND = hex2intarr(args.RAND)
2829 version = args.version
2830
2831 print '----------- INPUT -------------'
2832 print 'COMP128 version ' + str(version)
2833 print 'Ki: ' + intarr2hex(Ki)
2834 print 'RAND: ' + intarr2hex(RAND)
2835
2836 OUTPUT = comp128v23(Ki, RAND, version)
2837 SRES = OUTPUT[:4]
2838 Kc = OUTPUT[4:]
2839
2840 print '----------- OUTPUT -------------'
2841 print "SIM OUTPUT:" + intarr2hex(OUTPUT)
2842 print "SRES: " + intarr2hex(SRES)
2843 print "Kc: " + intarr2hex(Kc)