import javax.swing.JOptionPane; public class eg16_PassObject { public static void main (String[] args) { eg16_Rectangle mybox = new eg16_Rectangle("My Box", 12.0, 5.0); //create object displayRectangle (mybox); changeRectangle (mybox, 20, 10); displayRectangle (mybox); eg16_Rectangle myroom = new eg16_Rectangle("My Room", 100.0, 50.0); //create object displayRectangle (myroom); changeRectangle (myroom, 200.0, 150.0); displayRectangle (myroom); eg16_Rectangle mygym = get_eg16_Rectangle(); displayRectangle (mygym); //last 2 exercises, do toString first in eg_16_Rectangle.java //if you write a toString method for a class, Java will automatically call //the method when the object is passed as an argument to print or println System.out.println(mygym); //compare 2 objects using equals method if (mygym.equals(myroom)) System.out.println("\nmygym & myroom: both objects are the same"); else System.out.println("\nmygym & myroom: both objects are different"); System.out.println(); //use copy constructor to copy an object eg16_Rectangle car1 = new eg16_Rectangle("My Car", 30, 20); displayRectangle (car1); eg16_Rectangle car2 = new eg16_Rectangle(car1); displayRectangle (car2); if (car1.equals(car2)) System.out.println("\ncar1 & car2: both objects are the same"); else System.out.println("\ncar1 & car2: both objects are different"); } //pass object as an argument to method public static void displayRectangle(eg16_Rectangle r) { System.out.println(r.getName() + ": Length: "+ r.getLength() + " Width: " + r.getWidth()); } public static void changeRectangle(eg16_Rectangle r, double myLen, double myWid) { r.setLenWid(myLen, myWid); } public static eg16_Rectangle get_eg16_Rectangle() //return referece to object { String input, myName; double myLen, myWid; input = JOptionPane.showInputDialog ("Enter Name."); myName = input; input = JOptionPane.showInputDialog ("Enter Length."); myLen = Double.parseDouble (input); input = JOptionPane.showInputDialog ("Enter Width."); myWid = Double.parseDouble (input); return new eg16_Rectangle(myName, myLen, myWid); } }