SlideShare a Scribd company logo
1 of 14
Download to read offline
Week 7
A Breather
+
A Start on Objects
© 2004 Pearson Addison-Wesley. All rights reserved 7-2
Copyright Warning
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been copied and communicated to you by or
on behalf of Bond University pursuant to Part VB of the
Copyright Act 1968 (the Act).
The material in this communication may be subject to copyright
under the Act. Any further copying or communication of this
material by you may be the subject of copyright protection
under the Act.
Do not remove this notice.
© 2004 Pearson Addison-Wesley. All rights reserved 7-3
Halfway 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
© 2004 Pearson Addison-Wesley. All rights reserved 7-4
Programming, Not Java!
• None of these things are specific to Java.
• They are all applicable to most common
programming languages: C#, C++, PHP, Perl,
Actionscript, Javascript etc.
• This is why the course is called “Introduction to
Programming”, not “Java 1”
• Consider yourself as a budding tradesperson, e.g
carpenter, plumber
• You now have a toolbox of useful tools (loops,
methods, arrays, expressions)
• What you don't have yet is the experience of
knowing when is the right time to use each tool
© 2004 Pearson Addison-Wesley. All rights reserved 7-5
Some Tips
• Don't write code before having some form of a
design plan for your program
• Don't write the design before you have thought
about the problem
 worry about the data you have to store and manipulate
more than the code
• Think modular:
 “I'll have a method to do this, and a method to do..."
 Use a bit of top-down design and a bit of bottom-up
design
• Design your interfaces to your methods before
writing any of the code for the method
• A very good programmer will design unit tests for
each method at the same time
© 2004 Pearson Addison-Wesley. All rights reserved 7-6
Some More Tips
• NEVER, NEVER, write all the code in one hit
• Always write one bit and test that it works, even if
the code doesn't do everything you want
• Use print statements, which you can remove
• When a bug appears, fix it before adding any new
code!
 Learn to use the debugger, it will save your sanity
 Computers do what you tell them, not what you wanted
them to do. Beware your own assumptions!
• Always assume that the user is an idiot. Defend
your program against them.
 Similarly, test the inputs to your methods. Don't assume
that they are valid.
© 2004 Pearson Addison-Wesley. All rights reserved 7-7
Read Code Examples, Style
• Read code from other programmers. There is
usually a common 'style' or technique for doing
something
• Understand how it works. Don't learn a cookbook
answer: you may have to modify the technique to
suit your problem
• Style: write code as if the purpose of the code is to
explain to another programmer what the purpose
of 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?
© 2004 Pearson Addison-Wesley. All rights reserved 7-8
Questions?
• What are the biggest gaps in your programming
knowledge 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
© 2004 Pearson Addison-Wesley. All rights reserved 7-9
Group Exercise
• Sketch a design for a program to get a set of
scores from the user, and to print out the highest,
lowest, average score, and the percentage of
scores in each category: FL, PS, CR, DN, HD
• Don't write Java code
• What data is needed, how will it be stored?
• What high-level operations need to be done?
• What's the algorithm (in pseudo-code) to do the
operations?
• How will you make it modular?
© 2004 Pearson Addison-Wesley. All rights reserved 7-10
And 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 of
data items of the same type: arrays. What about
groups 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 a
human in Java?
© 2004 Pearson Addison-Wesley. All rights reserved 7-11
And 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 from
that class, e.g.
 Jenny is an example human
• All objects that come from a class will all have the
same set of attributes, e.g
 a name, an age, a height, a weight, a gender
• But, each object can have different values for
these attributes.
 Jenny will have a different name, age, height, weight
gender than Tomas
© 2004 Pearson Addison-Wesley. All rights reserved 7-12
Some Definitions
• A Java class defines the types of attributes that all
objects in this class have.
• A Java class also defines the actions, or methods
that each object in the class can perform.
• Finally, a Java class defines how we create, or
construct, an individual object.
• From a design point of view, we use a class to
hold together:
 a bunch of methods that are all somehow related, and
 the data that these methods might need to do their work
