CS1101 Group1 Discussion 7 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101.
CS1101: Programming Methodology comp.nus.sg/~cs1101x
description
Transcript of CS1101: Programming Methodology comp.nus.sg/~cs1101x
2What's in store in CS1101?
Java Appreciation
OR
What’s In Store In CS1101?
by Aaron Tan24 July 2009, 10am,
LT16
4What's in store in CS1101?
CS1101: 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.” No prior experience is assumed.
5What's in store in CS1101?
CS1101: 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!
6What's in store in CS1101?
CS1101: Your Friendly LecturersMr Tan Tuck Choy, Aaron
Seminar Groups: 1, 3, 9Office: COM1 #[email protected] http://www.comp.nus.edu.sg/~tantc/acad.html
A/P Wynne Hsu
Seminar Group: 2Office: COM1 #[email protected] http://www.comp.nus.edu.sg/~whsu
Mr Chan Wai Kit, Henry
Seminar Group: 4Office: COM1 #[email protected] http://www.comp.nus.edu.sg/~hchia
A/P Lee Mong Li, Janice
Seminar Group: 5Office: COM1 #[email protected] http://www.comp.nus.edu.sg/~leeml
A/P Tan Tiow Seng
Seminar Group: 6Office: AS6 #[email protected] http://www.comp.nus.edu.sg/~tants
Prof. Tan Chew Lim
Seminar Group: 7Office: AS6 #[email protected] http://www.comp.nus.edu.sg/~tancl
A/P Tan Soon Huat, Gary
Seminar Group: 8Office: COM1 #[email protected] http://www.comp.nus.edu.sg/~gtan
Discussion leaders (DLs):
Lead discussion in small discussion groups (DGs)
Will be made known later. Refer to module website (Module Info – Staff)
7What's in store in CS1101?
CS1101: 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.
9What's in store in CS1101?
IVLE http://ivle.nus.edu.sg
Watch out for announcements
Participate in the forum
10What's in store in CS1101?
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
12What's in store in CS1101?
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,
CourseMarker); mathematical maturity.
13What's in store in CS1101?
A Java Program (Hello.java)
// Display a message.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!! :-D");
}
}
Comments
Class name
Method name
Method body
Output
14What's in store in CS1101?
A Java Program (Welcome.java)
// 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 CS1101!\n");
}
}
API package
Creating a Scanner object
Input
An object of class String
15What's in store in CS1101?
Object-Oriented Programming (OOP) The fundamental OOP concept:
An object-oriented program uses objects.
Demo on BlueJ: Illustrate concepts of object and class Creation of objects Sending messages to objects
16What's in store in CS1101?
Objects and classes Object-oriented language
Models real-world objects you find in everyday life.
Objects Represent ‘things’ from the real world, or from some problem
domain (“that red 1200cc 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.
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.
17What's in store in CS1101?
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 =
ReadMoon StarerThey SeeHere Come DotsI’m A Dot In Place
20What's in store in CS1101?
Problem Solving (4/4) Task 4: Mastermind
Sinks: Correct colour, correct position Hits: Correct colour, wrong position
Secret code
Sinks Hits
Guess #1
Guess #2
1 1
1 2
Guess #3 2 2
Guess #4 4 0
Guess #1 1 0
0 1
1 0
1 1
Secret code
Sinks Hits
Guess #2
Guess #3
Guess #4
21What's in store in CS1101?
Think About It This set of slides will be available at
http://www.comp.nus.edu.sg/~cs1101
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! LT17, 29 July, Wednesday, 9am – 12nn
If you take CS1101, you will get a copy of “This CS1101 Handbook”
24What's in store in CS1101?
BlueJ 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/~cs1101/ (click on “Resources…” “Online”)
Download BlueJ and its documentation.
25What's in store in CS1101?
BlueJ: Creating objects (1/4) Choose example shapes.
Classes: Square, Circle, Triangle, Canvas.
26What's in store in CS1101?
BlueJ: Creating objects (2/4) Creating a new object
Right click on Circle class and choose new Circle() to create a new Circle object. Circle() is a constructor.
28What's in store in CS1101?
BlueJ: Creating objects (4/4) Creating a new object
A new Circle object circle1 is created.
29What's in store in CS1101?
BlueJ: Calling methods (1/3) Methods implement the behavior of the objects.
Right click on the circle1 object and select the makeVisible() method.
30What's in store in CS1101?
BlueJ: Calling methods (2/3) Methods implement the behavior of the objects.
Experiment with other methods: makeInvisible(), moveDown(), moveRight().
31What's in store in CS1101?
BlueJ: Calling methods (3/3) Methods with parameters:
changeColor(newcolor), changeSize(newDiameter), moveHorizontal(distance).
32What's in store in CS1101?
BlueJ: State (1/2) State: the set of values of all the attributes of an
object.
Right click on the circle1 object and select the Inspect function.
33What's in store in CS1101?
BlueJ: State (2/2) 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 circle1 object.
Fields:int diameterint xPositionint yPositionString Colorboolean isVisible
34What's in store in CS1101?
BlueJ: Source code (1/8) 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 xPosition;private int yPosition;private String color;private boolean isVisible;
35What's in store in CS1101?
BlueJ: Source code (2/8) // Create a new circle at default position with default color. public Circle( ) { diameter = 30; xPosition = 20; yPosition = 60; color = "blue"; isVisible = false; }
// Make this circle visible. If it was already visible, do nothing. public void makeVisible( ) { isVisible = true; draw( ); }
// Make this circle invisible. If it was already invisible, do nothing. public void makeInvisible( ) { erase( ); isVisible = false; }
36What's in store in CS1101?
BlueJ: Source code (3/8) // Move the circle a few pixels to the right. public void moveRight( ) { moveHorizontal(20); }
// Move the circle a few pixels to the left. public void moveLeft( ) { moveHorizontal(-20); }
// Move the circle a few pixels up. public void moveUp( ) { moveVertical(-20); }
// Move the circle a few pixels down. public void moveDown( ) { moveVertical(20); }
37What's in store in CS1101?
BlueJ: Source code (4/8) // Move the circle horizontally by 'distance' pixels. public void moveHorizontal(int distance) { erase( ); xPosition += distance; draw( ); }
// Move the circle vertically by 'distance' pixels. public void moveVertical(int distance) { erase( ); yPosition += distance; draw( ); }
38What's in store in CS1101?
BlueJ: Source code (5/8) // Slowly move the circle horizontally by 'distance' pixels. public void slowMoveHorizontal(int distance) { int delta;
if (distance < 0) { delta = -1; distance = -distance; } else { delta = 1; } for (int i = 0; i < distance; i++) { xPosition += delta; draw( ); } }
39What's in store in CS1101?
BlueJ: Source code (6/8) // Slowly move the circle vertically by 'distance' pixels. public void slowMoveVertical(int distance) { int delta;
if (distance < 0) { delta = -1; distance = -distance; } else { delta = 1; } for (int i = 0; i < distance; i++) { yPosition += delta; draw( ); } }
40What's in store in CS1101?
BlueJ: Source code (7/8) // Change the size to the new size (in pixels). // Size must be >= 0. public void changeSize(int newDiameter) { erase( ); diameter = newDiameter; draw( ); }
// Change the color. Valid colors are "red", "yellow", // "blue", "green", "magenta" and "black". public void changeColor(String newColor) { color = newColor; draw( ); }
41What's in store in CS1101?
BlueJ: Source code (8/8) // Draw the circle with current specifications on screen. private void draw( ) { if (isVisible) { Canvas canvas = Canvas.getCanvas( ); canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, diameter, diameter)); canvas.wait(10); } }
// Erase the circle on screen. private void erase( ) { if (isVisible) { Canvas canvas = Canvas.getCanvas( ); canvas.erase(this); } }}