Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <string>
#include <vector>
#include <iostream>
	{
	class Student
	{
	public:
		std::wstring studentName;
		int studentNum = 0;
		std::wstring SSN;
		double GPA = 0;
		double totalCredit = 0;
		   Student(const std::wstring &studentName, int studentNum, const std::wstring &SSN, double GPA, double totalCredit);
		virtual bool isequal(Student *student2);
		virtual void addGpa(double amount);
	};
}
{
	class Course
	{
	public:
		std::wstring courseName;
		int courseNum = 0;
		double creditHour = 0;
		std::vector<student*> array;
		int counter = 0;
		Course(const std::wstring &courseName, int courseNum, double creditHour, int registeringStudents);
		virtual void addStudent(Student *student);
		virtual bool validate(Student *student);
		virtual Student *getStudentByStno(int studentNum);
		virtual std::vector<student*> getStudentByGPA(double gpa);
		virtual double findMaxGpa();
	};
	;
}
{
	class main
	{
		public:
			static void main(std::vector<std::wstring> &args);
	};
}
//Helper class added by Java to C++ Converter:

#include <string>
#include <vector>

int main(int argc, char **argv)
{
    std::vector<std::wstring> args(argv + 1, argv + argc);
    javaapplication4::main::main(args);
}
//.cpp file code:

