CS 1101 Programming Methodology http www comp nus

  • Slides: 41
Download presentation
CS 1101: Programming Methodology http: //www. comp. nus. edu. sg/~cs 1101 x/

CS 1101: Programming Methodology http: //www. comp. nus. edu. sg/~cs 1101 x/

Java Appreciation OR What’s In Store In CS 1101? by Aaron Tan 24 July

Java Appreciation OR What’s In Store In CS 1101? by Aaron Tan 24 July 2009, 10 am, LT 16 What's in store in CS 1101? 2

Exercising Your Choice CS 1101 or CS 1101 S What's in store in CS

Exercising Your Choice CS 1101 or CS 1101 S What's in store in CS 1101? 3

CS 1101: Course Description “This module introduces the fundamental concepts of programming from an

CS 1101: Course Description “This module introduces the fundamental concepts of programming from an object-oriented perspective, and is perceived as the first and foremost introductory course to computing. ” What's in store in CS 1101? No prior experience is assumed. 4

CS 1101: Objectives § Learning about programming methodology and principles, using the object-oriented model.

CS 1101: Objectives § Learning about programming methodology and principles, using the object-oriented model. § Outcomes: § Know how to solve simple algorithmic problems. § Know how to write good small programs. § JAVA is merely a tool. § This is not a course on just JAVA alone! What's in store in CS 1101? 5

CS 1101: Your Friendly Lecturers Mr Tan Tuck Choy, Aaron A/P Wynne Hsu Mr

CS 1101: Your Friendly Lecturers Mr Tan Tuck Choy, Aaron A/P Wynne Hsu Mr Chan Wai Kit, Henry A/P Lee Mong Li, Janice Seminar Groups: 1, 3, 9 Office: COM 1 #03 -12 tantc@comp. nus. edu. sg http: //www. comp. nus. edu. sg /~tantc/acad. html Seminar Group: 2 Office: COM 1 #03 -18 whsu@comp. nus. edu. sg http: //www. comp. nus. edu. sg /~whsu Seminar Group: 4 Office: COM 1 #02 -27 hchia@comp. nus. edu. sg http: //www. comp. nus. edu. sg /~hchia Seminar Group: 5 Office: COM 1 #03 -19 leeml@comp. nus. edu. sg http: //www. comp. nus. edu. sg /~leeml A/P Tan Tiow Seng Prof. Tan Chew Lim A/P Tan Soon Huat, Gary Discussion leaders (DLs): §Lead discussion in small discussion groups (DGs) §Will be made known later. Refer to module website (Module Info – Staff) Seminar Group: 6 Office: AS 6 #04 -10 tants@comp. nus. edu. sg http: //www. comp. nus. edu. sg /~tants What's in store in CS 1101? Seminar Group: 7 Office: AS 6 #04 -05 tancl@comp. nus. edu. sg http: //www. comp. nus. edu. sg /~tancl Seminar Group: 8 Office: COM 1 #03 -27 gtan@comp. nus. edu. sg http: //www. comp. nus. edu. sg /~gtan 6

CS 1101: Lecture Groups § You will be pre-allocated your seminar group and discussion

CS 1101: Lecture Groups § You will be pre-allocated your seminar group and discussion group. § All groups cover the SAME syllabus, and have common tests. § Students in all groups are graded together as a whole. What's in store in CS 1101? 7

Module Website § http: //www. comp. nus. edu. sg/~cs 1101/ What's in store in

Module Website § http: //www. comp. nus. edu. sg/~cs 1101/ What's in store in CS 1101? 8

IVLE § http: //ivle. nus. edu. sg Watch out for announcements Participate in the

IVLE § http: //ivle. nus. edu. sg Watch out for announcements Participate in the forum What's in store in CS 1101? 9

Workload (5 MCs) § Lectures: § 3 hours/week in a lab setting. § Discussion

Workload (5 MCs) § Lectures: § 3 hours/week in a lab setting. § Discussion sessions: § 2 hours/week in a lab setting. § Continual assessment: § Take-home lab assignments § Sit-in lab tests § Mid-term test § Practical Exam What's in store in CS 1101? 10

Skills Set (1/2) Java constructs Problem solving Program What's in store in CS 1101?

Skills Set (1/2) Java constructs Problem solving Program What's in store in CS 1101? 11

Skills Set (2/2) § Java constructs (transferable skills) § Class and objects; types and

Skills Set (2/2) § Java constructs (transferable skills) § Class and objects; types and variables; control constructs (selection, repetition); APIs; arrays; exceptions; applications (searching, sorting). § Problem solving (more transferable skills) § Programming methodology (development cycle, top-down design, modularity); testing and debugging; abstraction. § Others § Software tools (editor, compiler, debugger, Course. Marker); mathematical maturity. What's in store in CS 1101? 12

A Java Program (Hello. java) Comments // Display a message. Class name public class

A Java Program (Hello. java) Comments // Display a message. Class name public class Hello { Method name public static void main(String[] args) { Method body System. out. println("Hello World!! : -D"); } Output } What's in store in CS 1101? 13

A Java Program (Welcome. java) API package // Author: Aaron Tan // Purpose: Ask

