* mahazuga: Mary Ann
* serepate: Serena
* hgarciah: Henri
* balbakr: Bakr
* ruifan: Rui
* mzelenin: Matthew
* jnp2: Jay
* zeyang: Zejun
  zhang486: Jingyun
* yiwecao: Yiwei
  rthammon: Ryan
* wang686: Jiaxing
* dweissma: Andrew
* kevcao: Kevin
* ssalmero: Salmeron, Santiago (TA)
* luo23: Yawen
* runxzhao: Runxia
* dgerman: German, Dan-Adrian (Primary Instructor)
  creba: Chris

Java programs are made of classes. 

Classes have members: methods and variables. 

Members can be: static and non-static. 

public class One {
  public static void main(String[] args) { // chapter 7 explains String[] 
    System.out.println("Howdy."); // chapter 3, 2 explains this 
  } 
}

Constructors initialize the instance variables (state). 

We worked with objects in Chapter 2, designed classes in Chapter 3. 

Chapter 8, 9, 10 will discuss additional concepts related to modeling. 

BigDecimal, Scanner, String, JFrame, JComponent, Graphics, Circle, Point. 

Types (primitive/reference) expressions, operators. 

Primitive types: boolean, char, int (long, short, byte), double (float). 

Assignment statements, if statements, loops (while, for, do-while). 

6W2 Summer 2018

Homework 4: Programming with Loops

Solve the following problems from your text:

Science P6.15 on page 301
P6.8, P6.9, P6.10 on page 300
P6.4, P6.5, P6.6, P6.7 on page 299
P6.2 on page 298
E6.22 on page 297
Upload/push a folder of folders to your repo with your answers.

Please let us know if you have any questions or need any help.

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int[] a 
> a
null
> a = new int[10]
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
> a[3] = 7
7
> a
{ 0, 0, 0, 7, 0, 0, 0, 0, 0, 0 }


Assume the customer wants 11 numbers now:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int[] a 
> a
null
> a = new int[10]
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
> a[3] = 7
7
> a
{ 0, 0, 0, 7, 0, 0, 0, 0, 0, 0 }
> a[1] = 3
3
> a
{ 0, 3, 0, 7, 0, 0, 0, 0, 0, 0 }
> for (int i = 0; i < a.length; i++) a[i] = 10 - i; 
> a
{ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }
> a[11] = 0
java.lang.ArrayIndexOutOfBoundsException
> a
{ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }
> for (int i = 0; i < a.length; i++) a[i] = i * i;
> a
{ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }


One option:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int[] a 
> a
null
> a = new int[10]
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
> a[3] = 7
7
> a
{ 0, 0, 0, 7, 0, 0, 0, 0, 0, 0 }
> a[1] = 3
3
> a
{ 0, 3, 0, 7, 0, 0, 0, 0, 0, 0 }
> for (int i = 0; i < a.length; i++) a[i] = 10 - i; 
> a
{ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }
> a[11] = 0
java.lang.ArrayIndexOutOfBoundsException
> a
{ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }
> for (int i = 0; i < a.length; i++) a[i] = i * i;
> a
{ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }
> import java.util.Arrays
> a
{ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }
> a = Arrays.copyOf(a, 11)
{ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 0 }
> Arrays.copyOf(a, 13)
{ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 0, 0 }
> a
{ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 0 }


Two more methods from the Arrays class:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int[] a = {4, 2, 3, 5, 1, 6, 7, 2, 1, 4}
> a
{ 4, 2, 3, 5, 1, 6, 7, 2, 1, 4 }
> import java.util.Arrays
> a.length
10
> Arrays.copyOf(a, 11)
{ 4, 2, 3, 5, 1, 6, 7, 2, 1, 4, 0 }
> a
{ 4, 2, 3, 5, 1, 6, 7, 2, 1, 4 }
> a = Arrays.copyOf(a, 11)
{ 4, 2, 3, 5, 1, 6, 7, 2, 1, 4, 0 }
> a
{ 4, 2, 3, 5, 1, 6, 7, 2, 1, 4, 0 }
> a[a.length-1] = 1
1
> a
{ 4, 2, 3, 5, 1, 6, 7, 2, 1, 4, 1 }
> Arrays.sort( a ) 
> a
{ 1, 1, 1, 2, 2, 3, 4, 4, 5, 6, 7 }
> Arrays.toString( a )
"[1, 1, 1, 2, 2, 3, 4, 4, 5, 6, 7]"


Now let's discuss ArrayLists:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int[] a = new int[3];
> a
{ 0, 0, 0 }
> ArrayList a
Static Error: Undefined class 'ArrayList'
> import java.util.ArrayList; // auto-import
ArrayList a
> a
null
> String b
> b
null
>

