Search the whole station

高级Java编程代写 移动应用程序开发代写 Programming代写

COMPS311 Advanced Java Programming and Mobile Application Development

高级Java编程代写 Instructions: 1. Answer this examination paper in English.2. Read the rubric(s) in the examination paper carefully and write your answers in the

Instructions:

  1. Answer this examination paper in English.
  2. Read the rubric(s) in the examination paper carefully and write your answers in the answer book as specified. Answers recorded elsewhere will not be marked. Begin each question on a new page and write the question number at the top of each page you have worked on.
  3. Write any rough work in the answer book or this examination paper and cross it through afterwards. Rough work will not be marked. No part of the answer book should be torn off.
  4. Write clearly. It may not be possible to award marks where the writing is very difficult to read.
  5. After the invigilator has announced that the examination has started, write your examination number, student number and course code on the cover of the examination paper and answer book as distributed by the invigilator. Failure to do so will mean that your work cannot be identified.
  6. At the end of the examination, hand over the examination paper and answer book to the invigilator.
  7. Do NOT open this examination paper until you are told to do so, otherwise you may be disqualified.[Inside Front Cover. This page is intentionally blank.]

Appendix is attached at the end of the paper, which contains some useful Java and Android APIs.

Part I [60 marks] 高级Java编程代写

You should attempt ALL questions in this part of the paper. This part carries 60% of the total examination marks. Ask for Answer Books if required and clearly identify yourself and your answer with the question number.

Question 1 [5 marks]

State whether each of the following statements is true or false:

(i) Thread is started by invoking the run() method.

(ii) A database connection can only be used in one query. Another database connection must be created for the next query.

(iii) JSP is a client-side script.

(iv) The secret key method can be used to protect the integrity of a message.

(v) Android application can only be developed by Java programming language.

Question 2 [6 marks] 高级Java编程代写

(a) State the output of executing this program.

public class ExceptionTest {
public static void main(String[] args) {
try {
System.out.println(10);
if (true) {
throw new RuntimeException();
}
System.out.println(20);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(30);
} catch (Exception e) {
System.out.println(40);
}
System.out.println(50);
}
}

(b) Complete the following code to convert the words to uppercase and collect the words that begin with “A”.

String[] words =
"Advanced Java Programming and Mobile Application Development"
.split(" ");
List<String> result = Stream.of(words) //Add code here ;
System.out.println(result); // [ADVANCED, AND, APPLICATION]

(c) Describe what an Optional object is, and how it can be used as a safer alternative than a reference that may be null.

Question 3 [7 marks]

Write a program CountIntegers that uses thread pool to count the total number of integers in text files within a directory. The program displays the total number of integers in each file and all files. Integers are delimited by whitespaces. A text file is a file with the .txt extension. Each text file is processed in a thread from the thread pool. The program should have two commandline parameters – the directory that contains text files, and the maximum number of threads in the thread pool.

For example, after executing the command:

> java CountIntegers filedir 5

May generate the output:

file1.txt has 5 integers
file3.txt has 3 integers
file2.txt has 8 integers
Total number of integers are 16.

Note that the order of the output corresponding to the number of integers for each of the file depends on which thread finishes first. You are not required to implement input validation of command-line parameters or handling of I/O errors.

Question 4 [6 marks] 高级Java编程代写

(a) Which methods of the Socket class do you use to open streams for reading data from and writing data to?

(b) State two advantages of using database system compared to file system.

(c) Suggest one scripting element for outputting an integer value in JSP.

Question 5 [6 marks]

(a) Consider the enforcement of this security policy file in a program.

grant
{
permission java.io.FilePermission "C:${/}Windows${/}", "read";
};

State whether each of the following operations is permitted.

(i) new File(“C:\\Windows\\”).list()

(ii) new File(“C:\\Windows\\file.txt”).exists()

(iii) new File(“C:\\Windows\\system32”).list()

(iv) new File(“C:\\Windows\\system32\\file.txt”).exists()[2]

(b) (i) What is the advantage of the secret key method over the public key method?

(ii) What is the advantage of the public key method over the secret key method?

Question 6 [8 marks]

(a) Given the distribution of Android devices in use:

高级Java编程代写

Which version has the largest share of the distribution? If you wish to target for 95% or more of Android devices, which API level should you use and what is the corresponding distribution?