© 2004 Pearson Addison-Wesley. All rights reserved 7-13
Designing a Class
• First up, think of what attributes every member of
the class will have.
• An example: most bank accounts are pretty
similar: you can deposit money and then withdraw
it as long as you know the PIN number.
• Each bank account has some identity, and the
amount of money in the bank.
• If we were to design a class for bank accounts,
what attributes should we include, and what Java
type will each attribute have?
• Finally, each Java class goes into its own file
© 2004 Pearson Addison-Wesley. All rights reserved 7-14
The BankAccount Class
• Let's go with these attributes:
 account number, balance, PIN number
• The Java file for the BankAccount class would
look like:
public class BankAccount
{
int accountNumber;
double balance;
int PinNumber;
}
• That's all for now. More next week.....

More Related Content

Similar to Week07

Assignment 3 Presenting With PowerPointJane R. Doe .docx
Assignment 3 Presenting With PowerPointJane R. Doe           .docxAssignment 3 Presenting With PowerPointJane R. Doe           .docx
Assignment 3 Presenting With PowerPointJane R. Doe .docx
rock73
 

Similar to Week07 (20)

Upstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOPUpstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOP
 
Top 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersTop 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed Answers
 
Building Large Sustainable Apps
Building Large Sustainable AppsBuilding Large Sustainable Apps
Building Large Sustainable Apps
 
LESSON 8 EXPERT SYSTEMS BASICS.ppt
LESSON 8 EXPERT SYSTEMS BASICS.pptLESSON 8 EXPERT SYSTEMS BASICS.ppt
LESSON 8 EXPERT SYSTEMS BASICS.ppt
 
Introduction To Design Patterns Class 4 Composition vs Inheritance
 Introduction To Design Patterns Class 4 Composition vs Inheritance Introduction To Design Patterns Class 4 Composition vs Inheritance
Introduction To Design Patterns Class 4 Composition vs Inheritance
 
Surviving the technical interview
Surviving the technical interviewSurviving the technical interview
Surviving the technical interview
 
OOPs concepts.ppt
OOPs concepts.pptOOPs concepts.ppt
OOPs concepts.ppt
 
OOPs concepts.ppt
OOPs concepts.pptOOPs concepts.ppt
OOPs concepts.ppt
 
OOPs concepts.ppt
OOPs concepts.pptOOPs concepts.ppt
OOPs concepts.ppt
 
OOPs concepts.ppt
OOPs concepts.pptOOPs concepts.ppt
OOPs concepts.ppt
 
OOPs concepts.ppt
OOPs concepts.pptOOPs concepts.ppt
OOPs concepts.ppt
 
Java basics
Java basicsJava basics
Java basics
 
Top 10 Interview Questions for Coding Job.docx
Top 10 Interview Questions for Coding Job.docxTop 10 Interview Questions for Coding Job.docx
Top 10 Interview Questions for Coding Job.docx
 
Top 10 Interview Questions for Coding Job.docx
Top 10 Interview Questions for Coding Job.docxTop 10 Interview Questions for Coding Job.docx
Top 10 Interview Questions for Coding Job.docx
 
CPP16 - Object Design
CPP16 - Object DesignCPP16 - Object Design
CPP16 - Object Design
 
It's XP Stupid (2019)
It's XP Stupid (2019)It's XP Stupid (2019)
It's XP Stupid (2019)
 
Assignment 3 Presenting With PowerPointJane R. Doe .docx
Assignment 3 Presenting With PowerPointJane R. Doe           .docxAssignment 3 Presenting With PowerPointJane R. Doe           .docx
Assignment 3 Presenting With PowerPointJane R. Doe .docx
 
Write code and find a job
Write code and find a jobWrite code and find a job
Write code and find a job
 
