grades - Canvas
Download
Report
Transcript grades - Canvas
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
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?
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).
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.
public class FileName3 extends Object
{
public static int printNum()
{
System.out.println("Going to print, some number of times!");
Class
Static Method
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!");
}
}
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 an
Object!
I’m a
Method!
Glad you
Called!
Nested Loops
FOR LOOP WALK THROUGH
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
public class NestedFor // FIRST TIME THROUGH THE LOOP
{
public static void main(String [] args)
{
i = 1
for (int i = 1; i <= 7; i++)
1 is less than or equal to 7
{
System.out.println();
New line
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
}
}
}
01
02 *
03
04
05
06
07
08
09
10
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
public class NestedFor // SECOND TIME THROUGH THE LOOP
{
public static void main(String [] args)
2
{
i = 2
for (int i = 1; i <= 7; i++)
2 is less than or equal to 7
{
System.out.println();
New line
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
}
}
}
01
02 *
03 **
04
05
06
07
08
09
10
j = 1
1 is less than or equal to 2
Print * , increment, and loop
2 is less than or equal to 2
Print * , increment, and loop
3 is NOT equal to 2, so fall
out and return to outer for
public class NestedFor // THIRD TIME THROUGH THE LOOP
{
public static void main(String [] args)
3
{
i = 3
for (int i = 1; i <= 7; i++)
3 is less than or equal to 7
{
System.out.println();
New line
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
}
}
}
01
02 *
03 **
04 ***
05
06
07
08
09
10
j = 1
1 is less than or equal to 3
Print * , increment, and loop
2 is less than or equal to 3
Print * , increment, and loop
3 is less than or equal to 3
Print * , increment, and loop
4 is NOT equal to 3, so fall
out and return to outer for
public class NestedFor // FOURTH TIME THROUGH THE LOOP
{
public static void main(String [] args)
4
{
i = 4
for (int i = 1; i <= 7; i++)
4 is less than or equal to 7
{
System.out.println();
New line
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
}
}
}
01
02 *
03 **
04 ***
05 ****
06
07
08
09
10
j = 1
1 is less than or equal to 4
Print * , increment, and loop
2 is less than or equal to 4
Print * , increment, and loop
3 is less than or equal to 4
Print * , increment, and loop
4 is less than or equal to 4
Print * , increment, and loop
5 is NOT equal to 4, so fall
out and return to outer for
public class NestedFor // FIFTH TIME THROUGH THE LOOP
{
public static void main(String [] args)
5
{
i = 5
for (int i = 1; i <= 7; i++)
5 is less than or equal to 7
{
System.out.println();
New line
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
}
}
}
01
02 *
03 **
04 ***
05 ****
06 *****
07
08
09
10
j = 1
1 is less than or equal to 5
Print * , increment, and loop
2 is less than or equal to 5
Print * , increment, and loop
3 is less than or equal to 5
Print * , increment, and loop
4 is less than or equal to 5
Print * , increment, and loop
5 is less than or equal to 5
Print * , increment, and loop
6 is NOT equal to 5, so fall
out and return to outer for
public class NestedFor // SIXTH TIME THROUGH THE LOOP
{
public static void main(String [] args)
6
{
i = 6
for (int i = 1; i <= 7; i++)
6 is less than or equal to 7
{
System.out.println();
New line
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
}
}
}
01
02 *
03 **
04 ***
05 ****
06 *****
07 ******
08
09
10
j = 1
1 is less than or equal to 6
Print * , increment, and loop
2 is less than or equal to 6
Print * , increment, and loop
3 is less than or equal to 6
Print * , increment, and loop
4 is less than or equal to 6
Print * , increment, and loop
5 is less than or equal to 6
Print * , increment, and loop
6 is less than or equal to 6
Print * , increment, and loop
7 is NOT equal to 6, so fall
out and return to outer for
public class NestedFor // SEVENTH TIME THROUGH LOOP
{
public static void main(String [] args)
7
{
i = 7
for (int i = 1; i <= 7; i++)
7 is less than or equal to 7
{
System.out.println();
New line
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
}
}
}
01
02 *
03 **
04 ***
05 ****
06 *****
07 ******
08 *******
09
10
j = 1
1 is less than or equal to 7
Print * , increment, and loop
2 is less than or equal to 7
Print * , increment, and loop
3 is less than or equal to 7
Print * , increment, and loop
4 is less than or equal to 7
Print * , increment, and loop
5 is less than or equal to 7
Print * , increment, and loop
6 is less than or equal to 7
Print * , increment, and loop
7 is less than or equal to 7
Print * , increment, and loop
8 is NOT equal to 7, so fall
out and return to outer for
public class NestedFor // EIGHTH TIME THROUGH LOOP
{
public static void main(String [] args)
8
{
i = 8
for (int i = 1; i <= 7; i++)
8 is NOT less than 7
{
System.out.println();
Break from for loop
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
}
}
}
01
02 *
03 **
04 ***
05 ****
06 *****
07 ******
08 *******
DONE!
See NestedFor.java
Nested Loops Demos
• NestWhileTest.java
• NestForWhileTest.java
• NestedFors.java
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
0
100
1
89
2
96
3
100
4
98
Introduction to Arrays
grades
0
100
1
89
2
96
3
100
4
98
Now, the way we might have declared data like this
up until now is to do something along these lines:
int
int
int
int
int
value1
value2
value3
value4
value5
=
=
=
=
=
100;
89;
96;
100;
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
0
100
1
89
2
96
3
100
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
0
100
1
89
2
96
3
100
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]
grades[1]
grades[2]
grades[3]
grades[4]
=
=
=
=
=
100;
89;
96;
100;
98;
grades
grades
grades
grades
grades
[0]
[1]
[2]
[3]
[4]
=
=
=
=
=
{
100;
89;
96;
100;
98;
Length of the
array is equal to
the number of
slots declared
This is called the index
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
Introduction to Arrays
grades
0
100
When you declare an array with a given data type, name
and number, like
1
89
grades
2
96
3
100
4
98
= 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)
0
1
2
3
4
space reserved for data
32-bits
32-bits
32-bit
32-bits
32-bits
value stored in element
0
0
0
0
0
0
1
2
3
4
100
89
96
100
98
array element index
grades[0]
grades[1]
grades[2]
grades[3]
grades[4]
=
=
=
=
=
100;
89;
96;
100;
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]
grades[1]
grades[2]
grades[3]
grades[4]
=
=
=
=
=
100;
89;
96;
100;
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