AND 9 To 11
AND 9 To 11
1320084 BCA-507
Practical No.: 9
Aim: Develop an application demonstrating Internal Storage to store private data on the device memory.
Solution:
.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:padding="10dp"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Practical on Read and Write data on internal Storage device"
android:textColor="@color/black"
android:textSize="20sp"
android:textAlignment="center"/>
<EditText
android:id="@+id/userInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:inputType="textPersonName"
android:hint="Enter data here"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/write_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Write" />
<Button
android:id="@+id/read_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Read" />
<TextView
Roll No. 1320084 BCA-507
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" File Content "
android:textAlignment="center"
android:layout_marginTop="20sp"
android:textColor="#000"
android:textSize="25sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:textAlignment="center"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:textColor="#000"/>
</LinearLayout>
.java file:
package com.example.practicals_exmp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Roll No. 1320084 BCA-507
read = findViewById(R.id.read_button);
write = findViewById(R.id.write_button);
userInput = findViewById(R.id.userInput);
fileContent = findViewById(R.id.content);
read.setOnClickListener(this);
write.setOnClickListener(this);
}
public void printMessage(String m) {
Toast.makeText(this, m, Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View view) {
Button b = (Button) view;
switch (b_text.toLowerCase()) {
case "write": {
writeData();
break;
}
case "read": {
readData();
break;
}
}
}
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
String data = userInput.getText().toString();
fos.write(data.getBytes());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
userInput.setText("");
printMessage("writing to file " + filename + "completed...");
}
Output:
Roll No. 1320084 BCA-507
Practical No.: 10
Aim: Create an application to make insert, delete, and update and retrieve operation on database.
Solution:
.Xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:padding="10dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Database operations"
android:textSize="30sp"
android:textColor="@color/black"
android:textAlignment="center"
android:layout_marginTop="30sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:layout_marginTop="100dp"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Surname"
android:id="@+id/textView2"
android:layout_below="@+id/editText_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
Roll No. 1320084 BCA-507
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Marks"
android:id="@+id/textView3"
android:layout_below="@+id/editText_surname"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_name"
android:layout_marginLeft="35dp"
android:layout_alignTop="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_surname"
android:layout_marginLeft="5dp"
android:layout_alignTop="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:layout_toEndOf="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_Marks"
android:layout_below="@+id/editText_surname"
android:layout_toRightOf="@+id/textView3"
android:layout_marginLeft="32dp"
android:layout_toEndOf="@+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="id"
android:id="@+id/textView_id"
android:layout_below="@+id/editText_Marks"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_id"
android:layout_alignTop="@+id/textView_id"
Roll No. 1320084 BCA-507
android:layout_toRightOf="@+id/textView3"
android:layout_marginLeft="32dp"
android:layout_toEndOf="@+id/textView3"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="330dp"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Data"
android:id="@+id/button_add"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View All"
android:id="@+id/button_viewAll" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update"
android:id="@+id/button_update" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/button_delete" />
</LinearLayout>
</RelativeLayout>
.DatabaseHelper class:
package com.example.practicals_exmp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY
AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
DatabaseHelper myDb;
EditText editName,editSurname,editMarks ,editTextId;
Button btnAddData;
Button btnviewAll;
Button btnDelete;
Button btnviewUpdate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editName = (EditText)findViewById(R.id.editText_name);
editSurname = (EditText)findViewById(R.id.editText_surname);
editMarks = (EditText)findViewById(R.id.editText_Marks);
editTextId = (EditText)findViewById(R.id.editText_id);
btnAddData = (Button)findViewById(R.id.button_add);
btnviewAll = (Button)findViewById(R.id.button_viewAll);
btnviewUpdate= (Button)findViewById(R.id.button_update);
btnDelete= (Button)findViewById(R.id.button_delete);
AddData();
viewAll();
Roll No. 1320084 BCA-507
UpdateData();
DeleteData();
Output:
Roll No. 1320084 BCA-507
Output:
Roll No. 1320084 BCA-507
Practical No.: 11
Aim: Develop an application that demonstrates the working of a Automation testing using JUnit.
Solution:
junitTesting.java
package tests;
public class UnitTesting {
public int square(int n) {
return n*n;
}
public int sum(int a,int b) {
return a+b;
}}
SumUnit.java
package tests;
public class UnitTesting {
public int square(int n) {
return n*n;
}
public int sum(int a,int b) {
return a+b;
}}
Output: