You are on page 1of 61

COMP 130

INTRODUCTION TO PROGRAMMING (JAVA)


Instructor: Ayça Tüzmen Yıldırım

Teaching assistants:  webpage

Section leaders:  webpage
PSs: starts next week
Lab-quiz: on syllabus (no lab, when there is no quiz)

Textbook:(Available in the bookstore)
– E. S. Roberts, The Art & Science of Java, Addison-Wesley.

Course webpage:
– http://courses.ku.edu.tr/comp130/
(add to your bookmarks, you will refer to it frequently)
– https://ku.blackboard.com/
(add to your bookmarks, you will refer to it frequently)
BIO
• TED Ankara College
• ODTÜ –Undergrad Architecture
• Bilkent – Master of Architecture
• New Jersey Institute of Technology – Master of Science
• Arizona State University – Master of Computer Science
• Arizona State University – PhD in Design
• TA – Bilkent Üni
• RA – New Jersey Institute of Technology
• TA/RA - Arizona State University
• Associate Prof – University of Sydney
• Validation Engineer – Intel
• Kaya Educational Services - Founder
• Managing Consultant – IBM
Clients: Morgan Stanley, İşbank, Akbank, Aksigorta, Anadolu Sigorta, Vodafone,
Avea, TFF, Adalet Bakanlığı,
• Assoc Prof – Okan Uni, Bahçeşehir Uni, Istanbul Aydın Uni, Kemerburgaz Uni
• Lecturer – Koç Uni
How many of you have no prior experience in:

-Programming? If so, no problem. You’re in the correct class!

-using our computer labs?

 We will use the labs for quizzes.

 If you have not been to the computer labs, please go to


one, check if you can login without a problem.
How many of you have no prior experience in:

-Programming? If so, no problem. You’re in the correct class!

-using our computer labs?

 We will use the labs for quizzes.

 If you have not been to the computer labs, please go to


one, check if you can login without a problem.
Computer Science

Computer science is no more about


computers than astronomy is about
telescopes, biology is about microscopes or
chemistry is about beakers and test tubes.
Science is not about tools, it is about how
we use them and what we find out when we
do.
- Michael Fellows and Ian Parberry
Learning Goals

 Learn how to harness computing


power to solve problems.
 To that end:
 Explore fundamental techniques in
computer programming.
 Develop good software engineering
style.
 Gain familiarity with the Java
programming language.
Programs
Computer programs, known as software, are instructions to the
computer.

You tell a computer what to do through programs. Without


programs, a computer is an empty machine. Computers do not
understand human languages, so you need to use computer languages
to communicate with them.

Programs are written using programming languages.

7
Why do engineers and scientists need to
learn programming?
• Developing new models, designs, technologies often
require developing programs

• You can work faster than those who do not know


programming: some manual work can be automatized

• You can find more creative solutions than those who do


not know programming. Because they are bound with
what is available, you are not.

• You can test more effectively.


Why Java?

http://www.codingdojo.com/blog/7-most-in-demand-programming-
2018
Course Outline

1. Karel

2. Variables, expressions, control statements

3. Methods, random numbers

4. Object oriented graphics:


Course Outline
5. Graphical structures:
YarnPattern
Course Outline
6. Characters and strings: processing text, Ex:
solving codes/ciphers
(Imitation Game)

7. Arrays, user interfaces and image manipulation


Course Outline
8. File input-out (File reading and writing)

9. ArrayList, HashMap, Collections: Ex: creating a phone book, searching in the phone
book

10. GUI, event driven programming

HelloGUI

Hello, world.
Hello, Eric.
11. Objects and classes, object oriented design

Name Eric
world
Learn By Doing
Java Programming Environment

We will be using the classic version of the Eclipse Integrated


Development Environment (IDE) for this course. This tool
is available free of charge at www.eclipse.org.

The Eclipse IDE has been installed on PCs in computer labs.


Familiarize yourself with Eclipse by going through the
tutorials installed with the tool as soon as possible.
Course Web Page

Very 1st Thing To Do


Install Java JDK and Eclipse ASAP

If you miss a lecture: - watch the corresponding video, - do the


