(Rectangle Class) need help to convert java to c++
Dec 5, 2012 at 1:03am UTC
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
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 Dec 5, 2012 at 4:51am UTC
Dec 5, 2012 at 1:11am UTC
Hello jiv!
What is your question? Is there any problem in your code that currently makes you sad?
Dec 5, 2012 at 1:53am UTC
i using this program in c++ but this is java program i want to convert this in c++ . if you can help
Topic archived. No new replies allowed.