#include "snippet.h"
{
	Student::Student(const std::wstring &studentName, int studentNum, const std::wstring &SSN, double GPA, double totalCredit)
	{
	 this->studentName = studentName;
	 this->studentNum = studentNum;
	 this->SSN = SSN;
	 this->GPA = GPA;
	 this->totalCredit = totalCredit;
	}
	bool Student::isequal(Student *student2)
	{
		if (this->GPA == student2->GPA && this->totalCredit == student2->totalCredit)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	void Student::addGpa(double amount)
	{
		if (amount + this->GPA < 4.0 && amount + this->GPA > 0.35)
		{
			this->GPA += amount;
		}
		else
		{
			std::wcout << L"Result is more then 4.0 or less then 0.35... please try a different amount" << std::endl;
		}
	}
}
{
	using ArrayList = java::util::ArrayList;

	Course::Course(const std::wstring &courseName, int courseNum, double creditHour, int registeringStudents)
	{
		this->courseName = courseName;
		this->courseNum = courseNum;
		this->creditHour = creditHour;
		this->array = std::vector<student*>(registeringStudents);
	}
	void Course::addStudent(Student *student)
	{
		if (this->array.size() == counter)
		{
			std::wcout << L"Failed... course is full" << std::endl;
		}
		else if (validate(student))
		{
			this->array[counter] = student;
			counter++;
			std::wcout << L"Success in adding student" << std::endl;
		}
		else
		{
			std::wcout << L"student number already registered" << std::endl;
		}
	}
	bool Course::validate(Student *student)
	{
		for (int i = 0; i < this->array.size(); i++)
		{
			if (array[i] == nullptr)
			{
				continue;
			}

			if (student->studentNum == this->array[i]->studentNum)
			{
				return false;
			}
		}
		return true;
	}
	Student *Course::getStudentByStno(int studentNum)
	{
		for (int i = 0; i < array.size(); i++)
		{
			if (studentNum == array[i]->studentNum)
			{
				std::wcout << L"found and returned" << std::endl;
				return array[i];
			}
		}
		 std::wcout << L"not found.. returned null" << std::endl;
		return nullptr;
	}
	std::vector<student*> Course::getStudentByGPA(double gpa)
	{
		std::vector<student*> temp(counter);
		int tempo = 0;
		for (int i = 0; i < array.size(); i++)
		{
			if (array[i] == nullptr)
			{
				continue;
			}
			if (gpa == array[i]->GPA)
			{
				temp[tempo] = array[i];
				tempo++;
			}
		}
		return temp;
	}
	double Course::findMaxGpa()
	{
		double max = 0;
		for (int i = 0; i < array.size(); i++)
		{
			if (array[i] == nullptr)
			{
				continue;
			}
			if (array[i]->GPA > max)
			{
				max = array[i]->GPA;
			}
		}
		return max;
	}
}
{
	void main::main(std::vector<std::wstring> &args)
	{
	Student *student1 = new Student(L"Saif", 01, L"02323123", 2.0, 3.0);
	Student *student2 = new Student(L"abdulla", 02, L"1231232", 1.0, 3.0);
	std::wcout << student1->isequal(student2) << std::endl;
	Student *student3 = new Student(L"hazem", 01, L"02323123", 3.0, 3.0);
	Student *student4 = new Student(L"khalid", 04, L"34534535", 3.0, 3.0);

	std::wcout << student3->isequal(student4) << std::endl;
			 Course *science = new Course(L"science", 01, 3.0, 10);
			 science->addStudent(student1);
			 science->addStudent(student2);
			 science->addStudent(student3);
			 science->addStudent(student4);

		std::wcout << L"student 1 gpa is: " << student1->GPA << std::endl;
		student1->addGpa(5.0);
		std::wcout << L"student 1 gpa is: " << student1->GPA << std::endl;
		student1->addGpa(0.5);
		std::wcout << L"student 1 gpa is: " << student1->GPA << std::endl;

		std::wcout << L"The pointer for student 1 is : " << student1 << std::endl;
		std::wcout << science->getStudentByStno(01) << std::endl;

	   std::vector<student*> array2 = science->getStudentByGPA(3.0);
		std::wcout << array2[0]->studentName << std::endl;

		  std::wcout << science->findMaxGpa() << std::endl;

		  Student *student5 = new Student(L"smartest", 05, L"24234234", 4.0, 3.0);
		  science->addStudent(student5);

		  std::wcout << science->findMaxGpa() << std::endl;

//JAVA TO C++ CONVERTER TODO TASK: A 'delete student5' statement was not added since student5 was passed to a method or constructor. Handle memory management manually.
				delete science;
//JAVA TO C++ CONVERTER TODO TASK: A 'delete student4' statement was not added since student4 was passed to a method or constructor. Handle memory management manually.
//JAVA TO C++ CONVERTER TODO TASK: A 'delete student3' statement was not added since student3 was passed to a method or constructor. Handle memory management manually.
//JAVA TO C++ CONVERTER TODO TASK: A 'delete student2' statement was not added since student2 was passed to a method or constructor. Handle memory management manually.
//JAVA TO C++ CONVERTER TODO TASK: A 'delete student1' statement was not added since student1 was passed to a method or constructor. Handle memory management manually.
	}
}


What I have tried:

tried a a lot but looks complicated, if anyone can take a look at it and try.
Posted
Updated 4-Aug-20 19:52pm
v2
Comments
[no name] 4-Aug-20 21:20pm    
Deleted (course) "science" while holding local references to students 1 - 5. It did a "shallow" delete of "science".
Dave Kreskowiak 4-Aug-20 22:32pm    
Why odes this sniff of a homework assignment?
Jon McKee 5-Aug-20 0:24am    
Because it's definitely a homework assignment in C++ which he's possibly avoiding learning because he knows Java.
varus01 5-Aug-20 10:49am    
it was an assignment, but i know java and next semester i will start with c++ , my friend gave me this 2 months ago saying " all u want to know in cpp" is in this if you solve it then you can learn c++.
Dave Kreskowiak 5-Aug-20 11:27am    
Your friend gave you code he converted from Java to C++?

Quote:
A 'delete student5' statement was not added since student5 was passed to a method or constructor. Handle memory management manually.
That is one of the areas where C++ and Java differ; memory management. What this comment is telling you is that student5 (and other student variables) is likely to scope out here, but since this was passed to the method, this tool will not call the delete on it assuming you require this object in the outer scope as well.
Quote:
Handle memory management manually.
You should learn how C++ programs work, and how you can manage the memory. This is important if you are working with pointers (dynamically allocated memory). If you do not do that, you end up leaking memory in the system.

If you create objects locally and do not pass them around in other methods, this tool (not that I am sure of, but it) would write the delete statement to clean up the resources.
delete science;
This line proves the point, as may be, the science object was scoped only the current method, it was removed as soon as the scope finished (method body finished).

Please check these resources to learn how to manage memory in C++ programs,
Dynamic memory - C++ Tutorials[^]
C++ Memory Management: new and delete[^]
c++ pointer scope - Stack Overflow[^]
 
Share this answer
 
This is the problem with blind translations: it may seem simpler to do, but it doesn't produce good code in the target language because they use different frameworks, and what works well in one language isn't a good idea in another.

Instead of finding code that nearly does what you want, (or writing it in a language you do know and hoping a translation will work) you really should be writing this from scratch in C++ - if you don't, you aren't learning the right things from your homework, and that means the next task (which will be more complex) will be even harder for you to do ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900