Ilovepdf - Merged (
Ilovepdf - Merged (
Experiment 2.1
1. Aim:
To create an Android app that uses Intent and one button to create a page and passes values
from one activity to another.
2. Objective:
The objective of this experiment is to understand the concept of Intents in Android app
development and learn how to pass data between different activities within an application.
By implementing this, students will grasp the fundamental mechanism of interactivity and
data exchange in Android apps.
3. Apparatus/Requirements:
Android Studio installed and configured.
Android device or emulator for testing the application.
4. Procedure:
1. Launch Android Studio and create a new Android project.
2. Design the user interface with at least one Button.
3. Implement an OnClickListener for the Button to handle user clicks.
4. Within the OnClickListener, create an Intent to switch to another activity.
5. Pass values from the current activity to the destination activity using Intent extras.
6. Retrieve the passed values in the destination activity.
7. Display the received values or perform any desired operation with them.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment12">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
android:theme="@style/Theme.Experiment12">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
MainActivity.java:
package com.example.experiment12;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString();
if (!name.isEmpty()) {
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
intent.putExtra("NAME", name); // Pass the name to the next activity
startActivity(intent);
}
}
});
}
}
DisplayMessageActivity.java:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
package com.example.experiment12;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
String name = getIntent().getStringExtra("NAME");
TextView textViewMessage = findViewById(R.id.textViewMessage);
textViewMessage.setText("Hello, " + name + "!");
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextName"
android:layout_width="258dp"
android:layout_height="42dp"
android:layout_centerHorizontal="true"
android:hint="@string/enter_your_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buttonOK"
android:layout_width="128dp"
android:layout_height="54dp"
android:layout_below="@id/editTextName"
android:layout_centerHorizontal="true"
android:text="OK"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
app:layout_constraintTop_toBottomOf="@+id/editTextName" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_display_message.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayMessageActivity">
<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"/>
</RelativeLayout>
5. Observations/Outcome:
Successful creation of the user interface with at least one Button.
Implementation of OnClickListener to handle button clicks.
Proper usage of Intent to switch between activities.
Successful passing of values from one activity to another using Intent extras.
Correct retrieval and utilization of the passed values in the destination activity.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Learning Outcome:
Through this experiment, one will gain practical experience in using Intents for inter-
activity communication and data transfer in Android applications. They will learn how to
navigate between different screens/pages within an app and exchange relevant information,
which is a crucial skill for building interactive and dynamic mobile applications.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 1. 4
Student Name: Privyanshu Rajan UID: 21BCS4742
Branch: CSE Section/Group:SC-902-A
Semester: 6th Date of Performance: 5/2/24
Subject Name: Advanced Programming lab2 Subject Code: 21CSP-251
1. Aim:
• To Solve Missing Number
• To Solve Word Pattern
2. Objective:
• Given an array nums containing n distinct numbers in the range [0, n], return the
only number in the range that is missing from the array.
Example 1:
Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the
missing number in the range since it does not appear in nums.
Example 2:
Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the
missing number in the range since it does not appear in nums.
• Given a pattern and a string s, find if s follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in
pattern and a non-empty word in s.
Example 1:
Input: pattern = "abba", s = "dog cat cat
dog" Output: true Example 2:
Input: pattern = "abba", s = "dog cat cat
fish" Output: false Example 3:
Input: pattern = "aaaa", s = "dog cat cat dog"
Output: false
Missing Number
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Word Pattern
class Solution { public:
bool wordPattern(string pattern, string s) {
unordered_map<char, int> p2i;
unordered_map<string, int> w2i;
istringstream in(s); string word; int i =
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
0, n = pattern.size(); for(word;
in>>word; i++){
if(i==n || p2i[pattern[i]] != w2i[word])
return false;
p2i[pattern[i]] = w2i[word] = i+1; }
return i==n;}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 2.1
Given the roots of two binary trees p and q, write a function to check if they are the
same or not.
Two binary trees are considered the same if they are structurally identical, and the
nodes have the same value.
Code:
class Solution
{
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null || q == null) return p == q;
return p.val == q.val && //
isSameTree(p.left, q.left) && //
isSameTree(p.right, q.right);
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric
around its center).
Code:
class Solution { public boolean
isSymmetric(TreeNode root) { return
isSymmetric(root, root);
}
private boolean isSymmetric(TreeNode p, TreeNode q) {
if (p == null || q == null) return p == q;
}
}
Output: