Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the...

15
Programming in Programming in Java Java Dr. M. Ahmadzadeh Dr. M. Ahmadzadeh

Transcript of Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the...

Page 1: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

Programming in JavaProgramming in JavaProgramming in JavaProgramming in Java

Dr. M. AhmadzadehDr. M. Ahmadzadeh

Page 2: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

Course Outline (subject to tiny changes)

• I will cover the following but not necessarily in this order.– Strings– ArrayList (Vector)– Variables: Primitive & References– Object oriented design & programming

• Inheritance• Overriding method• Getter & setter Methods• Encapsulation• Polymorphism• Initialization and clean up • Overloaded Constructor• Superclass Constructor• Exception Handling

– File I/O– If we had time, probably

• Packages• GUI• Generics• Release your code

Page 3: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

References• Head First Java, Kathy Sierra, Bert Bates, 2nd Edition, O’Reilly, 2005

• Thinking in Java, Bruce Eckel, 4th Edition, Prentice Hall, 2006

• Effective Java Programming Language guide, Joshua Bloch, Addison Wesley, 2001

Page 4: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

How to successfully pass this course.

• Practice, practice & practice• Don't give up easily• Do not just read

– Stop, think, and do it yourself– Use pen & paper

• Have a rest if you encounter a problem and come back later

• Talk about the problem loudly• Try to break the code• Do pair programming (of course not for the

exercises that is marked)

Page 5: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

Assessment• 50% Programming Exercises

– In average we would have 1 exercise per week. It depends on how big the exercises are.

– Deadline for each exercise will be Sunday at 3:45 pm. (of course it depends on how long you had time to do the exercise)

– You deliver your exercise to Ms. Tavakoli. No Exercises will be received after the deadline. In special cases, some marks will be reduced (20% per day).• Make your excuse as clear as possible to Ms. Tavakoli (write it down and sign

it), when you deliver it late. To my experience, 29 excuses out of 30 are not justifies. So I will not accept those exercises.

– Make a folder for each exercise. So Student with Student No. X will have a folder called X and a subfolder called 1 for first set of exercises, 2 for the second ….

– Some exercises are programming exercises. – Some exercises are debugging. What I need you to do is

• To find a bug.• To write it down. (in a text file)• How did you find the bug. (Which strategy? Had you seen the error before? Did

you use any special technique?)• To write the solution that you found.• How long did it take to correct the entire buggy program.• Remember: you will not be assessed by the above parameters. It is just the way

that I can check your progress. However if you do not have this report, you will not get the mark for that exercise.

• 50% final exam.– It might be on-line.

Page 6: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

Lectures• Sunday starts 8:15• Tuesday: The lecture takes place in lab

Page 7: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

A brief history of java• Java 1.02

– 250 classes– Slow

• Java 1.1– 500 classes– A little faster– Better GUI code– Friendlier

• Java 2– 2300 classes– Much faster– Becomes a language of choice for new enterprise (esp. web-based and mobile

application)• Java 5.0

– 3500 classes– More power, easier to develop– Giving a feature that were popular in other languages

• Java 6.0– http://java.sun.com/javase/6/webnotes/features.html

Page 8: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

Code Structure in Java• Put a class in a source file

– .java extension– Every java application has to have at least one Class

• Put methods in a class– A class has one or more methods– Each method declares inside a class (within the curly braces of the class)

– Every java application has to have one main method per application• It is where JVM starts executing

• Put statements in a method– Within the curly braces of the methods, write the instruction on how that method should perform

Page 9: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

What goes inside methods

• Variables, Literals, etc.• Statements Do something• Loops Do something again & again– While, do.. While, for

• Branches Do something under this condition– if, if.. else, nested if, switch

Page 10: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

String• The most common activity in computer programming

• They are in java.lang so they are imported implicitly

• Java Provides 3 classes to manipulate strings.– String: for constant Strings– StringBuilder: For the strings that can change

– StringBuffer: the same as StringBuilder with more security for thread programming

Page 11: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

String• String MyFirstString = new String()

– Initializes a newly created String object so that it represents an empty character sequence.

• String MySecondString = new (char[] value)– Allocates a new String so that it represents the

sequence of characters currently contained in the character array argument.

• String MyThirdString = new String( String original)– Initializes a newly created String object so that

it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

Because strings are so common, Java includes them in its syntax so we can have String str = “Hello World”;

Page 12: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

String, Overloaded Operator

• +, += are the only operators that are overloaded for strings– Overload = the operator has been given extra meaning when used with a particular class

Page 13: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

Strings• Objects of String are immutable

– Look at every method in class String that modifies a String, they actually creates a new object and return it containing the modification. The object String left untouched. example: immutable class

– It means that when a String is passed to a method, it is actually a copy of the reference to that string.

– The reference that is passed to the string method, will exist as long as the body of the string is being executed.

Page 14: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

Operations on Strings

• char charAt(int index) • int codePointAt(int index) • intcompareTo(String anotherString) • intcompareToIgnoreCase(String str)• String concat(String str)• Boolean

equalsIgnoreCase(String anotherString)•  intindexOf(char ch)•  intindexOf(char ch, int fromIndex)

•  intindexOf(String str)•  intlength()•  Stringreplace(char oldChar,

char newChar)• Stringsubstring(int bgnIndx)• char[]toCharArray()       • StringtoLowerCase()• StringtoUpperCase()• StringvalueOf(int i)

Example: Substring.java

AllStringClasses.java

Have a look at http://java.sun.com/javase/6/docs/api/

For more operation on strings.

Page 15: Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.

Exercise• String-First.txt

– Deadline Sunday 85/12/6 at 3:45 pm.