java - Calculating perimeter and area of a rectangle -
i need able input length , width of rectangle console , calculate perimeter , area. have working other accepting inputs calculations. know i'm close, can't seem figure out. in advance help. keep in mind i'm novice put nicely, answers may not make sense me @ first. cannot calculate values input console.
package edu.purdue.cnit325_lab1; public class rectangle { private static double length; private static double width; public rectangle() { length=0.0; width=0.0; } public rectangle(double l, double w) { length = l; width = w; } public double findarea() { return length*width; } public double findperim() { return length*2 + width*2; } } package edu.purdue.cnit325_lab1; import java.util.scanner; public class testrectangle { /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub scanner scanl = new scanner (system.in); system.out.print("please enter length of rectangle: "); double l = scanl.nextdouble(); scanner scanw = new scanner (system.in); system.out.print("please enter length of rectangle: "); double w = scanw.nextdouble(); //int w = scanw.nextint(); double rectanglearea; rectangle unitrectangle = new rectangle(); rectanglearea = unitrectangle.findarea(); system.out.println("the area of unit rectangle " + rectanglearea); double rectanglepermiter; rectangle perimrectangle = new rectangle(); rectanglepermiter = perimrectangle.findperim(); system.out.println("the permimiter of unit rectangle " + rectanglepermiter); } }
note calling rectangle constructore no arguments setting width , height zero, should use
rectangle unitrectangle = new rectangle(l,w);
and indeed other answer should use 1 scanner instance.
plus regarding coding style: not upercase variable names. quite confusing more "experienced" java developers. :-)
Comments
Post a Comment