{"id":"object-interaction-returning","slug":"returning-objects","title":"Methods that return objects","text":"A method can return an object, just as it returns a number or a String. You've seen some already: a getter such as `getAddress()` returns the object in an instance variable, and module 19's `findOldest` returned one of the pets in a list. This step is about methods that **make a new object** and return it:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        Photo photo = new Photo(800, 600);\n        Photo turned = photo.rotated();\n        Photo small = photo.scaled(25);\n        System.out.println(photo);\n        System.out.println(turned);\n        System.out.println(small);\n        System.out.println(photo.rotated().scaled(50));\n    }\n}\n// ==== Photo.java ====\npublic class Photo {\n    private int width;\n    private int height;\n\n    public Photo(int width, int height) {\n        this.width = width;\n        this.height = height;\n    }\n\n    public Photo rotated() {\n        return new Photo(this.height, this.width);\n    }\n\n    public Photo scaled(int percent) {\n        return new Photo(this.width * percent / 100, this.height * percent / 100);\n    }\n\n    public String toString() {\n        return this.width + \" x \" + this.height + \" pixels\";\n    }\n}\n```\n\n```output\n800 x 600 pixels\n600 x 800 pixels\n200 x 150 pixels\n300 x 400 pixels\n```\n\n- The return type is the class: `public Photo rotated()` returns a Photo.\n- `return new Photo(this.height, this.width);` makes a new Photo from this photo's values and returns it. The photo the method was called on doesn't change: after both calls, `photo` is still 800 x 600.\n- The caller keeps the new object in a variable, as in `Photo turned = photo.rotated();`, or uses it right away: `photo.rotated().scaled(50)` rotates the photo, and then scales the rotated one.\n- Like any return value, the new object is lost if the caller doesn't use it. `photo.rotated();` on a line of its own changes nothing.\n\n### Changing it, or returning a new one?\n\nA class could also have a method that turns the photo itself around:\n\n```java\npublic void rotate() {\n    int oldWidth = this.width;\n    this.width = this.height;\n    this.height = oldWidth;\n}\n```\n\n`photo.rotate()` changes that photo, and every variable that refers to it sees the change. `photo.rotated()` leaves it alone and gives back a new photo. Both are fine, and the names tell them apart. String methods never change the String they're called on: `word.toUpperCase()` returns the text in capitals, and `word` stays as it was.\n\n### Returning an object that's already there\n\nThe returned object doesn't have to be new. This method returns whichever of two parcels is heavier, either the other one or the parcel it was called on, which is `this`:\n\n```java\npublic Parcel heavierOf(Parcel other) {\n    if (other.grams > this.grams) {\n        return other;\n    }\n    return this;\n}\n```\n\nNo new parcel is made: the caller gets a reference to one of the two that already exist.\n","task":"In `Route.java`, write the method `reversed()`. It returns a new Route for the way back: from where this route ends to where it starts. The route it's called on stays as it was. The check runs:\n\n```java\nRoute there = new Route(\"Harbor Town\", \"Hill Park\");\nRoute back = there.reversed();\nSystem.out.println(there);\nSystem.out.println(back);\n```\n\nand expects:\n\n```\nHarbor Town to Hill Park\nHill Park to Harbor Town\n```\n","kind":"code","seed":"// ==== Route.java ====\npublic class Route {\n    private String from;\n    private String to;\n\n    public Route(String from, String to) {\n        this.from = from;\n        this.to = to;\n    }\n\n    // Write the reversed method here.\n\n    public String toString() {\n        return this.from + \" to \" + this.to;\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Route there = new Route(\"Harbor Town\", \"Hill Park\");\n        Route back = there.reversed();\n        System.out.println(there);\n        System.out.println(back);\n    }\n}\n","solution":"// ==== Route.java ====\npublic class Route {\n    private String from;\n    private String to;\n\n    public Route(String from, String to) {\n        this.from = from;\n        this.to = to;\n    }\n\n    public Route reversed() {\n        return new Route(this.to, this.from);\n    }\n\n    public String toString() {\n        return this.from + \" to \" + this.to;\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Route there = new Route(\"Harbor Town\", \"Hill Park\");\n        Route back = there.reversed();\n        System.out.println(there);\n        System.out.println(back);\n    }\n}\n","hints":["The method returns a Route: `public Route reversed() {`.","Make the new route with the ends swapped, and return it: `return new Route(this.to, this.from);`. Nothing in `this` changes."],"tests":[{"name":"Test 1","stdin":"","call":"Route there = new Route(\"Harbor Town\", \"Hill Park\");\nRoute back = there.reversed();\nSystem.out.println(there);\nSystem.out.println(back);\n","expect":"Harbor Town to Hill Park\nHill Park to Harbor Town","hidden":false},{"name":"Test 2","stdin":"","call":"Route route = new Route(\"Mill End\", \"Lake View\");\nroute.reversed();\nSystem.out.println(route);\nSystem.out.println(route.reversed().reversed());\n","expect":"Mill End to Lake View\nMill End to Lake View","hidden":true},{"name":"Test 3","stdin":"","call":"Route route = new Route(\"Ash Lane\", \"Oak Hill\");\nRoute back = route.reversed();\nSystem.out.println(back == route);\nSystem.out.println(back);\nSystem.out.println(route);\n","expect":"false\nOak Hill to Ash Lane\nAsh Lane to Oak Hill","hidden":true}],"require":[{"pattern":"\\bpublic\\s+Route\\s+reversed\\s*\\(\\s*\\)","message":"Write the method as `public Route reversed()`."}],"forbid":[],"more":[{"task":"A journey is made of legs. In `Leg.java`, write the method `then(Leg next)`. It returns a new Leg that starts where this leg starts and ends where the next leg ends, and its kilometers are the two legs' kilometers added up. Neither leg changes. The check runs:\n\n```java\nLeg first = new Leg(\"Northport\", \"Eastvale\", 120);\nLeg second = new Leg(\"Eastvale\", \"Southby\", 85);\nLeg whole = first.then(second);\nSystem.out.println(whole);\nSystem.out.println(first);\n```\n\nand expects:\n\n```\nNorthport to Southby, 205 km\nNorthport to Eastvale, 120 km\n```\n","kind":"code","seed":"// ==== Leg.java ====\npublic class Leg {\n    private String from;\n    private String to;\n    private int km;\n\n    public Leg(String from, String to, int km) {\n        this.from = from;\n        this.to = to;\n        this.km = km;\n    }\n\n    // Write the then method here.\n\n    public String toString() {\n        return this.from + \" to \" + this.to + \", \" + this.km + \" km\";\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Leg first = new Leg(\"Northport\", \"Eastvale\", 120);\n        Leg second = new Leg(\"Eastvale\", \"Southby\", 85);\n        Leg whole = first.then(second);\n        System.out.println(whole);\n        System.out.println(first);\n    }\n}\n","solution":"// ==== Leg.java ====\npublic class Leg {\n    private String from;\n    private String to;\n    private int km;\n\n    public Leg(String from, String to, int km) {\n        this.from = from;\n        this.to = to;\n        this.km = km;\n    }\n\n    public Leg then(Leg next) {\n        return new Leg(this.from, next.to, this.km + next.km);\n    }\n\n    public String toString() {\n        return this.from + \" to \" + this.to + \", \" + this.km + \" km\";\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Leg first = new Leg(\"Northport\", \"Eastvale\", 120);\n        Leg second = new Leg(\"Eastvale\", \"Southby\", 85);\n        Leg whole = first.then(second);\n        System.out.println(whole);\n        System.out.println(first);\n    }\n}\n","hints":["The method takes a Leg and returns a Leg: `public Leg then(Leg next) {`. Inside, `this.from` is where this leg starts, and `next.to` is where the next one ends.","`return new Leg(this.from, next.to, this.km + next.km);`"],"tests":[{"name":"Test 1","stdin":"","call":"Leg first = new Leg(\"Northport\", \"Eastvale\", 120);\nLeg second = new Leg(\"Eastvale\", \"Southby\", 85);\nLeg whole = first.then(second);\nSystem.out.println(whole);\nSystem.out.println(first);\n","expect":"Northport to Southby, 205 km\nNorthport to Eastvale, 120 km","hidden":false},{"name":"Test 2","stdin":"","call":"Leg a = new Leg(\"Brook\", \"Marsh\", 12);\nLeg b = new Leg(\"Marsh\", \"Ferry\", 30);\nLeg c = new Leg(\"Ferry\", \"Cliff\", 7);\nSystem.out.println(a.then(b).then(c));\nSystem.out.println(b);\nSystem.out.println(c);\n","expect":"Brook to Cliff, 49 km\nMarsh to Ferry, 30 km\nFerry to Cliff, 7 km","hidden":true},{"name":"Test 3","stdin":"","call":"Leg up = new Leg(\"Base\", \"Top\", 4);\nLeg down = new Leg(\"Top\", \"Base\", 4);\nLeg trip = up.then(down);\nSystem.out.println(trip);\nSystem.out.println(trip == up);\nSystem.out.println(up);\n","expect":"Base to Base, 8 km\nfalse\nBase to Top, 4 km","hidden":true}],"require":[{"pattern":"\\bpublic\\s+Leg\\s+then\\s*\\(\\s*Leg\\s+\\w+\\s*\\)","message":"Write the method as `public Leg then(Leg next)`."}],"forbid":[]},{"task":"What does this program print? A cutting is a small piece taken from a plant to grow a new one.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        Plant mother = new Plant(\"mint\", 20);\n        Plant same = mother;\n        Plant young = mother.cutting();\n        same.grow(10);\n        young.grow(3);\n        System.out.println(mother);\n        System.out.println(young);\n        System.out.println(mother.cutting());\n    }\n}\n// ==== Plant.java ====\npublic class Plant {\n    private String name;\n    private int height;\n\n    public Plant(String name, int height) {\n        this.name = name;\n        this.height = height;\n    }\n\n    public void grow(int cm) {\n        this.height = this.height + cm;\n    }\n\n    public Plant cutting() {\n        return new Plant(this.name, 5);\n    }\n\n    public String toString() {\n        return this.name + \", \" + this.height + \" cm\";\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        Plant mother = new Plant(\"mint\", 20);\n        Plant same = mother;\n        Plant young = mother.cutting();\n        same.grow(10);\n        young.grow(3);\n        System.out.println(mother);\n        System.out.println(young);\n        System.out.println(mother.cutting());\n    }\n}\n// ==== Plant.java ====\npublic class Plant {\n    private String name;\n    private int height;\n\n    public Plant(String name, int height) {\n        this.name = name;\n        this.height = height;\n    }\n\n    public void grow(int cm) {\n        this.height = this.height + cm;\n    }\n\n    public Plant cutting() {\n        return new Plant(this.name, 5);\n    }\n\n    public String toString() {\n        return this.name + \", \" + this.height + \" cm\";\n    }\n}\n","hints":["`same = mother` copies the reference: `same` and `mother` are one plant, so `same.grow(10)` makes it 30 cm.","`cutting()` makes a new, separate Plant of 5 cm each time it's called. `young` then grows 3 cm on its own, and the last line prints a brand-new cutting."],"tests":[{"name":"Output","stdin":"","expect":"mint, 30 cm\nmint, 8 cm\nmint, 5 cm","hidden":false}],"require":[],"forbid":[],"lines":["mint, 30 cm","mint, 8 cm","mint, 5 cm"]}]}