Monthly Archives: October 2015

RPN Calc Assignment

Use A Stack Class to create a RPN calculator

Part I : RPN Math Class

Input: String representing RPN syntax

Parse the String  (Maybe a separate method)

Calculate the return value of the RPN syntax.

Examples of Syntax

  • “3 -5 * “ -15

 

IF invalid syntax is entered, your method should throw an ArrithmeticException

Things to Support

  • negative numbers (vs subtraction)
  • decimal points
  • multiple spaces between characters

Part II: The GUI

  • Numbers 0-9
  • negative
  • 5 operators
  • Enter button
  • Clear button
  • System.exit(0) button
  • ** EC: support for keypresses including enter

Part III: **EC : Infix to postfix (allow users to enter numbers in infix)

 

Send me the jar file.

String Loop Assignments [Java]

Write the body for the methods described below.

Part I

public int countF(String str)

Description: This method returns the number of times that the letter ‘f’ occurs in str..

Method Call return value/output
countF(“abcdef”) 1
countF(“abcdfef”) 2
countF(“fff”) 3
countF(“xxx”) 0

public boolean has2Fs(String str)

Description: This method returns true if str has exactly two occurrences of the letter ‘f’ in it.

Method Call return value/output
has2Fs(“foo”) false
has2Fs(“fofo”) true
has2Fs(“foffo”) false

public boolean has2Fs3Gs(String str)

Description: This method returns true if str has exactly two occurrences of the letter ‘f’ in it and 3 occurences ‘g’.

Method Call return value/output
has2Fs3Gs(“foog”) false
has2Fs3Gs(“foggfog”) true
has2Fs3Gs(“foffoggg”) false

public int momOrDad(String str)

Description: This method returns the number of times that the String “mom” or the string “dad” occurs in the parameter str .

Method Call return value/output
momOrDad(“foog”) 0
momOrDad(“momf”) 1
momOrDad(“momdadmo”) 2
momOrDad(“momdadmom”) 3

public String threeTimes(String str)

Description: This method returns the String str concatenated with itself three times

Method Call return value/output
threeTimes(“foog”) “foogfoogfoog”
threeTimes(“abc”) “abcabcabc”
threeTimes(“zt”) “ztztzt”
threeTimes(“”) “”

public String nTimes(String str, int n)

Description: This method returns the String str concatenated with itself n times

Method Call return value/output
nTimes(“fg” , 2 ) “fgfg”
nTimes(“abc” , 0 ) “”
nTimes(“zt” , 3 ) “ztztzt”
nTimes(“uiz” , 4 ) “uizuizuizuiz”

public int countMiddleChar(String str)

Precondition: str.length() ≥ 3.

Description: This method returns the number of times the middle letter of str appears in str.

Method Call return value/output
countMiddleChar(“acbcb” ) 2
countMiddleChar(“acbcx” ) 1
countMiddleChar(“bbbbb” ) 5
countMiddleChar(“xytbtzy”) 1

public int indexOf(String haystack, String needle)

Description: Write your own indexOf() method. Obviously, you cannot make use of the String’s build in indexOf() method. This method returns the index of the first occurrence of needle in haystack . (Full credit if you can get this to work with Strings whose length is greater than 1).

Method Call return value/output
indexOf(“acxb”, “x” ) 2
indexOf(“zazt”, “z” ) 0
indexOf(“tayv”, “g” ) -1
indexOf(“hijklj”, “j” ) 2

If you want the extra credit, then it must also be able to complete the following example calls

Method Call return value/output
indexOf(“acxb”, “cx” ) 1
indexOf(“acxb”, “cxb” ) 1
indexOf(“acxb”, “cxbt” ) -1

public int countChars(String str, String chars)

Precondition:The length of chars is less than or equal to the length of str

Description: This method returns the number of times that charsoccurs in String str.

Method Call return value/output
countChars(“momdadmom” , “dad” ) 1
countChars(“foobofoo” , “foo”) 2
countChars(“foobofoofoo” , “foo”) 3
countChars(“foobofoofoo” , “xy” ) 0

Part II

public  String[] toArray(String str)

Description: This method returns an array comprised of the individual characters of str

Method Call return value/output
toArray(“abc”  ) { “a”, “b”, “c”}
toArray(“xyz”  ) {“x”, “y”, “z”}

 

 

**

public boolean isPalindrome(String str)

Description: This method returns true if str is a palindrome.

Method Call return value/output
isPalindrome(“abc”  ) false
isPalindrome(“aba”  ) true
 isPalindrome(“abba”  )  true

 

 

