Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

НАЦІОНАЛЬНИЙ УНІВЕРСИТЕТ БІОРЕСУРСІВ І

ПРИРОДОКОРИСТУВАННЯ УКРАЇНИ

Факультет інформаційних технологій


Кафедра комп’ютерних систем, мереж та кібербезпеки

Курс «Програмування для мобільних платформ»

ЗВІТ ІЗ ЛАБОРАТОРНОЇ РОБОТИ №3


Робота з файлами

Виконав:
студент 4 курсу
групи КІ-20009б
Колесніков О.О.

Київ – 2024
Виконання роботи

Лістинг коду Main Activity(java)


package com.example.l3;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends Activity {

private static final int REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE = 1;

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

requestPermission();

final EditText editTextStartX = findViewById(R.id.editTextStartX);


final EditText editTextEndX = findViewById(R.id.editTextEndX);
final EditText editTextStep = findViewById(R.id.editTextStep);
Button buttonCalculate = findViewById(R.id.buttonCalculate);
Button buttonSaveToFile = findViewById(R.id.buttonSaveToFile);
Button buttonViewFileContent = findViewById(R.id.buttonViewFileContent);
Button buttonViewAuthor = findViewById(R.id.buttonAboutAuthor);
final TextView textViewResult = findViewById(R.id.textViewResult);
final TextView textViewAuthor = findViewById(R.id.textViewAuthor);

buttonCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editTextStartX.getText().toString().isEmpty() ||
editTextEndX.getText().toString().isEmpty() ||
editTextStep.getText().toString().isEmpty()) {
textViewResult.setText("Введіть усі дані");
return;
}

double startX = Double.parseDouble(editTextStartX.getText().toString());


double endX = Double.parseDouble(editTextEndX.getText().toString());
double step = Double.parseDouble(editTextStep.getText().toString());

StringBuilder resultBuilder = new StringBuilder();


for (double x = startX; x <= endX; x += step) {
double y = calculateExpression(x);
resultBuilder.append(String.format("x=%.2f, y=%.2f\n", x, y));
}

textViewResult.setText(resultBuilder.toString());
}
});

buttonSaveToFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String dataToSave = textViewResult.getText().toString();
if (!dataToSave.isEmpty()) {
textViewResult.setText("Дані збережені у файл.");
saveToFile(dataToSave);
} else {
textViewResult.setText("Дані не збережено.");
}
}
});

buttonViewFileContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String fileContent = readFromFile();
textViewResult.setText(fileContent);
}
});

buttonViewAuthor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String info = "Kolesnikov Oleksandr \n Computer Engineering\n 4th year \n KI-20009b\n";
textViewAuthor.setText(info);
}
});
}

private double calculateExpression(double x) {


double a = 5;
double b = 10;

double numerator = a / (x - a);


double denominator = Math.pow(b, 1.0 / 3.0) + Math.cos(Math.pow(x, 3));
double fraction = numerator / denominator;
double R = fraction + Math.log10(a + 4.5);

return R;
}

private void saveToFile(String data) {


try {
File file = new File(getFilesDir(), "val_fun.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(data.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private String readFromFile() {


StringBuilder content = new StringBuilder();
try {
File file = new File(getFilesDir(), "val_fun.txt");
java.util.Scanner scanner = new java.util.Scanner(file);
while (scanner.hasNextLine()) {
content.append(scanner.nextLine()).append("\n");
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}

private void requestPermission() {


if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);
}
}
}
Лістинг коду Activity Main(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=".MainActivity">

<EditText
android:id="@+id/editTextStartX"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="Початкове значення x"
android:inputType="numberDecimal"
android:padding="12dp" />

<EditText
android:id="@+id/editTextEndX"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextStartX"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="Кінцеве значення x"
android:inputType="numberDecimal"
android:padding="12dp" />

<EditText
android:id="@+id/editTextStep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextEndX"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="Крок"
android:inputType="numberDecimal"
android:padding="12dp" />

<Button
android:id="@+id/buttonCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Розрахувати"
android:layout_below="@id/editTextStep"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>

<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/buttonCalculate"
android:layout_marginTop="16dp">

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

<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:padding="12dp" />
<TextView
android:id="@+id/textViewAuthor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:padding="12dp" />
</LinearLayout>
</ScrollView>

<Button
android:id="@+id/buttonAboutAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Про автора"
android:layout_below="@id/scrollView"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>

<Button
android:id="@+id/buttonSaveToFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Зберегти дані"
android:layout_below="@id/buttonAboutAuthor"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>

<Button
android:id="@+id/buttonViewFileContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Переглянути вміст файлу"
android:layout_below="@id/buttonSaveToFile"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>

</RelativeLayout>

Результат виведення функцій застосунку на пристрої

You might also like