i'm having issues code running, i'm trying program print strings below using input other classes. can see, info put new bride , location objects being put in wedding object , need try , retrieve details wedding object , display on screen so:
wedding data:
bride: amy cronos, age: 29
location: south rd, suburb: tonsley
but instead met 4 identical errors relating place.getname, place.getsuburb() etc. etc.
main.java:6: error: cannot find symbol system.out.println("location"+place.getstreet()+", suburb:
"+place.getsuburb());symbol: variable place
location: class main
i'm pretty sure has scope, cant work out need do.
what causing error , how fix it?
here code:
public class weddingdetails { public static void main(string[] args) { bride person = new bride("amy cronos", 29); location place = new location("tonsley", "south rd"); wedding wed = new wedding(person, place); show(wed); } public static void show(wedding wed) { system.out.println("wedding data:"); system.out.println("bride: " + person.getname() + ", age: " + person.getage()); system.out.println("location: " + place.getstreet() + ", suburb: " + place.getsuburb()); } public static class location { private string suburb; private string street; location(string suburb, string street) { this.suburb = suburb; this.street = street; } public string getsuburb() { return suburb; } public string getstreet() { return street; } } public static class bride { private string name; private int age; bride(string name, int age) { this.name = name; this.age = age; } public string getname() { return name; } public int getage() { return age; } } public static class wedding { private bride person; private location place; wedding(bride person, location place) { this.person = person; this.place = place; } public bride getbride() { return person; } public location getplace() { return place; } } }
the issue here println statements trying access methods within objects, calling methods on wrong object. should accessing bride , location objects wedding class' getters (getbride() , getplace(). complete call wed.getbride().getname() , wed.getplace().getstreet() on.
corrected code below. note: purposes of being able compile of code inside 1 class, added static
keyword bride, location , wedding class declarations. can remove static
, copy , paste each class .java files.
public class weddingdetails { public static void main(string[] args) { bride person = new bride("amy cronos", 29); location place = new location("tonsley", "south rd"); wedding wed = new wedding(person, place); show(wed); } public static void show(wedding wed) { system.out.println("wedding data:"); system.out.println("bride: " + wed.getbride().getname() + ", age: " + wed.getbride().getage()); system.out.println("location: " + wed.getplace().getstreet() + ", suburb: " + wed.getplace().getsuburb()); } public static class location { private string suburb; private string street; location(string suburb, string street) { this.suburb = suburb; this.street = street; } public string getsuburb() { return suburb; } public string getstreet() { return street; } } public static class bride { private string name; private int age; bride(string name, int age) { this.name = name; this.age = age; } public string getname() { return name; } public int getage() { return age; } } public static class wedding { private bride person; private location place; wedding(bride person, location place) { this.person = person; this.place = place; } public bride getbride() { return person; } public location getplace() { return place; } } }
Comments
Post a Comment