Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Plz help in writing java equivalent code for this c++ code. A real challege. (http://www.go4expert.com/forums/plz-help-writing-java-equivalent-code-t17964/)

simsim00 12Jun2009 20:33

Plz help in writing java equivalent code for this c++ code. A real challege.
This is the C++ code for the problem given below. But since i'm newbie to java, dont know how to write equivalent Java code. If you people can help, plz do so.

Code for Login page in C++

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
string readString(ifstream&);
int main(int argc, char *argv[])
char user[80];
char pass[80];
string line_in;
string::size_type pos;
ifstream fp_in;
int count = 0;
string user_pass[100][2];
if(fp_in == NULL)
cout << "Could not open user file, exiting." << endl;
return -1;
if(line_in.length() > 0)
pos = line_in.find("\t",0);
if( pos != string::npos)
user_pass[count][0] = line_in.substr(0,pos);
user_pass[count][1] = line_in.substr(pos+1);
if(!count > 0)
cout << "Userlist is empty, exiting" << endl;
return -2;
int logged_in=0;
cout << "Enter user name: ";
cin >> user;
cout << "Enter password: ";
cin >> pass;
int found = 0;
for(int x=0;x<count && !found;x++)
if(user_pass[x][0].compare(user) == 0)
found = 1;
if(user_pass[x][1].compare(pass) == 0)
found = 1;
logged_in = 1;
cout << "Invalid password!" << endl;
cout << "Invalid username!" << endl;
cout << "Logged in successfully!" << endl;
return 0;
string readString(ifstream& reader)
char buffer[256];
string str1 = buffer;
return str1;

Writing a maintainable code makes the product with High Quality. Here you are given a scenario where you have to write a maintainable code for the given problem.


Virtual Solutions is an IT solution provider company. It has hired you as a Software Engineer. Your job is to write code, but at the same it should be of High Quality. Your Team Lead has asked you to write a very small code of user login functionality. The code should be of High Quality.

You are required to

a. Write correct code for the user login in Java following the guidelines for writing code
b. Make use of Hungarian Notation for coding purpose

c. Make use of Comments in the Code where required necessary.

xpi0t0s 14Jun2009 03:53

Re: Plz help in writing java equivalent code for this c++ code. A real challege.
The question is based on the invalid premise that rewriting the code in some other language will make it "maintainable". Maintainable code is about how you write it, using clear formatting, sensible variable names, plenty of commenting and above all use of simple algorithms with complications ONLY where necessary (and extra-well commented when that happens) so it's clear what's going on.

Using those rules you can make maintainable code in C, assembler, even Brainf*ck, and by ignoring those rules you can make unmaintainable code in Java, Cobol, ADA and so on. There is no such thing as a language that enforces maintainability.

Now if they just want you to rewrite it in Java that's fine, but they shouldn't be spouting nonsense that this is somehow about maintainability.

Anyway, this looks like coursework/project, so we won't do it for you but we will help. How far have you got and where are you stuck? If you've been on a Java course then you already know how to write Java code and it's then just a case of using the C code as a detailed spec for the Java code that you will write.

simsim00 14Jun2009 11:14

Re: Plz help in writing java equivalent code for this c++ code. A real challege.
Now plz tell me is this code correct according to the given problem. Plz help me in bringing high quality to this code. I.E Hungarian Notation and use of comments.

HTML Code:

<title>Khalid Mahmood  </title>

 <p> <h3><center>Please enter your user name and

 <form  action="process2.jsp "    method = "post" >
 <center><input type = "text" name=
 <center><input type = "password" name =
 <center><input type="submit"    name="Submit"



HTML Code:

<%@ page import="java.util.*" %>
<jsp:useBean id="idHandler" class="foo.Login" scope="request">
<jsp:setProperty name="idHandler" property="*"/>

  if (idHandler.validate()) {

<jsp:forward page="success.jsp"/>
  }  else {

<jsp:forward page="retry.jsp"/>


package foo;
 import java.sql.*;

 public class Login {

  private String username = "";
 private String password = "";

  public Login() {

  public void setUsername(String username) {
    this.username = username;

  public void setPassword(String password) {
    this.password = password;

 public boolean  authenticate(String username2,
 String password2) {
  String query="select * from Registration;";
  String DbUserName="";
  String DbPassword="";
  String finalUser="";
  try {
  Statement stat=con.createStatement();
  ResultSet rst=stat.executeQuery(query);



  if (username2.equals(DbUserName) &&
 password2.equals(DbPassword)) {


 return true;
 }catch(Exception e){

 return false;

xpi0t0s 14Jun2009 14:45

Re: Plz help in writing java equivalent code for this c++ code. A real challege.
Use code blocks when posting code please; this retains the formatting.

So are you saying that you don't know what Hungarian notation is, and that you don't know where to put comments and how to write them?

All times are GMT +5.5. The time now is 17:51.