CS1101 Group1 Discussion 7 Lek Hsiang Hui comp.nus.edu.sg

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
CS-I Final Review Hao Jiang Computer Science Department Boston College.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Road Map Introduction to object oriented programming. Classes
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Chapter 3 Introduction to Collections – Stacks Modified
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CIT 590 Intro to Programming First lecture on Java.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
CS1101 Group1 Discussion 10 Lek Hsiang Hui comp.nus.edu.sg
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
17-Feb-16 String and StringBuilder Part I: String.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Lecture 5:Interfaces and Abstract Classes
More Sophisticated Behavior
CS Week 14 Jim Williams, PhD.
slides created by Ethan Apter
CMSC 202 Java Primer 2.
slides created by Ethan Apter
Core Concepts.
Chapter 14 Abstract Classes and Interfaces
Interfaces.
slides created by Ethan Apter and Marty Stepp
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

CS1101 Group1 Discussion 7 Lek Hsiang Hui comp.nus.edu.sg

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 ccomments/ ccomments/ –Appreciate why you 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 \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(“…”);

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