Computer Science Canada

Methods Java to Python help?

Author:  Kenster102.5 [ Mon Nov 02, 2009 10:21 am ]
Post subject:  Methods Java to Python help?

I have recently gotten started into Python, and am still very new at it, but I already have programing knowledge in Java and Turing.
My question is I have this Java program using Instance and Constructor methods, and am trying to re-write it in Python, but I really can't figure out what I am doing wrong.

Java Code:

Java:
import java.awt.*;
public class Fraction
{
 
//Instance Methods learned October 14.
 
//Creating Objects learned October 7.
  public static void main(String[]args)
  {
    Fraction f = new Fraction()
    f.num = 2;
    f.den = 3;
   
//     f's size.
    double s = f.size();
   
    System.out.println(f.num + f.den);
    System.out.println(s);
   
  }
 
  class Fraction
  {
    int num;
    int den;
   
    public Fraction larger (Fraction other)
    {
      if(this.size() >= other.size())
        return this;
      else
        return other;
    }
    public double size()
    {
      return Math.abs((double)num/den);
    }
  }
}



Python Code:
Python:
import math
def MainFraction():
    f = Fraction()
    f.num = 2
    f.den = 3
    s = f.size()
    print (f.num + f.den)
    println (s)

class Fraction:
    def __init__(self):
        num
        den

    def size():
        return math.fabs(num/den)


So what must I do differently?
Thanks
Ken

Author:  wtd [ Mon Nov 02, 2009 5:40 pm ]
Post subject:  RE:Methods Java to Python help?

It's worth noting that nowhere do you set up the num and den instance variables.

Author:  Kenster102.5 [ Tue Nov 03, 2009 7:13 am ]
Post subject:  Re: RE:Methods Java to Python help?

wtd @ Mon Nov 02, 2009 5:40 pm wrote:
It's worth noting that nowhere do you set up the num and den instance variables.


Ah, but sir they are stated as instance fields for the class Fraction.

Author:  wtd [ Tue Nov 03, 2009 11:48 am ]
Post subject:  RE:Methods Java to Python help?

code:
class Foo(object):
    def __init__(self, a):
        self.a = a

b = Foo(42)

print b.a

Author:  Kenster102.5 [ Wed Nov 04, 2009 10:03 am ]
Post subject:  Re: RE:Methods Java to Python help?

wtd @ Tue Nov 03, 2009 11:48 am wrote:
code:
class Foo(object):
    def __init__(self, a):
        self.a = a

b = Foo(42)

print b.a


Okay? Do I always have to do self.a = a or self.num = num for instance methods in Python, I can't just do num = num or num?

My second question:
pytho:
import math
class MainFraction():
    f = Fraction(2,3)
    print (f.num)
    print (f.den)
    s = Size()
    print (s)

class Fraction(object):
    def __init__(self,num,den):
       self.num = num
       self.den = den

class Size(num,den):
      return math.fabs(num/den)


So what do I need to do for the size method to work with the main method? Since it says that the return is outside of the method, also does Python cast automatically?
Ken

Author:  wtd [ Wed Nov 04, 2009 10:24 am ]
Post subject:  RE:Methods Java to Python help?

Have you read any Python tutorials?

Author:  [Gandalf] [ Wed Nov 04, 2009 10:42 pm ]
Post subject:  RE:Methods Java to Python help?

Indeed, it seems like you're a Java coder who just picked up the Python library documentation and started piecing together code. I could be wrong. Smile

Anyway, you'll definitely want to look into a step-by-step guide on OOP in Python.

Also, Python does not "cast automatically", take for instance:
code:
foo = 5 + "2"

Is foo a string or an integer? Is it neither?

Author:  Kenster102.5 [ Thu Nov 05, 2009 11:04 am ]
Post subject:  Re: RE:Methods Java to Python help?

[quote="[Gandalf] @ Wed Nov 04, 2009 10:42 pm"]Indeed, it seems like you're a Java coder who just picked up the Python library documentation and started piecing together code. I could be wrong. Smile

Anyway, you'll definitely want to look into a step-by-step guide on OOP in Python.

Also, Python does not "cast automatically", take for instance:
code:
foo = 5 + "2"

Is foo a string or an integer? Is it neither?[/quote]

Well yes you are totally right, I am a Java Coder who just picked up Python, because it was introduced unofficially into my class cirriculum by another student in my class. So I feel that I should at least try to explore it.

So what did I do wrong with calling my Size object?

Author:  wtd [ Thu Nov 05, 2009 12:16 pm ]
Post subject:  RE:Methods Java to Python help?

Is "Size" perhaps meant to be a function?

Author:  Kenster102.5 [ Thu Nov 05, 2009 5:58 pm ]
Post subject:  Re: RE:Methods Java to Python help?

wtd @ Thu Nov 05, 2009 12:16 pm wrote:
Is "Size" perhaps meant to be a function?


Yes it is supposed to be a function, I just noticed that myself. Silly me. I will reply back tomorrow, since I am busy right now.

Author:  Kenster102.5 [ Wed Nov 11, 2009 12:35 pm ]
Post subject:  Re: Methods Java to Python help?

I am sorry for not having such a quick reply, but now what should I do that would fit this scenario with the Size method.


: