1 / 24

Tracing and Strings

Learn to trace variables, manipulate strings, and use the BlueJ debugger effectively in Java programming. Discover the importance of variable values, swap tracing, and working with Strings. Enhance your coding skills with hands-on exercises and code examples.

ndobbins
Download Presentation

Tracing and Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tracing and Strings Pepper

  2. Goals • Know where to find your java tool sheet • Trace the variable values in a program. • Trace the line flow of a program • Use the BlueJ debugger • Learn how to work with Strings

  3. Java Tools • http://home.adelphi.edu/~pe16132/csc171/notes/chap01KnowBook2.html • Quiz coming

  4. Tracing variable values • What will this program print? public class Manipulate { public static void main() { int x = 5; int y = 3; int z = x + 7; x = 20; System.out.println("the value of z is " + z); } }

  5. Trace a swap Trace the swap on the handout. Fill in the missing lines of code. http://home.adelphi.edu/~pe16132/csc171/assignments/swapTraceExercise.htm What to understand: • Variables only hold 1 value at a time. • Variables do not remember what they used to be. • Variable values persist even if they are not changed. • Process statements in order. • Variable values usually only change if on left of =

  6. More on Variable Tracing • What will this print? public class Manipulate { public static void main() { int x = 5; int y = 3; int z = x + 7; x = x + 1; x = 35; z = x - 5; x = x * 2; System.out.println("the value of main's x is " + x); x = 100; } }

  7. One More on Variable Tracing • What will this print? public class Manipulate { public static void main() { int x = 5; int y = 3; int z = x + 7; x = x + 1; x = 35; z = x - 5; x = x + z; System.out.println("the value of main's x is " + x); x = x -50; } }

  8. Methods do not share local variables • What will this print? public class Manipulate { public static void main() { int x = 5; int y = 3; int z = x + 7; method1(); System.out.println("the value of main's x is " + x); method1(); } public static void method1() { int x = 99; x = x + 1; System.out.println("the value of method 1's x is " + x); } }

  9. One share: Constant – Class Variable • Does not change throughout the class • Can share with the entire class • Make static and final public class profit { public static final int TROOP_SIZE = 10;public static void main(String[] args) { kidsSchool1(); kidsSchool2(): }public static void kidsSchool1(){ int kids = 32; System.out.println (“School 1 needs “ + (double) kids/TROOP_SIZE); }public static void kidsSchool1(){ int kids = 33; System.out.println (“School 2 needs “ + (double) kids/TROOP_SIZE); } }

  10. Debugger BlueJ tool to trace the code as it executes. Open 3 windows at once Debugger – to show variable values and what the computer is thinking Code – to see what line of code is being executed Terminal window – to see what is being output by each code line Help : http://www.cis.upenn.edu/~matuszek/cit591-2003/Lectures/43-debugger.ppt http://debug.csi.muohio.edu/video/watch/28

  11. Peek Ahead - Method Parameters public static void addIt (intfirstCup, intsecCup){int total = firstCup + secCup; // use parms System.out.println( “Sum of “ + firstCup + “ and “ + secCup + “ is “ + total); firstCup = 22; } • Put variable declaration into method header • Gets value when it runs directly from BlueJ • Anything you can do to a variable you can do to a parm, because… parm is variable • When calling, must supply values : Swap(4,5);

  12. You try method parms • Write a method to multiply variables iX and iYand print the result. (set iX to 10 and iY to 5) • Run it and see it print 50. • Change the program to make iX a parameter. • Put intiX inside the method () • Remove intiX = 10 from your program • Run it (right click and choose your method, and then enter 3). See it print 15. Run again and enter 6 and see it print 30.

  13. Method program public class ParmClass{ public static void parmPlay(int iX){int iY = 5;int iTot = iX *iY; System.out.println(“The product of “ + iX + “ and “ + iY + “ is “ + iTot); } }

  14. STRINGS • Type : String Holds text • Enter with double quotes “abc” • Really a class, so capitalize String • Just a list of chars. Example Goodbye World String: • Start at 0 • byeString.charAt(3) is D • byeString.length() is 13 • byeString.equals(somethingOtherString) is either true or false • byeString.toUpperCase() is GOODBYE WORLD • byeString.toLowerCase() is goodbye world

  15. String Case – toLowerCase • String is smart – You can ask it questions • Say: “String, what does your word look like when it is all lowercase” and string is smart enough to tell you. • Programming way of asking: • byeString.toLowerCase(); // it gives back “goodbye world” • (You just told string to call its toLowerCase method) • Gives you back another string • Take the answer and use it: • System.out.println(byeString.toLowerCase()); • (notice the matching parentheses) • String newString = byeString.toLowerCase(); // byeString doesn’t change • byeString = byeString.toLowerCase(); // now byeString changes

  16. String Length • String is smart – You can ask it questions • Say: “String, what is your length” and string is smart enough to tell you. • Programming way of asking: • byeString.length(); // it gives back 13 • (You just told string to call its length method) • Gives you back an integer. • Take the answer and use it: • System.out.println(byeString.length()); • (notice the matching parentheses) • IntiVar = byeString.length(); • Number of characters, not last position • Last position + 1

  17. charAt - Pick out a Character • You can ask String to tell you any of its characters. • You have to tell String which position you want. • Take from a string type ( “ “ ) into a character type ( ‘ ‘ ) • Start at 0, not 1 • Don’t take more than the end or  • CRASH • byeString.charAt(3) is D • byeString.charAt(byeString.charAt.length()-1)) is D • byeString.charAt.length()-1 is 12

  18. You try Create a method that plays with Strings: • Create a string variable and put “Goodbye world” into it. • Create another variable to hold that string in uppercase and set it. yourvar.toUpperCase() • Print both strings. • Print the character at position 1. Was it the first character? (no) yourvar.charAt(1) • Create a character variable and set it to the first character of your string variable • Create an integer variable called iLen to hold the length. yourvar.length() • Create another character variable and set it to the last character. yourvar.charAt(iLen-1) • Replace the first string variable with the first character + the uppercase string + the last character. yourFirstCharVar + yourUppercaseVar + your LastCharVar • What happens when you try to get charAt(iLen())? (crash- too long)

  19. Work with strings result: • public class PlayWithString { • public static void main() • { stringPlay(); } • public static void stringPlay() • { • String origByeString = "Goodbye world"; • String upperByeString = origByeString.toUpperCase(); • System.out.println(upperByeString.charAt(1)); • System.out.println(origByeString.charAt(1)); • char firstLetter = origByeString.charAt(0); • intiLen = origByeString.length(); • char lastLetter = origByeString.charAt(iLen-1); • String newByeString = firstLetter + origByeString + lastLetter; • System.out.println("upper bye string is " + upperByeString); • System.out.println("orig bye string is " + origByeString ); • System.out.println("new bye string is " + newByeString ); • }}

  20. IndexOf - What box holds an “O”? • You can ask String to tell you where its characters are found • You have to tell String which character you want. • If String does not have that character, it says -1 • If String has the value, it tells you the # of the first box with it • Starts looking at 0, not 1, unless you give it a starting box • Don’t start at more than the end or CRASH • byeString.indexOf(‘O’) is 1 • byeString.indexOf(‘O’,3 ) is 9 • byeString.indexOf(‘O’,2) is 2 • byeString.indexOf(‘O’,byeString.indexOf(‘ ‘)) is 9

  21. STRINGS - substring • You can ask String to give you a small portion of itself • You have to tell String which box to start at and which to stop at (It stops before the box you give it.) • If you don’t tell it where to end, it stops at the end of itself • Don’t start at more than the end or CRASH • Starts counting at 0 • byeString.substring(6) is “E WORLD” • byeString.substring(2,13) is “ODBYE WORLD” • byeString.substring(2,12) is “ODBYE WORL” • byeString.substring(byeString.indexOf(‘0’)+1) = “WORLD” • byeString.substring(0,byeString.indexOf(‘0’)) = “GOODBYE”

  22. More on Strings • No method changes a string • You can create a new string and replace the old one as in: • myString = myString.toUpperCase(); • More String methods: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

  23. Summary • String variable • Ask it for information about itself • Tracing code • Order • Variable values • Debugging • Variables • One value at a time; persists until changed • Local to method (except class – public final type) • Set values with local parameter

  24. Project with String (for Lab) • Step 1: Write a program with the variable first set to your first name. The name should be lowercase. Your program should then create a string that contains your name in pig latin with the first letter capitalized. Use the pig latin rule of moving the first letter to the end of the word and adding “ay”. Then, make the first letter of the resulting name be the only capitalized letter. Use substring and toUpperCase String methods. • Ex: Kris becomes risKay (See in class exercises for extensions to this)

More Related