Tuesday, July 14, 2009

Convert java coding program into c/c++.?

i had written it in java. understand the logic n implement it in C/C++. its better to u also and u can understand the logic also.


import javax.swing.*;


import java.awt.event.*;





class Cal extends JFrame implements ActionListener


{


JPanel p;


JLabel da,mo,ye;


JTextField dat,yea,mon;


//JComboBox mon;


JButton get,Exit;


JLabel res;


//String months[] = {"Jan","Feb","Mar","Apr","May"...


int months[] = {31,28,31,30,31,30,31,31,30,31...


int date,month,year,stdate = 1,stmonth = 1,styear = 1,total = 0,tot;


public Cal()


{


p = new JPanel();





da = new JLabel("Enter the date : ");


mo = new JLabel("-");


ye = new JLabel("-");





dat = new JTextField(2);


mon = new JTextField(2);


yea = new JTextField(4);





//mon = new JComboBox(months);





get = new JButton("Obtain");


Exit = new JButton("Exit");





res = new JLabel();





add(p);





p.add(da);


p.add(dat);





p.add(mo);


p.add(mon);





p.add(ye);


p.add(yea);





p.add(get);


p.add(Exit);





p.add(res);





get.addActionListener(this);


Exit.addActionListener(new ExitListener());





setSize(250,100);


setVisible(true);


setDefaultCloseOperation(3);


}


public class ExitListener implements ActionListener


{


public void actionPerformed(ActionEvent ae)


{


System.exit(0);


}


}


public void actionPerformed(ActionEvent ae)


{


total = 0;


date = Integer.parseInt(dat.getText()...


month = Integer.parseInt(mon.getText()...


year = Integer.parseInt(yea.getText()...





int i,ly = 1;





if (ly == leapYear(year) %26amp;%26amp; month %26gt; 2)


{


total += 1;


}





chkerr();





for(i = styear;i %26lt; year;i++)


{


ly = 0;


ly = leapYear(i);


total += 365 + ly;


}





for(i = 1;i %26lt; month;i++)


total += months[i-1];





total += date;





tot = total % 7;


String str = "";


switch(tot)


{


case 1 : str = "Monday";


break;


case 2 : str = "Tuesday";


break;


case 3 : str = "Wednesday";


break;


case 4 : str = "Thursday";


break;


case 5 : str = "Friday";


break;


case 6 : str = "Saturday";


break;


case 0 : str = "Sunday";


break;


}


res.setText(str);


}


public int leapYear(int lyear)


{


if(((lyear%4==0)%26amp;%26amp;(lyear%100!=...


{


return 1;


}


return 0;


}


public void chkerr()


{


String errmsg = "";


int err = 0;





if(date %26lt; 1 || date %26gt; 31)


{


errmsg = "Invalid Date";


err = 1;


}


if(month %26lt; 1 || month %26gt; 12)


{


errmsg = "Invalid Month";


err = 1;


}


if(year %26lt; 1)


{


errmsg = "Invalid Year";


err = 1;


}


if(month == 2)


{


if(leapYear(year) == 1)


{


if(date %26gt; 29)


{


errmsg = "Inavlid Date(For February)";


err = 1;


}


}


else if(date %26gt; 28)


{


errmsg = "Invalid Date(For February)";


err = 1;


}


}


if (err == 1)


{


JOptionPane.showMessageDialog(...


System.exit(0);


}


}


public static void main(String[] args)


{


new Cal();


}


}

Convert java coding program into c/c++.?
I don't understand your question. Are you asking someone to translate it for you, or asking whether it is easier to undertand in C/C++? C/C++ is easy to understand, especially if you leave appropriate comments.
Reply:Before you could convert it to c++ you would need to know what platform you are targetting, that is, MS Windows, UNIX, Linux, Apple, etc.





The reason for this is the different platforms offer different windowing systems. The thing that display the windows you are currently looking at. In the c++ standard, there is no mention of computer graphics, and therefore no standard way of displaying these windows.





Therefore, you would need to know what platform you are targetting, and also if using MS Windows, you need to know what framework you want to use. Some are Win32 API, MFC, OLE, .NET, etc. Then you will better able to know how to build the Graphical User Interface (GUI) like the JFrame, JOptionPane, JLabel, etc.





Converting the other non GUI code should be fairly easy.








P.S. The idea of allowing people to select the month, and then the day and year from a combo box was a very good idea. You should pursue that one.





To select the days to display for the month, you could set up what is called a lookup table. As there are only 4 varieties of days in month, eg 28 feb non leap year, 29 feb leap year, 30 and 31, you only need to 4 instances in this table. Then after finding the index of the combo box item that was selected, use this index to look into the lookup table that holds a link to the instance of 28, 29, 30, or 31 days.





The table could be easily set up as a 4 element array, each entry being another array of the numbers from 1-28, 29, etc. The value to index this table would be 0 for feb in non leap year, 1 for feb in leap year, 2 for 30 days, and 3 for 31 days.





You then update the combo box with the array of items that you just indexed.





Do the same thing for the years, etc, although you would probably need a bigger array for them, eg, 1973-3000 would be require a bigger array than 4 or 31 elements.





This way, you would not have to parse the integers, eg parseInt(...) as you are not checking for an errors, ie, a letter in the text instead of a number, which will produce a number of exceptions.


try


{


Integer.parseInt(dat.getText()...


}


catch (NumberFormatException ex)


{


// Error, a number was not input


// Inform user, reset fields, etc.


}








After looking at your code, I am going to make some changes, so you can remove the GUI stuff from the non GUI stuff, and therefore, hopefully make the transition from Java to c++ easier.








Changes:


(1) Added method checkParam() that does the checking you did in the action performed method.


(1.1) The code in ActionPerformed to reflect above change.


(2) Added the above try - catch clause.


(3) The method 'chkerr()' calls a new method to display the error, so as to seperate the GUI from the other.


(3.1) Updated chkerr() to reflect above, and call the method when an error occurs, instead of waiting until the end of the method to deal with the error. Just semantic changes.





The other thing to remember is that in c++ the 'string' class has a lower case s, not an upper case S like in Java :)





Here is the updated code. Starting with the class ExitListener, the code above that was not changed.





public class ExitListener implements ActionListener


{


public void actionPerformed(ActionEvent ae)


{


System.exit(0);


}


}





public void actionPerformed(ActionEvent ae)


{


total = 0;


try


{


date = Integer.parseInt(dat.getText()...


month = Integer.parseInt(mon.getText()...


year = Integer.parseInt(yea.getText()...


}


catch (NumberFormatException ex)


{


// Inform user of error, reset fields as neccessary, etc.


}





chkParam();


}





public void chparam()


{


int i, ly = 0;





if (ly == leapYear(year) %26amp;%26amp; month %26gt; 2)


{


total += 1;


}





chkerr();





for(i = styear;i %26lt; year;i++)


{


ly = 0;


ly = leapYear(i);


total += 365 + ly;


}





for(i = 1;i %26lt; month;i++)


total += months[i-1];





total += date;





tot = total % 7;


String str = "";


switch(tot)


{


case 1 : str = "Monday";


break;


case 2 : str = "Tuesday";


break;


case 3 : str = "Wednesday";


break;


case 4 : str = "Thursday";


break;


case 5 : str = "Friday";


break;


case 6 : str = "Saturday";


break;


case 0 : str = "Sunday";


break;


}


res.setText(str);


}





public int leapYear(int lyear)


{


if(((lyear%4==0)%26amp;%26amp;(lyear%100!=...


{


return 1;


}


return 0;


}





public void chkerr()


{





if(date %26lt; 1 || date %26gt; 31)


{


showMessage("Invalid Date");


}


if(month %26lt; 1 || month %26gt; 12)


{


showMessage("Invalid Month");


}


if(year %26lt; 1)


{


showMessage("Invalid Year");


}


if(month == 2)


{


if(leapYear(year))


{


if(date %26gt; 29)


{


showMessage("Invalid Date(For February)");


}


}


else if(date %26gt; 28)


{


showMessage("Invalid Date(For February)");


}


}


}





public void showMessage(String msg)


{


JOptionPane.showMessageDialog(...


System.exit(0);


}





public static void main(String[] args)


{


new Cal();


}


}


No comments:

Post a Comment