Convert the following C++ code into Java ---------------------------------------
ID: 3817390 • Letter: C
Question
Convert the following C++ code into Java
------------------------------------------------------------------------------------------------------------------
ClassMain.cpp
#include <iostream>
#include "Shape.h"
using namespace std;
int main()
{
Shape* shape1 = new Shape(1, 2);
shape1->Print();
cout << "Area for shape1: " << shape1->GetArea() << endl;
delete shape1;
cout << "------------------------------------------------------------" << endl << endl;
system("pause");
return 0;
}
---------------------------------------------------------------------------------------
Point.cpp
#include "Point.h"
Point::Point()
{
_x = 0;
_y = 0;
}
Point::Point(int x, int y)
{
_x = x;
_y = y;
}
int Point::GetX()
{
return _x;
}
int Point::GetY()
{
return _y;
}
ostream& operator<<(ostream& os, const Point& point)
{
os << "(" << point._x << ", " << point._y << ")";
return os;
}
------------------------------------------------------------------------------------------------------------------------
Shape.cpp
#include <iostream>
#include "Shape.h"
using namespace std;
Shape::Shape()
{
_center = new Point(0, 0);
cout << "Creating a new default shape..." << endl;
}
Shape::~Shape()
{
cout << "Cleaning up the shape..." << endl;
delete _center;
_center = nullptr;
}
Shape::Shape(int x, int y)
{
_center = new Point(x, y);
cout << "Creating a new shape..." << endl;
}
float Shape::GetArea()
{
return 0; // A base shape has no area
}
float Shape::GetPerimeter()
{
return 1;
}
void Shape::Print()
{
cout << "I am a shape. My center is " << *_center << endl;
}
----------------------------------------------------------------------------------------------------------------
Point.h
#ifndef POINT_H
#define POINT_H
#include <ostream>
using namespace std;
class Point
{
public:
Point();
Point(int x, int y);
int GetX();
int GetY();
friend ostream& operator<<(ostream& os, const Point& point);
protected:
int _x;
private:
int _y;
};
#endif
------------------------------------------------------------------------------------------------------------------
Shape.h
#ifndef SHAPE_H
#define SHAPE_H
#include "Point.h"
class Shape
{
public:
Shape();
virtual ~Shape();
Shape(int x, int y);
virtual float GetArea();
virtual float GetPerimeter();
virtual void Print();
protected:
Point* _center;
};
#endif // !SHAPE_H
Explanation / Answer
PROGRAM CODE:
Point.java
package shapes;
public class Point {
protected int _x;
private int _y;
public Point() {
_x = 0;
_y = 0;
}
public Point(int x, int y) {
_x = x;
_y = y;
}
public int GetX()
{
return _x;
}
public int GetY()
{
return _y;
}
@Override
public String toString() {
return "(" + _x + "," + _y + ")";
}
}
Shape.java
package shapes;
public class Shape {
protected Point _center;
public Shape() {
_center = new Point();
System.out.println("Creating a new default shape...");
}
public Shape(int x, int y)
{
_center = new Point(x, y);
System.out.println("Creating a new shape...");
}
public float GetArea()
{
return 0;
}
public float GetPerimeter()
{
return 1;
}
public void print()
{
System.out.println("I am a shape. My center is " + _center);
}
}
ShapeDriver.java
package shapes;
public class ShapeDriver {
public static void main(String[] args) {
Shape shape = new Shape(1,2);
shape.print();
System.out.println("Area for shape1: " + shape.GetArea());
System.out.println("-----------------------------------------------------------");
}
}
OUTPUT:
Creating a new shape...
I am a shape. My center is (1,2)
Area for shape1: 0.0
-----------------------------------------------------------