BIT 115: Introduction To Programming

Slides:



Advertisements
Similar presentations
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Advertisements

CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Lecture 9a Instructor: Craig Duckett CALL ME. Assignment 2 Revision DUE THIS Wednesday, August 5 th Assignment 3 DUE Monday, August 10 th Assignment 3.
Java Unit 9: Arrays Declaring and Processing Arrays.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
ArrayList, Multidimensional Arrays
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CSE 1201 Object Oriented Programming ArrayList 1.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
BIT115: Introduction to Programming
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Lecture 14/15 Instructor: Craig Duckett. 2 ANNOUNCEMENTS For various reasons, the Cascadia Faculty web server occasionally goes down, making it impossible.
Lecture 11 Instructor: Craig Duckett Instance Variables.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
BIT115: Introduction to Programming
BIT 115: Introduction To Programming
CMSC 202 ArrayList Aug 9, 2007.
BIT 115: Introduction To Programming
BIT 115: Introduction To Programming
EGR 2261 Unit 10 Two-dimensional Arrays
Some Eclipse shortcuts
BIT 115: Introduction To Programming
BIT115: Introduction to Programming
BIT 115: Introduction To Programming
BIT 115: Introduction To Programming
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
BIT115: Introduction to Programming
BIT115: Introduction to Programming
Arrays, For loop While loop Do while loop
Class Structure 16-Nov-18.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
CMSC 202 ArrayList Aug 9, 2007.
Class Structure 7-Dec-18.
Introduction To Programming Information Technology , 1’st Semester
BIT115: Introduction to Programming
Class Structure 2-Jan-19.
CMSC 202 ArrayList Aug 9, 2007.
CS2011 Introduction to Programming I Arrays (I)
Arrays ICS2O.
Class Structure 25-Feb-19.
Arrays in Java.
Java Programming Language
Peer Instruction 4 Control Loops.
LCC 6310 Computation as an Expressive Medium
CISC101 Reminders Assignment 3 due today.
Review for Midterm 3.
Arrays.
Presentation transcript:

BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana” https://cascadia.instructure.com/courses/1175442

Lecture 16 Log into Canvas Announcements Quiz Lecture ICE16 Put your name sticker on your computer Announcements Quiz Lecture Static vs Instance Methods Nested loops A Gentle Intro to Arrays (of primitives) ICE16

Announcements Graded Midterm Java code (and corrected Question 16) You can have more than one constructor, as long they have different parameters! You call a particular constructor depending on the object instance you want At the end of an exam/ICE, check your work until you run out of time Re-read the instructions! Getting ready for Assignment 4 You’ll need up to today’s lecture before you can start Did you take the 5-point Mid-Course Survey? Did you take the 5-point Assignment 4 Partner Preferences Survey? Plan to post A4 before class on Wednesday

Quiz16 Access Code: happy returns

A Quick Word About Static No, not these kinds of static

Static Defined: In Java, a static member is a member of a class that is not associated with an instance (object) of a class. Instead, the member belongs to the class itself. As a result, you can access the static member without first creating a class instance (object).

