Question: (Java code) Download one version of the complete real estate portfolio manager program. Choose either of the two array list versions (version 2 using static
(Java code)
Download one version of the complete real estate portfolio manager program. Choose either of the two array list versions (version 2 using static methods or version 3 using a collection class) and add the following features to it:
1. Two speculators may choose to make an even swap of two properties. Write a method called swap that is passed either two lists of properties (if you are writing a static method) or one portfolio (the other list is contained in the collection you are applying the method to) and two integer positions (one from each list). Swap the properties at the given positions from one list to the other, but do not change their position. For example, if speculator A swaps their property at position 2 with speculator B's property at position 0, then the swapped properties will be at position 2 for speculator A and at position 0 for speculator B. Use the ArrayList set method. Return the difference in value from your method, which may be negative depending on the direction of the swap.
2. Add code to the main program to adequately test your new method. You should call it more than once!
Submit both your swap method and the lines of code added to your main program (and not any of the code you were already given) by the due date specified in the course schedule.
public class Activity4A {
public static void main(String[] args) {
Property a, b, c, d;
a = new Property(16, 8, 160);
b = new Property(9, 13, 120);
System.out.print("a: " + a);
System.out.println("; total value is $" + a.value());
System.out.print("b: " + b);
System.out.println("; total value is $" + b.value());
c = a.subdivide(true);
d = b.subdivide(false);
System.out.print("a: " + a);
System.out.println("; total value is $" + a.value());
System.out.print("b: " + b);
System.out.println("; total value is $" + b.value());
System.out.print("c: " + c);
System.out.println("; total value is $" + c.value());
System.out.print("d: " + d);
System.out.println("; total value is $" + d.value());
System.out.println(" End of processing.");
}
}
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)";
}
}
import java.util.ArrayList;
public class Activity4B {
public static void main(String[] args) {
// Lists of properties belonging to two speculators
ArrayList
Property property;
jim = new ArrayList
jim.add(new Property(16, 8, 160));
jim.add(new Property(24, 17, 130));
jim.add(new Property(129, 180, 35));
helen = new ArrayList
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(" End of processing.");
}
public static void printProperties(String speculator, ArrayList
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 toSubdivide;
Property subdivision;
toSubdivide = list.get(position);
subdivision = toSubdivide.subdivide(lengthwise);
list.add(subdivision);
}
public static Property transfer(ArrayList
ArrayList
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)";
}
}
import java.util.ArrayList;
public class Activity4C {
public static void main(String[] args) {
Portfolio jim, helen;
Property property;
jim = new Portfolio("Jim");
jim.add(new Property(16, 8, 160));
jim.add(new Property(24, 17, 130));
jim.add(new Property(129, 180, 35));
helen = new Portfolio("Helen");
helen.add(new Property(9, 13, 120));
helen.add(new Property(15, 15, 210));
helen.add(new Property(9, 13, 120));
jim.print();
helen.print();
jim.subdivide(0, true);
jim.subdivide(2, false);
helen.subdivide(0, false);
property = jim.transfer(1, helen);
System.out.println(property + " transferred from Jim to Helen for $" + property.value());
property = helen.transfer(3, jim);
System.out.println(property + " transferred from Helen to Jim for $" + property.value());
jim.print();
helen.print();
System.out.println(" End of processing.");
}
}
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)";
}
}
class Portfolio {
private ArrayList
private String owner;
public Portfolio(String owner) {
list = new ArrayList
this.owner = owner;
}
public void add(Property p) {
list.add(p);
}
public void print() {
int totalValue;
System.out.println(owner + "'s portfolio:");
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 void subdivide(int position, boolean lengthwise) {
Property toSubdivide;
Property subdivision;
toSubdivide = list.get(position);
subdivision = toSubdivide.subdivide(lengthwise);
list.add(subdivision);
}
public Property transfer(int position, Portfolio buyer) {
Property toTransfer;
toTransfer = list.get(position);
list.remove(position);
buyer.add(toTransfer);
return toTransfer;
}
}
import java.util.ArrayList;
public class Activity4C {
public static void main(String[] args) {
Portfolio jim, helen;
Property property;
jim = new Portfolio("Jim");
jim.add(new Property(16, 8, 160));
jim.add(new Property(24, 17, 130));
jim.add(new Property(129, 180, 35));
helen = new Portfolio("Helen");
helen.add(new Property(9, 13, 120));
helen.add(new Property(15, 15, 210));
helen.add(new Property(9, 13, 120));
jim.print();
helen.print();
jim.subdivide(0, true);
jim.subdivide(2, false);
helen.subdivide(0, false);
property = jim.transfer(1, helen);
System.out.println(property + " transferred from Jim to Helen for $" + property.value());
property = helen.transfer(3, jim);
System.out.println(property + " transferred from Helen to Jim for $" + property.value());
jim.print();
helen.print();
System.out.println(" End of processing.");
}
}
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)";
}
}
class Portfolio {
private ArrayList
private String owner;
public Portfolio(String owner) {
list = new ArrayList
this.owner = owner;
}
public void add(Property p) {
list.add(p);
}
public void print() {
int totalValue;
System.out.println(owner + "'s portfolio:");
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 void subdivide(int position, boolean lengthwise) {
Property toSubdivide;
Property subdivision;
toSubdivide = list.get(position);
subdivision = toSubdivide.subdivide(lengthwise);
list.add(subdivision);
}
public Property transfer(int position, Portfolio buyer) {
Property toTransfer;
toTransfer = list.get(position);
list.remove(position);
buyer.add(toTransfer);
return toTransfer;
}
}
import java.util.ArrayList;
public class Activity4C {
public static void main(String[] args) {
Portfolio jim, helen;
Property property;
jim = new Portfolio("Jim");
jim.add(new Property(16, 8, 160));
jim.add(new Property(24, 17, 130));
jim.add(new Property(129, 180, 35));
helen = new Portfolio("Helen");
helen.add(new Property(9, 13, 120));
helen.add(new Property(15, 15, 210));
helen.add(new Property(9, 13, 120));
jim.print();
helen.print();
jim.subdivide(0, true);
jim.subdivide(2, false);
helen.subdivide(0, false);
property = jim.transfer(1, helen);
System.out.println(property + " transferred from Jim to Helen for $" + property.value());
property = helen.transfer(3, jim);
System.out.println(property + " transferred from Helen to Jim for $" + property.value());
jim.print();
helen.print();
System.out.println(" End of processing.");
}
}
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)";
}
}
class Portfolio {
private ArrayList
private String owner;
public Portfolio(String owner) {
list = new ArrayList
this.owner = owner;
}
public void add(Property p) {
list.add(p);
}
public void print() {
int totalValue;
System.out.println(owner + "'s portfolio:");
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 void subdivide(int position, boolean lengthwise) {
Property toSubdivide;
Property subdivision;
toSubdivide = list.get(position);
subdivision = toSubdivide.subdivide(lengthwise);
list.add(subdivision);
}
public Property transfer(int position, Portfolio buyer) {
Property toTransfer;
toTransfer = list.get(position);
list.remove(position);
buyer.add(toTransfer);
return toTransfer;
}
}
import java.util.ArrayList;
public class Activity4CAlt {
public static void main(String[] args) {
Portfolio jim, helen;
Property property;
jim = new Portfolio("Jim");
jim.add(new Property(16, 8, 160));
jim.add(new Property(24, 17, 130));
jim.add(new Property(129, 180, 35));
helen = new Portfolio("Helen");
helen.add(new Property(9, 13, 120));
helen.add(new Property(15, 15, 210));
helen.add(new Property(9, 13, 120));
jim.print();
helen.print();
jim.subdivide(0, true);
jim.subdivide(2, false);
helen.subdivide(0, false);
property = jim.transfer(1, helen);
System.out.println(property + " transferred from Jim to Helen for $" + property.value());
property = helen.transfer(3, jim);
System.out.println(property + " transferred from Helen to Jim for $" + property.value());
jim.print();
helen.print();
System.out.println(" End of processing.");
}
}
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)";
}
}
class Portfolio {
private Property[] list;
int size;
private String owner;
public Portfolio(String owner) {
// assume that a typical portfolio has at most 3 properties
list = new Property[3];
// but it has none initially
size = 0;
this.owner = owner;
}
public void add(Property p) {
if (size >= list.length) {
// make the new list twice as big as the old
Property[] newList = new Property[list.length * 2];
// copy the old contents to the new list
System.arraycopy(list, 0, newList, 0, list.length);
// replace the old list with the new one
list = newList;
}
list[size] = p;
size++;
}
public void print() {
int totalValue;
System.out.println(owner + "'s portfolio:");
totalValue = 0;
for (int i = 0; i < size; i++) {
System.out.println(" " + list[i]);
totalValue += list[i].value();
}
System.out.println("Total value: $" + totalValue);
}
public void subdivide(int position, boolean lengthwise) {
Property toSubdivide;
Property subdivision;
toSubdivide = list[position];
subdivision = toSubdivide.subdivide(lengthwise);
add(subdivision);
}
public Property transfer(int position, Portfolio buyer) {
Property toTransfer;
toTransfer = list[position];
remove(position);
buyer.add(toTransfer);
return toTransfer;
}
private void remove(int position) {
for (int i = position + 1; i < size; i++) {
list[i - 1] = list[i];
}
size--;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
