You are using a version without Ads of this website. Please, consider donating:

### (Rectangle Class) need help to convert java to c++

1. (Rectangle Class) Create a class Rectangle. The class has attributes length and width, each
of which defaults to 1. It has methods that calculate the perimeter and the area of the rectangle. It
has set and get methods for both length and width. The set methods should verify that length and
width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test
class Rectangle.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114`` ``````public class Rectangle { private double length; // the length of the rectangle private double width; // the width of the rectangle // constructor without parameters public Rectangle() { setLength( 1.0 ); setWidth( 1.0 ); } // end Rectangle no-argument constructor // constructor with length and width supplied public Rectangle( double theLength, double theWidth ) { setLength( theLength ); setWidth( theWidth ); } // end Rectangle two-argument constructor // validate and set length public void setLength( double theLength ) { length = ( theLength > 0.0 && theLength < 20.0 ? theLength : 1.0 ); } // end method setLength // validate and set width public void setWidth( double theWidth ) { width = ( theWidth > 0 && theWidth < 20.0 ? theWidth : 1.0 ); } // end method setWidth // get value of length public double getLength() { return length; } // end method getLength // get value of width public double getWidth() { return width; } // end method getWidth // calculate rectangle's perimeter public double perimeter() { return 2 * length + 2 * width; } // end method perimeter // calculate rectangle's area public double area() { return length * width; } // end method area // convert to String public String toString() { return String.format( "%s: %f\n%s: %f\n%s: %f\n%s: %f", "Length", length, "Width", width, "Perimeter", perimeter(), "Area", area() ); } // end method toRectangleString } // end class Rectangle import java.util.Scanner; public class RectangleTest { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); Rectangle rectangle = new Rectangle(); int choice = getMenuChoice(); while ( choice != 3 ) { switch ( choice ) { case 1: System.out.print( "Enter length: " ); rectangle.setLength( input.nextDouble() ); break; case 2: System.out.print ( "Enter width: " ); rectangle.setWidth( input.nextDouble() ); break; } // end switch System.out.println ( rectangle.toString() ); choice = getMenuChoice(); } // end while } // end main // prints a menu and returns a value coressponding to the menu choice private static int getMenuChoice() { Scanner input = new Scanner( System.in ); System.out.println( "1. Set Length" ); System.out.println( "2. Set Width" ); System.out.println( "3. Exit" ); System.out.print( "Choice: " ); return input.nextInt(); } }``````

Last edited on
Hello jiv!