Java programs are made out of classes. 

Inside classes you can put: 

  static and non-static members

  also constructors

A member is a procedure or a variable.

Static members belong to the class and should be referred to as such.

Examples: 

  Math.PI

  Math.pow(..., ...)

  Integer.parseInt(...)

Non-static (instance) members belong to objects of that class.

Please access instance members through the instance that holds them:

  BigDecimal a = new BigDecimal("2.3"); 

  System.out.println( a.add(...) ); 

  // 2nd example 

  Scanner s = new Scanner(System.in); 

  String line = s.nextLine(); 

Constructors are initialization procedures. 

There are four kinds of variables in Java.

Variables have types. 

In Java we have primitive types and non-primitive types. 

Primitive types are four (actually eight). 

Classes are user-defined (non-primitive) types. 

Arrays are non-primitive types.

A non-primitive type is also called reference type. 

An array is a collection of values of the same type. 

You acces the values through the name and an index. 

An ArrayList is a collection of values of the same type. 

You acces the values through the name and an index. 

Arrays and array lists could have more than one dimension. 

A matrix is a two dimensional array.

Recall that we defined Point, Line, Triangle, Circle last time. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int[] a
> int b[]
> int[] c[]
> int[][] d
> a = new int[3]
{ 0, 0, 0 }
> a
{ 0, 0, 0 }
> a[0] = 6
6
> a
{ 6, 0, 0 }
> b= new int[20]
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
> b
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
> b[b.length-1] = 14
14
> b
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14 }


R7.1 d 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int[] a = new int[10]
> a[0] = 17
17
> a[a.length - 1] = 29
29
> a
{ 17, 0, 0, 0, 0, 0, 0, 0, 0, 29 }
> for (int i = 0; i < a.length; i++) if (a[i] == 0) a[i] = -1; 
> a
{ 17, -1, -1, -1, -1, -1, -1, -1, -1, 29 }
>

Now you solve R7.5 (e) and (g)

import java.util.Arrays; 

public class Second {
  public static void main(String[] args) {
    int[] g = new int[10];
    System.out.println( Arrays.toString( g ) ); 
    for (int i = 0; i < g.length; i++)
      g[i] = i % 5; 
    System.out.println( Arrays.toString( g ) ); 
    int[] w = new int[] { 1, 4, 9, 16, 9, 7, 4, 9, 11 }; 
    System.out.println( Arrays.toString( w ) ); 
  }
}

Variation:

import java.util.Arrays; 

