Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIT 115: Introduction To Programming

Similar presentations


Presentation on theme: "BIT 115: Introduction To Programming"— Presentation transcript:

1 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”

2 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

3 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

4 Quiz16 Access Code: happy returns

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

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

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

8 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 ); } }

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

10 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

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

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

13 Nested Loops

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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 *******

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

24 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

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

26 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];

27 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

28 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

29 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]); } } }

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


Download ppt "BIT 115: Introduction To Programming"

Similar presentations


Ads by Google