E Learning Management System By Tuhin Roy Using PHP
E Learning Management System By Tuhin Roy Using PHPE Learning Management System By Tuhin Roy Using PHP
E Learning Management System By Tuhin Roy Using PHP
 
Introduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallIntroduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John Mulhall
 

More from hccit

Snr ipt 10_syll
Snr ipt 10_syllSnr ipt 10_syll
Snr ipt 10_syll
hccit
 
Snr ipt 10_guide
Snr ipt 10_guideSnr ipt 10_guide
Snr ipt 10_guide
hccit
 
3 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr123 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr12
hccit
 
3 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr113 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr11
hccit
 
10 ict photoshop_proj_2014
10 ict photoshop_proj_201410 ict photoshop_proj_2014
10 ict photoshop_proj_2014
hccit
 
Photoshop
PhotoshopPhotoshop
Photoshop
hccit
 
Flash
FlashFlash
Flash
hccit
 
University partnerships programs email
University partnerships programs emailUniversity partnerships programs email
University partnerships programs email
hccit
 
Griffith sciences pathway programs overview
Griffith sciences pathway programs overviewGriffith sciences pathway programs overview
Griffith sciences pathway programs overview
hccit
 
Griffith info tech brochure
Griffith info tech brochureGriffith info tech brochure
Griffith info tech brochure
hccit
 
Pm sql exercises
Pm sql exercisesPm sql exercises
Pm sql exercises
hccit
 
Movies questions
Movies questionsMovies questions
Movies questions
hccit
 
Australian birds questions
Australian birds questionsAustralian birds questions
Australian birds questions
hccit
 
Section b
Section bSection b
Section b
hccit
 
Section a
Section aSection a
Section a
hccit
 
Ask manual rev5
Ask manual rev5Ask manual rev5
Ask manual rev5
hccit
 
Case study report mj
Case study report mjCase study report mj
Case study report mj
hccit
 
Mj example case_study_layout_intro_completedq
Mj example case_study_layout_intro_completedqMj example case_study_layout_intro_completedq
Mj example case_study_layout_intro_completedq
hccit
 

More from hccit (20)

Snr ipt 10_syll
Snr ipt 10_syllSnr ipt 10_syll
Snr ipt 10_syll
 
Snr ipt 10_guide
Snr ipt 10_guideSnr ipt 10_guide
Snr ipt 10_guide
 
3 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr123 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr12
 
3 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr113 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr11
 
10 ict photoshop_proj_2014
10 ict photoshop_proj_201410 ict photoshop_proj_2014
10 ict photoshop_proj_2014
 
Photoshop
PhotoshopPhotoshop
Photoshop
 
Flash
FlashFlash
Flash
 
University partnerships programs email
University partnerships programs emailUniversity partnerships programs email
University partnerships programs email
 
Griffith sciences pathway programs overview
Griffith sciences pathway programs overviewGriffith sciences pathway programs overview
Griffith sciences pathway programs overview
 
Griffith info tech brochure
Griffith info tech brochureGriffith info tech brochure
Griffith info tech brochure
 
Pm sql exercises
Pm sql exercisesPm sql exercises
Pm sql exercises
 
Movies questions
Movies questionsMovies questions
Movies questions
 
Australian birds questions
Australian birds questionsAustralian birds questions
Australian birds questions
 
Section b
Section bSection b
Section b
 
B
BB
B
 
A
AA
A
 
Section a
Section aSection a
Section a
 
Ask manual rev5
Ask manual rev5Ask manual rev5
Ask manual rev5
 
Case study report mj
Case study report mjCase study report mj
Case study report mj
 
