cs1101 group1
Download
Skip this Video
Download Presentation
CS1101 Group1

Loading in 2 Seconds...

play fullscreen
1 / 22

CS1101 Group1 - PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS1101 Group1' - trish


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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
Scope of discussion
  • Mastermind codes
  • CityFlood codes (discussion 5 exercise)
  • Go through Sudoku
  • In class exercise : MyString (lab8)
    • Javadoc
sudoku
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.
sudoku1
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
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 impt1
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
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
this keyword
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 keyword1
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 keyword2
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 keyword3
this keyword
  • Constructor caserefer to discussion 6
this keyword4
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 keyword5
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
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
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
mystring1
\0MyString
  • 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(“…”);

mystring2
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?
mystring3
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){

}

}

mystring4
MyString

public class MyString{

public String toString(){

//?

}

}

mystring5
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?
mystring6
MyString

public class MyString{

//add in the constructors

//and any additional constructors

//you think you need

}

mystring7
MyString

public class MyString{

//reverse?

//charAt?

}