RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

constructors
the truth!

Review terms

Local variable
can be used/modified only inside the method it's declared in
example: s1, s2, s2a are local vars in testAll (lines 48–51)); t, a, etc. are params for new Song2 (line 9)).
parameter
A type of local-variable
It gets initialized when the function is called (using the value passed to the function)
So don't you initialize it yourself! (Also a best-practice: don't re-assign to a parameter1, even though you can.)
example: s, space are params for willFit (line 23)); t, a, etc. are params for new Song2 (line 9)).
argument
The value passed to a function
example: 2, 20 are arguments to Math.pow (line 24)); "Abba" is one of the arguments to new Song2 (line 50); 150 is one of the arguments to new Song2 (line 56) and in turn the song that new Song2 returns is one of the arguments to assertEquals (line 54)
field
Declared inside class Foo { }, but not inside any of its methods/functions.
example: artist, title, isCopyRighted inside class Song2 (lines 2–5 below)
While it looks like a local-variable declaration in java, it's very very different! If you make 20 Song2s, there are 20 different titles running around, even when you're not running any of the functions/methods inside Song2.

Constructors

They're just regular functions, but Java sure tries to obscure that with special rules:

A very typical constructor is very boring, doing nothing more than assigning to each field:
    Song2( String t, String a, double l, boolean isC ) {
        // Song2 this = /* a Song2 object w/ all fields "zero-ish" */;  // implicit -- don't write it!
        this.title = t;
        this.artist = a;
        this.length = l;
        this.isCopyrighted = isC;
        // return this;    // implicit -- don't write it!
     }
  


Functions returning Objects

We will copy/rename class Song into class Song2 (before, after). and then add a method “remix” (starting with test-cases, to figure out exactly what parameters that function needs, and what it should do in corner cases).

 1  class Song2 extends Object120 {
 2      String title;
 3      String artist;
 4      double length;  // running time, in s.
 5      boolean isCopyrighted;
 6      
 7      
 8      // the "constructor":
 9      Song2( String t, String a, double l, boolean isC ) {
10          // Song2 this = /* a Song2 object w/ all fields "zero-ish" */;  // implicit -- don't write it!
11          this.title = t;
12          this.artist = a;
13          this.length = l;
14          this.isCopyrighted = isC;
15          // return this;    // implicit -- don't write it!
16      }
17      
18       
19      /** return whether `s` can be stored using `space` disk-space.
20       * @param s the song to consider
21       * @param space the amount of available space it has to fit into, in B.
22       */
23      static boolean willFit( Song2 s, int space ) {
24          return (s.length/60)*3.0*Math.pow(2,20) <= space;
25      }
26      
27      /** return whether one song is shorter than another.
28       * @return whether s1 is shorter than s2.
29       */
30      static boolean isShorter( Song2 s1, Song2 s2 ) {
31          return (s1.length < s2.length);
32      }
33      
34      /** return a remix of a song.
35       * @return `orig` remixed by D.J. `dj`, who added `newMaterialLength` seconds of ill beatz.
36       */
37      static Song2 remix( Song2 orig, String dj, double newMaterialLength ) {
38          return new Song2( orig.title + " (" + dj + " remix)",
39                            orig.artist + " vs " + dj,
40                            orig.length + newMaterialLength,
41                            false );
42      }
43      
44      
45      
46      static void testAll() {
47          // some examples of Song2 objects:
48          Song2 s1 = new Song2("Old Town Road", "Lil Nas X", 120, true);
49          Song2 s2 = new Song2("Ring Ring", "Abba", 180, true);
50          Song2 s2a = new Song2("Ring Ring", "Abba", 180, true);
51          
52          
53          printTestMsg("remix");
54          assertEquals( new Song2( "Old Town Road (ibarland remix)",
55                                   "Lil Nas X vs ibarland",
56                                   150,
57                                   false ),
58                        remix(s1, "ibarland", 30 ) );
59          assertEquals( new Song2("Ring Ring (saltman remix)", "Abba vs saltman", 180, false),
60                        remix(s2, "saltman", 0 ) );
61          printTestSummary();
62      }
63      
64      public static void main( String[] args ) { testAll(); }
65      
66  }

1 Pro tip: Java lets you declare your parameters final! E.g. int someFunc( final int n, final String s ) { }      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.