Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How Do I: Convert to Java

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question How Do I: Convert to Java

    Hi buddy, do you know how to convert this C++ code into Java? I'm still fairly new to Java, hope you can guide me
    Microsoft Visual C++ 2010

    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    template<typename T>
    class myShape{
    protected:
    T height, width;
    };

    template<typename T>
    class CSquare : public myShape<T>
    {
    public:
    CSquare(int height, int width){
    this->height = height;
    this->width = width;
    }

    T area(){
    return this->height * this->width;
    }
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
    CSquare<int> *s = new CSquare<int>(3,4);
    cout<<s->area();
    }

    -:
    ...by the way, if you familiar with C++, please help me here to fix the Turbo C++ 4.5 code. Thanks friends
    Last edited by mexists; November 28th, 2011 at 10:34 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How Do I: Convert to Java

    Start out by converting the program to English. What does it do? Break it down into small steps- as small as possible. Then convert that to a Java program.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    mexists (November 28th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Re: How Do I: Convert to Java

    okay, so it will be like this right?
    class myShape<T>{
    	protected T height, width;
    }
     
    class CSquare<T> extends myShape<T>
    {
    	public CSquare(T height, T width){
    		this.height = height;
    		this.width = width;
    	}
     
    	T area(){
    		return this.height * this.width;   // error: operator * cannot be applied to T,T
    	}	
    }
     
    public class main{
     
    	public static void main (String[] args) {
     
    		//...	
     
    	}
    }

    but there are still have an error there..
    return this.height * this.width;

    i get this error message:
    operator * cannot be applied to T,T

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How Do I: Convert to Java

    Seems pretty reasonable to me. You aren't guaranteeing that height or width are Numbers, so you can't multiply them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    mexists (November 28th, 2011)

  7. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile Re: How Do I: Convert to Java

    hmm... how? add more statement in the main method?
    public class main{
     
    	public static void main (String[] args) {
     
    		CSquare<Integer> s = new CSquare<Integer>(5,5);
    		System.out.println (s.area());
    	}
    }

    if so, I still get the same error...
    Last edited by mexists; November 28th, 2011 at 12:14 PM.

  8. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How Do I: Convert to Java

    No, because your CSquare class still has no idea what that type T is going to be. What types of values do you want to store as height and width?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How Do I: Convert to Java

    Nevermind, looks like I'm just wasting my time anyway.

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/51912-how-do-i-convert-java.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #8
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How Do I: Convert to Java

    C++ templates are far more flexible and can do things like what you want to do because it will generate specialized classes. Java, on the other hand, will not generate any specialized classes. Rather, generics is implemented with a technique called type erasure and exist to improve type safety (since you dont have to make as many casts). What this means is that you cannot use operators like * on generic types in Java because of how generics are implemented under the hood.
    C++ templates
    Java generics

    EDIT:

    In the case of your code... Java has no clue what the type of a generic type is at runtime. So when you multiply two generic variables it would have been compiled to multiplying two instances of Object, if the compiler did not realize that would be an error. That is why your code wont compile.
    Last edited by Kerr; November 28th, 2011 at 06:41 PM.

  11. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How Do I: Convert to Java

    okay, i get it! thanks for your time guys.. i'm sorry if i'd vexatious you.. i'm really really sorry...

    Problem Solved!

Similar Threads

  1. How to convert java executed pgm in eclipse to excel sheet..
    By rajeshwari in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 17th, 2011, 07:32 AM
  2. Convert C++ to Java ?
    By doaa in forum Java Theory & Questions
    Replies: 1
    Last Post: January 15th, 2011, 10:52 AM
  3. Convert C/C++ code to Java
    By cristianll in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 14th, 2009, 02:43 AM
  4. Convert from 'C' to "java" (switch)
    By chronoz13 in forum Loops & Control Statements
    Replies: 7
    Last Post: October 6th, 2009, 08:20 PM
  5. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM