CISC124 Labs start this week in JEFF 155.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
VB .NET Programming Fundamentals
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Java Unit 9: Arrays Declaring and Processing Arrays.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
PHP Constructs Advance Database Management Systems Lab no.3.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
Last Time Unary operators: Other Assignment operators
EGR 2261 Unit 10 Two-dimensional Arrays
Loops BIS1523 – Lecture 10.
Introduction To Repetition The for loop
© 2016 Pearson Education, Ltd. All rights reserved.
EGR 2261 Unit 4 Control Structures I: Selection
Loop Structures.
CISC124 Labs start this week in JEFF 155: Meet your TA.
CISC/CMPE320 - Prof. McLeod
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
JavaScript: Functions.
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CISC/CMPE320 - Prof. McLeod
Arrays, For loop While loop Do while loop
Java - Data Types, Variables, and Arrays
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Winter 2018 CISC101 11/27/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Arrays 6-Dec-18.
T. Jumana Abu Shmais – AOU - Riyadh
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Matlab tutorial course
CISC124 Assignment 3 due tomorrow at 7pm.
MSIS 655 Advanced Business Applications Programming
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC124 TA names and s have been added to the course web site.
CISC124 Labs start this week in JEFF 155. Fall 2018
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Beginning Style 27-Feb-19.
CMPE212 – Reminders The other four assignments are now posted.
Winter 2019 CISC101 4/8/2019 CISC101 Reminders
CISC101 Reminders All assignments are now posted.
Loops and Arrays in JavaScript
Functions continued.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CMPE212 – Reminders The other four assignments are now posted.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Tutorial 10: Programming with javascript
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
CISC101 Reminders Quiz 1 marking underway.
CISC101 Reminders All assignments are now posted.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Unit 3: Variables in Java
Chapter 2 Primitive Data Types and Operations
CISC101 Reminders Assignment 3 due today.
Presentation transcript:

CISC124 Labs start this week in JEFF 155. Fall 2018 CISC124 2/16/2019 CISC124 Labs start this week in JEFF 155. Assignment 1 due next Friday, 7pm. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

QWIC Program Tutorial Open to all students in this course. First one will be held: On Sept. 24 (Monday) at 8pm In Mac-Corry D201. By Hannah LeBlanc. I sent out an email Tuesday with more information and an RSVP link. Fall 2018 CISC124 - Prof. McLeod

Today Finish up Loops We covered while, do/while and for loops. Left off at “for each” loops. Multi-Dimensional Arrays, a Loop Demo. Assignment 1 Style Hints. Fall 2018 CISC124 - Prof. McLeod

But First, What is “wrong” with this code: boolean continueFlag = true; String userInput; while (continueFlag != false) { userInput = IOHelper.getString("Enter \"yes\" to stop loop: "); continueFlag = !userInput.equals("yes"); } Fall 2018 CISC124 - Prof. McLeod

And, 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; } Fall 2018 CISC124 - Prof. McLeod

Also, 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; } Fall 2018 CISC124 - Prof. McLeod

“for each” Loop Often, you will want to visit every element in a collection, not just a part. Syntax of the “for each” loop: for (type variable : collection) { // statements } Fall 2018 CISC124 - Prof. McLeod

“for each” Loop, Cont. For example, suppose we have an array called data, containing a collection of double type numbers, and you want to add them all up: double sum = 0; for (double e : data) sum = sum + e; var can be used to type the element variable in a for each loop. or sum += e; Fall 2018 CISC124 - Prof. McLeod

“for each” Loop, Cont. Equivalent normal for loop: double sum = 0; for (int i = 0; i < data.length; i++) sum = sum + data[i]; The “for each” loop is a bit easier with arrays, but is even better suited for other kinds of collections. Fall 2018 CISC124 - Prof. McLeod

Loops - Misc. Don’t declare variables inside loops, as the repeated declaration process uses up time and memory unnecessarily. There is no limit in Java to how many levels you can nest loops. It is customary, but not necessary, to use the variables i, j, k as loop counters when the counter has no intrinsic meaning. Fall 2018 CISC124 - Prof. McLeod

Other Java Keywords Used With Loops break and continue The continue statement interrupts the execution of a loop, and returns control to the top of the loop. The break statement interrupts the execution of a loop, and transfers control to the first statement after the loop. Fall 2018 CISC124 - Prof. McLeod

Use of continue For example: Displays: for (i = 1; i <= 5; i++) { if ( i == 3 ) continue; System.out.println(“i = ” + i); } System.out.println(“End of Loop!”); Displays: i = 1 i = 2 i = 4 i = 5 End of Loop! Fall 2018 CISC124 - Prof. McLeod

Use of break For example: Displays: for (i = 1; i <= 5; i++) { if ( i == 3 ) break; System.out.println(“i = ” + i); } System.out.println(“End of Loop!”); Displays: i = 1 i = 2 End of Loop! Fall 2018 CISC124 - Prof. McLeod

Use of “break” & “continue” Only use these keywords when it makes your code easier to read. Avoid the use of more than one break or continue inside a loop. If you use a simple condition to issue a break statement, then why can’t you put that condition in the loop test? Overuse of break statements can lead to “spaghetti” code - just like the use of “goto” statements! Fall 2018 CISC124 - Prof. McLeod

Introduction to Multi-Dimensional Arrays Fall 2013 CISC124 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; Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

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; Fall 2018 CISC124 - Prof. McLeod

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. Fall 2018 CISC124 - Prof. McLeod

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… Fall 2018 CISC124 - Prof. McLeod

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

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. Fall 2018 CISC124 - Prof. McLeod

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. Fall 2018 CISC124 - Prof. McLeod

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). You can use _ in these names… Fall 2018 CISC124 - Prof. McLeod

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! Fall 2018 CISC124 - Prof. McLeod

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 him some effort! Fall 2018 CISC124 - Prof. McLeod

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. Fall 2018 CISC124 - Prof. McLeod