1 Replies - 24505 Views - Last Post: 20 August 2008 - 05:08 AM Rate Topic: -----

#1 shaynicb25  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 15-July 08

Convert from C++ to Java

Post icon  Posted 19 August 2008 - 11:15 PM

Is there an easy way to convert code from C++ to Java.

The program should store the user entered day. Set the day, return the day, return the previous day, calculate and return the day by adding certain days to the current day. For example if the day is Tuesday and you add 2 days the returned day is Thursday.

Both codes are below. Is there an easier way to do this or can someone please help me?

Here is the completed C++ code:

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

class dayType
{
public:
//constructor
dayType();

//days of week stored in array
string day[7];
int dayNumber;
int tempDay;

//member functions
void setDay(int _day);
void printDay();
void returnDay(int &day);
void returnNextDay();
void returnPreviousDay();
void calculateDay(int _changeDay);

};



int _tmain(int argc, _TCHAR* argv[])
{
dayType today;

int changeDay;
int day=7;
today.printDay();

cout << "Type in a number corresponding to the current day to change the day" << endl
<< "0: Sunday" << endl
<< "1: Monday" << endl
<< "2: Tuesday" << endl
<< "3: Wednesday" << endl
<< "4: Thursday" << endl
<< "5: Friday" << endl
<< "6: Saturday" << endl;
while(day<0 || day>6)
cin >> day; //allows the user to input the current day 

today.setDay(day);
today.printDay();

today.returnDay(day);
today.printDay();

cout << "If it were tomorrow, then the following statement would be true: " << endl;
today.returnNextDay();
today.printDay();
today.dayNumber--;

cout << "If it were yesterday, then the following statement would be true: " << endl;
today.returnPreviousDay();
today.printDay();
today.dayNumber++;


cout << "Add a number of days to today and see what day it will be: " << endl;
cin >> changeDay;

today.calculateDay(changeDay);
today.printDay(); 

return 0;
};

dayType::dayType()
{
dayNumber=0;
day[0]="Sun";
day[1]="Mon";
day[2]="Tues";
day[3]="Wednes";
day[4]="Thurs";
day[5]="Fri";
day[6]="Satur";

};

void dayType::printDay() 
{
cout << "Today is: " << day[dayNumber] << "day" << endl;
};

void dayType::setDay(int _day)
{
dayNumber=_day;
};

void dayType::returnDay(int &day)
{
day=dayNumber;
};

void dayType::returnNextDay()
{
dayNumber++;
};

void dayType::returnPreviousDay()
{
dayNumber--;
};

void dayType::calculateDay(int _changeDay)
{
tempDay=(dayNumber+_changeDay);
dayNumber=(tempDay%7);
};



Here is what I have tried to convert to Java

import java.io.*;
import java.util.*;

public class dayType
{

	public static void main(String args[])
	{
	dayType today = new dayType();

	int changeDay;
	int day =7;
	today.printDay();

	System.out.print("Type in a number corresponding to the current day to change the day");
	System.out.print("\n");
	System.out.print("0: Sunday");
	System.out.print("\n");
	System.out.print("1: Monday");
	System.out.print("\n");
	System.out.print("2: Tuesday");
	System.out.print("\n");
	System.out.print("3: Wednesday");
	System.out.print("\n");
	System.out.print("4: Thursday");
	System.out.print("\n");
	System.out.print("5: Friday");
	System.out.print("\n");
	System.out.print("6: Saturday");
	System.out.print("\n");
	while(day<0 || day>6)



Is This A Good Question/Topic? 0
  • +

Replies To: Convert from C++ to Java

#2 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: Convert from C++ to Java

Posted 20 August 2008 - 05:08 AM

This question asked before many times, check this thread.

Check search results.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1