Question

In: Computer Science

Your task is to build an Android application that satisfies the following requirements: Has two activities:...

Your task is to build an Android application that satisfies the following requirements:

  • Has two activities: MainActivity that implements the main functionality of the application and AboutActivity that shows your full name as per college record and student id

  • The AboutActivity should be available through a menu

  • A back button should appear in the ActionBar of the AboutActivity that takes user back

    to the MainActivity. It should be done through configuration!

  • MainActivity should implement the following functionality

    o read the following input from the user: § number of hours worked

    § hourly rate
    o calculate the pay using the following formula

    § if no of hours is less or equal than 40 then pay=no_of_hours*hourly_rate

    § else, pay=(no_of_hours-40)*hourly_rate*1.5 + 40*hourly_rate o calculate the tax using the following formula

    § tax=pay*0.18
    o present following data to the user

§ pay
§ overtime pay § total pay
§ tax

Solutions

Expert Solution

Hi there! You first need to create a new project and then add the required files. I have attached screenshots for steps and added comments for explanation in code. I have also attached screenshots of the app. Please upvote if it helped.

Steps to create new Project

1. Open Android Studio:

2. Click on Start a new Android Studio Project:

3.Create a new project on Android Studio(with an Empty Activity and package name as com.project.taxcalculator and Application Name of your choice):

Creation of the required files

Since we need menu to navigate to AboutActivity I will first show you how to create a menu xml file.

1. Make sure that you are in the Project type structure as shown in the following picture and then navigate to app->src->res and right click on res and then follow New->Android Resource File(If you cannot see the panel on the left press Alt+1)

2. Then in the following pop-up, select the resource type as menu and file name as menu.xml and click on OK.

3. Copy paste the following contents to app->res->menu->menu.xml created.

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<!--XML file for creating a menu with a single item-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/about_me"
        android:title="About me" />
</menu>

Copy paste the code in the following respective files:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<!--A linearlayout is used to display elements in a linear fashion, either horizontally or vertically-->
<!--the horizontal or vertical fashion can be specified using the orientation attribute of linearlayout-->
<!--Created a vertical linearlayout of horizontal linearlayouts-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="30dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<!--A layout_weight attribute gives us control about the space occupied by any element inside a linearlayout-->
<!--layout_weight:1 helps us evenly distribute the space between the children of a linearlayout        -->
<!--since width is controlled by weight we specify width as 0px-->
        <TextView
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Number of hours worked: " />

        <EditText
            android:id="@+id/num_hours_edit_text"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

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

        <TextView
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Hourly rate: " />

        <EditText
            android:id="@+id/hourly_rate_edit_text"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
<!--This button will be used for calculating and displaying tax and pay-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="calculateTaxAndPay"
        android:text="Calculate Tax and Pay" />

<!--All the following elements are used for displaying output-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Pay: " />

        <TextView
            android:id="@+id/pay_text_view"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

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

        <TextView
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Overtime pay: " />

        <TextView
            android:id="@+id/overtime_pay_text_view"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

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

        <TextView
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Total pay: " />

        <TextView
            android:id="@+id/total_pay_text_view"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

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

        <TextView
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Tax: " />

        <TextView
            android:id="@+id/tax_text_view"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

MainActivity.java

package com.project.taxcalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //creating EditTexts and TextViews as global variables so that they can be accessible inside the onClick method calculateTaxAndPay
    EditText noOfHoursEditText;
    EditText hourlyRateEditText;
    TextView payTextView, overtimePayTextView, totalPayTextView, taxTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // we need to get the EditTexts and TextViews we defined in XML into our Java file in order to take input and display output to the user
        // we do it by specifying an Id in XML file and referencing the Id to fetch the elements in Java code
        // these EditTexts are used for taking input from the user
        noOfHoursEditText = findViewById(R.id.num_hours_edit_text);
        hourlyRateEditText = findViewById(R.id.hourly_rate_edit_text);
        // these TextViews are used for displaying output to the user
        payTextView = findViewById(R.id.pay_text_view);
        overtimePayTextView = findViewById(R.id.overtime_pay_text_view);
        totalPayTextView = findViewById(R.id.total_pay_text_view);
        taxTextView = findViewById(R.id.tax_text_view);
    }

    // as we have specified this method in the onClick attribute of our button, this method will be called if the user clicks on the button
    public void calculateTaxAndPay(View view) {
        try {
            // after the button is clicked we fetch the data entered in the EditTexts using getText and toString methods
            // we then convert the string into double type using parseDouble method
            double noOfHoursWorked = Double.parseDouble(noOfHoursEditText.getText().toString());
            double hourlyRate = Double.parseDouble(hourlyRateEditText.getText().toString());
            // initializing methods for displaying in the output
            double pay = 0;
            double overtimePay = 0;
            double totalPay = 0;
            double tax = 0;
            // calculating the output using the given formulas
            if (noOfHoursWorked <= 40) {
                pay = noOfHoursWorked * hourlyRate;
                overtimePay = 0;
                totalPay = pay;
            } else {
                pay = 40 * hourlyRate;
                overtimePay = (noOfHoursWorked - 40) * hourlyRate;
                totalPay = pay + overtimePay;
            }
            tax = totalPay * 0.18;
            // displaying the output to the user by using setText method on TextViews
            // also used String format method in order to show only decimal places for double values
            payTextView.setText(String.format("%.2f", pay));
            overtimePayTextView.setText(String.format("%.2f", overtimePay));
            totalPayTextView.setText(String.format("%.2f", totalPay));
            taxTextView.setText(String.format("%.2f", tax));
        } catch (NumberFormatException nfe) {
            // If the user does not give valid input a toast message will be displayed
            Toast.makeText(this, "Invalid input!", Toast.LENGTH_SHORT).show();
        }
    }

    // this method is used to inflate(render) the menu from the XML file
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    // this method is used to implement functionality when any of the menu items are clicked
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.about_me:
                // startActivity method is used to navigate from one activity to another
                startActivity(new Intent(MainActivity.this, AboutActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

Then create a new activity by right clicking on java->New->Activity->Empty Activity from the right pane as shown in the picture:

Then type in AboutActivity under Activity Name and click Finish

activity_about.xml(here in the TextView you need to type in your name and student id) For example Developed by Andrew\nStudent Id: 12345

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<!--You can change your name and Student Id below-->
<!--layout_centerInParent attribute will center the textview for us-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Developed by x\nStudent Id: 123" />
</RelativeLayout>

AboutActivity.java

package com.project.taxcalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class AboutActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
    }
}