Mj example case_study_layout_intro_completedq
Mj example case_study_layout_intro_completedqMj example case_study_layout_intro_completedq
Mj example case_study_layout_intro_completedq
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Week07

  • 1. Week 7 A Breather + A Start on Objects
  • 2. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 Copyright Warning COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been copied and communicated to you by or on behalf of Bond University pursuant to Part VB of the Copyright Act 1968 (the Act). The material in this communication may be subject to copyright under the Act. Any further copying or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.
  • 3. © 2004 Pearson Addison-Wesley. All rights reserved 7-3 Halfway 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-4 Programming, Not Java! • None of these things are specific to Java. • They are all applicable to most common programming languages: C#, C++, PHP, Perl, Actionscript, Javascript etc. • This is why the course is called “Introduction to Programming”, not “Java 1” • Consider yourself as a budding tradesperson, e.g carpenter, plumber • You now have a toolbox of useful tools (loops, methods, arrays, expressions) • What you don't have yet is the experience of knowing when is the right time to use each tool
  • 5. © 2004 Pearson Addison-Wesley. All rights reserved 7-5 Some Tips • Don't write code before having some form of a design plan for your program • Don't write the design before you have thought about the problem  worry about the data you have to store and manipulate more than the code • Think modular:  “I'll have a method to do this, and a method to do..."  Use a bit of top-down design and a bit of bottom-up design • Design your interfaces to your methods before writing any of the code for the method • A very good programmer will design unit tests for each method at the same time
  • 6. © 2004 Pearson Addison-Wesley. All rights reserved 7-6 Some More Tips • NEVER, NEVER, write all the code in one hit • Always write one bit and test that it works, even if the code doesn't do everything you want • Use print statements, which you can remove • When a bug appears, fix it before adding any new code!  Learn to use the debugger, it will save your sanity  Computers do what you tell them, not what you wanted them to do. Beware your own assumptions! • Always assume that the user is an idiot. Defend your program against them.  Similarly, test the inputs to your methods. Don't assume that they are valid.
  • 7. © 2004 Pearson Addison-Wesley. All rights reserved 7-7 Read Code Examples, Style • Read code from other programmers. There is usually a common 'style' or technique for doing something • Understand how it works. Don't learn a cookbook answer: you may have to modify the technique to suit your problem • Style: write code as if the purpose of the code is to explain to another programmer what the purpose of 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-8 Questions? • What are the biggest gaps in your programming knowledge 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-9 Group Exercise • Sketch a design for a program to get a set of scores from the user, and to print out the highest, lowest, average score, and the percentage of scores in each category: FL, PS, CR, DN, HD • Don't write Java code • What data is needed, how will it be stored? • What high-level operations need to be done? • What's the algorithm (in pseudo-code) to do the operations? • How will you make it modular?
  • 10. © 2004 Pearson Addison-Wesley. All rights reserved 7-10 And 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 of data items of the same type: arrays. What about groups 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 a human in Java?
  • 11. © 2004 Pearson Addison-Wesley. All rights reserved 7-11 And 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 from that class, e.g.  Jenny is an example human • All objects that come from a class will all have the same set of attributes, e.g  a name, an age, a height, a weight, a gender • But, each object can have different values for these attributes.  Jenny will have a different name, age, height, weight gender than Tomas
  • 12. © 2004 Pearson Addison-Wesley. All rights reserved 7-12 Some Definitions • A Java class defines the types of attributes that all objects in this class have. • A Java class also defines the actions, or methods that each object in the class can perform. • Finally, a Java class defines how we create, or construct, an individual object. • From a design point of view, we use a class to hold 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-13 Designing a Class • First up, think of what attributes every member of the class will have. • An example: most bank accounts are pretty similar: you can deposit money and then withdraw it as long as you know the PIN number. • Each bank account has some identity, and the amount of money in the bank. • If we were to design a class for bank accounts, what attributes should we include, and what Java type will each attribute have? • Finally, each Java class goes into its own file
  • 14. © 2004 Pearson Addison-Wesley. All rights reserved 7-14 The BankAccount Class • Let's go with these attributes:  account number, balance, PIN number • The Java file for the BankAccount class would look like: public class BankAccount { int accountNumber; double balance; int PinNumber; } • That's all for now. More next week.....