Class Object ! What the hay? public class FileName4 extends Object { public static void main(String[] args) { System.out.println( “I printed!"); } } Class … but no Object ! What the hay? The main method is going to run whether it contains an instance (object) of a class or not. It is declared static for just such this reason—static means that main does not need an instance (object) of the class that contains it to be created in order for main to run, because main is set up to be its own instance. In this way, main acts like the starter on a car, it doesn’t need a starter to start the starter…it is the starter. Interesting note: you can compile your Java program without a main method, you just can’t run it! (just like you can build a car without a starter, but you can’t start it without the starter). Now, if you are wanting to use other classes (their “actions” and “attributes”) down in main, then you do need to create an instance of those classes ( a named object) that can actually do the something (whatever that something is) that you want done. For instance (pun intended!) when we are using the Becker Robot class, then we need to create a named instance of the Robot class (e.g., a Robot object called “Karel” or “Jo” or “Mary”) if we want to see any activities and actions done (move, pickThing, putThing, etc). Without an Object calling the actions, these methods only remain unused and potential…

This will not compile (there is no object to do the action) public class StaticDemo { int my_member_variable = 99999; public static void main (String args[]) { // Access a non-static member from static method System.out.println ("This generates a compiler error :-( " + my_member_variable); } } This will compile (there is an object called demo to do the action) public class NonStaticDemo { int my_member_variable = 99999; public static void main (String args[]) { NonStaticDemo demo = new NonStaticDemo(); // Access member variable of demo System.out.println ("This WON'T generate an error! --> " + demo.my_member_variable ); } }

Static Method A static method does not need an object to call it. It can call itself! You there? I’m here! One of the basic rules of working with static methods is that you can’t access a nonstatic method or field from a static method because the static method doesn’t have an instance of the class to use to reference instance methods or fields.

Class Static Method Method is called in main and a value is returned public class FileName3 extends Object { public static int printNum() { System.out.println("Going to print, some number of times!"); int howManyPrints = 0; while(howManyPrints < 2) { System.out.println("Printing!"); howManyPrints++; // This is a basic counter } return howManyPrints; } public static void main(String[] args) { int num = 0; num = printNum(); // <-- Notice this method call has no object System.out.println( "The method printed " + num + " times!"); } } Class Static Method Method is called in main and a value is returned and put into num

Some Additional Static Examples StaticDemo1.java Does Not Compile StaticDemo2.java Does Compile StaticDemo3.java A Static Method

Public (or ‘Instance’) Method A public method does need an object to call it. It can not call itself! Therefore, in order to use a public method down in main you need to create an instance of an object from the class that contains the method. I’m a Method! Glad you Called! I’m an Object!

Nested Loops

FOR LOOP WALK THROUGH See NestedFor.java public class NestedFor { public static void main(String [] args) { for (int i = 1; i <= 7; i++) { System.out.println(); for (int j = 1; j <= i; j++) { System.out.print("*"); } } } } See NestedFor.java

01 02 * 03 04 05 06 07 08 09 10 i = 1 1 is less than or equal to 7 public class NestedFor // FIRST TIME THROUGH THE LOOP { public static void main(String [] args) { for (int i = 1; i <= 7; i++) { System.out.println(); for (int j = 1; j <= i; j++) { System.out.print("*"); } } } } i = 1 1 is less than or equal to 7 New line j = 1 1 is less than or equal to 1 Print * , increment, and loop 2 is NOT equal to 1 so fall out and return to outer for 01 02 * 03 04 05 06 07 08 09 10

01 02 * 03 ** 04 05 06 07 08 09 10 i = 2 2 is less than or equal to 7 public class NestedFor // SECOND TIME THROUGH THE LOOP { public static void main(String [] args) { for (int i = 1; i <= 7; i++) { System.out.println(); for (int j = 1; j <= i; j++) { System.out.print("*"); } } } } 2 i = 2 2 is less than or equal to 7 New line j = 1 1 is less than or equal to 2 Print * , increment, and loop 2 is less than or equal to 2 3 is NOT equal to 2, so fall out and return to outer for 01 02 * 03 ** 04 05 06 07 08 09 10

public class NestedFor // THIRD TIME THROUGH THE LOOP { public static void main(String [] args) { for (int i = 1; i <= 7; i++) { System.out.println(); for (int j = 1; j <= i; j++) { System.out.print("*"); } } } } 3 i = 3 3 is less than or equal to 7 New line j = 1 1 is less than or equal to 3 Print * , increment, and loop 2 is less than or equal to 3 3 is less than or equal to 3 4 is NOT equal to 3, so fall out and return to outer for 01 02 * 03 ** 04 *** 05 06 07 08 09 10

public class NestedFor // FOURTH TIME THROUGH THE LOOP { public static void main(String [] args) { for (int i = 1; i <= 7; i++) { System.out.println(); for (int j = 1; j <= i; j++) { System.out.print("*"); } } } } 4 i = 4 4 is less than or equal to 7 New line j = 1 1 is less than or equal to 4 Print * , increment, and loop 2 is less than or equal to 4 3 is less than or equal to 4 4 is less than or equal to 4 5 is NOT equal to 4, so fall out and return to outer for 01 02 * 03 ** 04 *** 05 **** 06 07 08 09 10

public class NestedFor // FIFTH TIME THROUGH THE LOOP { public static void main(String [] args) { for (int i = 1; i <= 7; i++) { System.out.println(); for (int j = 1; j <= i; j++) { System.out.print("*"); } } } } 5 i = 5 5 is less than or equal to 7 New line j = 1 1 is less than or equal to 5 Print * , increment, and loop 2 is less than or equal to 5 3 is less than or equal to 5 4 is less than or equal to 5 5 is less than or equal to 5 6 is NOT equal to 5, so fall out and return to outer for 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 08 09 10

01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 09 10 i = 6 public class NestedFor // SIXTH TIME THROUGH THE LOOP { public static void main(String [] args) { for (int i = 1; i <= 7; i++) { System.out.println(); for (int j = 1; j <= i; j++) { System.out.print("*"); } } } } 6 i = 6 6 is less than or equal to 7 New line j = 1 1 is less than or equal to 6 Print * , increment, and loop 2 is less than or equal to 6 3 is less than or equal to 6 4 is less than or equal to 6 5 is less than or equal to 6 6 is less than or equal to 6 7 is NOT equal to 6, so fall out and return to outer for 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 09 10

01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 ******* 09 10 i = 7 public class NestedFor // SEVENTH TIME THROUGH LOOP { public static void main(String [] args) { for (int i = 1; i <= 7; i++) { System.out.println(); for (int j = 1; j <= i; j++) { System.out.print("*"); } } } } 7 i = 7 7 is less than or equal to 7 New line j = 1 1 is less than or equal to 7 Print * , increment, and loop 2 is less than or equal to 7 3 is less than or equal to 7 4 is less than or equal to 7 5 is less than or equal to 7 6 is less than or equal to 7 7 is less than or equal to 7 8 is NOT equal to 7, so fall out and return to outer for 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 ******* 09 10

DONE! See NestedFor.java 01 02 * 03 ** 04 *** 05 **** 06 ***** public class NestedFor // EIGHTH TIME THROUGH LOOP { public static void main(String [] args) { for (int i = 1; i <= 7; i++) { System.out.println(); for (int j = 1; j <= i; j++) { System.out.print("*"); } } } } 8 i = 8 8 is NOT less than 7 Break from for loop DONE! See NestedFor.java 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 *******

Nested Loops Demos NestWhileTest.java NestForWhileTest.java NestedFors.java INSTRUCTOR NOTE: Show demos

Introduction to Arrays What is an Array? So far, you have been working with variables that hold only one value. The integer variables you have set up have held only one number (and later in the quarter we will see how string variables will hold one long string of text). An array is a collection to hold more than one value at a time. It's like a list of items. Think of an array as like the columns in a spreadsheet. You can have a spreadsheet with only one column, or several columns. The data held in a single-list (one-dimensional) array might look like this: grades 100 1 89 2 96 3 4 98

Introduction to Arrays grades 100 1 89 2 96 3 4 98 Now, the way we might have declared data like this up until now is to do something along these lines: int value1 = 100; int value2 = 89; int value3 = 96; int value4 = 100; int value5 = 98; However, if we knew before hand that we were going to be declaring six int integers (or ten, or fifteen, etc), we could accomplish the same type of declaration by using an array. To set up an array of numbers like that in the table above, you have to tell Java what type of data is going into the array, then how many positions the array has. You’d set it up like this: int[ ] grades; NOTE: Arrays must be of the same data type, i.e., all integers (whole numbers) or all doubles (floating-point numbers) or all strings (text characters)—you cannot “mix-and-match” data types in an array.

Introduction to Arrays grades 100 1 89 2 96 3 4 98 int[ ] grades; The only difference between setting up a normal integer variable and an array is a pair of square brackets after the data type. The square brackets are enough to tell Java that you want to set up an array. The name of the array above is grades. Just like normal variables, you can call them almost anything you like (except Java defined keywords). The square brackets tells Java that you want to set up an integer array. It doesn't say how many positions the array should hold. To do that, you have to set up a new array object: grades = new int[5]; In between the square brackets you need the pre-defined size of the array. The size is how many positions the array should hold. If you prefer, you can put all that on one line: int[ ] grades = new int[5];

Introduction to Arrays grades 100 1 89 2 96 3 4 98 You can also declare the int separately and call it by its given name, like this: int summer2012 = 5; int[ ] grades = new int[summer2012]; (Whether you call the number inside the brackets or a named variable is up to your particular style of coding and preference.) So we are telling Java to set up an array with 5 positions in it. After this line is executed, Java will assign default values for the array. Because we've set up an integer array, the default values for all 5 positions will be zero ( 0 ). To assign values to the various positions in an array, you do it in the normal way: grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; If you know what values are going to be in the array, you can set them up like this instead: int[ ] grades = { 100, 89, 96, 100, 98 }; // Java treats as a // new instance { grades [0] = 100; grades [1] = 89; grades [2] = 96; grades [3] = 100; grades [4] = 98; Length of the array is equal to the number of slots declared This is called the index

Introduction to Arrays grades 100 1 89 2 96 3 4 98 When you declare an array with a given data type, name and number, like grades = new int[5]; You are reserving a collection space in memory by that name, sized according to data type, and the large enough to separately contain the data for the declared number. grades (a named reserved space set aside to hold exactly five 32-bit elements all initializing to zero) array element index  1 2 3 4 32-bits 32-bit space reserved for data  value stored in element  grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; 1 2 3 4 100 89 96 98

Introduction to Arrays: Example import java.util.*; public class Array_Demo extends Object { public static void main(String[] args) { // Setting up the integer 5-element array: int [] grades = new int[5]; grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; // Of course you could have done it this way: // int [] grades = {100, 89, 96, 100, 98}; int i; for(i = 0; i < grades.length; i++) { System.out.println("Grade " + (i + 1) + " is: " + grades[i]); } } }

ICE16 Don’t sit alone! Your professor will grade Part 1 in-class today. Submit Parts 2-5 to the Java Code Critic