Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The default set is false falsefalsefalsefalse but when I tried the code below, it will eventually turn everything to true any suggestion here is the

The default set is false falsefalsefalsefalse

but when I tried the code below, it will eventually turn everything to true

any suggestion

here is the games description

when the user clicks on a frog it will flip the clicked frog's color from green to grey (or vice versa) and also will flip the colors of the frog to its left and to its right. if the frog is to the far end then it flips only one frog, which is next to it. You win the game if you can have all frogs have color of green.

here is the code

public class Frog {

/*

* Basic Frog object

* A frog can be green or grey, denoted by the boolean attribute green

* When a frog is flipped, this boolean attribute frog is toggled.

*/

private boolean green;

public Frog() {

// by default assume the frog is not green

this(false);

}

public Frog(boolean green) {

this.green = green;

}

/*

* Requires: Nothing

* Modifies: Nothing

* EFFECTS: Return the current state of green - True or false

*/

public booleanisGreen() {

//STUDENT IMPLEMENT THIS METHOD

green = true;

return green;

}

/*

* Requires: boolean- true if the frog is green and false if it is not

* Modifies: green

* EFFECTS: Set green to the passed value

*/

public void setGreen(booleanval) {

//STUDENT IMPLEMENT THIS METHOD

if(val) {

green = true;

}

else {

green = false;

}

}

/*

* Requires: Nothing

* Modifies: green

* EFFECTS: Toggles the value of the green attribute

*/

public void flip() {

//STUDENT IMPLEMENT THIS METHOD

if(green) {

green = true;

}else {

green = false;

}

}

//DONOT MODIFY THIS

@Override

public String toString() {

return super.toString() + " isGreen: " + green;

}

}

import java.util.ArrayList;

import java.util.List;

public class FrogList{

/*

* A list of Frogs

* In addition to having most functionality of a list

* a frog at a given index can be flipped

* doing so will flip its neighbors as well

*

*TODO: it might be better to extend the List class instead of creating intermediate methods

*/

List frogs;

public FrogList() {

// instanciate the list

frogs = new ArrayList();

}

/*

* REQUIRES: boolean green represents the color of the frog (true if Frog is green)

* MODIFIES: frogs - adds a new frog to the list called frogs

* EFFECTS: Adds a Frog to the ArrayList of Frogs - The frog will be of color passed to it

*/

public void addFrog(boolean green) {

//STUDENT IMPLEMENT THIS METHOD

// green = false;

frogs.add(new Frog(green));

}

/*

* EFFECTS: frog at the given index - index must be within the limits of the ArrayList

* if index is out of bounds it return null

*/

public Frog getFrog(int index) {

//STUDENT IMPLEMENT THIS METHOD

if((index=0)) {())&&(index>

return frogs.get(index);

}else {

return null;

}

}

/*

* REQUIRES: int is an Integer postion denoting the frog's index in the list

* EFFECTS: Checks to see if the frog at the given index is green

* return true if the frog is green otherwise false

*/

public booleanisFrogGreen(int index) {

//STUDENT IMPLEMENT THIS METHOD

if(getFrog(index)!=null){

return getFrog(index).isGreen();

}

else {

return false;

}

}

/*

* EFFECTS: Iterates through the list to check if each frog is green

* Returns true if all frogs are green and false otherwise

*/

public booleanallFrogsGreen() {

//STUDENT IMPLEMENT THIS METHOD

for(Frog frog:frogs) {

if(frog.isGreen()) {

return true;

}

}

return false;

}

/*

* REQUIRES: the Integer index denoting the frog's index in the list

* MODIFIES: Flips frog at current index, as well as its neighbors

* Therefore frogs at index, index + 1, and index - 1

*/

public void flip(int index) {

//STUDENT IMPLEMENT THIS METHOD

if(getFrog(index)!=null) {

getFrog(index).flip();

if(getFrog(index-1)!=null) {

getFrog(index-1).flip();

}

if(getFrog(index+1)!=null) {

getFrog(index+1).flip();

}

// if(getFrog(index-2)!=null) {

// getFrog(index-2).flip();

// }

// if(getFrog(index+2)!=null) {

// getFrog(index+2).flip();

// }

}

}

// DONOT CHANGE THIS

@Override

public String toString() {

/*

* Overrides Object toString method. Lists order that frogs are green and grey

*

* REQUIRES:

* Nothing

* MODIFIES:

* Nothing

* RETURNS:

* A string listing color of the frog in order

*/

String s = "";

for(Frog frog: frogs) {

if(frog.isGreen()) {

s += "green";

}else {

s += "grey";

}

}

return s;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Core Ios Developer S Cookbook Core Recipes For Programmers

Authors: Erica Sadun ,Rich Wardwell

5th Edition

0321948106, 978-0321948106

More Books

Students also viewed these Programming questions

Question

Are extensive disclosures required for deferred taxes? Explain.

Answered: 1 week ago

Question

Is it tenure-track, tenured, or something other designation?

Answered: 1 week ago

Question

QQ Understand how the Web works.

Answered: 1 week ago

Question

QQ Explain the current structure of the Internet.

Answered: 1 week ago

Question

QQ Understand the impact of mobile applications.

Answered: 1 week ago