Week07
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
324
On Slideshare
159
From Embeds
165
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 165

http://mycsa.edu.au 165

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Week 7A Breather+A Start on Objects
  • 2. © 2004 Pearson Addison-Wesley. All rights reserved 7-2Copyright WarningCOMMONWEALTH OF AUSTRALIACopyright Regulations 1969WARNINGThis material has been copied and communicated to you by oron behalf of Bond University pursuant to Part VB of theCopyright Act 1968 (the Act).The material in this communication may be subject to copyrightunder the Act. Any further copying or communication of thismaterial by you may be the subject of copyright protectionunder the Act.Do not remove this notice.
  • 3. © 2004 Pearson Addison-Wesley. All rights reserved 7-3Halfway Through• We are halfway through the course!• You now know the basics of programming: basic data types operators and expressions assignment statements selection statements: IF ... ELSE loops: WHILE, DO ... WHILE, FOR basic user input and output making code modular with methods• copying arguments into methods• returning results out of methods arrays to hold a set of data of the same type
  • 4. © 2004 Pearson Addison-Wesley. All rights reserved 7-4Programming, Not Java!• None of these things are specific to Java.• They are all applicable to most commonprogramming languages: C#, C++, PHP, Perl,Actionscript, Javascript etc.• This is why the course is called “Introduction toProgramming”, not “Java 1”• Consider yourself as a budding tradesperson, e.gcarpenter, plumber• You now have a toolbox of useful tools (loops,methods, arrays, expressions)• What you dont have yet is the experience ofknowing when is the right time to use each tool
  • 5. © 2004 Pearson Addison-Wesley. All rights reserved 7-5Some Tips• Dont write code before having some form of adesign plan for your program• Dont write the design before you have thoughtabout the problem worry about the data you have to store and manipulatemore than the code• Think modular: “Ill have a method to do this, and a method to do..." Use a bit of top-down design and a bit of bottom-updesign• Design your interfaces to your methods beforewriting any of the code for the method• A very good programmer will design unit tests foreach method at the same time
  • 6. © 2004 Pearson Addison-Wesley. All rights reserved 7-6Some More Tips• NEVER, NEVER, write all the code in one hit• Always write one bit and test that it works, even ifthe code doesnt do everything you want• Use print statements, which you can remove• When a bug appears, fix it before adding any newcode! Learn to use the debugger, it will save your sanity Computers do what you tell them, not what you wantedthem to do. Beware your own assumptions!• Always assume that the user is an idiot. Defendyour program against them. Similarly, test the inputs to your methods. Dont assumethat they are valid.
  • 7. © 2004 Pearson Addison-Wesley. All rights reserved 7-7Read Code Examples, Style• Read code from other programmers. There isusually a common style or technique for doingsomething• Understand how it works. Dont learn a cookbookanswer: you may have to modify the technique tosuit your problem• Style: write code as if the purpose of the code is toexplain to another programmer what the purposeof the program is indenting and spacing comments: why this is being done, not how good variable names good structure: methods• Will you understand your program in 6 months?
  • 8. © 2004 Pearson Addison-Wesley. All rights reserved 7-8Questions?• What are the biggest gaps in your programmingknowledge so far?• How can we help you to fill those gaps?• What is still conceptually hard to grasp?• Do you need more examples to read?• Do YOU need to spend more time writing code?“The only way to learn programming is to write code"- Ken Thompson
  • 9. © 2004 Pearson Addison-Wesley. All rights reserved 7-9Group Exercise• Sketch a design for a program to get a set ofscores from the user, and to print out the highest,lowest, average score, and the percentage ofscores in each category: FL, PS, CR, DN, HD• Dont write Java code• What data is needed, how will it be stored?• What high-level operations need to be done?• Whats the algorithm (in pseudo-code) to do theoperations?• How will you make it modular?
  • 10. © 2004 Pearson Addison-Wesley. All rights reserved 7-10And Now, Classes and Objects...• We have seen how to store individual data items:int, char, double• We have seen how to store and manipulate sets ofdata items of the same type: arrays. What aboutgroups of data of different types?• In the real world, things have several attributes,e.g a human has a name an age a height a weight a gender: male or female• How can we represent a complex thing like ahuman in Java?
  • 11. © 2004 Pearson Addison-Wesley. All rights reserved 7-11And Now, Classes and Objects...• In Java, classes are used to group related data,and to manipulate that data.• A class is a template for the objects in a class• An object is a particular example of a thing fromthat class, e.g. Jenny is an example human• All objects that come from a class will all have thesame set of attributes, e.g a name, an age, a height, a weight, a gender• But, each object can have different values forthese attributes. Jenny will have a different name, age, height, weightgender than Tomas
  • 12. © 2004 Pearson Addison-Wesley. All rights reserved 7-12Some Definitions• A Java class defines the types of attributes that allobjects in this class have.• A Java class also defines the actions, or methodsthat each object in the class can perform.• Finally, a Java class defines how we create, orconstruct, an individual object.• From a design point of view, we use a class tohold together: a bunch of methods that are all somehow related, and the data that these methods might need to do their work
  • 13. © 2004 Pearson Addison-Wesley. All rights reserved 7-13Designing a Class• First up, think of what attributes every member ofthe class will have.• An example: most bank accounts are prettysimilar: you can deposit money and then withdrawit as long as you know the PIN number.• Each bank account has some identity, and theamount of money in the bank.• If we were to design a class for bank accounts,what attributes should we include, and what Javatype will each attribute have?• Finally, each Java class goes into its own file
  • 14. © 2004 Pearson Addison-Wesley. All rights reserved 7-14The BankAccount Class• Lets go with these attributes: account number, balance, PIN number• The Java file for the BankAccount class wouldlook like:public class BankAccount{int accountNumber;double balance;int PinNumber;}• Thats all for now. More next week.....