Convert this Java program made in netbeans to a C++ program public class Rectang
ID: 3617180 • Letter: C
Question
Convert this Java program made in netbeans to a C++ programpublic class Rectangle{
private double height;
private double width;
private String color;
public Rectangle(double wid,double high){
height = high;
width = wid;
}
public Rectangle(){
height = 1;
width = 1;
color = "White";
}
public voidsetHeight(double high){
height = high;
}
public voidsetWidth(double wid){
width = wid;
}
public voidsetColor(String col){
color = col;
}
public doublegetArea(){
return height*width;
}
public doublegetPerimeter(){
return 2*(height + width);
}
public voidgetColor(){
System.out.println("Color is: " + color +" ");
return;
}
}
public class RectangleDemo {
public static voidmain(String[] args){
Rectangle box1 = new Rectangle();
Rectangle box2 = new Rectangle(4, 40);
Rectangle box3 = new Rectangle(3.5, 35.9);
StringColor = "Red";
box1.setColor(Color);
box2.setColor(Color);
box3.setColor(Color);
box1.getColor();
box2.getColor();
box3.getColor();
System.out.println("The perimeter of the first box is: " +box1.getPerimeter() + " ");
System.out.println("The perimeter of the second box is: " +box2.getPerimeter() + " ");
System.out.println("The perimeter of the third box is: " +box3.getPerimeter() + " ");
System.out.println("The area of the first box is: " +box1.getArea() + " ");
System.out.println("The area of the second box is: " +box2.getArea() + " ");
System.out.println("The area of the third box is: " +box3.getArea() + " ");
}
}
Explanation / Answer
// Convert Java To C++ // March 3rd 2010 // Created By Nic Raboy#include <iostream> #include <string>
using namespace std;
class Rectangle { private: double height; double width; string color; public: Rectangle(double wid, double high) { height = high; width = wid; }
Rectangle() { height = 1; width = 1; color = "White"; } void setHeight(double high) { height = high; } void setWidth(double wid) { width = wid; } void setColor(string col) { color = col; } double getArea() { return height*width; } double getPerimeter() { return 2*(height + width); } void getColor() { cout << "Color is: " + color << endl; } };
int main() { Rectangle box1; Rectangle box2(4, 40); Rectangle box3(3.5, 35.9); string Color = "Red"; box1.setColor(Color); box2.setColor(Color); box3.setColor(Color); box1.getColor(); box2.getColor(); box3.getColor(); cout << "The perimeter of the first box is: " <<box1.getPerimeter() << endl; cout << "The perimeter of the second box is: " <<box2.getPerimeter() << endl; cout << "The perimeter of the third box is: " <<box3.getPerimeter() << endl; cout << "The area of the first box is: " <<box1.getArea() << endl; cout << "The area of the second box is: " <<box2.getArea() << endl; cout << "The area of the third box is: " <<box3.getArea() << endl; return 0; } ----------------------------------------- This should be what you're looking for. I took exactlywhat you had and converted it into C++. I compiled and testedit with the g++ compiler. Let me know if you have anyquestions regarding it.
Nic Raboy