Commit 3f973679 authored by Ava Intindola's avatar Ava Intindola
Browse files

lesson 2 solution

parent 8aa9f8ed
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<Objective-C-extensions>
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" fileNamingConvention="NONE" />
<pair source="c" header="h" fileNamingConvention="NONE" />
</extensions>
</Objective-C-extensions>
</code_scheme>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
......@@ -5,11 +5,25 @@ public class ArrayDemos {
// TODO: Initialize each value in the array to the square of its index,
// so it will contain {0, 1, 4, 9, 16, 25, ...}
private static void initializeWithSquares(int[] array) {
for(int i=0; i < array.length; i++) {
// System.out.println("DOING POSITION"+ i);
array[i]=i*i;
}
}
// TODO: Add up all the integers in the array, and return the sum.
private static int sumOfArray(int[] array) {
return 0; // placeholder, change this!
int sum = 0;
for(int i=0; i < array.length; i++) {
sum = sum + array [i];
}
return sum;
}
private static String yesOrNo(boolean okay) {
......
......@@ -9,20 +9,87 @@ public class FloodGame {
// TODO
static void randomize(FloodGrid grid, Random rng, int numColors) {
for (int i = 0; i < grid.numCells(); i++) {
grid.setColorAt(i, rng.nextInt(numColors));
}
}
// TODO
static String toString(FloodGrid grid) {
return "(grid)\n"; // placeholder
StringBuilder buf = new StringBuilder();
for (int r = 0; r < grid.edgeSize(); r++) {
for (int c = 0; c < grid.edgeSize(); c++) {
buf.append(grid.getColorAt(r, c));
}
buf.append('\n');
}
return buf.toString(); // placeholder
}
// TODO
static boolean gameOver(FloodGrid grid) {
return false; // placeholder
int targetColor = grid.getColorAt(0);
for (int i = 1; i < grid.numCells(); i++) {
if (grid.getColorAt(i) != targetColor) {
return false;
}
}
return true;
}
// TODO
static void flood(FloodGrid grid, int newColor) {
boolean[][] visited = new boolean[grid.edgeSize()][grid.edgeSize()];
int prevColor = grid.getColorAt(0, 0);
flood(grid, visited, 0, 0, prevColor, newColor);
}
private static void flood(
FloodGrid grid,
boolean[][] visited,
int row,
int column,
int prevColor,
int newColor)
{
visited[row][column] = true;
grid.setColorAt(row, column, newColor);
// Down
if(okayToVisit(grid, visited, row+1, column, prevColor)) {
flood(grid, visited, row+1, column, prevColor, newColor);
}
// Right
if(okayToVisit(grid, visited, row, column+1, prevColor)) {
flood(grid, visited, row, column+1, prevColor, newColor);
}
// Up
if(okayToVisit(grid, visited, row-1, column, prevColor)) {
flood(grid, visited, row-1, column, prevColor, newColor);
}
// Left
if(okayToVisit(grid, visited, row, column-1, prevColor)) {
flood(grid, visited, row, column-1, prevColor, newColor);
}
}
private static boolean okayToVisit(
FloodGrid grid,
boolean[][] visited,
int row,
int column,
int color)
{
return (row >= 0
&& row < grid.edgeSize()
&& column >= 0
&& column < grid.edgeSize()
&& !visited[row][column]
&& grid.getColorAt(row, column) == color);
}
public static void main(String[] args) throws IOException {
......
......@@ -11,8 +11,7 @@ class FloodGridArray2D implements FloodGrid {
grid = new int[edgeSize][edgeSize];
}
public int edgeSize() {
return grid.length;
public int edgeSize() { return grid.length;
}
public int numCells() {
......@@ -21,19 +20,24 @@ class FloodGridArray2D implements FloodGrid {
// TODO
public int getColorAt(int row, int column) {
return 0; // placeholder
return grid [row][column];
}
// TODO
public int getColorAt(int index) {
return 0; // placeholder
return getColorAt(index/edgeSize(), index%edgeSize());
}
// TODO
public void setColorAt(int row, int column, int color) {
grid [row][column]=color;
}
// TODO
public void setColorAt(int index, int color) {
setColorAt(index/edgeSize(),index%edgeSize(), color);
}
}
......@@ -2,6 +2,8 @@ package edu.liu.arrays;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
......@@ -21,5 +23,14 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);
// TODO: Fill TextViews with Strings from names array
LinearLayout textContainer = findViewById(R.id.textContainer);
for (int i = 0; i < textContainer.getChildCount(); i++) {
if (i < names.length) {
TextView tv = (TextView) textContainer.getChildAt(i);
tv.setText(names[i]);
}
}
}
}
......@@ -49,5 +49,20 @@
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment