Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Commit

Permalink
Done
Browse files Browse the repository at this point in the history
  • Loading branch information
eminmuhammadi committed Apr 20, 2021
1 parent fefbbd8 commit 0234b13
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 1 deletion.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions app/src/main/java/com/example/week9fm5cc5/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,52 @@
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.week9fm5cc5.helpers.GenerateAscii;
import com.example.week9fm5cc5.helpers.ReplaceCharacters;

public class MainActivity extends AppCompatActivity {

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

final Button buttonGenerateAscii = (Button) findViewById(R.id.generateAscii);
final Button buttonReplaceCharacters = (Button) findViewById(R.id.replaceCharacters);
final EditText inputBox = (EditText) findViewById(R.id.inputBox);
final TextView resultText = (TextView) findViewById(R.id.resultText);

/*
|-------------------------------
| Calculate sum of Ascii
|-------------------------------
*/
buttonGenerateAscii.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GenerateAscii generateAscii = new GenerateAscii();
String result = String.valueOf((int) generateAscii.sum(inputBox.getText().toString()));
resultText.setText("Sum of ASCII = " + result);
}
});

/*
|-------------------------------
| Replace Characters
|-------------------------------
*/
buttonReplaceCharacters.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ReplaceCharacters replaceCharacters = new ReplaceCharacters();
String result = replaceCharacters.generate(inputBox.getText().toString());
resultText.setText("Result = \"" + result + "\"");
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.week9fm5cc5.helpers;

import java.util.*;
import java.lang.*;

public class GenerateAscii {

/*
|-------------------------------
| Sum
|-------------------------------
*/
public int sum(String string) {
int sum = 0;

for (int i=0; i < string.length(); i++) {
int asciiValue = string.charAt(i);
sum += asciiValue;
}

return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.week9fm5cc5.helpers;

import java.util.*;
import java.lang.*;

public class ReplaceCharacters {

/*
|-------------------------------
| Generate
| FM5CC5
| f => m , F => M
| 5 => c, 5 => C, (no logic)
| c => c, C => C, (no logic)
| c => 5, C => 5
|-------------------------------
*/
public String generate(String string) {

String updatedString = string
.replace("f", "m")
.replace("F", "M")
.replace("c", "5")
.replace("C", "5");

return updatedString;
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<string name="app_name">Week9 (FM5CC5)</string>
<string name="app_name">Week9 (FM5CC5) - Application</string>
<string name="generateAsciiSum">Calculate Ascii</string>
<string name="replaceCharacters">Replace Characters</string>
<string name="input"></string>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.example.week9fm5cc5.helpers;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class GenerateAsciiTest {
GenerateAscii generateAscii;

@Before
public void setup() {
generateAscii = new GenerateAscii();
}

@Test
public void testEmptyAndSpace() {
assertEquals(0, generateAscii.sum(""));
assertEquals(32, generateAscii.sum(" "));
assertEquals(64, generateAscii.sum(" "));
}

@Test
public void testRandomAlphabet() {
assertEquals(104, generateAscii.sum("h"));
assertEquals(101, generateAscii.sum("e"));
assertEquals(108, generateAscii.sum("l"));
assertEquals(119, generateAscii.sum("w"));
assertEquals(111, generateAscii.sum("o"));
assertEquals(114, generateAscii.sum("r"));
assertEquals(100, generateAscii.sum("d"));
}

@Test
public void testString() {
assertEquals(104+101+108+119+111+114+100, generateAscii.sum("helword"));
}

@Test
public void testCustomAlphabet() {
// Azerbaijani (🇦🇿)
assertEquals(231, generateAscii.sum("ç"));
assertEquals(246, generateAscii.sum("ö"));
assertEquals(601, generateAscii.sum("ə"));
assertEquals(287, generateAscii.sum("ğ"));
assertEquals(351, generateAscii.sum("ş"));
assertEquals(305, generateAscii.sum("ı"));
assertEquals(252, generateAscii.sum("ü"));
}

@Test
public void testNumbers() {
assertEquals(49, generateAscii.sum("1"));
assertEquals(50, generateAscii.sum("2"));
assertEquals(51, generateAscii.sum("3"));
assertEquals(52, generateAscii.sum("4"));
assertEquals(53, generateAscii.sum("5"));
assertEquals(54, generateAscii.sum("6"));
assertEquals(55, generateAscii.sum("7"));
}

@Test
public void testNonAlpha() {
assertEquals(45, generateAscii.sum("-"));
assertEquals(46, generateAscii.sum("."));
assertEquals(41, generateAscii.sum(")"));
assertEquals(40, generateAscii.sum("("));
assertEquals(61, generateAscii.sum("="));
assertEquals(43, generateAscii.sum("+"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.week9fm5cc5.helpers;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class ReplaceCharactersTest {
ReplaceCharacters replaceCharacters;

@Before
public void setup() {
replaceCharacters = new ReplaceCharacters();
}

@Test
public void testNull() {
assertEquals("", replaceCharacters.generate(""));
assertEquals(" ", replaceCharacters.generate(" "));
}

@Test
public void testNoChanges() {
assertEquals("ABDE", replaceCharacters.generate("ABDE"));
assertEquals("abde", replaceCharacters.generate("abde"));
assertEquals("12345", replaceCharacters.generate("12345"));
assertEquals("------", replaceCharacters.generate("------"));
}

@Test
public void testPositive() {
assertEquals("m", replaceCharacters.generate("f"));
assertEquals("M", replaceCharacters.generate("F"));
assertEquals("5", replaceCharacters.generate("c"));
assertEquals("5", replaceCharacters.generate("C"));
}

@Test
public void testLongString() {
assertEquals("10m01", replaceCharacters.generate("10f01"));
assertEquals("000000000MAB", replaceCharacters.generate("000000000FAB"));
assertEquals("AAAAAAAAAAAAA5AAAAAAAAAAAA", replaceCharacters.generate("AAAAAAAAAAAAAcAAAAAAAAAAAA"));
assertEquals("ssss--5--ssss", replaceCharacters.generate("ssss--C--ssss"));
}

@Test
public void testPositiveLong() {
assertEquals("mmmm", replaceCharacters.generate("ffff"));
assertEquals("MMMM", replaceCharacters.generate("FFFF"));
assertEquals("5555", replaceCharacters.generate("cccc"));
assertEquals("5555", replaceCharacters.generate("CCCC"));
}
}

0 comments on commit 0234b13

Please sign in to comment.