sr24 25 Share Posted January 27, 2019 Question, I'm trying to understand why I can't use one value from one class in another. Like lets say I have class a and class b. in class a, I have declared a public String name; Used constructors and getters for class a. Now when it comes to class b and I'm trying to use that name, it is not inheriting it. Hopefully this makes sense, I don't wanna post all the code but i'm just trying to make sense of this. Link to comment Share on other sites More sharing options...
Nuclear Nezz 2040 Share Posted January 27, 2019 you're probably going to have to post code, though all you need to post is the relevant code. Link to comment Share on other sites More sharing options...
sr24 25 Author Share Posted January 27, 2019 12 minutes ago, Nuclear Nezz said: you're probably going to have to post code, though all you need to post is the relevant code. Quote I am wanting to call couch from furniture in a sout. Quote public class Furniture { public String couch; private String dinningTable; private int numberOfChairs; private int tvStands; public Furniture(String couch, String dinningTable, int numberOfChairs, int tvStandsm) { this.couch = couch; this.dinningTable = dinningTable; this.numberOfChairs = numberOfChairs; this.tvStands = tvStands; } public String getCouch() { return couch; } public String getDinningTable() { return dinningTable; } public int getNumberOfChairs() { return numberOfChairs; } public int getTvStands() { return tvStands; } } In my Electrical class I have a watchTV(). I wanted to use + couch in there but I can not do so. So I'm trying to figure out how I could do that without moving couch from Furniture to Electrical.. Quote public class Electrical { private int outlets; private int light; private int fans; private String microWave; private String refrigerator; public int tvSize; public String tvBrand; public Electrical(int outlets, int light, int fans, String microWave, String refrigerator, int tvSize, String tvBrand) { this.outlets = outlets; this.light = light; this.fans = fans; this.microWave = microWave; this.refrigerator = refrigerator; this.tvSize = tvSize; this.tvBrand = tvBrand; } public int getOutlets() { return outlets; } public int getLight() { return light; } public int getFans() { return fans; } public String getMicroWave() { return microWave; } public String getRefrigerator() { return refrigerator; } public int getTvSize(){ return tvSize; } public String getTvBrand(){ return tvBrand; } public void watchTV(){ System.out.println("Watching " + tvBrand + " " + tvSize + " tv"); } } Link to comment Share on other sites More sharing options...
Nuclear Nezz 2040 Share Posted January 27, 2019 couch is a variable of Furniture, you can't access it from Electrical unless you have an instance of Furniture. eg: Furniture couch = new Furniture(); sout(couch.getCouch()) Though really, your whole thing you've got here is sort of odd, you've got zero inheritance going on at all. I think what you're looking for is something like public class Furniture{ protected String name; public Furniture(){} public String getName(){return name;} } public class Couch extends Furniture{ public Couch(){this.name = "Couch";} } then Furniture f = new Couch(); sout(f.getName();} Link to comment Share on other sites More sharing options...
sr24 25 Author Share Posted January 27, 2019 26 minutes ago, Nuclear Nezz said: couch is a variable of Furniture, you can't access it from Electrical unless you have an instance of Furniture. eg: Furniture couch = new Furniture(); sout(couch.getCouch()) Though really, your whole thing you've got here is sort of odd, you've got zero inheritance going on at all. I think what you're looking for is something like public class Furniture{ protected String name; public Furniture(){} public String getName(){return name;} } public class Couch extends Furniture{ public Couch(){this.name = "Couch";} } then Furniture f = new Couch(); sout(f.getName();} I the whole thing I was trying to do which I didn't include in the code was System.out.println("Watching " + tvBrand + " " + tvSize + " tv on the " + couch); That was the main line i was trying to accomplish. Furniture, Electrical and some others tie into the class Room. Then everything comes together in the Main class. I'll have to switch to my laptop to post those later though Link to comment Share on other sites More sharing options...
Nuclear Nezz 2040 Share Posted January 27, 2019 14 minutes ago, sr24 said: I the whole thing I was trying to do which I didn't include in the code was System.out.println("Watching " + tvBrand + " " + tvSize + " tv on the " + couch); That was the main line i was trying to accomplish. Furniture, Electrical and some others tie into the class Room. Then everything comes together in the Main class. I'll have to switch to my laptop to post those later though Like I said, to access couch you need to have an instance of Furniture. Furniture f = new Furniture(); f.getCouch(); Without that instance, you cannot access couch. Link to comment Share on other sites More sharing options...
sr24 25 Author Share Posted January 27, 2019 29 minutes ago, Nuclear Nezz said: Like I said, to access couch you need to have an instance of Furniture. Furniture f = new Furniture(); f.getCouch(); Without that instance, you cannot access couch. Thanks for your patience and help with me. I'm going to paste it all. I understand the f.getCouch(). However the Furniture f = new Furniture(); doesn't work for me because it passes 4 parameters if I'm not mistaken. Classes I have are: Dimensions, Electrical, Room, Furniture and Main. Dimensions: Quote public class Dimensions { private int width; private int height; public Dimensions(int width, int height) { this.width = width; this.height = height; } public int getWidth() { return width; } public int getHeight() { return height; } } Furniture: Quote public class Furniture { public String couch; private String dinningTable; private int numberOfChairs; private int tvStands; public Furniture(String couch, String dinningTable, int numberOfChairs, int tvStandsm) { this.couch = couch; this.dinningTable = dinningTable; this.numberOfChairs = numberOfChairs; this.tvStands = tvStands; } public String getCouch() { return couch; } public String getDinningTable() { return dinningTable; } public int getNumberOfChairs() { return numberOfChairs; } public int getTvStands() { return tvStands; } } Electrical: Quote public class Electrical { private int outlets; private int light; private int fans; private String microWave; private String refrigerator; public int tvSize; public String tvBrand; Furniture f = new Furniture(); public Electrical(int outlets, int light, int fans, String microWave, String refrigerator, int tvSize, String tvBrand) { this.outlets = outlets; this.light = light; this.fans = fans; this.microWave = microWave; this.refrigerator = refrigerator; this.tvSize = tvSize; this.tvBrand = tvBrand; } public Furniture getF() { return f; } public int getOutlets() { return outlets; } public int getLight() { return light; } public int getFans() { return fans; } public String getMicroWave() { return microWave; } public String getRefrigerator() { return refrigerator; } public int getTvSize(){ return tvSize; } public String getTvBrand(){ return tvBrand; } public void watchTV(){ f.getCouch(); System.out.println("Watching " + tvBrand + " " + tvSize + " tv on the "); } } Room: Quote public class Room { private Furniture theFurniture; private Electrical theElectrical; private Dimensions theDimensions; public Room(Furniture theFurniture, Electrical theElectrical, Dimensions theDimensions) { this.theFurniture = theFurniture; this.theElectrical = theElectrical; this.theDimensions = theDimensions; } public void letsWatchTV(){ theElectrical.watchTV(); theFurniture.getCouch(); } } and finally Main. Quote public class Main { public static void main(String[] args) { Dimensions dimensions = new Dimensions(10, 12); Furniture theFurniture = new Furniture("sectional", "6 Person Dinning Table", 6, 1); Electrical theElectrics = new Electrical(3, 2, 1, "Samsung Microwave", "Samsung All Blackstainless", 42, "Samsung"); Room livingArea = new Room(theFurniture, theElectrics, dimensions); theElectrics.watchTV(); } } I think I'm misunderstanding stuff somewhere along the way. Link to comment Share on other sites More sharing options...
Vhagar 25 Share Posted January 27, 2019 Hey, Your 'watchTv' method could return a string consisting of the tv brand and size something like: public String watchTV(){ return "Watching " + tvBrand + " " + tvSize + " tv on the "; } then in your Room class change the method letsWatchTv to output (println) the combined strings from your electrics and furniture classes. Atm you're calling both methods in letsWatchTv and doing nothing with the returned strings. public void letsWatchTV(){ System.out.println(theElectrical.watchTV() + theFurniture.getCouch()); } You should remove the Furniture object you've added within electronics class because you're accessing both objects within the room class. You should call livingArea.watchT() within the Main class aswell. Might be worth making the coach string private after. On a side note it might be better to get tvBrand and tvSize separately using getters and format the string how you want in letsWatchTv but either way. Link to comment Share on other sites More sharing options...
Evil Bob 5 Share Posted January 27, 2019 public class Electrical { Furniture f = new Furniture(); public Furniture getF() { return f; } public void watchTV(){ f.getCouch(); System.out.println("Watching " + tvBrand + " " + tvSize + " tv on the "); } } Even if you pass in the correct number and type of arguments to the Furniture constructor and the code compiles, this code will not allow you to access the desired couch variable because you are creating a different instance of the class Furniture, instead of using the instance created in the main class, which is this one: Furniture theFurniture = new Furniture("sectional", "6 Person Dinning Table", 6, 1); You will need to somehow allow the Electrical class access to this "theFurniture" variable from the main class, from which you can retrieve the couch. You can try this: public class Electrical { private Furniture furniture; public Electrical(int outlets, int light, int fans, String microWave, String refrigerator, int tvSize, String tvBrand, Furniture furniture) { this.outlets = outlets; this.light = light; this.fans = fans; this.microWave = microWave; this.refrigerator = refrigerator; this.tvSize = tvSize; this.tvBrand = tvBrand; this.furniture = furniture; } public void watchTV(){ f.getCouch(); System.out.println("Watching " + tvBrand + " " + tvSize + " tv on the " + furniture.couch); } } public class Main { public static void main(String[] args) { Dimensions dimensions = new Dimensions(10, 12); Furniture theFurniture = new Furniture("sectional", "6 Person Dinning Table", 6, 1); Electrical theElectrics = new Electrical(3, 2, 1, "Samsung Microwave", "Samsung All Blackstainless", 42, "Samsung", theFurniture); Room livingArea = new Room(theFurniture, theElectrics, dimensions); theElectrics.watchTV(); } } Link to comment Share on other sites More sharing options...
Nuclear Nezz 2040 Share Posted January 27, 2019 I only did new Furniture() because i didn't want to type it all out. I'm a fan of giving more pseudo type code than actual code. It's a pain to type it out with proper format on the forums hah tbh I'm not entirely sure what you're looking to do. If you're looking to make a logical type of approach to fill up a room with objects, what I would suggest is: A room has more than one piece of furniture, so you should have either Furniture[] theFurniture, or List<Furniture> theFurniture Furniture should be a parent class, to which you should make various pieces of furniture underneath it (if you're looking to learn about inheritance) eg: public class Couch extends Furniture then Furniture would just have the name of the furniture, # of "spots" (eg: a desk is furniture, but doesn't have seats, but it does have 1 "spot" for an electrical device) so public Furniture(String name, int spots) then couch would look like public Couch(String name, int spots){ super(name,spots); } you'd want to override toString in your lower classes, that way printing them out is easier. @Override public String toString(){return "This is a " + name + " with " + spots + " seats!";} //this would be your Couch toString say you made a desk, you could override it to be return "This is a " name + " with " + spots + " spots available!"; So again, this whole exercise depends on what you're actually trying to learn. Is it inheritance? Then look at simplifying your Furniture class to be a parent, and have specific furniture classes that extends it (Couch, Desk, Table) and do the same with electronics. (TV, Radio, PC, etc) where your electronics would probably have a name and spots needed. If you're just looking to do something simple, then I'd still say simplify the whole thing because having a Furniture class that has hardcoded values in it isn't going to be very helpful for you to learn, at that point you're getting very close to just writing a hello world program, just a tid more complex. I'd be happy to give you more ideas of how to expand on this program, but I suggest you scrap it and start over, with a specific goal in mind. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.