How to assign an object property value to a variable in Java? -


the following code i'm having issues understanding:

public class rectangle {      public rectangle() {         double width = 1;         double height = 1;     }      public rectangle(double w, double h) {         double width = w;         double height = h;     }      public double getarea(double w, double h) {         return (w*h);     }      public double getperimeter(double w, double h) {         return ((2*w)+(2*h));     }      public static void main(string[] args) {         rectangle oldrectangle = new rectangle(4, 40);         rectangle newrectangle = new rectangle(3.5, 35.9);         double height1 = oldrectangle.height;         double height2 = newrectangle.height;         double width1 = oldrectangle.width;         double width2 = newrectangle.width;          system.out.println("width of rectangle 1 is: " + 4);         system.out.println("height of rectangle 1 is: " + 40);         system.out.println("area of rectangle 1 is: " + oldrectangle.getarea(4, 40));         system.out.println("perimeter of rectangle 1 is: " + oldrectangle.getperimeter(4, 40));         system.out.println("width of rectangle 1 is: " + 3.5);         system.out.println("height of rectangle 1 is: " + 35.9);         system.out.println("area of rectangle 1 is: " + newrectangle.getarea(3.5, 35.9));         system.out.println("perimeter of rectangle 1 is: " + newrectangle.getperimeter(3.5, 35.9));      }  } 

i instructed create 2 constructors rectangle class—one no arguments assigned default value of 1 both variables width , height. second constructor contain parameters take in 2 doubles assigned appropriate variable.

i told create 2 'get()' methods returned respective values—in case, perimeter , area of said rectangle.

i instructed create 2 rectangle instances, 1 width of 4 , height of 40—and width of 4.5 , height of 35.9. so, did , made 2 new rectangle objects, can see.

lastly, instructed print out width, height, perimeter, , area of both rectangle objects. issue don't know of way reference them. took beginners tutorial class javascript , if i'm not mistaken, recall there way reference property value of object assigning variable. again, i'm saying "if i'm not mistaken", wrong. it's been while...

i realize java , java script entirely different things in own right. java script scripting language developed , based off of java.

anyway, grand.

please feel free me understand how can implement i'm trying giving examples. don't have use exact code, i'd able code make more sense.. i'm using eclipse btw.

you on right track. looking this:

public double getarea(rectangle r){       return r.width*r.height; } public double getperimeter(rectangle r){       return (2*r.width + 2*r.height); } 

for print statements hard-coding in values dont have do.

 system.out.println("width of rectangle 1 is: " + oldrectangle.width);     system.out.println("height of rectangle 1 is: " + oldrectangle.height);     system.out.println("area of rectangle 1 is: " + getarea(oldrectangle));     system.out.println("perimeter of rectangle 1 is: " + getperimeter(oldrectangle)); 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -