4_1 done
4_2 done
4_3 done
4_4 yes, resolved
4_5 wasn't that hard, done
The recursive sum method helped visualize things, but a little redundant to me.
To create infinite recursion with a positive number, I would have to delete the base case or alter it.
4_6
This took a while because of my syntax of ArrayStackList, but i had the logic down pretty quickly
4_2 done
4_3 done
4_4 yes, resolved
4_5 wasn't that hard, done
The recursive sum method helped visualize things, but a little redundant to me.
To create infinite recursion with a positive number, I would have to delete the base case or alter it.
4_6
This took a while because of my syntax of ArrayStackList, but i had the logic down pretty quickly
public class StringReverseDemo extends ArrayListStack
{
//uses stacks (class ArrayStack) to reverse word.
public static String getReverse(String word)
{
ArrayListStack stack1=new ArrayListStack();
String result = "";
for (int i=0;i<word.length();i++){
stack1.push(word.substring(i, i+1));
}
while (!stack1.isEmpty()){
result+=stack1.pop();
}
return result;
}//getReverse
//uses recursion to reverse word
public static String getReverseRecursive(String word)
{
if (word.length()==1){return word;}
else
return word.substring(word.length()-1,word.length())+getReverseRecursive(word.substring(0,word.length()-1));
}//getReverseRecursive
}//class String
4_7
I believe recursion simplifies code dramatically, at least for me, as long as you understand it. The iterative approach is easier to think about but is more prone to errors.
4_8
public static boolean isPalindrome(String word){
return word==getReverseRecursive(word);
}
4_9
Ha!... I'm not sure.
TOWERS OF HANOI
I am really really frustrated because it won't let me run the TowersOfHanoi application. I can still do the other implementation and alterations but I can't check that it completley works.....
4_10
2n^(n-1)
4_11
I didn't really understand this so I got help from classmates.
public void play()
{
setUpPegs();
ArrayStack blue=new ArrayStack(maxPegs);
ArrayStack green=new ArrayStack(maxPegs);
ArrayStack yellow=new ArrayStack(maxPegs);
int difficulty;
SimpleInput inputDifficulty = new SimpleInput();
difficulty=inputDifficulty.getInt("How many disks would you like?");
for (int i=0;i<difficulty;i++)
{
disk temp=new disk(20,100-(i*10),0+(i*5),180-(i*20),"red",true);
blue.push(temp);
temp.makeVisible();
}//for
reDraw(blue,green,yellow);
while(!(blue.isEmpty() && green.isEmpty()))
{
char from, to;
SimpleInput moveCharacter = new SimpleInput();
from = moveCharacter.getChar("from: b=blue, g=green, y=yellow");
to = moveCharacter.getChar("to: b=blue, g=green, y=yellow");
disk carry=null;
boolean valid = false;
while (!valid)
{
switch (from)
{
case 'b': carry=(disk)blue.pop(); valid = true; break;
case 'g': carry=(disk)green.pop(); valid = true; break;
case 'y': carry=(disk)yellow.pop(); valid = true; break;
default : System.out.println("error, try again");
}//switch
}
boolean nextValid = false;
while (!nextValid)
{
switch (to)
{
case 'b': if (blue.isEmpty() || carry.getWidth()<((disk)blue.peekTop()).getWidth())
{
carry.changePosition(carry.getXPosition()%100,180-(stackHeight(blue)*20));
blue.push(carry); nextValid = true; break;
}
else
{
System.out.println("Cannot put bigger disk on smaller disk");
}
case 'g': if (green.isEmpty() || carry.getWidth()<((disk)green.peekTop()).getWidth())
{
carry.changePosition((carry.getXPosition()%100)+100,180-(stackHeight(green)*20));
green.push(carry); nextValid = true; break;
}
else
{
System.out.println("Cannot put bigger disk on smaller disk");
}
case 'y': if (yellow.isEmpty() || carry.getWidth()<((disk)yellow.peekTop()).getWidth())
{
carry.changePosition((carry.getXPosition()%100)+200,180-(stackHeight(yellow)*20));
yellow.push(carry); nextValid = true; break;
}
else
{
System.out.println("Cannot put bigger disk on smaller disk");
}
default : System.out.println("error, try again");
}//switch
}
reDraw(blue,green,yellow);
}//while(main loop)
System.out.println("Well Done");
}//play
4_12
I have NO idea how to implement the algorithm i found online (which I believe is correct) into a solve method.
4_13
Everything worked perfectly, for (a/0) I just got an error, as expected.
4_14
Worked fine, it returned false.
4_15
I honestly don't know how to start this. I looked at my classmate's work for this and it's not that I don't understand the syntax.. this time im having trouble with the logic.
4_16
You would need to change every int in your code into into double, besides ofcourse for the int i =... in loops.
4_17
Easy.
switch (operator)
{
case '+': result=operand1+operand2; break;
case '*': result=operand1*operand2; break;
case '/': {if (operand1==0) {System.out.println("can't divide by 0");return 0;} result=operand2/operand1; break;}
case '-': result=operand2-operand1; break;
}//switch
4_18
I have a few ideas of how to approach this, however I am really running out of time. This is something for me to come back to hopefully sometime this week.
Finally. I caught up.