Java Stack Assignment

A Stack is a “LIFO” data structure (last in first out).

Here’ s an animation of how stacks are used in an RPN calculator.

More RPN practice

 

Create a stack Class that is based off of your linkedList code.

 

A stack has the following 3 methods

 

 

 

Create an RPN calculator Class

 

 

 

String Assignments SUPA 2015-16

Unit 3 Strings

substrings1

Alice Unit 2

Unit 2 Quiz (mid -november-ish)

  • Review sheet with answers posted in Google Drive
  • converting other bases to decimals (unit 1)
  • methods vs functions
    • definitions
    • meaning of ‘return a value’
    • screenshots from Alice with connection to these definitions
  • variables
    • definition
    • types of variables
      • number
      • boolean
      • string
    • use of strings
  • identify where the ‘local variable’ button is in Alice

return-value-to-variable-alice-2

 


Drag Racing:

  1.  ( DO NOT SUBMIT THIS VIA Google classroom.   Show me this in class)
  2. Use at least 1  comment  Link
  3. Create a world with a road (from the City collection) and two cars (from the Vehicles collection).
  4. Resize the road as necessary to match the size of the cars. The cars should be lined up side by side at one end of the road, which is a drag strip.
  5. Put an object at the end of the race to signify the ‘finish line’
  6. When the world is played, the cars should simultaneously move to the other end of the road and stop (You must use “do together”).
  7. use the “move” method for both carsmove-method
  8. Change the duration  of the move method (see pic below) so that one car arrives before the other).
    alice-duration-website
  9. Position the camera  so you can see the cars as they approach the end of the drag strip.
  10.  use a function to figure out how far they should move (You want to replace the value “5 meters” with a distance function)

Then modify it

  1. create a variable called  driveDistance  and set its value to 1
    1. set the value of driveDistance   to the returned value of a the distance in front of  function
      unit-2-drag-race-variable-drivedisatnce-highlight
  2.  the car should move forward   driveDistance   meters

Space Ship Repair

spaceship_repair

Create a space world with an astronaut and two spaceships(from the Web gallery’s Space collection). One of the spaceships is stranded with engine trouble, and the astronaut has arrived in the other spaceship to perform a repair.

  1. The astronaut should initially be positioned just outside one of the spaceships, about to perform a space walk to the other ship. When the world is played, the astronaut should initially be positioned just outside one of the spaceships, about to perform a space walk to the other ship.
  2. create a variable called walkingDistance (to represent how far the astronaut must walk)
  3. set the value of walkingDistance  to the returned value of  a proximity function.screenshot.44
  4. When the world is played, the astronaut should float to the stranded spaceship, appear to work for a few moments, and then float back to the rescue ship. Hint : to get the spaceship guy to move in the right way (towards the ship). Use the “turn to face” method turn-to-face
  5. The repaired ship should then fly away, off the screen.

Before submitting check out some examples of code that prior students used and that did not get full credit here .

 


Apollo 15

apollo-15-animated_gif_louis

  • During the Apollo 15 mission to the moon, astronaut David Scott performed an experiment to prove that Galileo was right when he said that any two objects dropped at the same time would land on the ground at the same time in the absence of air. Scott, standing on the surface of the moon, dropped a hammer and a feather and indeed both objects hit the ground simultaneously.(Again, you must use “do together”)
    1. Create an Alice world that recreates the experiment. You will find classes for the astronaut, lunar Lander, and the moon’s surface in the Web gallery’s Space collection. You can find a class for the Hammer in the Objects collection. Substitute any object of your choice for the feather.
    2. Create a variable called  dropDistance
    3. set the value of this variable to the returned value of a distance function to make the objects fall the distance above the ground. Use screenshot.1289
    4. Make the two objects fall down the value of the dropDistance

 

 

B Before handing it in, look at some examples of work that students did and that did not receive full credit,. here.


 

End of Unit Open assignment

  • Tell a short story that
  • at least 3 objects
  • at least 2  variables
  • at least 2 functions
    • sets the value of a variable to a returned value of a function
  • uses that variable to move one of the objects
    • ie make sure that your variable is actually used to do something.

Below is just one example. Your story can be about any topic.

end-of-unit2-alice


Old assignments (ignore)

Penguin on a Table: Create a world with a penguin and a table. The penguin should be facing the table, standing next to it. The penguin should simultaneously flap its wings (use the wing_flap method) and move up in the air a distance that is exactly 1 meter higher than the height of the table. You can call the table’s height function to get its height. The penguin should then move forward to approximately the center of the table, and then move down to the surface of the table

 

What as a String