projects on your own for that lecture.
Grading
1. Quizzes (40%)
2. HW assignments (20%)
3. Final (25%)
4. Attendance to lectures (8%)
• will be graded as score=8*[min(a,23)/23]
– “a” stands for your attendance (we’ll have 27
classes in total)
5. Attendance to Discussion Sessions with Section
Leaders (7%)
• will be grades as score=7*[min(a,12)/12]
- “a” stands for your attendance (we’ll have 13
DSs in total)
Requirement to Enter Final Exam
• In order to enter the final exam, the average
grade for 4 Homeworks and 4 Quizzes in total
should be higher than 40 points.
• Those below 40 point will automatically fail the
course with F grade.
How to fail this course !

• Keep your mobile on during the classes


– Human beings cannot do real controlled parallel
processing, that is just an illusion of your
cognitive system.
– Even multiple personalities cannot exist at the
same instant
Things to do to succeed in this course

• Attend classes (and pay attention, ask questions, interact,...) – it


will be hands-on;
• Attend discussion sessions with your SECTION LEADER
• Do your homework assignments (on your own);
• Read your textbook;
• Get prepared to classes, note down your problems and ask for help!
• Ask when you don’t understand; don’t get lost! There are no dumb
questions!
• The only way to learn Java is to program in Java!

Pace of the course


• Important!

If you ever get lost and feel helpless, please come and see
your SECTION LEADER or ME or your TAs. We
want all of you to succeed.
Quizzes and Grading

• Mon 16:00-17:15: There is no lab, just quizzes (4 quizzes). We


will start next week with a demo quiz – Quiz 0!
• Quiz content: programming on your own and submitting the
result.
• In Quiz 0 next week, you will learn the procedure. A few
students fail this course every semester just because they don't
learn the details of the exam procedure.
• If the weighted average of your quiz average and HW average is
below 40 (out of 100) you will fail this class (will be given a letter
grade of F) and you’re not allowed to take the final exam nor final
remedial exam!
• Each student’s worst quiz grade will be excluded.
• There will be no make-ups for quizzes. There is already one extra!
Please review the Quiz tutorial before coming to quiz

The quizzes are open book,


open notes, projects

!! Cell-phone or USB during


quiz is considered as
cheating. !!
Homework Assignments
• Tentatively 4 HW assignments will be given. Your
section leader(SL) will grade your HW and give
feedback about it in a meeting that you and your SL
schedule.
• If you don’t see your section leader for the
feedback, you will get 0 from the assignment.

Keep in mind:
• There is no single algorithm/solution to perform a task
• The simplicity of an algorithm may mislead you: it may
not be simple to put it together on your own code.
• Making errors and trying to understand the source of
the problem is the best way to learn! (also in life :) )
Homework Assignments
Honor Code

You are obligated to follow all of the following rules:

1.Rule 1: You must NOT look at the code that are not your own.
2.Rule 2: You must NOT share your code with other students.
3.Rule 3: You must indicate on your submission any assistance you received.
(if that person is not a section leader, TA or lecturer of the course). The assistance you
receive must only consists of general advice including the discussion of ideas and
strategies. First and second rules always apply!

Conclusion: Discuss ideas together, but do the coding on your own!


Homework Assignments
Honor Code

All submissions are subject to automated plagiarism detection.


The tools are capable of comparing the submissions
with other submissions
from
current
&
past semesters
&
solutions from online sources.
2
F
8o Lets Get Started
o
t
e
r

T
e
x
t

03/02/22
Meet Karel the Robot

Good morning
Karel Speaks Java

s
Karel’s World

3 + + + + + North

2 + + + + + West East

1 + + + + +
South
1 2 3 4 5
Programming Karel

KAREL
Karel the Robot was developed at Stanford University by
Richard Pattis over 30 years ago. Since then Karel has been
used to give a “gentle introduction” to programming and problem
solving.
Walls

3 + + + +

2 + + + +

1 + + + +

1 2 3 4
Beepers

3 + + + +

2 + + + +

1 + + + +

1 2 3 4
Knows Four Commands

move();
turnLeft();
putBeeper();

pickBeeper();
move();
move();

3 + + + +

2 + + + +

1 + + + +

1 2 3 4
move();

3 + + + +

2 + + + +

1 + + + +

1 2 3 4
turnLeft();
turnLeft();

3 + + + +

2 + + + +

1 + + + +

1 2 3 4
turnLeft();

3 + + + +

2 + + + +

1 + + + +

1 2 3 4
pickBeeper();
pickBeeper();

3 + + + +

2 + + + +

1 + + + +

1 2 3 4
pickBeeper();

3 + + + +

