a4_v2.html.zip

Content/activities/activity4/a4_v2.html

COMP 1020

 

Version 2: A list of properties

A speculator's list of properties is changing constantly as land is bought and sold. To make it possible to record and easily change this list, an ArrayList will be used to store the properties. Properties can be added to and removed from the list using ArrayList methods, but more complex operations like transferring (buying and selling), splitting, and printing the list will be handled by static methods.

As an alternative to our usual programming strategy, we can write the main program before we write the static methods it depends upon. This will give us an idea of what those methods need to do before we set down to writing them. To allow us to compile and run the program, calls to the non-existent methods will be commented out, and will be uncommented as we write them.

public static void main(String[] args) {   // Lists of properties belonging to two speculators   ArrayList<Property> jim, helen;   Property property;      jim = new ArrayList<Property>();   jim.add(new Property(16, 8, 160));   jim.add(new Property(24, 17, 130));   jim.add(new Property(129, 180, 35));   helen = new ArrayList<Property>();   helen.add(new Property(9, 13, 120));   helen.add(new Property(15, 15, 210));   helen.add(new Property(9, 13, 120));      // printProperties("Jim", jim);   // printProperties("Helen", helen);      // subdivide(jim, 0, true);   // subdivide(jim, 2, false);   // subdivide(helen, 0, false);   // property = transfer(jim, 1, helen);   // System.out.println(property + " transferred from Jim to Helen for $" + property.value());   // property = transfer(helen, 3, jim);   // System.out.println(property + " transferred from Helen to Jim for $" + property.value());   // printProperties("Jim", jim);   // printProperties("Helen", helen); } Don't forget that the program also needs to import java.util.ArrayList; at the top.

First, to get some meaningful output, we will write the printProperties method. It could use the ArrayList built-in toString() method to print its results, but the output isn't very neat. Since we will also calculate and print the total value of all the properties in the list, we will use a loop for both printing and summing.

public static void printProperties(String speculator, ArrayList<Property> list) {   int totalValue;      System.out.println("Properties belonging to " + speculator + ":");   totalValue = 0;   for (int i = 0; i < list.size(); i++) {     System.out.println(" " + list.get(i));     totalValue += list.get(i).value();   }      System.out.println("Total value: $" + totalValue); }

Uncommenting the calls to printProperties in the main program and running it will produce some output. Next is the subdivide method. It will be given the list of properties, the position in the list of the property to subdivide, and the parameter for the subdivide instance method. Since the subdivided property still belongs to the speculator, it will be added back to the list.

public static void subdivide(ArrayList<Property> list, int position, boolean lengthwise) {   Property toSubdivide;   Property subdivision;      toSubdivide = list.get(position);   subdivision = toSubdivide.subdivide(lengthwise);      list.add(subdivision); }

With an ArrayList, changing the contents of the list is easy; there's no need to return any values. Uncommenting the calls to subdivide in the main program produces some more interesting output.

One thing that is missing is error checking. If the position is invalid, the program will crash.

The final method is transfer. This method is given a list and the position of the property to transfer (from the seller), as before. The transferred property is removed from the seller's list and added to the buyer's list. For convenience, a reference to the transferred property is returned.

public static Property transfer(ArrayList<Property> seller, int position,                                 ArrayList<Property> buyer) {   Property toTransfer;      toTransfer = seller.get(position);   seller.remove(position);   buyer.add(toTransfer);      return toTransfer; }

Remember, the property returned from this method is a reference to the same property in the buyer's list, and not a copy. If you were to change the property after transferring it (like property = transfer(jim, 1, helen); property.subdivide(true);) the property in the buyer's list would be subdivided (and the other half of the subdivision would be lost).

Java file Version 2 link. Click on the link and save the Zip file to your computer.

Content/css/comp_1020_new.css

@charset "utf-8"; /* CSS Document */ body {height: auto;margin-top: 10px; margin-left: 25px; margin-bottom: 10px;} #container { width: 780px; height:468px; padding-left: 47px; padding-top: 50px; padding-right: 35px; margin: auto; background-image: url(unit1.jpg); left: 38px; } #containersmall { width: 780px; height:468px; padding-left: 47px; padding-top: 0px; padding-right: 35px; margin: auto; background-image: url(unit1.jpg); left: 38px; } #banner { width:800px; height:100px; } .h1 { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; color:#7eae37; margin-bottom:-10pt; } .h1b { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#000000; } .h1w { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#006699; } .h2 { font-family:Arial, Helvetica, sans-serif; font-size:13pt; font-weight:bold; color:#244062; font-style:normal; position: relative; background-color: #FFFFFF; } .h2b { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-weight:bold; font-variant:small-caps; color:#999966; font-style:normal; } .h3 { font-family:Arial, Helvetica, sans-serif; font-size:11.5pt; font-style:normal; font-weight:bold; color:#244062; margin-bottom:-7pt; } .h3b { font-family:Arial, Helvetica, sans-serif; font-size:16pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336699 } #page { width:780px; height:994px; left: 7px; top: 166px; padding-left: 10px; padding-right: 10px; } .body { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:normal; color:#000000; line-height:14pt; } .body_box { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:normal; color:#000000; line-height:14pt; } .body_bold { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:bold; color:#006699 } .body_bold_box { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:bold; color:##006699 } .body_dr_green { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#336600 } .definition { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:italic; font-weight:normal; border-width:.25pt; border-style:solid; border-color: #CCCCCC; border-collapse:separate; empty-cells:hide; } .toc { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#669933 } a:link {color:#0000FF; text-decoration:underline; font-family:Arial, Helvetica, sans-serif;} a:visited {color:#0000FF; text-decoration:underline;font-family:Arial, Helvetica, sans-serif;} .top { font-family:Arial, Helvetica, sans-serif; font-size:8pt; font-style:normal; font-weight:normal; } p.ind { text-indent:-20px; margin-left:20px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .quote { margin-left:40px; margin-right:40px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .blackBox { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color:#FFFFFF; border: 1.5px solid #000; padding-top: 10px; padding-left: 15px; padding-right: 5px; padding-bottom: 10px; margin-right: 200px; margin-left: 3px; } .blueBox { background-color: #daeef4; border: 1.5px solid #FFF; padding-top: 10px; padding-left: 15px; padding-right: 5px; padding-bottom: 10px; margin-left: -10px; margin-right: -15px; } .yellowBox { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color: #FFFF99; border: 1.5px solid #FFF; padding-top: 10px; padding-left: 15px; padding-right: 5px; padding-bottom: 10px; margin-left: -10px; margin-right: -15px; } .blackBorder { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color: #FFFFFF; border: 1.5px solid #00000; padding-top: 10px; padding-right: -17px; padding-bottom: 14px; margin-right: 200px; margin-left: 3px; } .blackBorderwy { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color: #FFFFCC; border: 1.5px solid #00000; padding-top: 10px; padding-right: -17px; padding-bottom: 14px; margin-right: 200px; margin-left: 3px; } .blueBorder { background-color: #FFFFFF; border: 1.5px solid #669966; padding-top: 10px; padding-right: -17px; padding-bottom: 14px; margin-right: 200px; margin-left: 3px; } .biol_head { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-style:normal; font-weight:bold; color:#FFFFFF; } .body_white { font-family:Arial, Helvetica, sans-serif; font-size:9pt; font-style:normal; font-weight:normal; color:#FFFFFF } .body_white_bold { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:bold; color:#FFFFFF }

Content/css/COMP1020.css

@charset "utf-8"; /* CSS Document */ body {margin-top: 10px; margin-left: 25px; margin-bottom: 10px;} #container { width: 780px; height:591px; padding-left: 47px; padding-top: 50px; padding-right: 35px; margin: auto; } #banner { left:25px; top: 10px; width:800px; height:100px; padding-bottom: 30px; } .heading_banner { margin: 0px 0px 0px 0px; padding: 20px 0px 0px 30px; font-size: 34px; font-weight: 400; font-family: Arial, Helvetica, sans-serif; color: #FFF; height: 100px; width: 90%; line-height: 1.1em; opacity: 0.75; letter-spacing: 0.05em; position:absolute; } .h1 { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336600; margin-bottom:-9pt; } .h1b { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#000000; } .h1w { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#FFFFFF; } .h2 { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-weight:bold; font-variant:small-caps; color:#336600; font-style:normal; position: relative; margin-bottom:3pt; } .h2b { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-weight:bold; font-variant:small-caps; color:#336699; font-style:normal; } .h3 { font-family:Arial, Helvetica, sans-serif; font-size:16pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336600; margin-bottom:-7pt; } .h3b { font-family:Arial, Helvetica, sans-serif; font-size:16pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336699 } .body { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000; } .body_bold { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:bold; color:#000000 } .definition { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:italic; font-weight:normal; border-width:.25pt; border-style:solid; border-color: #CCCCCC; border-collapse:separate; empty-cells:hide; } a:link {color:#336699; text-decoration:underline; font-family:Arial, Helvetica, sans-serif;} a:visited {color:#669933; text-decoration:underline;font-family:Arial, Helvetica, sans-serif;} .top { font-family:Arial, Helvetica, sans-serif; font-size:8pt; font-style:normal; font-weight:normal; } p.ind { text-indent:-20px; margin-left:20px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .quote { margin-left:40px; margin-right:40px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .blueBox { background-color: #336699; border: 1px solid #336699; padding-top: 10px; padding-right: 5px; padding-bottom: 10px; padding-left: 10px; } .blueBorder { background-color: #FFFFFF; border: 1px solid #336699; padding-top: 10px; padding-right: 5px; padding-bottom: 10px; padding-left: 10px; } .biol_head { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-style:normal; font-weight:bold; color:#FFFFFF; } .body_white { font-family:Arial, Helvetica, sans-serif; font-size:9pt; font-style:normal; font-weight:normal; color:#FFFFFF } .body_white_bold { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:bold; color:#FFFFFF } .Highlight { background-color: #FFFF00; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-top: .5em; padding-bottom: .5em; padding-left: 1em; padding-right: 1em; } .Aside { background-color: lightGrey; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-top: .5em; padding-bottom: .5em; padding-left: 1em; padding-right: 1em; } .Action { background-color: #FFFFC0; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-top: .5em; padding-bottom: .5em; padding-left: 1em; padding-right: 1em; } .key { background-color: #a9a9a9; color: white; border: solid thin black; font-style: italic; } .menu { background-color: #a9a9a9; color: white; border: solid thin black; } .version { color: olive; font-style: italic; } code { font-family: "Lucida Sans Typewriter", Courier, monospace; color: #222200; background-color: #FFFFE0; } .code { font-family: "Lucida Sans Typewriter", Courier, monospace; background-color: #FFFFB0; border-top: dotted thin black; border-bottom: dotted thin black; padding-top: 1em; padding-bottom: 1em; padding-left: .5em; padding-right: .5em; margin-right: 20px; margin-left: 20px; } .Aside .code { margin-right: 10px; margin-left: 10px; } pre.code { white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */ white-space: -pre-wrap; /* Opera 4 - 6 */ white-space: -o-pre-wrap; /* Opera 7 */ white-space: pre-wrap; /* CSS3 - Text module (Candidate Recommendation) http://www.w3.org/TR/css3-text/#white-space */ word-wrap: break-word; /* IE 5.5+ */ } dfn { font-weight: bold; color: brown; } .Example { background-color: aqua; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-left: .5em; padding-right: .5em; } .ref { color: red; font-style: italic; } .filename { font-family: Arial, Helvetica, sans-serif; color: green; text-decoration: underline; } .output { font-family: "Lucida Sans Typewriter", Courier, monospace; color: purple; font-style: italic; } table.bordered { border-width: thin; border-style: solid; border-spacing: 0; } table.bordered td { border-width: thin; border-style: solid; border-spacing: 0; padding-left: 4px; padding-right: 4px; } table.bordered th { border-width: thin; border-style: solid; border-spacing: 0; padding-left: 8px; padding-right: 8px; } th.side { font-weight: bold; color: black; background-color: #ffd0d0; } th.right { font-weight: bold; color: black; background-color: #ffffd0; } th.top { font-weight: bold; color: black; background-color: #ffd0d0; text-alignment: center; } th.side code { background-color: inherit; } th.top code { background-color: inherit; } .InlineTitle { font-weight: bold; } .view { background-color: black; color: white; border: solid thin black; padding: 0.25em 1em; text-decoration: none; } a.view { background-color: black; color: white; border: solid thin black; } a.view:hover { background-color: blue; color: yellow; text-decoration: none; } /*added for IE as above (hover) does not work in IE*/ a.viewOn { background-color: blue; color: yellow; border: solid thin black; padding: 0.25em 1em; text-decoration: none; } ol li { margin-top: 0.3em; margin-bottom: 0.3em; } #footer {margin: 0px 0px 0px 0px; border: thin solid red;}

Content/images/comp1020_banner.jpg

Content/activities/java/Activity4B.zip

Activity4B.java

Activity4B.java

import  java . util . ArrayList ;

public   class   Activity4B   {
     public   static   void  main ( String []  args )   {
         // Lists of properties belonging to two speculators
         ArrayList < Property >  jim ,  helen ;
         Property  property ;

        jim  =   new   ArrayList < Property > ();
        jim . add ( new   Property ( 16 ,   8 ,   160 ));
        jim . add ( new   Property ( 24 ,   17 ,   130 ));
        jim . add ( new   Property ( 129 ,   180 ,   35 ));

        helen  =   new   ArrayList < Property > ();
        helen . add ( new   Property ( 9 ,   13 ,   120 ));
        helen . add ( new   Property ( 15 ,   15 ,   210 ));
        helen . add ( new   Property ( 9 ,   13 ,   120 ));

        printProperties ( "Jim" ,  jim );
        printProperties ( "Helen" ,  helen );

        subdivide ( jim ,   0 ,   true );
        subdivide ( jim ,   2 ,   false );
        subdivide ( helen ,   0 ,   false );

        property  =  transfer ( jim ,   1 ,  helen );
         System . out . println ( property  +   " transferred from Jim to Helen for $"   +  property . value ());
        property  =  transfer ( helen ,   3 ,  jim );
         System . out . println ( property  +   " transferred from Helen to Jim for $"   +  property . value ());

        printProperties ( "Jim" ,  jim );
        printProperties ( "Helen" ,  helen );

         System . out . println ( "\nEnd of processing." );
     }
    
     public   static   void  printProperties ( String  speculator ,   ArrayList < Property >  list )   {
         int  totalValue ;

         System . out . println ( "Properties belonging to "   +  speculator  +   ":" );

        totalValue  =   0 ;
         for   ( int  i  =   0 ;  i  <  list . size ();  i ++ )   {
             System . out . println ( " "   +  list . get ( i ));
            totalValue  +=  list . get ( i ). value ();
         }

         System . out . println ( "Total value: $"   +  totalValue );
     }
    
     public   static   void  subdivide ( ArrayList < Property >  list ,   int  position ,   boolean  lengthwise )   {
         Property  toSubdivide ;
         Property  subdivision ;

        toSubdivide  =  list . get ( position );
        subdivision  =  toSubdivide . subdivide ( lengthwise );

        list . add ( subdivision );
     }
    
     public   static   Property  transfer ( ArrayList < Property >  seller ,   int  position ,
                                     ArrayList < Property >  buyer )   {

         Property  toTransfer ;

        toTransfer  =  seller . get ( position );
        seller . remove ( position );
        buyer . add ( toTransfer );

         return  toTransfer ;
     }
}

class   Property   {
     private   int  length ,  width ;   // both are in metres
     private   int  valuePerSqM ;     // value in $ per square metre
    
     public   Property ( int  length ,   int  width ,   int  valuePerSqM )   {
         this . length  =  length ;
         this . width  =  width ;
         this . valuePerSqM  =  valuePerSqM ;
     }
    
     public   int  value ()   {
         return  length  *  width  *  valuePerSqM ;
     }
    
     public   Property  subdivide ( boolean  lengthwise )   {
         Property  subdivision ;
        
         if   ( lengthwise )   {
            subdivision  =   new   Property ( length  /   2 ,  width ,  valuePerSqM );
            length  =   ( length  +   1 )   /   2 ;
         }   else   {
            subdivision  =   new   Property ( length ,  width  /   2 ,  valuePerSqM );
            width  =   ( width  +   1 )   /   2 ;
         }
        
         return  subdivision ;
     }
    
     public   String  toString ()   {
         return   "Property: "   +  length  +   "m long by "   +  width  +   "m wide ($"   +
            valuePerSqM  +   " per square metre)" ;
     }
}