cs1101 group1

22
CS1101 Group1 Discussion 7 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101

Upload: trish

Post on 09-Jan-2016

48 views

Category:

Documents


5 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS1101 Group1

CS1101 Group1

Discussion 7

Lek Hsiang Hui

lekhsian @ comp.nus.edu.sg

http://www.comp.nus.edu.sg/~lekhsian/cs1101

Page 2: CS1101 Group1

Scope of discussion

• Mastermind codes

• CityFlood codes (discussion 5 exercise)

• Go through Sudoku

• In class exercise : MyString (lab8)– Javadoc

Page 3: CS1101 Group1

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.

Page 4: CS1101 Group1

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?!

Page 5: CS1101 Group1

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

}

Page 6: CS1101 Group1

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)

Page 7: CS1101 Group1

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

Page 8: CS1101 Group1

this keyword

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

this.color = color;}

}

Page 9: CS1101 Group1

this keyword

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

this.color = color;}

}

Page 10: CS1101 Group1

this keyword

E.g. 2class Car{private String color;…public void setColor(String color){

this.color = color;}

public void paintBlue(){this.setColor(“blue”);

}}

Page 11: CS1101 Group1

this keyword

• Constructor caserefer to discussion 6

Page 12: CS1101 Group1

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;}

}

Page 13: CS1101 Group1

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;}

}

Page 14: CS1101 Group1

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

Page 15: CS1101 Group1

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

Page 16: CS1101 Group1

MyString

\0

• 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(“…”);

Page 17: CS1101 Group1

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?

Page 18: CS1101 Group1

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//valueprivate void ensureCapacity(int minCapacity){

…}

}

Page 19: CS1101 Group1

MyString

public class MyString{

public String toString(){

//?

}

}

Page 20: CS1101 Group1

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?

Page 21: CS1101 Group1

MyString

public class MyString{

//add in the constructors

//and any additional constructors

//you think you need

}

Page 22: CS1101 Group1

MyString

public class MyString{

//reverse?

//charAt?

}