Created
May 30, 2018 00:05
-
-
Save edenizk/15f4899e2f495f03950ceed866e40511 to your computer and use it in GitHub Desktop.
multidimensional array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.deniz.multidimensional; | |
import android.widget.TextView; | |
public class Game { | |
private String gameName; | |
private int[][] scores; | |
public Game(String gameName, int[][] scores){ | |
this.gameName = gameName; | |
this.scores = scores; | |
} | |
public void setGameName(String gameName){ | |
this.gameName =gameName; | |
} | |
public String getGameName(){ | |
return gameName; | |
} | |
public int getMinimumScore(){ | |
int lowestScore = scores[0][0]; | |
for(int[] gameScores : scores){ | |
for(int score : gameScores){ | |
if(score < lowestScore){ | |
lowestScore = score; | |
} | |
} | |
} | |
return lowestScore; | |
} | |
public int getMaximumScore(){ | |
int highestScore = scores[0][0]; | |
for(int[] gameScores : scores){ | |
for(int score : gameScores){ | |
if(score > highestScore){ | |
highestScore = score; | |
} | |
} | |
} | |
return highestScore; | |
} | |
public double getTheAverageValueOfScores(int[] scores){ | |
int total = 0; | |
for(int score : scores){ | |
total+=score; | |
} | |
return (double) total / scores.length; | |
} | |
public void letsOutPutTheScoresToTheScreen(TextView textView){ | |
String oldTextViewValue; | |
for(int gameIndex = 0; gameIndex < scores.length; gameIndex++){ | |
for(int gameScores : scores[gameIndex]){ | |
oldTextViewValue = textView.getText().toString(); | |
textView.setText(String.format(oldTextViewValue + "Game Number %2d: %3d \n\n\n", | |
(gameIndex + 1),gameScores)); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.deniz.multidimensional; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
TextView txtGameName = (TextView) findViewById(R.id.txtGameName); | |
TextView txtGameScores = (TextView) findViewById(R.id.txtGameScores); | |
TextView txtGameLowestScore = (TextView) findViewById(R.id.txtGameLowestScore); | |
TextView txtGameHighestScore = (TextView) findViewById(R.id.txtGameHighestScore); | |
TextView txtGameAverageValueOfScores = (TextView) findViewById(R.id.txtGameAvaraageValueOfScores); | |
int[][] gameScoresArray = {{45,67,34}, | |
{23,56,49}, | |
{23,69,88}, | |
{17,28,84}, | |
{38,54,75}, | |
{51,34,71}, | |
{15,83,46}, | |
{36,47,15}, | |
{83,94,34}, | |
{17,37,0}}; | |
Game myGame = new Game("God of War", gameScoresArray); | |
txtGameName.setText(myGame.getGameName()); | |
myGame.letsOutPutTheScoresToTheScreen(txtGameScores); | |
txtGameHighestScore.setText(myGame.getMaximumScore()+""); | |
txtGameLowestScore.setText(myGame.getMinimumScore()+""); | |
String oldTxtGameAverageValueOfScores; | |
for (int gameIndex = 0; gameIndex < gameScoresArray.length; gameIndex++){ | |
oldTxtGameAverageValueOfScores = txtGameAverageValueOfScores.getText().toString() + (gameIndex + 1) + "- "; | |
double averageValue = myGame.getTheAverageValueOfScores(gameScoresArray[gameIndex]); | |
txtGameAverageValueOfScores.setText(oldTxtGameAverageValueOfScores + averageValue + "\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment