Cs1101 group1
This presentation is the property of its rightful owner.
Sponsored Links
1 / 22

CS1101 Group1 PowerPoint PPT Presentation


  • 55 Views
  • Uploaded on
  • Presentation posted in: General

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.

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.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

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


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?

}


  • Login