(b) State two core system services provided by Linux kernel in Android system.

(c) Describe the three important loops for controlling an activity lifecycle.

Question 7 [10 marks]

(a) There are two ways in which the Handler class sends code to the UI thread for execution. What are they?

(b) In the listing below, the “run()” method of the Runnable class “PostTask” is defined to accumulate the total from 1 to value of the integer variable “max”. After the evaluation completed, the TextView object “result” will be updated with the accumulated result.

public class MainActivity extends AppCompatActivity implements
OnClickListener {
private Button button;
private TextView result;
private EditText input;
private long max;

@Override
public void onClick(View v) {
result.setText("Start compute...");
max = Long.parseLong(input.getText().toString().trim());
Runnable task = new PostTask();
Thread thread = new Thread(task);
thread.start();
}

private class PostTask implements Runnable {
@Override
public void run() {
long total = sum(max);
String resultText = "The accumulated value from 1 to " +
max + "is " + total;
result.setText(resultText);
}
}

// Method for summing up from 1 to the value of parameter input
public long sum(long input) {
// Add code for part
}
}

(i) Complete the “sum” method which returns the sum from 1 to the value of parameter “input”.

(ii) According to the rule of single thread model, UI component cannot be accessed outside from the UI thread. Modify the above program to solve this problem by using the “post(Runnable)” method of the “View” class.

Question 8 [4 marks]

Given the following listing, suggest an approach to improve the performance. Modify the code for illustration.


private class Ball {
...
void drawOn(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(color);
canvas.drawCircle(x, y, radius, paint);
}
}

It is assumed that the “drawOn” method is invoked several times for each second.

Question 9 [8 marks] 高级Java编程代写

(a) What types of transformation are supported by view animation in Android?

(b) In no more than 200 words, describes and illustrates with diagram about what you would see when the Android application program “MainActivity” is started.

Listing below depicts the layout resource file “main.xml”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Animation"
android:id="@+id/textView1" />
</LinearLayout>

Listing below depicts the animation resource file “animation.xml”

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000">
<alpha
android:fromAlpha="0"
android:toAlpha="1" />
<scale
android:fromXScale="0"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%" />
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%" >
</rotate>
</set>

Listing below depicts the activity class “MainActivity.java”

public class MainActivity extends AppCompatActivity {
  TextView textView1; // Corresponding to TextView with id textView1

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
…
    /* Performs animation on a TextView. */
    Animation animation =
      AnimationUtils.loadAnimation(this, R.anim.animation);
     textView1.startAnimation(animation);
  }
}

Part II [40 marks] 高级Java编程代写

Answer no more than FOUR (4) questions from this part of the paper. Each question is worth 10 marks. This part carries 40% of the total examination marks. Ask for Answer Books if required and clearly identify yourself and your answer with the question number.

Question 10 [10 marks]

Write a multithreaded Java TCP server. The communication between the client and server is described as follows.

– The server listens to requests at port 12345.
– When a client request arrives at the port, a new thread is created to serve it.
– Upon connection, the client sends to the server a string (in a line) that represents the client’ s name.
– The client then sends a number of strings. The server checks whether the string matches a connected client’s name; it returns a string of “yes” or “no” to reply whether there is a match.
– The client finally disconnects from the server by closing the socket at the client side.
You only need to implement the server, not the client.

[Hint: You need a data structure to store the names of currently-connected clients. Either use a built-in thread-safe collection, or implement synchronization in your code. You need to add the client’s name to the data structure when a client connects, and remove the name when the client disconnects. You can assume that all users behave normally and do not need to handle exceptional cases.]

Question 11 [10 marks]

In this question, you develop a JSP that displays product information in a database table. The database table, called Products, has three columns.

– A Name column, of type CHAR(30), contains the name of a product.
– A Code column, of type CHAR(8), contains the product code.
– A Quantity column, of type INTEGER, contains the quantity of the product in stock.

高级Java编程代写
高级Java编程代写

A skeleton of the JSP is given below. It contains the database connection information and the SQL command. Complete the JSP.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Check product stock</title>
  </head>
  <body>
    <%@page import="java.sql.*" %>
    <%
      String driver = "org.apache.derby.jdbc.ClientDriver";
      Class.forName(driver);
      String dbUrl = "jdbc:derby://localhost:1527/COREJAVA;create=true";
      String dbUsername = "dbuser";
      String dbPassword = "secret";
    %>

    // Add code here 

  </body>
