Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert the following Java programming language code to C++ programming language

ID: 3859005 • Letter: C

Question

Convert the following Java programming language code to C++ programming language for the following program:

public ArrayList<GeometricObject> readObjects(String filename) {

File f = new File(filename);

ArrayList<GeometricObject> al = new ArrayList<GeometricObject>();

try {

Scanner s = new Scanner(f);

while (s.hasNextLine()) {

Scanner s2 = new Scanner(s.nextLine(), “,”);

// type

String type = s2.next();

// color

Scanner s3 = new Scanner(s2.next());

s3.next();

String color = s3.next();

// double1

s3 = new Scanner(s2.next());

s3.next();

double num1 = s3.nextDouble();

GeometricObject go = null;

if (type.equals(“Circle”)) {

go = new Circle(color, num1);

}

else {

s3 = new Scanner(s2.next());

s3.next();

double height = s3.nextDouble();

go = newRectangle(color, num1, height);

}

al.add(go);

}

} catch (IO Exception e) {

System.out.println(“Some error reading file”);

}

return al;

}

Explanation / Answer


Sample Java File:


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


public class Test1 {
public ArrayList<GeometricObject> readObjects(String filename) {
File f = new File(filename);
ArrayList<GeometricObject> al = new ArrayList<GeometricObject>();
try {
Scanner s = new Scanner(f);
while (s.hasNextLine()) {
Scanner s2 = new Scanner(s.nextLine(), “,”);
// type
String type = s2.next();
// color
Scanner s3 = new Scanner(s2.next());
s3.next();
String color = s3.next();
// double1
s3 = new Scanner(s2.next());
s3.next();
double num1 = s3.nextDouble();
GeometricObject go = null;
if (type.equals(“Circle”)) {
go = new Circle(color, num1);
}
else {
s3 = new Scanner(s2.next());
s3.next();
double height = s3.nextDouble();
go = newRectangle(color, num1, height);
}
al.add(go);
}
} catch (IO Exception e) {
System.out.println(“Some error reading file”);
}
return al;
}


}

Modified C++ Files

Test1.h
#pragma once
#include <string>
#include <vector>
#include <iostream>
namespace tt
{


class Test1
{
public:
virtual std::vector<GeometricObject*> readObjects(const std::wstring &filename);



static void main(std::vector<std::wstring> &args);
};
}

Test1.cpp

#include "Test1.h"
namespace tt
{
using javafx::scene::shape::Circle;
std::vector<GeometricObject*> Test1::readObjects(const std::wstring &filename)
{
File *f = new File(filename);
std::vector<GeometricObject*> al;
try
{
Scanner *s = new Scanner(f);
while (s->hasNextLine())
{
Scanner *s2 = new Scanner(s->nextLine(), L",");
// type
std::wstring type = s2->next();
// color
Scanner *s3 = new Scanner(s2->next());
s3->next();
std::wstring color = s3->next();
// double1
s3 = new Scanner(s2->next());
s3->next();
double num1 = s3->nextDouble();
GeometricObject *go = nullptr;
if (type == L"Circle")
{
go = new Circle(color, num1);
}
else
{
s3 = new Scanner(s2->next());
s3->next();
double height = s3->nextDouble();
go = newRectangle(color, num1, height);
}
al.push_back(go);
}
}
catch (const IOException &e)
{
std::wcout << L"Some error reading file" << std::endl;
}
return al;
}
void Test1::main(std::vector<std::wstring> &args)
{

}
}