Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 • 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 this that method do that when I call, I should get this result, I don’t care how it’s implemented. 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?! 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 } 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) Javadoc • http://java.sun.com/j2se/javadoc/writingdo ccomments/ – Appreciate why you write @author XXX • If you write your program conforming to the javadoc style, you can generate the nice API pages 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; } } 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; } } this keyword E.g. 2 class Car{ private String color; … public void setColor(String color){ this.color = color; } public void paintBlue(){ this.setColor(“blue”); } } this keyword • Constructor case refer to discussion 6 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; } } 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; } } 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 MyString • In class exercise for this week and the next few weeks • Appreciate OO programming • See yourself as a API developer i.e. write libraries for others to use 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(“…”); \0 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? 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){ … } } MyString public class MyString{ … public String toString(){ //? } … } 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? MyString public class MyString{ //add in the constructors //and any additional constructors //you think you need } MyString public class MyString{ //reverse? //charAt? }