</html>

Question 12 [10 marks] 高级Java编程代写

Develop two programs that perform DES encryption and decryption, respectively.

The encryption program works like this:

> java DesEncrypt keyFile inputFile outputFile

The keyFile is a file that contains a secret key using the DES algorithm. You should read the key as a SecretKey object using an ObjectInputStream. The inputFile contains the input to be encrypted, and the result is saved in the outputFile.

The decryption program works like this:

> java DesDecrypt keyFile inputFile outputFile

Again, the keyFile contains a secret key using the DES algorithm. The inputFile contains the file to be decrypted, and the result is saved in the outputFile.

Question 13 [10 marks] 高级Java编程代写

(a) Define an Android string resource with the ID author_name and content “Peter Au”. State how to refer to the resource ID in Java code.

(b) In this question, you work on an Android app that rolls three dice.

Listing below depicts the layout resource file “activity_main.xml”.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Dice Roller"
android:textSize="30sp" />

<TextView
android:id="@+id/dice1"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="2"
android:textSize="100sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/dice2"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="4"
android:textSize="100sp" />

<TextView
android:id="@+id/dice3"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="6"
android:textSize="100sp" />
</LinearLayout>

<Button
android:id="@+id/roll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:text="Roll!"
android:textSize="30sp" />

</LinearLayout>

(i) Illustrate with diagram about what you would see when the Android application that loads the layout file “activity_main.xml” is started.

(ii) Listing below depicts part of the activity class “MainActivity.java”.

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Add code here
}
}

Add code to the onCreate() method for the following features:

(i) Load the layout file “activity_main”.

(ii) Find the views of the three dice and the button, and keep them in variables.

(iii) Set up the button to handle click events. The handler generates three random integers, from 1 to 6 inclusive, and displays them in the three dice views.

Question 14 [10 marks] 高级Java编程代写

(a) Name and briefly describe the two forms of intents.

(b) The preference framework in Android provides a flexible and convenient mechanism for manipulating shared preferences.

Listing below depicts part of the string resource file “strings.xml”.


<!-- Key preference items and the corresponding default value -->
<string name="text_color_key">text_color</string>
<string name="text_color_default">White</string>

<string name="text_size_key">text_size</string>
<string name="text_size_default">20</string>

<string-array name="color_names">
<item>White</item>
<item>Red</item>
<item>Yellow</item>
<item>Green</item>
</string-array>

Listing below depicts the preference screen layout resource file “prefs.xml”.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory
android:title="Text settings">

<ListPreference
android:key="text_color"
android:title="Text color"
android:summary="Color of text in the game"
android:entries="@array/color_names"
android:entryValues="@array/color_names"
android:defaultValue="white" />

<EditTextPreference
android:key="text_size"
android:title="Text size"
android:summary="Size of text in the game"
android:inputType="number"
android:defaultValue="20" />

</PreferenceCategory>

</PreferenceScreen>

Illustrates with diagrams about what you would see when the preference screen defined in the layout resource file “prefs.xml” is shown. Sketches with diagram if preference component such as CheckBoxPreference, EditTextPreference, ListPreference, or MultiSelectListPreference is declared and selected.

(c) Specify the interface or abstract classes that used in the preference framework for representing the shared preferences and showing a screen of preferences for interacting with the user. Name the element used in the XML resource file for declaring a preference screen.

Please check that you have written your examination number and student number on the cover of each answer book used. Failure to do so will mean that your work cannot be identified. Please note that some classes, methods, attributes, variables, constants are omitted if they are not relevant.

Appendix A: Selected Java APIs 高级Java编程代写

高级Java编程代写
高级Java编程代写
高级Java编程代写
高级Java编程代写
高级Java编程代写
高级Java编程代写

更多代写:CS北美网课代修价格  pte代考被抓  英国Maths代写  article北美文科代写  北美作文代写  duolinguo考试作弊

合作平台:essay代写 论文代写 写手招聘 英国留学生代写

The prev: The next:

Related recommendations

1
您有新消息,点击联系!