AndroidManifest.xml

You need to add parentActivityName attribute for AboutActivity in AndroidManifest.xml.

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
<!--parentActivityName attribute is used to implement the back functionality using configuration as mentioned in the question-->
        <activity
            android:name=".AboutActivity"
            android:parentActivityName=".MainActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity screenshot

AboutActivity Screenshot(You can add your details by going on activity_about.xml)

Dont forget to upvote!


Related Solutions

Your mission is to create your own application with the following minimum requirements: A JFrame and...
Your mission is to create your own application with the following minimum requirements: A JFrame and a (non-modal) JDialog. A JTabbedPane and JScrollPane. Nested JPanels including the following layout managers: GridLayout, FlowLayout, BorderLayout Some interaction widgets (JButton, etc.) on every JPanel and tab. Reasonable behavior when the JFrame is resized. NOTE: You may not use GridBagLayout, Free Design, Box, Overlay, Null or Absolute Layout anywhere in the project.
Discuss whether or not each of the following activities is a data mining task. Provide your...
Discuss whether or not each of the following activities is a data mining task. Provide your reasons as well in detail. Fill in your answers in the space provided below. (a) Dividing the customers of a company according to their gender. (b) Dividing the customers of a company according to their profitability. ( c) Computing the total sales of a company. (d) Sorting a student database based on student identification numbers. (e) Predicting the outcomes of tossing a (fair) pair...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
Explain any android application in detail and on your own words it should include the purpose,...
Explain any android application in detail and on your own words it should include the purpose, security features and you suggest to improve it
Read the following assessment task. Pay careful attention to the exact requirements of the task. Then...
Read the following assessment task. Pay careful attention to the exact requirements of the task. Then look at the student’s response. For this task you should : a.Write a couple of sentences assessing how well the student has carried out the task. b.Write the student some feedback based on whether they have completed the task well or whether they have omitted parts of it. Use supportive language while you are providing advice on how they can improve in future assessment...
Your task is to build a wastewater treatment plant to handle all of the domestic wastewater...
Your task is to build a wastewater treatment plant to handle all of the domestic wastewater produced by a housing estate. Using population equivalent, PE = 65000, design, describe and calculate briefly the sizes of all the unit processes in the treatment plant that are going to be built. You may assume the value that is not given.
Your task is to build a wastewater treatment plant to handle all of the domestic wastewater...
Your task is to build a wastewater treatment plant to handle all of the domestic wastewater produced by a housing estate. Using population equivalent, PE = 65000, design, describe and calculate briefly the sizes of all the unit processes in the treatment plant that are going to be built. You may assume the value that is not given.
Your task is to build a wastewater treatment plant to handle all of the domestic wastewater...
Your task is to build a wastewater treatment plant to handle all of the domestic wastewater produced by a housing estate. Using population equivalent, PE = 65000, design, describe and calculate briefly the sizes of all the unit processes in the treatment plant that are going to be built. You may assume the value that is not given.
Your task is to build a wastewater treatment plant to handle all of the domestic wastewater...
Your task is to build a wastewater treatment plant to handle all of the domestic wastewater produced by a housing estate. Using population equivalent, PE = 65000, design, describe and calculate briefly the sizes of all the unit processes in the treatment plant that are going to be built. You may assume the value that is not given.
Your task is to build a wastewater treatment plant to handle all of the domestic wastewater...
Your task is to build a wastewater treatment plant to handle all of the domestic wastewater produced by a housing estate. Using population equivalent, PE = 65000, design, describe and calculate briefly the sizes of all the unit processes in the treatment plant that are going to be built. You may assume the value that is not given.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT