1 / 13

Week 7

Week 7. A Breather + A Start on Objects. 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

chul
Download Presentation

Week 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 7 A Breather + A Start on Objects

  2. 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

  3. Programming, Not Java! None of these things are specific to Java. They are all applicable to most common programming languages: C#, C++, PHP, Perl, Javascript etc. This is why the course is called “Software Development 1”, 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

  4. 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

  5. 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.

  6. 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?

  7. 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

  8. 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?

  9. 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?

  10. 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

  11. 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

  12. 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

  13. 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