A Java Program (Welcome. java) API package // Author: Aaron Tan // Purpose: Ask for user’s name and display a welcome message. import java. util. *; public class Welcome { public static void main(String[] args) { Scanner scanner = new Scanner(System. in); System. out. print("What is your name? "); String name = scanner. next(); System. out. println("Hi " + name + ". "); System. out. println("Welcome to CS 1101!n"); } } What's in store in CS 1101? Creating a Scanner object Input An object of class String 14

Object-Oriented Programming (OOP) n n The fundamental OOP concept: An object-oriented program uses objects.

Object-Oriented Programming (OOP) n n The fundamental OOP concept: An object-oriented program uses objects. Demo on Blue. J: § Illustrate concepts of object and class § Creation of objects § Sending messages to objects What's in store in CS 1101? 15

Objects and classes n Object-oriented language ¨ n Models real-world objects you find in

Objects and classes n Object-oriented language ¨ n Models real-world objects you find in everyday life. Objects Represent ‘things’ from the real world, or from some problem domain (“that red 1200 cc Honda car”, “my 2 -year old Persian cat with a short tail”). ¨ Have name, state (set of values for its attributes) and behaviors (via methods). ¨ Perform actions or interact by sending messages. ¨ n Classes Represent all objects of a kind (“car”, “cat”). ¨ A class is a model/blueprint/prototype for an object. An object is an ‘instance’ of a class. Many instances (objects) can be created from a single class. ¨ What's in store in CS 1101? 16

Problem Solving (1/4) § Problem Algorithm Program § Task 1: Anagram § Text that

Problem Solving (1/4) § Problem Algorithm Program § Task 1: Anagram § Text that is formed by rearrangement of letters. § Examples: § Dear § Astronomer § The Eyes § The Morse Code § A Decimal Point What's in store in CS 1101? = = = Read Moon Starer They See Here Come Dots I’m A Dot In Place 17

Problem Solving (2/4) § Task 2: Maze What's in store in CS 1101? 18

Problem Solving (2/4) § Task 2: Maze What's in store in CS 1101? 18

Problem Solving (3/4) § Task 3: Sudoku What's in store in CS 1101? 19

Problem Solving (3/4) § Task 3: Sudoku What's in store in CS 1101? 19

Problem Solving (4/4) § Task 4: Mastermind § Sinks: Correct colour, correct position §

Problem Solving (4/4) § Task 4: Mastermind § Sinks: Correct colour, correct position § Hits: Correct colour, wrong position Secret code Sinks Hits Guess #1 1 1 Guess #1 1 0 Guess #2 1 2 Guess #2 0 1 Guess #3 2 2 Guess #3 1 0 Guess #4 4 0 Guess #4 1 1 What's in store in CS 1101? 20

Think About It § This set of slides will be available at § http:

Think About It § This set of slides will be available at § http: //www. comp. nus. edu. sg/~cs 1101 § Administrative and registration issues § Indicate your choice on this website § § § http: //ntsa. comp. nus. edu. sg/freshmen/ You will be pre-allocated your seminar group and discussion group. Nonetheless, do attend the Course Briefing – important! LT 17, 29 July, Wednesday, 9 am – 12 nn § If you take CS 1101, you will get a copy of “This CS 1101 Handbook” What's in store in CS 1101? 21

This CS 1101 Handbook What's in store in CS 1101? 22

This CS 1101 Handbook What's in store in CS 1101? 22

Have a great time in School of Computing! What's in store in CS 1101?

Have a great time in School of Computing! What's in store in CS 1101? 23

Blue. J n n n Designed at Monash University, Australia. Runs on top of

Blue. J n n n Designed at Monash University, Australia. Runs on top of Sun Microsystems’ Java Development Kit (JDK). Provides an Integrated Development Environment (IDE) with editor, compiler and debugger. Simple user interface, easy to use. Do not need to write complete application. Refer to course website http: //www. comp. nus. edu. sg/~cs 1101/ (click on “Resources…” “Online”) Download Blue. J and its documentation. What's in store in CS 1101? 24

Blue. J: Creating objects (1/4) n Choose example shapes. ¨ Classes: Square, Circle, Triangle,

Blue. J: Creating objects (1/4) n Choose example shapes. ¨ Classes: Square, Circle, Triangle, Canvas. What's in store in CS 1101? 25

Blue. J: Creating objects (2/4) n Creating a new object Right click on Circle

Blue. J: Creating objects (2/4) n Creating a new object Right click on Circle class and choose new Circle() to create a new Circle object. Circle() is a constructor. What's in store in CS 1101? 26

Blue. J: Creating objects (3/4) n Creating a new object What's in store in

Blue. J: Creating objects (3/4) n Creating a new object What's in store in CS 1101? 27

Blue. J: Creating objects (4/4) n Creating a new object A new Circle object

Blue. J: Creating objects (4/4) n Creating a new object A new Circle object circle 1 is created. What's in store in CS 1101? 28

Blue. J: Calling methods (1/3) n Methods implement the behavior of the objects. Right

