1 / 22

CS1101 Group1

CS1101 Group1. Discussion 7. Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101. Scope of discussion. Mastermind codes CityFlood codes (discussion 5 exercise) Go through Sudoku In class exercise : MyString (lab8) Javadoc. Sudoku.

trish
Download Presentation

CS1101 Group1

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. CS1101 Group1 Discussion 7 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101

  2. Scope of discussion • Mastermind codes • CityFlood codes (discussion 5 exercise) • Go through Sudoku • In class exercise : MyString (lab8) • Javadoc

  3. Sudoku • Organization of your program • Don’t just only write codes for solving a problem • Instead, you should format your program in a more modular way.i.e.this method do thisthat method do thatwhen I call, I should get this result, I don’t care how it’s implemented.

  4. Sudoku • It shouldn’t be the case where you call a method and do some extra codes outside to process the result which should be done by the method. E.g. … while(…){ simpleSolver(puzzle); } … //Method to solve the puzzle. static void simpleSolver(int puzzle[][]) { … } Shouldn’t simpleSolver be solving the puzzle?!

  5. General ways to tackle a programming problem (impt!) • Read the question, plan what methods you need. • Write out the method skeletons without the implementation (comment the method if you need) • If you don’t know how to implement a certain method, add in stubs to make sure your program compiles. Think about the implementation later e.g. //this method return a given word //the original word is not modified public String reverse(String word){ return null; //stub }

  6. General ways to tackle a programming problem (impt!) • If a group of codes is always being called at different places, don’t just copy and paste.(Maybe it’s better to create a method for it) • Never hardcode the cases unless you have no choice.(Most probably you will miss out some case)

  7. Javadoc • http://java.sun.com/j2se/javadoc/writingdoccomments/ • Appreciate why you write @author XXX • If you write your program conforming to the javadoc style, you can generate the nice API pages

  8. this keyword • It is a self referencing pointer to this instance. • When is it used? E.g. 1 class Car{ private String color; … public void setColor(String color){ this.color = color; } }

  9. this keyword • It is a self referencing pointer to this instance. • When is it used? E.g. 1 class Car{ private String color; … public void setColor(String color){ this.color = color; } }

  10. this keyword E.g. 2 class Car{ private String color; … public void setColor(String color){ this.color = color; } public void paintBlue(){ this.setColor(“blue”); } }

  11. this keyword • Constructor caserefer to discussion 6

  12. this keyword • It is a self referencing pointer to this *instance*. • When is it not used? E.g. class Car{ private static final String FAV_COLOR; … public static void getFavColor(){ return this.FAV_COLOR; } }

  13. this keyword • It is a self referencing pointer to this *instance*. • When is it not used? E.g. class Car{ private static final String FAV_COLOR; … public static void getFavColor(){ return this.FAV_COLOR; } }

  14. Object is the mother of all classes • As mentioned previously, all user defined classes implicitly extends the java.lang.Object i.e. class A{} A a = new A(); boolean isObject = (a instanceof Object); //true

  15. MyString • In class exercise for this week and the next few weeks • Appreciate OO programming • See yourself as a API developeri.e. write libraries for others to use

  16. \0 MyString • A String is really made up of an array of characters • However the String class by java is not mutable, so you cannot do something like String s = “….”; s.setString(“…”);

  17. MyString • For MyString, we are going to implement a mutable “String” class • What is the special thing about this? • It’s size is variable (so the size can increase) • You are supposed to manually increase it yourself. (do not use ArrayList) • Maybe write a method that create a new char[] that is bigger and transfer all the items there?

  18. MyString • This week you will try to implement the method (in class), will go around the class to see whether you need help: public class MyString{ private char[] charArray; … //this method extend the size of existing charArray //with a larger one, retaining all its previous //value private void ensureCapacity(int minCapacity){ … } }

  19. MyString public class MyString{ … public String toString(){ //? } … }

  20. MyString MyString(java.lang.String str) Constructs a MyString object initialized to the contents of the specified string. • Do you see any way this constructor would need to use one of the MyString method?

  21. MyString public class MyString{ //add in the constructors //and any additional constructors //you think you need }

  22. MyString public class MyString{ //reverse? //charAt? }

More Related