Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
MAP524/DPS924 – MOBILE APP DEVELOPMENT (ANDROID) MIDTERM TEST – OCTOBER 2013 STUDENT NAME ____________________________ STUDENT NUMBER _________________________ Please answer all questions on the question sheet This is an open book/notes test. You are allowed to use 1 text book plus your own notes. No electronic aids are allowed. This is a 90 minute test and is worth 30% of your final grade. There are 22 questions worth 40 marks in total. Multiple Choice. NOTE: Each question has only one BEST answer. Each question is worth one mark. 01. The plus (+) sign in the statement “android:id="@+id/my_id" will a) identify it as an id resource. b) create a new id and add it to the project resources. c) call the xml parser to expand the id string. d) both a) and b). 02. Which of the following Java code samples could be used to apply an XML layout resource to an Activity? a) findViewById(R.id.help); b) setContentView(R.layout.help); c) addContentView(R.layout.help); d) setVisible(true); 03. Which of the following is not one of the pre-defined Android View Groups? a) RelativeLayout b) FixedLayout c) GridLayout d) TableLayout 04. Which of these methods is called first when an Activity is initially launched? a) onStart() b) onPause() c) onCreate() d) onResume() 05. In which of the following locations would you include XML code to define a menu resource, including the menu items? a) res/xml/main_menu.xml b) res/menu/main_menu.xml c) res/menus/main_menu.xml d) res/layout/main_menu.xml 06. Which of the following defines a row within the XML for a TableLayout? a) <TableRow> b) <Row> c) <TableRowView> d) <RowView> 07. Which of the following classes is used to display a message only for a moment? a) Alert b) Context c) Toast d) Message 08. Which of the following is the correct signature for the "onClick" method in an OnClickListener? a) public void onClick(Activity a) b) public void onClick(View v) c) public void onClick(Context c) d) public View onClick(this) 09. Which of the following is used to bind data to a layout View? a) Activity b) Context c) Intent d) Adapter 10. When a) on b) on c) on d) on does a context menu appear? pressing long-pressing pressing the menu button swiping Write one line Android CLI command for each of the following. Each question is worth 1 mark. 11. List all current Android devices. adb devices or android list avd 12. Copy a local file named “android.mp4” to the /sdcard/movies directory. adb push ~/android.mp4 /sdcard/movies/android.mp4 13. Copy a database file named “students.db” from your phone to a local file. The package name is “ca.on.senecac.myapp”. adb pull /data/data/ca.on.senecac.myapp/databases/students.db ~/students.db 14. Start a shell on the current Android device. adb shell 15. Install an Android application package named “MyApp.apk” onto the current Android device. adb install MyApp.apk 16. View the current logcat file. adb logcat 17. Create a new Android project with package name “com.example.helloandroid” and activity name of “HelloAndroid”. android create project –name HelloAndroid –package com.example.helloandroid 18. Launch an emulator with name of “my_phone”. emulator -avd my_phone 19. List all packages installed on the current Android device. adb shell pm list packages 20. Compile your project in debug mode. ant debug 21. Draw the layout represented by the following XML file. Identify the colours used for the texts and backgrounds as well as the position of all views on the screen. [ 10 marks ] <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00f" android:gravity="center" android:padding="25dp" android:text="RED" android:textColor="#fff" /> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="#ffff00" android:gravity="center" android:padding="25dp" android:text="GREEN" android:textColor="#000" /> <TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/TextView02" android:background="#0f0" android:gravity="center" android:padding="25dp" android:text="YELLOW" android:textColor="#000" /> <TextView android:id="@+id/TextView04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/TextView03" android:background="#ff0000" android:gravity="center" android:padding="25dp" android:text="BLACK" android:textColor="#fff" /> <TextView android:id="@+id/TextView05" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#000" android:gravity="center" android:padding="25dp" android:text="BLUE" android:textColor="#fff" /> </RelativeLayout> 22. The following is an incomplete Android database application which is supposed to add, find and delete items from a local database. Examine the code (yes it's long) and complete the app by inserting the missing lines of code. Note: there are 10 places with missing lines. Each correct line is worth 1 mark. res/layouts/activity_database.xml <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".DatabaseActivity" > <TextView android:id="@+id/productID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="36dp" android:layout_toRightOf="@+id/button1" android:text="@string/product_id" /> <EditText android:id="@+id/productName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button2" android:layout_below="@+id/productID" android:layout_marginTop="40dp" android:text="@string/product_name"/> _________________________ #1 <EditText android:id="@+id/productQuantity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/productName" android:layout_below="@+id/productName" android:layout_marginTop="50dp" android:text="@string/product_quantity"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:onClick="newProduct" android:text="Add" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_centerHorizontal="true" android:onClick="lookupProduct" android:text="Find" /> ___________________ #2 <Button android:id="@+id/button3" android:onClick="removeProduct" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:text="Delete" /> </RelativeLayout> res/values/strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">DatabaseActivity</string> <string name="action_settings">Settings</string> <string name="product_name">Product Name</string> <string name="product_id">Product ID</string> _______________________ #3 <string name="product_quantity">Product Quantity</string> </resources> DatabaseActivity.java package com.example.databaseactivity; import import import import import android.os.Bundle; android.app.Activity; android.view.View; android.widget.EditText; android.widget.TextView; public class DatabaseActivity extends Activity { TextView idView; EditText productBox; EditText quantityBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_database); idView = (TextView) findViewById(R.id.productID); ____________ #4 productBox = (EditText) findViewById(R.id.productName); quantityBox = (EditText) findViewById(R.id.productQuantity); } public void newProduct (View view) { MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1); int quantity = Integer.parseInt(quantityBox.getText().toString()); Product product = new Product(productBox.getText().toString(), quantity); } dbHandler.addProduct(product); productBox.setText(""); quantityBox.setText(""); public void lookupProduct (View view) { MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1); Product product = dbHandler.findProduct(productBox.getText().toString()); } if (product != null) { idView.setText(String.valueOf(product.getID())); quantityBox.setText(String.valueOf(product.getQuantity())); } else { idView.setText("No Match Found"); ________________________ #5 } public void removeProduct (View view) { MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1); boolean result = dbHandler.deleteProduct( productBox.getText().toString()); } if (result) { idView.setText("Record Deleted"); productBox.setText(""); quantityBox.setText(""); } else idView.setText("No Match Found"); } MyDBHandler.java package com.example.databaseactivity; import import import import import import android.content.ContentValues; android.content.Context; android.database.Cursor; android.database.sqlite.SQLiteDatabase; android.database.sqlite.SQLiteDatabase.CursorFactory; android.database.sqlite.SQLiteOpenHelper; public class MyDBHandler extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "productDB.db"; private static final String TABLE_PRODUCTS = "products"; public static final String COLUMN_ID = "_id"; public static final String COLUMN_PRODUCTNAME = "productname"; public static final String COLUMN_QUANTITY = "quantity"; _____________ #6 public MyDBHandler(Context context, String name, CursorFactory factory, int version) { super(context, DATABASE_NAME, factory, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_PRODUCTNAME + " TEXT," + COLUMN_QUANTITY + " INTEGER" + ")"; db.execSQL(CREATE_PRODUCTS_TABLE); ________________________ #7 } @Override public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS); onCreate(db); ___________ #8 } public void addProduct(Product product) { ContentValues values = new ContentValues(); values.put(COLUMN_PRODUCTNAME, product.getProductName()); values.put(COLUMN_QUANTITY, product.getQuantity()); SQLiteDatabase db = this.getWritableDatabase(); db.insert(TABLE_PRODUCTS, null, values); db.close(); } public Product findProduct(String productname) { String query = "Select * FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + " = \"" + productname + "\""; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(query, null); Product product = new Product(); } if (cursor.moveToFirst()) { cursor.moveToFirst(); product.setID(Integer.parseInt(cursor.getString(0))); product.setProductName(cursor.getString(1)); product.setQuantity(Integer.parseInt(cursor.getString(2))); cursor.close(); } else { product = null; } db.close(); return product; public boolean deleteProduct(String productname) { boolean result = false; String query = "Select * FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + " = \"" + productname + "\""; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(query, null); _________________ #9 Product product = new Product(); } if (cursor.moveToFirst()) { product.setID(Integer.parseInt(cursor.getString(0))); db.delete(TABLE_PRODUCTS, COLUMN_ID + " = ?", new String[] { String.valueOf(product.getID()) }); cursor.close(); result = true; } db.close(); return result; } Product.java package com.example.databaseactivity; public class Product { private int _id; private String _productname; private int _quantity; public Product() { } public Product(int id, String productname, int quantity) { this._id = id; this._productname = productname; this._quantity = quantity; } public Product(String productname, int quantity) { this._productname = productname; this._quantity = quantity; } public void setID(int id) { this._id = id; } public int getID() { return this._id; } public void setProductName(String productname) { this._productname = productname; } public String getProductName() { return this._productname; } public void setQuantity(int quantity) { this._quantity = quantity; } public int getQuantity() { return this._quantity; } } __________________________ #10