Blue. J: Calling methods (1/3) n Methods implement the behavior of the objects. Right click on the circle 1 object and select the make. Visible() method. What's in store in CS 1101? 29

Blue. J: Calling methods (2/3) n Methods implement the behavior of the objects. Experiment

Blue. J: Calling methods (2/3) n Methods implement the behavior of the objects. Experiment with other methods: make. Invisible(), move. Down(), move. Right(). What's in store in CS 1101? 30

Blue. J: Calling methods (3/3) n Methods with parameters: ¨ change. Color(newcolor), change. Size(new.

Blue. J: Calling methods (3/3) n Methods with parameters: ¨ change. Color(newcolor), change. Size(new. Diameter), move. Horizontal(distance). What's in store in CS 1101? 31

Blue. J: State (1/2) n State: the set of values of all the attributes

Blue. J: State (1/2) n State: the set of values of all the attributes of an object. Right click on the circle 1 object and select the Inspect function. What's in store in CS 1101? 32

Blue. J: State (2/2) n Objects of the same class have the same fields/attributes.

Blue. J: State (2/2) n Objects of the same class have the same fields/attributes. But each object may have it own set of values for its attributes. State of circle 1 object. Fields: int diameter int x. Position int y. Position String Color boolean is. Visible What's in store in CS 1101? 33

Blue. J: Source code (1/8) n Right click on class Circle and select “Open

Blue. J: Source code (1/8) n Right click on class Circle and select “Open Editor”. import java. awt. *; import java. awt. geom. *; /** * A circle that can be manipulated and that draws itself * on a canvas. * @author Michael Kolling and David J. Barnes * @version 1. 0 (15 July 2000) */ public class Circle { private int diameter; private int x. Position; private int y. Position; private String color; private boolean is. Visible; What's in store in CS 1101? 34

Blue. J: Source code (2/8) // Create a new circle at default position with

Blue. J: Source code (2/8) // Create a new circle at default position with default color. public Circle( ) { diameter = 30; x. Position = 20; y. Position = 60; color = "blue"; is. Visible = false; } // Make this circle visible. If it was already visible, do nothing. public void make. Visible( ) { is. Visible = true; draw( ); } // Make this circle invisible. If it was already invisible, do nothing. public void make. Invisible( ) { erase( ); is. Visible = false; } What's in store in CS 1101? 35

Blue. J: Source code (3/8) // Move the circle a few pixels to the

Blue. J: Source code (3/8) // Move the circle a few pixels to the right. public void move. Right( ) { move. Horizontal(20); } // Move the circle a few pixels to the left. public void move. Left( ) { move. Horizontal(-20); } // Move the circle a few pixels up. public void move. Up( ) { move. Vertical(-20); } // Move the circle a few pixels down. public void move. Down( ) { move. Vertical(20); } What's in store in CS 1101? 36

Blue. J: Source code (4/8) // Move the circle horizontally by 'distance' pixels. public

Blue. J: Source code (4/8) // Move the circle horizontally by 'distance' pixels. public void move. Horizontal(int distance) { erase( ); x. Position += distance; draw( ); } // Move the circle vertically by 'distance' pixels. public void move. Vertical(int distance) { erase( ); y. Position += distance; draw( ); } What's in store in CS 1101? 37

Blue. J: Source code (5/8) // Slowly move the circle horizontally by 'distance' pixels.

Blue. J: Source code (5/8) // Slowly move the circle horizontally by 'distance' pixels. public void slow. Move. Horizontal(int distance) { int delta; if (distance < 0) { delta = -1; distance = -distance; } else { delta = 1; } for (int i = 0; i < distance; i++) { x. Position += delta; draw( ); } } What's in store in CS 1101? 38

Blue. J: Source code (6/8) // Slowly move the circle vertically by 'distance' pixels.

Blue. J: Source code (6/8) // Slowly move the circle vertically by 'distance' pixels. public void slow. Move. Vertical(int distance) { int delta; if (distance < 0) { delta = -1; distance = -distance; } else { delta = 1; } for (int i = 0; i < distance; i++) { y. Position += delta; draw( ); } } What's in store in CS 1101? 39

Blue. J: Source code (7/8) // Change the size to the new size (in

Blue. J: Source code (7/8) // Change the size to the new size (in pixels). // Size must be >= 0. public void change. Size(int new. Diameter) { erase( ); diameter = new. Diameter; draw( ); } // Change the color. Valid colors are "red", "yellow", // "blue", "green", "magenta" and "black". public void change. Color(String new. Color) { color = new. Color; draw( ); } What's in store in CS 1101? 40

Blue. J: Source code (8/8) // Draw the circle with current specifications on screen.

Blue. J: Source code (8/8) // Draw the circle with current specifications on screen. private void draw( ) { if (is. Visible) { Canvas canvas = Canvas. get. Canvas( ); canvas. draw(this, color, new Ellipse 2 D. Double(x. Position, y. Position, diameter)); canvas. wait(10); } } // Erase the circle on screen. private void erase( ) { if (is. Visible) { Canvas canvas = Canvas. get. Canvas( ); canvas. erase(this); } } } What's in store in CS 1101? 41