public class Second {
  public static void main(String[] args) {
    int[] g = new int[10];
    System.out.println( Arrays.toString( g ) ); 
    for (int i = 0; i < g.length; i++)
      g[i] = i % 5; 
    System.out.println( Arrays.toString( g ) ); 
    int[] w =  { 1, 4, 9, 16, 9, 7, 4, 9, 11 }; 
    System.out.println( Arrays.toString( w ) ); 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int[][] m
> m = new int[3][3]
{ { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }
> m[0][0] = 4
4
> m
{ { 4, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }
> m[1][2] = 7
7
> m
{ { 4, 0, 0 }, { 0, 0, 7 }, { 0, 0, 0 } }
> m[m.length-1][m[m.length-1].length-1] = 6
6
> m
{ { 4, 0, 0 }, { 0, 0, 7 }, { 0, 0, 6 } }


You cannot put elements of primitive type in an arraylist. 

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


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> ArrayList<Integer> row;
Static Error: Undefined class 'ArrayList'
> import java.util.ArrayList; // auto-import
ArrayList<Integer> row;
> ArrayList<ArrayList<Integer>> m
> m
null
> m = new ArrayList<ArrayList<Integer>>()
[]
> row = new ArrayList<Integer>()
[]
> row.add(4)
true
> row.add(9)
true
> row.add(2)
true
> row
[4, 9, 2]
> m
[]
> m.add(row)
true
> m
[[4, 9, 2]]
> row = new ArrayList<Integer>()
[]
> row.add(3)
true
> row.add(5)
true
> row.add(7)
true
> row
[3, 5, 7]
> m
[[4, 9, 2]]
> m.add(row)
true
> m
[[4, 9, 2], [3, 5, 7]]
> row = new ArrayList<Integer>()
[]
> row
[]
> m
[[4, 9, 2], [3, 5, 7]]
> row.add(8)
true
> row.add(1)
true
> row
[8, 1]
> m
[[4, 9, 2], [3, 5, 7]]
> row.add(6)
true
> row
[8, 1, 6]
> m
[[4, 9, 2], [3, 5, 7]]
> m.add(row)
true
> m
[[4, 9, 2], [3, 5, 7], [8, 1, 6]]


int[] a

Circle[] a

Triangle[] b

Can you create an array in which you can store both Circles and Triangles?

--

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> ArrayList<Integer> row;
Static Error: Undefined class 'ArrayList'
> import java.util.ArrayList; // auto-import
ArrayList<Integer> row;
> ArrayList<ArrayList<Integer>> m
> m
null
> m = new ArrayList<ArrayList<Integer>>()
[]
> row = new ArrayList<Integer>()
[]
> row.add(4)
true
> row.add(9)
true
> row.add(2)
true
> row
[4, 9, 2]
> m
[]
> m.add(row)
true
> m
[[4, 9, 2]]
> row = new ArrayList<Integer>()
[]
> row.add(3)
true
> row.add(5)
true
> row.add(7)
true
> row
[3, 5, 7]
> m
[[4, 9, 2]]
> m.add(row)
true
> m
[[4, 9, 2], [3, 5, 7]]
> row = new ArrayList<Integer>()
[]
> row
[]
> m
[[4, 9, 2], [3, 5, 7]]
> row.add(8)
true
> row.add(1)
true
> row
[8, 1]
> m
[[4, 9, 2], [3, 5, 7]]
> row.add(6)
true
> row
[8, 1, 6]
> m
[[4, 9, 2], [3, 5, 7]]
> m.add(row)
true
> m
[[4, 9, 2], [3, 5, 7], [8, 1, 6]]
> m.get(1)
[3, 5, 7]
> m.get(1).get(0)
3
> m.get(1).set(0, -1)
3
> m
[[4, 9, 2], [-1, 5, 7], [8, 1, 6]]
> m.get(1).set(0, 3)
-1
> m
[[4, 9, 2], [3, 5, 7], [8, 1, 6]]


Our attendance today: 

     ejcroke-- --------- aohslund- --------- mwoehr--- --------- jrqualls  --------- ninico--- malimova  eweating  
curtpark- clamcumm- --------- jlewisiv- --------- ebshield- --------- --------- --------- sk93----- --------- jaennis-- 
     ptngo---- --------- egarciar- --------- etiffany- bmarzec-- --------- rperigo-- --------- mashment- benlrich- 
lcompto-- --------- japelleg- cjkazmie- --------- ckosins-- --------- --------- calbscot- jj266---- jkvukas-- schmitmi- 
     mjbouvet- zhmercha- --------- kdrudge-- --------- amanpate- --------- --------- --------- lutcobb-- mcguirjj- 
nfrasco-- --------- ssoucie-- rictran-- --------- cjdummer- --------- --------- kyoshimo- bengraml-- stonebr-- kpendse-- 
     anjphill- sttwatso- --------- aem1----- haydwill- --------- bhake---- campbchm- yixyin--- edwarkel- linsean-- 
jk216---- olishin-- leehyem-- --------- jamdumas-- luklein-- --------- --------- mlraglin- jrjoliet- --------- fenglou-- 
     linyliu-- yj6------ jvnguyen- --------- matwyric- bmcshane- mobridge- --------- bsiefers- ctgarvin- wang657-- 
yiwezhan- wang671-- lf75----- --------- --------- jemisull- helkchen- yc15----- --------- kykinser- alivera-- cyanchen- 
     woboland  niirms--- tl46----- haozyuan- --------- --------- --------- evdelph-- joskrave- adsing--- zsalahud-- 
csimmer   wilflemi  hayelton  vlrios--- nonander- --------- --------- phharper- maxdoeri- zcmonroe- mmeeker-- gjlahman- 

                                        bpennant  hahmann   keanmoor  shjbyers 
                                        rdnieves  racornel  mjawad    ahshahz