null is the absence of a pointer value to an object (instance of reference type). 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> String[] a = new String[5]
> a
{ null, null, null, null, null }
> a[0] = "Mary"
"Mary"
> a[1] = "had"
"had"
> a[2] = "a"
"a"
> a[3] = "little"
"little"
> a
{ Mary, had, a, little, null }
> a[4] = "lamb."
"lamb."
> a
{ Mary, had, a, little, lamb. }


Let's do the same thing with ArrayLists:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> import java.util.ArrayList;
> ArrayList<String> a = new ArrayList<String>()
> a
[]
> a.add( "Mary" ); 
> a
[Mary]
> a.add( "had" );
> a.add( "a" );
> a.add( "little" );
> a
[Mary, had, a, little]
> a.size()
4
> a.add( "lamb." );
> a.size()
5
> a
[Mary, had, a, little, lamb.]


Here's how you access a specific element:

> a
[Mary, had, a, little, lamb.]
> a.get(3)
"little"


ArrayLists do not accept values of primitive type:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> import java.util.ArrayList;
> ArrayList<int> a = new ArrayList<int>()
Static Error: Type has incompatible bounds
> ArrayList<double> a = new ArrayList<double>()
Static Error: Type has incompatible bounds
> ArrayList<Integer> a = new ArrayList<Integer>()
> a
[]
> a.add(3)
true
> a
[3]
> a.add(1)
true
> a
[3, 1]
> a.add(5)
true
> a
[3, 1, 5]
> a.size()
3


Introducing wrapper classes. 

What is the difference between an int and an Integer?

Related question: predict the outcome of this expression

  Welcome to DrJava.  Working directory is C:\Users\dgerman
  > "what" + "ever" == "whatever"
  false
  > 

The second experiment: 

  public class Whatever {
    public static void main(String[] args) {
      System.out.println("what" + "ever" == "whatever"); // expected: true 
    }
  }

Here's the proof:

  Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
  > run Whatever
  true
  > 

I rest my case. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> String[][] sb221 = { { null, null, "Yawen", "Henri", "Zejun", "Yiwei", "Jiaxing" }, 
                       { null, null, "Runxia", null, "Mary Ann", "Ryan", "Bakr", "Santiago" }, 
                       { null, "Rui", null, "Matthew", null, "Jay", null, null}, 
                       { null, "Kevin", "Andrew", "Serena", null, null, null, null, null}
                     };
> sb221
{ { null, null, Yawen, Henri, Zejun, Yiwei, Jiaxing }, { null, null, Runxia, null, Mary Ann, Ryan, Bakr, Santiago }, { null, Rui, null, Matthew, null, Jay, null, null }, { null, Kevin, Andrew, Serena, null, null, null, null, null } }
> sb221[1]
{ null, null, Runxia, null, Mary Ann, Ryan, Bakr, Santiago }
> sb221[1][2]
"Runxia"
> sb221[3][3]
"Serena"
> sb221[3][2]
"Andrew"


This is a two-dimensional structure. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int[][] m = new int[3][3]
> m
{ { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }
> m[2][1] = 1
1
> m
{ { 0, 0, 0 }, { 0, 0, 0 }, { 0, 1, 0 } }
> m[0][2] = 2
2
> m[1][0] = 3
3
> m[0][0] = 4
4
> m
{ { 4, 0, 2 }, { 3, 0, 0 }, { 0, 1, 0 } }
> if (m[1][1] == 0) m[1][1] = 5; else m[0][1] = 5; // the else is inaccurate here
> m
{ { 4, 0, 2 }, { 3, 5, 0 }, { 0, 1, 0 } }
> if (m[2][2] == 0) m[2][2] = 6; else m[0][1] = 6;
> m
{ { 4, 0, 2 }, { 3, 5, 0 }, { 0, 1, 6 } }
> if (m[0][0] == 0) m[0][0] = 7; else m[1][2] = 7;
> m
{ { 4, 0, 2 }, { 3, 5, 7 }, { 0, 1, 6 } }
> if (m[2][0] == 0) m[2][0] = 8; else m[0][2] = 8;
> m
{ { 4, 0, 2 }, { 3, 5, 7 }, { 8, 1, 6 } }
> if (m[0][1] == 0) m[0][1] = 9; else m[1][0] = 9;
> m
{ { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 } }


So that's how you create a magic square of size 3.

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> import java.util.ArrayList
> ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
> a
[]
> a.add(new ArrayList<Integer>())
true
> a
[[]]
> a.add(new ArrayList<Integer>())
true
> a
[[], []]
> a.add(new ArrayList<Integer>())
true
> a
[[], [], []]
> a.get(0).add(4)
true
> a
[[4], [], []]
> a.get(0).add(9)
true
> a.get(0).add(2)
true
> a
[[4, 9, 2], [], []]
> a.get(1).add(3)
true
> a
[[4, 9, 2], [3], []]
> a.get(2)
[]
> a.get(2).add(8)
true
> a.get(2).add(1)
true
> a
[[4, 9, 2], [3], [8, 1]]


This is how you create a two-dimensional array list of integers. 

--