2 + + + +

1 + + + +

1 2 3 4
Bird’s Eye View

3 + + + +

2 + + + +

1 + + + +

1 2 3 4

Karel is facing East


Bird’s Eye View

Karel is facing East


Turn Left

Karel is facing North


Turn Left

Karel is facing West


Turn Left

Karel is facing South


Move
Meet Karel the Robot
• Initially,
Karelonly
Karel’s
The the
world
other
Robot
Karel is
objects
surrounded
composed
was developed
understandsthatonly
exist
of
bystreets
ain
here
foursolid
Karel’s
and
atwall
primitiveStanford
avenues
world
through
are
by
numbered
commands: which
beepers,
Richard
it
Pattis
from
cannot
which the
over
are
move()move.
southwest
small
30 Depending
years
plastic
corner.
ago.
Move cones
onSince
that
the
forward(As problem,
then
emit
one insquare
Manhattan,
aKarel
quietthere
has
beeping
may
given
streets
also
noise.
many
run
be
generations
east-west
walls
In thisinworld,
the
and
turnLeft() interior
ofavenues
for
CSexample,
of
106A
the
Turnrun
90world
students
north-south.)
I’ve that
degrees added
toathe
block
“gentle
aleftbeeper
InKarel’s
this
introduction”
to
world,
passage.
the corner
Karel
to
programming
is
of facing
1pickBeeper()
st
Street
eastand
atand
the
2ndproblem
Avenue.
corner
Pick upofsolving.
1st Street
a beeper fromand 1st Avenue.
the current square
putBeeper() Put down a beeper on the current square
avenues

3 + + + + +

West East
streets 2 + + + + +

beeper
1 + + + + +

1 2 3 4 5
Help video available on the web-page for demonstrating loading
and running of a Karel project.
Rules of Karel’s world
• Karel’s bag is large enough to carry infinite number of
beepers (unless otherwise specified). If there are no
beepers in the bag, Karel can not put a beeper.
• If Karel tries to do something illegal, such as moving
through a wall or picking up a nonexistent beeper, an error
condition occurs. At this point, Karel displays an error
message and does not execute any remaining commands.
• () appears in each of these commands is part of the
common syntax shared by Karel and Java.
Our First Challenge
• How would you program Karel to pick up the beeper and
transport it to the top of the ledge? Karel should drop the
beeper at the corner of 2nd Street and 4th Avenue and then
continue one more corner to the east, ending up on 5th Avenue.

3 + + + + +

2 + + + + +

1 + + + + +

1 2 3 4 5
Project Messenger: how to load Comp130-libs and the project
Our First Karel Program
/*
* File: Messenger.java
FirstKarelProgram.java
* ----------------------------
comments
Comments
* This program moves a beeper up to a ledge.
*/
import stanford.karel.*; including Karel
Import the library
Stanford Karel libraries
public class Messenger
FirstKarelProgram
extends extends
Karel { Karel { header
public void run() {
move(); body of the ‘run’
pickBeeper();
move(); method
turnLeft();
move();
turnLeft(); A Karelthe
The run method, which specifies program class
operations
turnLeft();
turnLeft();
move();
putBeeper(); commands
move();
}
}
Java and the Object-Oriented Paradigm
• Programming languages typically support a particular style of
use, which is called its programming paradigm.
• Traditional languages like FORTRAN, Pascal, and C use the
procedural paradigm, in which the programmer defines the
algorithmic operations and data structures independently.
• Modern languages like Java tend to favor the object-oriented
paradigm in which the programmer defines the algorithmic
and data structure of a program in a more integrated way.
• In Java, programs are written as collections of classes, which
serve as templates for individual objects.
• Each object is an instance of a particular class; a single
class can serve as a pattern for many different objects.
Messenger extends Karel =

The new Messenger class automatically acquires the state


attributes and the behavior of the Karel class from which it is
derived. The process of taking on the structure and behavior
of the parent class is called inheritance.

Messenger is a subclass of Karel. Karel is a superclass of


Messenger
Inheritance

Superclass Karel Generalization

nds
e
ext

Subclass LazyKarel SpeedyKarel Specialisation


Please import the project: CleanYourRoom.
It's your time to write the program

Puff, I wish there were a turnRight() method.


Please import the project: RoadMaintenance.

A turnAround() method would also be helpful.


Exercise 2 U: GoGetItKarel.

You might also like