java to c++

S

spl

Anyone who knows java and C++, can convert this java prog into c++.
This program is for sorting array of objects.

// objectSort.java
// demonstrates sorting objects (uses insertion sort)
// to run this program: C>java ObjectSortApp
////////////////////////////////////////////////////////////////
class Person
{
private String lastName;
private String firstName;
private int age;
//-----------------------------------------------------------
public Person(String last, String first, int a)
{ // constructor
lastName = last;
firstName = first;
age = a;
}
//-----------------------------------------------------------
public void displayPerson()
{
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Age: " + age);
}
//-----------------------------------------------------------
public String getLast() // get last name
{ return lastName; }
} // end class Person
////////////////////////////////////////////////////////////////
class ArrayInOb
{
private Person[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArrayInOb(int max) // constructor
{
a = new Person[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
// put person into array
public void insert(String last, String first, int age)
{
a[nElems] = new Person(last, first, age);
nElems++; // increment size
}
//--------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
a[j].displayPerson(); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void insertionSort()
{
int in, out;

for(out=1; out<nElems; out++) // out is dividing line
{
Person temp = a[out]; // remove marked person
in = out; // start shifting at out

while(in>0 && // until smaller one found,
a[in-1].getLast().compareTo(temp.getLast())>0)
{
a[in] = a[in-1]; // shift item to the right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()
//--------------------------------------------------------------
} // end class ArrayInOb
////////////////////////////////////////////////////////////////
class ObjectSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayInOb arr; // reference to array
arr = new ArrayInOb(maxSize); // create the array

arr.insert("Evans", "Patty", 24);
arr.insert("Smith", "Doc", 59);
arr.insert("Smith", "Lorraine", 37);
arr.insert("Smith", "Paul", 37);
arr.insert("Yee", "Tom", 43);
arr.insert("Hashimoto", "Sato", 21);
arr.insert("Stimson", "Henry", 29);
arr.insert("Velasquez", "Jose", 72);
arr.insert("Vang", "Minh", 22);
arr.insert("Creswell", "Lucinda", 18);

System.out.println("Before sorting:");
arr.display(); // display items

arr.insertionSort(); // insertion-sort them

System.out.println("After sorting:");
arr.display(); // display them again
} // end main()
} // end class ObjectSortApp
////////////////////////////////////////////////////////////////
 
H

Hendrik Maryns

spl schreef:
Anyone who knows java and C++, can convert this java prog into c++.

Yes. So?

H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFHu/bve+7xMGD3itQRApszAJ9cwSPf1cJJdVNuiEqqDfo0B5HwrwCfcgey
Fm8fT5fi1UHAxVX9QRw6GNY=
=aTS4
-----END PGP SIGNATURE-----
 
U

Ulrich Eckhardt

spl said:
Anyone who knows java and C++, can convert this java prog into c++.

I could, but I don't see a reason to do your homework. Mostly, it's just a
translation along these lines:
- String -> std::string.
- Person[] -> std::vector<Person>

Note that if you assume that a 'Person' object is copyable, i.e. that it is
a record with data for a person, you will not need to use 'new', which is
used differently in C++ than in Java. Lastly, you could save some effort
using std::sort, unless you are bound by the assignment to use insertion
sort.

Otherwise, I would suggest you start hacking and then, if problems come up
with the C++ code, post to a C++ newsgroup.

cheers

Uli
 
S

spl

yes, I know well about C++, but I just confused in this part,

int in, out;

for(out=1; out<nElems; out++) // out is dividing line
{
Person temp = a[out]; // remove marked person
in = out; // start shifting at out

while(in>0 && // until smaller one found,
a[in-1].getLast().compareTo(temp.getLast())>0)
{
a[in] = a[in-1]; // shift item to the right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for

ie., assigning objects in C++, how will do this?
 
R

Roedy Green

Anyone who knows java and C++, can convert this java prog into c++.
This program is for sorting array of objects.

Are you asking someone to do this for you? Are you just commenting on
the similarity of Java and C++?
 
A

Arne Vajhøj

Ulrich said:
spl said:
Anyone who knows java and C++, can convert this java prog into c++.

I could, but I don't see a reason to do your homework. Mostly, it's just a
translation along these lines:
- String -> std::string.
- Person[] -> std::vector<Person>

It is not obvious to me that a Java array should be converted
to a C++ collection (if you forgive me for using the Java term).

Arne
 
U

Ulrich Eckhardt

Arne said:
Ulrich said:
spl said:
Anyone who knows java and C++, can convert this java prog into c++.

I could, but I don't see a reason to do your homework. Mostly, it's just
a translation along these lines:
- String -> std::string.
- Person[] -> std::vector<Person>

It is not obvious to me that a Java array should be converted
to a C++ collection (if you forgive me for using the Java term).

This is getting a bit OT here, but anyway....

I'm not a Java pro, rather a beginner myself, but IIUC Java arrays can hold
a variable number of elements, i.e. you can insert and delete elements,
right? That is why I suggested using std::vector as replacement in C++
(though std::list and std::deque will also work). Using arrays in C++ means
you have to specify the number of elements either at compile-time or when
invoking new. However, the latter has the distinct disadvantage that you
need to do manual resource management which std::vector doesn't suffer
from.

What would you have taken?

Uli
 
L

Lew

Ulrich said:
I'm not a Java pro, rather a beginner myself, but IIUC Java arrays can hold
a variable number of elements, i.e. you can insert and delete elements,
right? That is why I suggested using std::vector as replacement in C++
(though std::list and std::deque will also work). Using arrays in C++ means
you have to specify the number of elements either at compile-time or when
invoking new. However, the latter has the distinct disadvantage that you
need to do manual resource management which std::vector doesn't suffer
from.

from
 
A

Arne Vajhøj

Ulrich said:
Arne said:
Ulrich said:
spl wrote:
Anyone who knows java and C++, can convert this java prog into c++.
I could, but I don't see a reason to do your homework. Mostly, it's just
a translation along these lines:
- String -> std::string.
- Person[] -> std::vector<Person>
It is not obvious to me that a Java array should be converted
to a C++ collection (if you forgive me for using the Java term).

This is getting a bit OT here, but anyway....

I'm not a Java pro, rather a beginner myself, but IIUC Java arrays can hold
a variable number of elements, i.e. you can insert and delete elements,
right? That is why I suggested using std::vector as replacement in C++
(though std::list and std::deque will also work). Using arrays in C++ means
you have to specify the number of elements either at compile-time or when
invoking new. However, the latter has the distinct disadvantage that you
need to do manual resource management which std::vector doesn't suffer
from.

No - Java array are fixed size like in C/C++.

The equivalent of vector<Person> in Java would be
ArrayList said:
What would you have taken?

For a 1:1 translation: array.

Both C++ and Java probably should use a collection.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top