Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPE212 – Reminders The other four assignments are now posted.

Similar presentations


Presentation on theme: "CMPE212 – Reminders The other four assignments are now posted."— Presentation transcript:

1 CMPE212 – Reminders The other four assignments are now posted.
Winter 2019 CMPE212 4/11/2019 CMPE212 – Reminders The other four assignments are now posted. First quiz in the lab next week, starting Monday. More info to follow. You are in two groups in onQ – your Lab and your Grader. Assignment 1 due next Friday. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

2 Quiz 1 Topics Everything up to and including 2D Arrays.
Exercises 1 and 3, and assignment 1 are fair game and would be good preparation for the Quiz. More details on next slide: Winter 2019 CMPE212 - Prof. McLeod

3 Quiz 1 Topics, Cont. Emphasis on: Java history and operation.
Primitive types, variable declaration and initialization, expressions, conditionals and loops. Type casting of primitive types. Screen (or "console") output using System.out.println() and System.out.printf(). Screen input using the Scanner class. Arrays – one and two dimensional. Methods and attributes – use, declaration and coding. Winter 2019 CMPE212 - Prof. McLeod

4 Quiz 1 Restrictions The quiz will start by showing that it is open only for the Monday lab between 12:30 and 2:30am. When this lab is over the quiz will be re-opened for the Wednesday lab. You must write the quiz in the lab you chose in SOLUS. This is the Lab group you have been assigned to in onQ. If you cannot do so, you will need to contact me with a good reason to change labs. The quiz can only be opened in JEFF155 – it is IP address restricted to only the lab machines. Winter 2019 CMPE212 - Prof. McLeod

5 Quiz 1 Format Electronic – in onQ. ~45 minutes. T/F and short answer.
Short answer could be a code segment, writing a complete method or possibly a complete program. Code writing will be towards the end of the quiz – leave yourself enough time!!! You will need to be able to read and write procedural Java code. Winter 2019 CMPE212 - Prof. McLeod

6 Quiz 1 Format, Cont. When you type code directly into a multi-line answer box, avoid using the <tab> key. This key will not give you a tabbed space but will just move the cursor out of the box, which is really annoying. Use just the spacebar. Style is not critical, but nice looking code is easier to read and grade by an old guy like me… Winter 2019 CMPE212 - Prof. McLeod

7 Quiz 1 Rules You will use onQ only and no other programs can be used on any device. No other aids. The TA must be able to see your screen. No talking to other students when writing. If you have written, please keep the noise down until all other students have finished writing the quiz. Winter 2019 CMPE212 - Prof. McLeod

8 Quiz 1 Rules, Cont. Do not discuss the quiz with students who have not written it. Do not provide quiz answers to students who have not written it. The TAs will note the names of any “violators” and their quizzes will become zero! The latter half of the lab is available for assignment help. Winter 2019 CMPE212 - Prof. McLeod

9 Today Bad Code… Multi-Dimensional Arrays.
Style rules for assignment 1. Start discussing Building Modular Code. Winter 2019 CMPE212 - Prof. McLeod

10 Bad Code 1 What is “wrong” with this code? (It compiles fine.)
boolean continueFlag = true; String userInput; while (continueFlag != false) { userInput = IOHelper.getString("Enter \"yes\" to stop loop: "); continueFlag = !userInput.equals("yes"); } Winter 2019 CMPE212 - Prof. McLeod

11 Bad Code 2 What is “wrong” with this kind of loop structure?:
int i = 0; while (true) { System.out.println("i = " + i); if (i >= 20) break; i = i + 1; } Winter 2019 CMPE212 - Prof. McLeod

12 Bad Code 3 What is “wrong” with this structure?: String userInput;
for (int i = 0; i < 100; i++) { System.out.println("i = " + i); userInput = IOHelper.getString("Enter \"yes\" to stop loop: "); if (userInput.equals("yes")) i = 100; } Winter 2019 CMPE212 - Prof. McLeod

13 Introduction to Multi-Dimensional Arrays
Fall 2013 CMPE212 Introduction to Multi-Dimensional Arrays We have discussed the declaration of 1D arrays. For example: double[] oneD = new double[100]; To assign values, for example: for (int i = 0; i < oneD.length; i++) oneD[i] = i; Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

14 Multi-Dimensional Arrays, Cont.
For each dimension, just add another set of [ ]: int[][] twoD = new int[4][20]; Has room for 80 values. To assign, for example: int row, col; for (row = 0; row < twoD.length; row++) for (col = 0; col < twoD[row].length; col++) twoD[row][col] = row * col; Winter 2019 CMPE212 - Prof. McLeod

15 Multi-Dimensional Arrays, Cont.
It is helpful to think of a 2D array as storing tabular data, like a spreadsheet. For convenience (and to help you code consistently) you can think of the first dimension as the rows and the second dimension as the columns. To get the number of rows, use twoD.length, and to get the number of columns, grab one of the rows and use twoD[0].length. Winter 2019 CMPE212 - Prof. McLeod

16 Multi-Dimensional Arrays, Cont.
You can use three sets of [ ] to get a 3 dimensional array. Using the spreadsheet analogy, the third dimension could be the sheet number, where each sheet contains a table. For a good analogy for a 4D array you might need to look up a Star Trek script… Winter 2019 CMPE212 - Prof. McLeod

