יום ראשון, 23 באוקטובר 2011

Long Overdue Stacks

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

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.



יום ראשון, 2 באוקטובר 2011

The CatchUp- Part 1

I went back to my previous post and attempted the last exercise and noticed how simple it was. I therefore managed to successfully modify LinkedListTest_Application to incorporate the new methods I made before.
-------------
Here's my recursiveFind method. Wasn't a problem at all.


public ListNode recursiveFind(ListNode list, Object goal){
 if (goal.toString().compareTo(list.getData().toString()) == 0){return list;}
 if (list.getData().toString().compareTo(goal.toString()) == 1){return null;}
 return recursiveFind(list.getNext(), goal);
}

Here is my recursive Length method.  Not a problem either.

public int recursiveListSize(ListNode list){
int count = 0;
if (list.getData()==null
{return count;}
{count++;}{return recursiveListSize(list.getNext());}
}

for 3_8 I couldn't think of a good way to approach this. The only thing i thought of doing was doing a count, putting the data on an array and then printing that backwards, but I felt like that was somewhat, cheap.

------------
CircularListNodes wasn't tough, it was pretty straight forward. I have it coded on my Eclipse workspace.
3_11 Having an end node complicates it for me and I get confused however it saves you a bunch of coding lines which you can ignore, but I would prefer writing those lines rather than to get confused.

------------
All I want to say at this stage is that a DoubleList is something beautiful and I wish I knew about it earlier.


3_12-3_14 I did, but I wish there were more specifications so I could know if my  method is right or wrong.


------------
LINKED LIST APPLICATION.


Mr Daly - I would just like to say that I spent about 10 minutes trying to fix your spelling mistakes in your code which gave me errors. It drove me mad.


I am now on 3_16 and can't keep my eyes open, I will continue my catchup mission tomorrow.