17 Iterating 2D Arrays See TestLoops.java
Can a for/each loop be used to assign or alter values in an array? Winter 2019 CMPE212 - Prof. McLeod

18 Assignment 1 Style Hints
Let Eclipse help you with style! It will “suggest” indentation and spacing – let it! Separate methods with a blank line to make them stand out. Minimize the use of blank lines inside methods. Follow the { } alignment conventions discussed in class. Put spaces on either side of binary operators. Do not put spaces before , or ; Put a single space after , in a parameter list and after ; in a for loop. Winter 2019 CMPE212 - Prof. McLeod

19 Assignment 1 Style Hints, Cont.
There is never any need for more than one empty line at a time. Once a line or comment is indented you don’t have any need for multiple consecutive spaces in the rest of the line. If your code lines are shorter, you can choose to use in-line comments lined up at the RHS of your code, but: If your lines of code are longer, then use comments above and lined up with the code they are commenting. Winter 2019 CMPE212 - Prof. McLeod

20 Assignment 1 Style Hints, Cont.
Use descriptive method, attribute and variable names. Usually a name can have one or two nouns or a verb/noun combination, for a method. Don’t get carried away! Be concise, yet descriptive. Use camelCase or the underscore, _, but not both. Remember to capitalize constant attribute names (the only kinds of attributes allowed in assignment 1, along with Scanner and/or Random class objects). You can use _ in these names… Winter 2019 CMPE212 - Prof. McLeod

21 Assignment 1 Commenting
Place a block comment at the top of your program with your name and SN. Also, in general terms, describe what your program does (a few sentences only). Use one or two sentences before each method to describe what it does and any assumptions made. Indicate what the parameters are. Use in-line comments to specify units, where needed. A few comments inside methods might help to describe what is going on – don’t get carried away! Winter 2019 CMPE212 - Prof. McLeod

22 More Style Hints Leaving commented out code in your program is really irritating to someone trying to read your program – it makes it look unfinished. If your program is not finished – go ahead and admit this in a comment above the method or class. Note what you think the problem is. Your TA is going to find out anyways if the code is broken – so you might as well just come clean and save her some effort! Winter 2019 CMPE212 - Prof. McLeod

23 Style Demo Programs See: NoStyle.java PoorStyle.java GoodStyle.java
They all are syntactically correct, will compile without error and all do the same thing. Winter 2019 CMPE212 - Prof. McLeod

24 Building Modular Code You should already know the advantages to modularity: Easier to build. Easier to test. Easier to debug. Easier to modify. Easier to share. Building objects just takes modular design to the next level. But for now, let’s focus on methods (or functions). Winter 2019 CMPE212 - Prof. McLeod

25 Designing Methods Methods are written to avoid repeating code. So, make sure you only have one method to do the “one thing”. Methods should be short: How short is “short”? One to ten lines? If you can satisfy all the other rules and the code still explains itself, then the method is short enough. Winter 2019 CMPE212 - Prof. McLeod

26 Designing Methods, Cont.
Methods should only do one thing and do it well. Yah, but how can we define “one thing”? Write the most abstract description of what the method does – does it still have the word “and” in it? Look for loosely coupled sections within a method if it seems too large – these are natural dividing points. Sometimes the coder puts an empty line between sections as an unconscious admission of loose coupling. Look at the data flow in the method – do all parts of the method need all of the arguments? Winter 2019 CMPE212 - Prof. McLeod

27 Designing Methods, Cont.
Keep all code within the method at the same level of abstraction. If you find yourself writing detailed code right beside high level code, you are not following this rule. Your program should be readable as a top-down narrative. The most abstract methods will be at the top of the program, leading to the least abstract methods further down. (BTW, I’m bad and I don’t always follow this rule.) Winter 2019 CMPE212 - Prof. McLeod

28 Designing Methods, Cont.
Use less than three parameters wherever possible – the best number is zero, but not at the expense of introducing more attributes. Try to use parameters for input only. Flag parameters are usually ugly – they are saying that the method does at least two things. For example, how to write methods when you have a computer and a human player? One method for both, or two methods that are slightly different? Winter 2019 CMPE212 - Prof. McLeod

29 Designing Methods, Cont.
If needed, multiple parameters can be grouped into an object or list, provided they share a theme. For example: drawCircle(Point centrePoint, int radius) is better than drawCircle(int centreX, int centreY, int radius) Check to see how readable the method name is when it has its parameter list – does it read like a sentence? (verb then noun?). Winter 2019 CMPE212 - Prof. McLeod

30 Designing Methods, Cont.
The method should not spawn any side effects. Such as changing the contents of variables passed by reference. Or spawning off another process that is at the same level of abstraction as the method itself. The method should only invoke methods that are at a lower level of abstraction than itself. A method should either do something or answer something, not both. Winter 2019 CMPE212 - Prof. McLeod

31 How to Write Good Methods
It is not easy to do, especially if you are used to writing much larger methods. Be prepared to write and then re-write your code several times. Each time, you will probably be breaking larger methods into smaller ones. Each time you re-write (or refactor) the code it gets better (tidier, easier to read, and often shorter!). Will a multiple method program be faster than a single method version that does the same thing? Winter 2019 CMPE212 - Prof. McLeod


Download ppt "CMPE212 – Reminders The other four assignments are now posted."

Similar presentations


Ads by Google