14 Replies - 2618 Views - Last Post: 28 September 2012 - 05:27 AM Rate Topic: -----

#1 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Help with converting awesome conversion classes from Python to Java

Posted 25 September 2012 - 01:38 PM

Greetings! I figured more people in the Python forum would know about Java than the other way around, so I posted here. Please beat me mercilessly if I am mistaken.


I'm trying to port my Python class to Java on Android, but I'm not sure how to achieve exactly what I want.

I need to create a dictionary-like object which uses a class as a key and contains subclass member methods as values.
In Python I have:
        self.convert = {   Inch:self.toInch,
                           Foot:self.toFoot,
                           Yard:self.toYard,
                           Mile:self.toMile,
                           Centimeter:self.toCentimeter,
                           Meter:self.toMeter,
                           Kilometer:self.toKilometer}



where Inch is a class name and self.toInch is a method defined in the current object which is a subclass of BaseUnit.
I think I need to use a HashMap in Java, but I can't wrap my head around the types of all these things and so suffer teremendously while searching through a 3 page long error report in a tiny LogCat window that says something like NullPointerException, ClassNotFoundException, YouReallySuckAtJavaException, etc.


Additionally, the magic I found in Python with my Units class is
return self.units[to_unit.getName()](result)
which allows me to call the subclass member method for a result, look up a class by name, instantiate it with a parameter (result), and return the instantiated object. How can I do this in Java?


Thanks for any assistance on this exciting task! Android is awesome, but java less so!



Units class
class Units:
    units = {"Inch":Inch, "Yard":Yard, "Foot":Foot, "Mile":Mile, "Centimeter":Centimeter, "Meter":Meter, "Kilometer":Kilometer}
    
    def convert(self, from_unit, to_unit):
        to_unit_type = to_unit.__type__()
        result = from_unit.convert[to_unit_type]()
        print "Conversion: {0} {1} is {3} {2}".format(from_unit.getValue(), from_unit.getName(), to_unit.getName(), result)
        return self.units[to_unit.getName()](result)














factors.py
#Inches
inch_foot = 0.08333333333333333
foot_inch = 12.0

inch_yard = 0.027777777777777776
yard_inch = 36.0

inch_mile = 0.000015782828282828283
mile_inch = 63360.0

inch_centimeter = 2.54
centimeter_inch = 0.39370078740157477

inch_meter = 0.0254
meter_inch = 39.37007874015748

inch_kilometer = 0.0000254
kilometer_inch = 39370.07874015748

#Feet
foot_inch = 12.0
inch_foot = 0.08333333333333333

foot_yard = 0.3333333333333333
yard_foot = 3.0

foot_mile = 0.0001893939393939394
mile_foot = 5280.0

foot_centimeter = 30.48
centimeter_foot = 0.03280839895013123

foot_meter = 0.3048
meter_foot = 3.2808398950131234

foot_kilometer = 0.0003048
kilometer_foot = 3280.839895013123

#Yards
yard_inch = 36.0
inch_yard = 0.027777777777777776

yard_foot = 3.0
foot_yard = 0.3333333333333333

yard_mile = 0.0005681818181818182
mile_yard = 1760

yard_centimeter = 91.44
centimeter_yard = 0.010936132983377079

yard_meter = 0.9144
meter_yard = 1.0936132983377078

yard_kilometer = 0.0009144
kilometer_yard = 1093.6132983377078

#Miles
mile_inch = 63360.0
inch_mile = 0.000015782828282828283

mile_foot = 5280.0
foot_mile = 0.0001893939393939394

mile_yard = 1760.0
yard_mile = 0.0005681818181818182

mile_centimeter = 160934.4
centimeter_mile = 0.00000621371192237334

mile_meter = 1609.344
meter_mile = 0.000621371192237334

mile_kilometer = 1.6093439999999998
kilometer_mile = 0.621371192237334

#Centimeters
centimeter_inch = 0.39370078740157477
inch_centimeter = 2.54

centimeter_foot = 0.03280839895013123
foot_centimeter = 30.48

centimeter_yard = 0.010936132983377079
yard_centimeter = 91.44

centimeter_mile = 0.00000621371192237334
mile_centimeter = 160934.4

centimeter_meter = 0.01
meter_centimeter = 100.0

centimeter_kilometer = 0.00001
kilometer_centimeter = 100000.0

#Meters
meter_inch = 39.37007874015748
inch_meter = 0.0254

meter_foot = 3.2808398950131234
foot_meter = 0.3048

meter_yard = 1.0936132983377078
yard_meter = 0.9144

meter_mile = 0.0006213711922373339
mile_meter = 1609.344

meter_centimeter = 100.0
centimeter_meter = 0.01

meter_kilometer = 0.001
kilometer_meter = 1000.0

#Kilometers
kilometer_inch = 39370.07874015748
inch_kilometer = 0.0000254

kilometer_foot = 3280.839895013123
foot_kilometer = 0.0003048

kilometer_yard = 1093.6132983377078
yard_kilometer = 0.0009144

kilometer_mile = 0.621371192237334
mile_kilometer = 1.609344

kilometer_centimeter = 100000.0
centimeter_kilometer = 0.00001

kilometer_meter = 1000.0
meter_kilometer = 0.001



Unit.py
import factors

class BaseUnit(object):
    def __init__(self, value, name):
        self.value = value
        self.name = name
        self.convert = {   Inch:self.toInch,
                           Foot:self.toFoot,
                           Yard:self.toYard,
                           Mile:self.toMile,
                           Centimeter:self.toCentimeter,
                           Meter:self.toMeter,
                           Kilometer:self.toKilometer}
        
    def getName(self):
        return self.name
    def getValue(self):
        return self.value
    def setValue(self, value=None):
        self.value = value
        

class Inch(BaseUnit):
    def __init__(self, value=None):
        super(Inch, self).__init__(value, "Inch")
        
    def __type__(self):
        return Inch
    
    def toInch(self):
        return self.getValue()
        
    def toFoot(self):
        return self.getValue()/factors.foot_inch
    
    def toYard(self):
        return self.getValue()/factors.yard_inch
    
    def toMile(self):
        return self.getValue()/factors.mile_inch
    
    def toCentimeter(self):
        return self.getValue()/factors.centimeter_inch
    
    def toMeter(self):
        return self.getValue()/factors.meter_inch
    
    def toKilometer(self):
        return self.getValue()/factors.kilometer_inch
    
class Foot(BaseUnit):
    def __init__(self, value=None):
        super(Foot, self).__init__(value, "Foot")
        
    def __type__(self):
        return Foot
    
    def toInch(self):
        return self.getValue()/factors.inch_foot
    
    def toFoot(self):
        return self.getValue()
    
    def toYard(self):
        return self.getValue()/factors.yard_foot
    
    def toMile(self):
        return self.getValue()/factors.mile_foot
    
    def toCentimeter(self):
        return self.getValue()/factors.centimeter_foot
    
    def toMeter(self):
        return self.getValue()/factors.meter_foot
    
    def toKilometer(self):
        return self.getValue()/factors.kilometer_foot

class Yard(BaseUnit):
    def __init__(self, value=None):
        super(Yard, self).__init__(value, "Yard")
        
    def __type__(self):
        return Yard
        
    def toInch(self):
        return self.getValue()/factors.inch_yard
    
    def toFoot(self):
        return self.getValue()/factors.foot_yard
    
    def toYard(self):
        return self.getValue()
    
    def toMile(self):
        return self.getValue()/factors.mile_yard
    
    def toCentimeter(self):
        return self.getValue()/factors.centimeter_yard
    
    def toMeter(self):
        return self.getValue()/factors.meter_yard
    
    def toKilometer(self):
        return self.getValue()/factors.kilometer_yard
    
class Mile(BaseUnit):
    def __init__(self, value=None):
        super(Mile, self).__init__(value, "Mile")
    
    def __type__(self):
        return Mile
    
    def toInch(self):
        return self.getValue()/factors.inch_mile
    
    def toFoot(self):
        return self.getValue()/factors.foot_mile
    
    def toYard(self):
        return self.getValue()/factors.yard_mile
    
    def toMile(self):
        return self.getValue()
    
    def toCentimeter(self):
        return self.getValue()/factors.centimeter_mile
    
    def toMeter(self):
        return self.getValue()/factors.meter_mile
    
    def toKilometer(self):
        return self.getValue()/factors.kilometer_mile

class Centimeter(BaseUnit):
    def __init__(self, value=None):
        super(Centimeter, self).__init__(value, "Centimeter")
        
    def __type__(self):
        return Centimeter
    
    def toInch(self):
        return self.getValue()/factors.inch_centimeter
    
    def toFoot(self):
        return self.getValue()/factors.foot_centimeter
    
    def toYard(self):
        return self.getValue()/factors.yard_centimeter
    
    def toMile(self):
        return self.getValue()/factors.mile_centimeter
    
    def toCentimeter(self):
        return self.getValue()
    
    def toMeter(self):
        return self.getValue()/factors.meter_centimeter
    
    def toKilometer(self):
        return self.getValue()/factors.kilometer_centimeter

class Meter(BaseUnit):
    def __init__(self, value=None):
        super(Meter, self).__init__(value, "Meter")
    
    def __type__(self):
        return Meter
    
    def toInch(self):
        return self.getValue()/factors.inch_meter
    
    def toFoot(self):
        return self.getValue()/factors.foot_meter
    
    def toYard(self):
        return self.getValue()/factors.yard_meter
    
    def toMile(self):
        return self.getValue()/factors.mile_meter
    
    def toCentimeter(self):
        return self.getValue()/factors.centimeter_meter
    
    def toMeter(self):
        return self.getValue()
    
    def toKilometer(self):
        return self.getValue()/factors.kilometer_meter
    
class Kilometer(BaseUnit):
    def __init__(self, value=None):
        super(Kilometer, self).__init__(value, "Kilometer")
        
    def __type__(self):
        return Kilometer
    
    def toInch(self):
        return self.getValue()/factors.inch_kilometer
    
    def toFoot(self):
        return self.getValue()/factors.foot_kilometer
    
    def toYard(self):
        return self.getValue()/factors.yard_kilometer
    
    def toMile(self):
        return self.getValue()/factors.mile_kilometer
    
    def toCentimeter(self):
        return self.getValue()/factors.centimeter_kilometer
    
    def toMeter(self):
        return self.getValue()/factors.meter_kilometer
    
    def toKilometer(self):
        return self.getValue()
    
class Units:
    units = {"Inch":Inch, "Yard":Yard, "Foot":Foot, "Mile":Mile, "Centimeter":Centimeter, "Meter":Meter, "Kilometer":Kilometer}
    
    def convert(self, from_unit, to_unit):
        to_unit_type = to_unit.__type__()
        result = from_unit.convert[to_unit_type]()
        print "Conversion: {0} {1} is {3} {2}".format(from_unit.getValue(), from_unit.getName(), to_unit.getName(), result)
        return self.units[to_unit.getName()](result)
        
        
if __name__ == "__main__":
    i = Inch(1)
    f = Foot(1)
    y = Yard(1)
    m = Mile(1)
    cm = Centimeter(1)
    me = Meter(1)
    km = Kilometer(1)
    
    units = Units()
    units.convert(i, Yard())
    units.convert(y, Inch())
    units.convert(f, Inch())
    units.convert(f, Yard())
    units.convert(m, Inch())
    units.convert(m, Foot())
    units.convert(m, Yard())
    units.convert(km, Inch())
    units.convert(cm, Kilometer())
    units.convert(km, Mile())
    units.convert(m, Meter())
    units.convert(i, Kilometer())



Is This A Good Question/Topic? 0
  • +

Replies To: Help with converting awesome conversion classes from Python to Java

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7630
  • View blog
  • Posts: 12,866
  • Joined: 19-March 11

Re: Help with converting awesome conversion classes from Python to Java

Posted 25 September 2012 - 01:43 PM

Why not jython?
Was This Post Helpful? 0
  • +
  • -

#3 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Help with converting awesome conversion classes from Python to Java

Posted 25 September 2012 - 01:54 PM

Are you saying I can create Java classes from Python with Jython that I can use in my Android application written in Java?
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help with converting awesome conversion classes from Python to Java

Posted 25 September 2012 - 01:59 PM

Does it need to be Java or just run on an Android capable phone?

If the latter, you want to start from here probably: http://code.google.c...roid-scripting/

This post has been edited by Simown: 25 September 2012 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Help with converting awesome conversion classes from Python to Java

Posted 25 September 2012 - 02:43 PM

To understand specifically what I need to do, consider the following code... (Not to scale)

package com.example.PythonMadeIt;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.util.HashMap;
import com.example.PythonMadeIt.MyCrazyUnitConversionClass

public class HelloWorldActivity extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        testclass t = new testclass();
        Log.d("DEBUG", "Test: " + t.value);
        HashMap h = new HashMap();
        h.put(t.name, testclass.class);
        h.put("test2", 24);
		Log.d("DEBUG", "What is this?: " + h.get(t.name));
		Log.d("DEBUG", "And this?: " + h.get(t.name).getClass());
		Log.d("DEBUG", "How bout this?: " + h.get("test2"));

       MyCrazyUnitConversionClass mcucc = new MyCrazyUnitConversionClass();
       mcucc.dosomecoolstuff(), etc, and then some.
    }

    public void onclick(View v) {
    }
}




As you can see, I could not care less what it is that ends up being useful to me, just that I can import it into an app I'm working on and use it to great effect.



Also, for more clarification, you can see my tarded attempt to implement this as a set of Java classes WHICH KIND OF WORKS, but ultimately I end up wishing I could get a handle on an actual object instead of having to track states as strings or indexes or whatever. I want to be a better developer than that.


The ultimate hardship of my approach is having to do things like:
ScopeMeasurementValue = unit.new MIL(unit.new MOA(UserScopeMeasurementEntry));

instead of things like
units.convert(i, Yard())




which is quite ugly, probably inefficient, and exceedingly annoying.

Units.java
package com.example.uitrials;
import java.text.DecimalFormat;
import android.util.Log;
//7 units
//6 constructors per unit
public class Unit{
	Conversion c = new Conversion();
	DecimalFormat fmt = new DecimalFormat("0.000");
	double value;
	private String name = "Unit";
	public Unit(){
	}
	public Unit(double val){
		value = val;
	}
	public Unit(double value, String from, String to){
		
	}
	public double Value(){
		return Double.parseDouble(fmt.format(value));
	}
	public MIL toMil(Unit u){
		return new MIL(u);
	}
	public MOA toMoa(Unit u){
		return new MOA(u);
	}
	public Inch toInch(Unit u){
		return new Inch(u);
	}
	public Foot toFoot(Unit u){
		return new Foot(u);
	}
	public Yard toYard(Unit u){
		return new Yard(u);
	}
	public Mile toMile(Unit u){
		return new Mile(u);
	}
	public Centimeter toCentimeter(Unit u){
		return new Centimeter(u);
	}
	public Meter toMeter(Unit u){
		return new Meter(u);
	}
	public Kilometer toKilometer(Unit u){
		return new Kilometer(u);
	}
	public String getName(){
		return name;
	}
	public class MIL extends Unit{
		double value;
		private String name = "MIL";
		public MIL(){
			setValue(0.00);
		}
		public MIL(double val){
			setValue(val);
		}
		public MIL(Unit unit){
			setValue(unit.Value());
		}
		public MIL(MIL mil){
			setValue(mil.Value());
		}
		public MIL(MOA moa){
			Log.d("MIL_MOA_constructor", "Converting from MOA to MIL: " + moa.Value() + "moa * " + c.moa_mil + " = " + moa.Value()*c.moa_mil);
			setValue(moa.Value()*c.moa_mil);
		}
		public void setValue(double val){
			Log.d("MIL_setValue", "going from: " + Value() + " to " + val);
			value = val;
		}
		public double Value(){
			return Double.parseDouble(fmt.format(value));
		}
		public String getName(){
			return name;
		}
	}
	public class MOA extends Unit{
		double value;
		private String name = "MOA";
		public MOA(){
			setValue(0.00);
		}
		public MOA(double val){
			setValue(val);
		}
		public MOA(Unit unit){
			setValue(unit.Value());
		}
		public MOA(MOA moa){
			setValue(moa.Value());
		}
		public MOA(MIL mil){
			Log.d("MOA_MIL_constructor", "Converting from MIL to MOA: " + mil.Value() + "mil * " + c.mil_moa + " = " + mil.Value()*c.mil_moa);
			setValue(mil.Value()*c.mil_moa);
		}
		public void setValue(double val){
			Log.d("MOA_setValue", "going from: " + Value() + " to " + val);
			value = val;
		}
		public double Value(){
			return Double.parseDouble(fmt.format(value));
		}
		public String getName(){
			return name;
		}
	}
	public class Inch extends Unit{
		double value;
		private String name = "Inch";
		public Inch(){
			setValue(0.00);
		}
		public Inch(double val){
			setValue(val);
		}
		public Inch(Foot foot){
			setValue(foot.Value()*c.foot_inch);
		}
		public Inch(Yard yard){
			setValue(yard.Value()*c.yard_inch);
		}
		public Inch(Mile mile){
			setValue(mile.Value()*c.mile_inch);
		}
		public Inch(Centimeter centimeter){
			setValue(centimeter.Value()*c.centimeter_inch);
		}
		public Inch(Meter meter){
			setValue(meter.Value()*c.meter_inch);
		}
		public Inch(Kilometer kilometer){
			setValue(kilometer.Value()*c.kilometer_inch);
		}
		public Inch(Unit u){
			setValue(u.Value());
		}
		public void setValue(double newVal){
			value = newVal;
		}
		public double Value(){
			return Double.parseDouble(fmt.format(value));
		}
		public String getName(){
			return name;
		}
	}
	public class Foot extends Unit{
		double value;
		private String name = "Foot";
		public Foot(){
			setValue(0.00);
		}
		public Foot(double val){
			setValue(val);
		}
		public Foot(Inch inch){
			setValue(inch.Value()*c.inch_foot);
		}
		public Foot(Yard yard){
			setValue(yard.Value()*c.yard_foot);
		}
		public Foot(Mile mile){
			setValue(mile.Value()*c.mile_foot);	
		}
		public Foot(Centimeter centimeter){
			setValue(centimeter.Value()*c.centimeter_foot);
		}
		public Foot(Meter meter){
			setValue(meter.Value()*c.meter_foot);
		}
		public Foot(Kilometer kilometer){
			setValue(kilometer.Value()*c.kilometer_foot);
		}
		public Foot(Unit u){
			setValue(u.Value());
		}
		public void setValue(double newVal){
			value = newVal;
		}
		public double Value(){
			return Double.parseDouble(fmt.format(value));
		}
		public String getName(){
			return name;
		}
	}
	public class Yard extends Unit{
		double value;
		private String name = "Yard";
		public Yard(){
			setValue(0.00);
		}
		public Yard(double val){
			setValue(val);
		}
		public Yard(Inch inch){
			setValue(inch.Value()*c.inch_yard);
		}
		public Yard(Foot foot){
			setValue(foot.Value()*c.foot_yard);
		}
		public Yard(Mile mile){
			setValue(mile.Value()*c.mile_yard);
		}
		public Yard(Centimeter centimeter){
			setValue(centimeter.Value()*c.centimeter_yard);
		}
		public Yard(Meter meter){
			setValue(meter.Value()*c.meter_yard);
		}
		public Yard(Kilometer kilometer){
			setValue(kilometer.Value()*c.kilometer_yard);
		}
		public Yard(Unit u){
			setValue(u.Value());
		}
		public void setValue(double newVal){
			value = newVal;
		}
		public double Value(){
			return Double.parseDouble(fmt.format(value));
		}
		public String getName(){
			return name;
		}
	}
	public class Mile extends Unit{
		double value;
		private String name = "Mile";
		public Mile(){
			setValue(0.00);
		}
		public Mile(double val){
			setValue(val);
		}
		public Mile(Inch inch){
			setValue(inch.Value()*c.inch_mile);
		}
		public Mile(Foot foot){
			setValue(foot.Value()*c.foot_mile);
		}
		public Mile(Yard yard){
			setValue(yard.Value()*c.yard_mile);
		}
		public Mile(Centimeter centimeter){
			setValue(centimeter.Value()*c.centimeter_mile);
		}
		public Mile(Meter meter){
			setValue(meter.Value()*c.meter_mile);
		}
		public Mile(Kilometer kilometer){
			setValue(kilometer.Value()*c.kilometer_mile);
		}
		public Mile(Unit u){
			setValue(u.Value());
		}
		public void setValue(double newVal){
			value = newVal;
		}
		public double Value(){
			return Double.parseDouble(fmt.format(value));
		}
		public String getName(){
			return name;
		}
	}
	public class Centimeter extends Unit{
		double value;
		private String name = "Centimeter";
		public Centimeter(){
			setValue(0.00);
		}
		public Centimeter(double val){
			setValue(val);
		}
		public Centimeter(Inch inch){
			setValue(inch.Value()*c.inch_centimeter);
		}
		public Centimeter(Foot foot){
			setValue(foot.Value()*c.foot_centimeter);
		}
		public Centimeter(Yard yard){
			setValue(yard.Value()*c.yard_centimeter);
		}
		public Centimeter(Mile mile){
			setValue(mile.Value()*c.mile_centimeter);
		}
		public Centimeter(Meter meter){
			setValue(meter.Value()*c.meter_centimeter);
		}
		public Centimeter(Kilometer kilometer){
			setValue(kilometer.Value()*c.kilometer_centimeter);
		}
		public Centimeter(Unit u){
			setValue(u.Value());
		}
		public void setValue(double newVal){
			value = newVal;
		}
		public double Value(){
			return Double.parseDouble(fmt.format(value));
		}
		public String getName(){
			return name;
		}
	}
	public class Meter extends Unit{
		double value;
		private String name = "Meter";
		public Meter(){
			setValue(0.00);
		}
		public Meter(double val){
			setValue(val);
		}
		public Meter(Inch inch){
			setValue(inch.Value()*c.inch_meter);
		}
		public Meter(Foot foot){
			setValue(foot.Value()*c.foot_meter);
		}
		public Meter(Yard yard){
			setValue(yard.Value()*c.yard_meter);
		}
		public Meter(Mile mile){
			setValue(mile.Value()*c.mile_meter);
		}
		public Meter(Centimeter centimeter){
			setValue(centimeter.Value()*c.centimeter_meter);
		}
		public Meter(Kilometer kilometer){
			setValue(kilometer.Value()*c.kilometer_meter);
		}
		public Meter(Unit u){
			setValue(u.Value());
		}
		public void setValue(double newVal){
			value = newVal;
		}
		public double Value(){
			return Double.parseDouble(fmt.format(value));
		}
		public String getName(){
			return name;
		}
	}
	public class Kilometer extends Unit{
		double value;
		private String name = "Kilometer";
		public Kilometer(){
			setValue(0.00);
		}
		public Kilometer(double val){
			setValue(val);
		}
		public Kilometer(Inch inch){
			setValue(inch.Value()*c.inch_kilometer);
		}
		public Kilometer(Foot foot){
			setValue(foot.Value()*c.foot_kilometer);
		}
		public Kilometer(Yard yard){
			setValue(yard.Value()*c.yard_kilometer);
		}
		public Kilometer(Mile mile){
			setValue(mile.Value()*c.mile_kilometer);
		}
		public Kilometer(Centimeter centimeter){
			setValue(centimeter.Value()*c.centimeter_kilometer);
		}
		public Kilometer(Meter meter){
			setValue(meter.Value()*c.meter_kilometer);
		}
		public Kilometer(Unit u){
			setValue(u.Value());
		}
		public void setValue(double newVal){
			value = newVal;
		}
		public double Value(){
			return Double.parseDouble(fmt.format(value));
		}
		public String getName(){
			return name;
		}
	}
}





Implementation code:
        ReticleUnitSelectionSpinner = (Spinner) findViewById(R.id.first_row_units);
        ReticleUnitSelectionSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
				ReticleUnitSelectionIndex = arg2;
				Unit UserScopeMeasurementEntry = new Unit(getUserScopeMeasurementEntry());
				switch(ReticleUnitSelectionIndex){
				case MILLIRADIAN:
					ScopeMeasurementValue = unit.new MIL(unit.new MOA(UserScopeMeasurementEntry));
					break;
				case MINUTEOFANGLE:
					ScopeMeasurementValue = unit.new MOA(unit.new MIL(UserScopeMeasurementEntry));
					break;					
				default:
					Log.d("UserScopeMeasurementEntry", "Not doing anything.");
					ScopeMeasurementValue = UserScopeMeasurementEntry;				
				}				
				setUserVisibleScopeMeasurementEntry(ScopeMeasurementValue.Value());
			}


This post has been edited by Python_4_President: 25 September 2012 - 02:49 PM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Help with converting awesome conversion classes from Python to Java

Posted 25 September 2012 - 06:08 PM

You really are working to hard with that Python thingy. You can get basically the same results with something like:
class Unit:
	data = {'centimeter': 1.0,'inch': 2.54,'foot': 30.48,'yard': 91.44,'mile': 160934.4,'meter': 100.0,'kilometer': 100000.0 }
	
	def __init__(self, value, name):
		self.value = value * Unit.data[name]
		self.name = name

	def getName(self): return self.name
	def getValue(self): return self.value / Unit.data[self.name]

	def __toInstance(self, name): return Unit(self.value / Unit.data[name], name)
	def toInch(self): return self.__toInstance('inch')
	def toFoot(self): return self.__toInstance('foot')
	def toYard(self): return self.__toInstance('yard')
	def toMile(self): return self.__toInstance('mile')
	def toCentimeter(self): return self.__toInstance('centimeter')
	def toMeter(self): return self.__toInstance('meter')
	def toKilometer(self): return self.__toInstance('kilometer')

	@staticmethod
	def Inch(value): return Unit(value, 'inch')
	@staticmethod
	def Foot(value): return Unit(value, 'foot')
	@staticmethod
	def Yard(value): return Unit(value, 'yard')
	@staticmethod
	def Mile(value): return Unit(value, 'mile')
	@staticmethod
	def Centimeter(value): return Unit(value, 'centimeter')
	@staticmethod
	def Meter(value): return Unit(value, 'meter')
	@staticmethod
	def Kilometer(value): return Unit(value, 'kilometer')
	
	def __str__(self):
		return "{0} {1}".format(self.getValue(), self.getName())

def test(from_unit, to_unit):
	print "Conversion: {0} {1} is {3} {2}".format(
		from_unit.getValue(), from_unit.getName(), 
		to_unit.getName(), to_unit.getValue())

	
if __name__ == "__main__":
	i = Unit.Inch(1)
	f = Unit.Foot(1)
	y = Unit.Yard(1)
	m = Unit.Mile(1)
	cm = Unit.Centimeter(1)
	me = Unit.Meter(1)
	km = Unit.Kilometer(1)
	
	test(i, i.toYard())
	tests = ((y, 'Inch'), (f, 'Inch'), (f, 'Yard'),
		(m, 'Inch'), (m, 'Foot'), (m, 'Yard'),
		(km, 'Inch'), (cm, 'Kilometer'), (km, 'Mile'),
		(m, 'Meter'), (i, 'Kilometer')
		)
	
	for from_unit, func in tests:
		to_unit = Unit.__dict__['to' + func](from_unit)
		test(from_unit, to_unit)



For unit conversions, you pick one, and only one, unit as 1 and work around it. Meters is usually 1, but i picked centimeters for this.

Your Java could be near identical.

Regardless of how you want to do it, work from the interface. What methods do you want available to you in Java. Also, in Java, an enum would work well for this; one of the few things Python doesn't really have.

Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Help with converting awesome conversion classes from Python to Java

Posted 26 September 2012 - 04:22 AM

You posted Java code! Didn't see it before. Sorry.

Right, the Python code I offered as Java:
import java.text.DecimalFormat;

public class Unit {
	public enum UnitType {
		Centimeter(1.0), Inch(2.54), Foot(30.48),Yard(91.44),
		Mile(160934.4), Meter(100.0), Kilometer(100000);
		
		public final double factor;
		UnitType(double factor) { this.factor = factor; }
	}
	
	private static final DecimalFormat fmt = new DecimalFormat("0.000");
	public final UnitType unitType;
	private final double value;
	
	public Unit(UnitType unitType, double value) {
		this.unitType = unitType;
		this.value = value * unitType.factor;
	}
	
	public String getName() { return unitType.name(); }
	public double getValue() { return value / unitType.factor; }
	
	public Unit convert(UnitType newType) { return new Unit(newType, value / newType.factor); }
	public Unit toInch() { return convert(UnitType.Inch); }
	public Unit toFoot() { return convert(UnitType.Foot); }
	public Unit toYard() { return convert(UnitType.Yard); }
	public Unit toMile() { return convert(UnitType.Mile); }
	public Unit toCentimeter() { return convert(UnitType.Centimeter); }
	public Unit toMeter() { return convert(UnitType.Meter); }
	public Unit toKilometer() { return convert(UnitType.Kilometer); }

	public static Unit Inch(double value) { return new Unit(UnitType.Inch, value); }
	public static Unit Foot(double value) { return new Unit(UnitType.Foot, value); }
	public static Unit Yard(double value) { return new Unit(UnitType.Yard, value); }
	public static Unit Mile(double value) { return new Unit(UnitType.Mile, value); }
	public static Unit Centimeter(double value) { return new Unit(UnitType.Centimeter, value); }
	public static Unit Meter(double value) { return new Unit(UnitType.Meter, value); }
	public static Unit Kilometer(double value) { return new Unit(UnitType.Kilometer, value); }
	
	public String toString() {
		return fmt.format(this.getValue()) + " " + this.getName();
	}
}



Some tester code, same as the Python:
void test(Unit from, UnitType to) {
	System.out.println("Conversion: " + from + " is " + from.convert(to));
}

Unit i = Unit.Inch(1);
Unit f = Unit.Foot(1);
Unit y = Unit.Yard(1);
Unit m = Unit.Mile(1);
Unit cm = Unit.Centimeter(1);
Unit me = Unit.Meter(1);
Unit km = Unit.Kilometer(1);

test(i, Unit.UnitType.Yard);
test(y, Unit.UnitType.Inch);
test(f, Unit.UnitType.Inch);
test(f, Unit.UnitType.Yard);
test(m, Unit.UnitType.Inch);
test(m, Unit.UnitType.Foot);
test(m, Unit.UnitType.Yard);
test(km, Unit.UnitType.Inch);
test(cm, Unit.UnitType.Kilometer);
test(km, Unit.UnitType.Mile);
test(m, Unit.UnitType.Meter);
test(i, Unit.UnitType.Kilometer);



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#8 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Help with converting awesome conversion classes from Python to Java

Posted 26 September 2012 - 08:10 AM

You're an amazing fellow, Baavgai. Pardon me while I study everything you just did there =)
Was This Post Helpful? 0
  • +
  • -

#9 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Help with converting awesome conversion classes from Python to Java

Posted 26 September 2012 - 11:31 AM

The staticmethod decorator is something I haven't ever used before (mainly because good documentation on decorators is something I've had trouble locating). I thought @property was awesome when I figured out how to use it, and @ICanMakeMyOwnDecoratorsNow was even more awesome, but @staticmethod appears to be the missing secret sauce I've been waiting for all my life!

Yay, no more of these!
TypeError: unbound method MyNonStaticMethod() must be called with ItsClass instance as first argument (got YourArg's instance instead (but you probably didn't want that, did you?))




I was also grinding the gears in my brain trying to figure out how I could reduce all that extra code in my Python class that deals with conversion to just one function which accounts for the current object and the appropriate conversion factor.

My Inadequate Attempt:
return self.getValue()/factors.foot_inch



You nailed that with:
def __toInstance(self, name): return Unit(self.value / Unit.data[name], name)




I think I'm approaching an understanding of your statement,

Quote

For unit conversions, you pick one, and only one, unit as 1 and work around it. Meters is usually 1, but i picked centimeters for this.
, but I never liked any of my math teachers growing up, thus I spent most of my time ignoring them. Maybe you could elaborate on that a bit =)

I just got your java example up and running in eclipse, too. Can't wait to see how much else I didn't know!

From the cursory inspection after first reviewing the Python bit, it looks like Enum will be the most foreign concept. I know about enumeration from C, but I don't recall seeing parameters being passed to items in the enum, variable declarations, or whatever you might call this ->
UnitType(double factor) { this.factor = factor; }
inside the enum UnitType.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Help with converting awesome conversion classes from Python to Java

Posted 26 September 2012 - 05:20 PM

View PostPython_4_President, on 26 September 2012 - 02:31 PM, said:

I know about enumeration from C, but I don't recall seeing parameters being passed to items in the enum, variable declarations


Java enumeration is uniquely odd. You'll want to look at Java's enum tutorial and read it over a few dozen times. The syntax doesn't quite go with the rest of the language. You declare your enum at the top, but then define kind of a class constructor later on. It's basically a static collection of instances with extra sugar.

View PostPython_4_President, on 26 September 2012 - 02:31 PM, said:

Quote

For unit conversions, you pick one, and only one, unit as 1 and work around it. Meters is usually 1, but i picked centimeters for this.
, but I never liked any of my math teachers growing up, thus I spent most of my time ignoring them. Maybe you could elaborate on that a bit =)


I'm not the most math oriented guy, but this is really "don't get taken at the cash register" kind of math.

Let's go Imperial. You know that a foot is 12 inches and that a yard is 3 feet. How many inches in a yard? How many yards in an inch? The real trick to conversion is to pick one unit and use it as your base line. To express everything in terms of feet, you'd say:
foot = 1
inch = 1/12
yard = 3



If you want to know how many inches in 2 yards, the math looks like:
2 yards = 2 * 3/1 feet = 6 feet
6 feet = 6 * 12/1 inches = 72 inches
-- other way
72 inches = 72 * 1/12 feet = 6 feet
6 feet = 6 * 1/3 yard = 2 yards



You can express all your lengths as a factor of one length. Then, conversion is just expressing the value based on that factor. If you look at my code, I used centimeter as my unit. However, the base unit is arbitrary. I used your numbers. Let's do the same thing, with your numbers, for meter.

#Meters
meter_inch = 39.37007874015748
# inch_meter == 1/meter_inch == 0.0254
meter_foot = 3.2808398950131234
# foot_meter == 1/meter_foot == 0.3048
meter_yard = 1.0936132983377078
meter_mile = 0.0006213711922373339
meter_centimeter = 100.0
meter_kilometer = 0.001



Just pointing out you don't need the inverse there. Let's make a list:
trans_table = ( ('cm', 100.0), ('inch', 39.37007874015748), ('foot', 3.2808398950131234), 
	('meter', 1.0), ('yard', 1.0936132983377078), 
	)



Could we show all conversion values from that?
>>> lines = [ '       ' + ''.join(' {0:>9}'.format(n) for n,v in trans_table)  ]
>>> lines.extend([(' {0:<6}'.format(nRow) + ''.join( ' {0:9.5f}'.format(v/vRow) for n,v in trans_table ) ) for nRow,vRow in trans_table])
>>> print('\n'.join(lines))
               cm      inch      foot     meter      yard
 cm       1.00000   0.39370   0.03281   0.01000   0.01094
 inch     2.54000   1.00000   0.08333   0.02540   0.02778
 foot    30.48000  12.00000   1.00000   0.30480   0.33333
 meter  100.00000  39.37008   3.28084   1.00000   1.09361
 yard    91.44000  36.00000   3.00000   0.91440   1.00000
>>> 



Neat.

Hope that makes sense.
Was This Post Helpful? 1
  • +
  • -

#11 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Help with converting awesome conversion classes from Python to Java

Posted 27 September 2012 - 09:38 AM

Baavgai, you've helped me immensely, sir.

With a few print statements, I figured out what was going on with the enum.

I also understand your perspective on the conversion factors. I had to and from conversion factors, but all I really needed was a base unit and to work from that. (Plus it looks much cleaner and consistent)

Truth be told, I got an itch to create a nifty distance finder app for my Android device, spent most of my time developing the idea of the user interface (flash card drawings, sketches in a notebook, etc), and tried to make all the logic fit on the fly. (and I got pretty far by doing that, but with the inevitability of having to redesign my support classes once I figured out what I really was trying to accomplish.)

Somehow, you took my crazy ramblings and turned them into pure gold. (twice! In Python first, and then in Java)

I find it hard to believe that I conveyed my idea that well. Are you an alien? If you can read my mind, I'm sure you know how much I appreciate your help.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#12 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Help with converting awesome conversion classes from Python to Java

Posted 27 September 2012 - 11:00 AM

Upon further analysis, I see why it was convenient for me to go through all the effort of specifying conversion factors for each type of unit to each other type of unit.

I'll be working with units of linear measurement, and units of angular measurement. I was trying to pack them all into one class that had to do with Units in general. Since a Minute of Angle doesn't directly translate to a centimeter or an inch, but does translate directly to a milliradian, including those entries in the UnitType enum would be nonsensical.



So I turned your class into LinearUnit.java and created AngularUnit.java. I also created HashMaps in the UnitTest.java file so I could work with Strings. In my user interface in Android, I can get either an index or the string at that index. With the string, I can now take advantage of the unit classes and their power to convert things to other things.

LinearUnit.java
package unitTest;

import java.text.DecimalFormat;

public class LinearUnit {
	public enum LinearUnitType {
		Centimeter(1.0), Inch(2.54), Foot(30.48),Yard(91.44),
		Mile(160934.4), Meter(100.0), Kilometer(100000);
		
		public final double factor;
		LinearUnitType(double factor) { this.factor = factor; }
	}
	
	private static final DecimalFormat fmt = new DecimalFormat("0.000");
	public final LinearUnitType unitType;
	private final double value;
	
	public LinearUnit(LinearUnitType unitType, double value) {
		this.unitType = unitType;
		this.value = value * unitType.factor;
	}
	
	public String getName() { return unitType.name(); }
	public double getValue() { return value / unitType.factor; }
	
	public LinearUnit convert(LinearUnitType newType) { return new LinearUnit(newType, value / newType.factor); }
	public LinearUnit toInch() { return convert(LinearUnitType.Inch); }
	public LinearUnit toFoot() { return convert(LinearUnitType.Foot); }
	public LinearUnit toYard() { return convert(LinearUnitType.Yard); }
	public LinearUnit toMile() { return convert(LinearUnitType.Mile); }
	public LinearUnit toCentimeter() { return convert(LinearUnitType.Centimeter); }
	public LinearUnit toMeter() { return convert(LinearUnitType.Meter); }
	public LinearUnit toKilometer() { return convert(LinearUnitType.Kilometer); }

	public static LinearUnit Inch(double value) { return new LinearUnit(LinearUnitType.Inch, value); }
	public static LinearUnit Foot(double value) { return new LinearUnit(LinearUnitType.Foot, value); }
	public static LinearUnit Yard(double value) { return new LinearUnit(LinearUnitType.Yard, value); }
	public static LinearUnit Mile(double value) { return new LinearUnit(LinearUnitType.Mile, value); }
	public static LinearUnit Centimeter(double value) { return new LinearUnit(LinearUnitType.Centimeter, value); }
	public static LinearUnit Meter(double value) { return new LinearUnit(LinearUnitType.Meter, value); }
	public static LinearUnit Kilometer(double value) { return new LinearUnit(LinearUnitType.Kilometer, value); }
	
	public String toString() {
		return fmt.format(this.getValue()) + " " + this.getName();
	}
}




AngularUnit.java
package unitTest;


import java.text.DecimalFormat;

public class AngularUnit {
	public enum AngularUnitType {
		MOA(1.0), MIL(3.438);
		
		public final double factor;
		AngularUnitType(double factor) { this.factor = factor; }
	}
	
	private static final DecimalFormat fmt = new DecimalFormat("0.000");
	public final AngularUnitType unitType;
	private final double value;
	
	public AngularUnit(AngularUnitType unitType, double value) {
		this.unitType = unitType;
		this.value = value * unitType.factor;
	}
	
	public String getName() { return unitType.name(); }
	public double getValue() { return value / unitType.factor; }
	
	public AngularUnit convert(AngularUnitType newType) { return new AngularUnit(newType, value / newType.factor); }
	public AngularUnit toMIL() { return convert(AngularUnitType.MIL); }
	public AngularUnit toMOA() { return convert(AngularUnitType.MOA); }


	public static AngularUnit MIL(double value) { return new AngularUnit(AngularUnitType.MIL, value); }
	public static AngularUnit MOA(double value) { return new AngularUnit(AngularUnitType.MOA, value); }	
	public String toString() {
		return fmt.format(this.getValue()) + " " + this.getName();
	}
}





UnitTest.java

package unitTest;
import java.util.HashMap;

import unitTest.AngularUnit.AngularUnitType;
import unitTest.LinearUnit.LinearUnitType;

public class UnitTest {
	public static final HashMap<String, LinearUnitType> LinearUnitMap = new HashMap<String, LinearUnitType>();
	public static final HashMap<String, AngularUnitType> AngularUnitMap = new HashMap<String, AngularUnitType>();
	static void test(LinearUnit from, LinearUnitType to) {
		System.out.println("Conversion: " + from + " is " + from.convert(to));
	}
	static void test(AngularUnit from, AngularUnitType to){
		System.out.println("Conversion: " + from + " is " + from.convert(to));
	}
	public static void main(String[] args){
		LinearUnitMap.put("Centimeter", LinearUnitType.Centimeter);
		LinearUnitMap.put("Inch", LinearUnitType.Inch);
		LinearUnitMap.put("Foot", LinearUnitType.Foot);
		LinearUnitMap.put("Yard", LinearUnitType.Yard);
		LinearUnitMap.put("Meter", LinearUnitType.Meter);
		LinearUnitMap.put("Kilometer", LinearUnitType.Kilometer);
		LinearUnitMap.put("Mile", LinearUnitType.Mile);
		AngularUnitMap.put("MIL", AngularUnitType.MIL);
		AngularUnitMap.put("MOA", AngularUnitType.MOA);
		
		

		LinearUnit i = LinearUnit.Inch(1);
		LinearUnit f = LinearUnit.Foot(1);
		LinearUnit y = LinearUnit.Yard(1);
		LinearUnit m = LinearUnit.Mile(1);
		LinearUnit cm = LinearUnit.Centimeter(1);
		LinearUnit me = LinearUnit.Meter(1);
		LinearUnit km = LinearUnit.Kilometer(1);
		AngularUnit mil = AngularUnit.MIL(1);
		AngularUnit moa = AngularUnit.MOA(1);
	
		test(i, LinearUnitMap.get("Centimeter"));
		test(mil, AngularUnit.AngularUnitType.MOA);
		test(mil, AngularUnitMap.get("MOA"));
		test(moa, AngularUnit.AngularUnitType.MIL);
		test(moa, AngularUnitMap.get("MIL"));
		
		test(i, LinearUnit.LinearUnitType.Yard);
		test(y, LinearUnit.LinearUnitType.Inch);
		test(f, LinearUnit.LinearUnitType.Inch);
		test(f, LinearUnit.LinearUnitType.Yard);
		test(m, LinearUnit.LinearUnitType.Inch);
		test(m, LinearUnit.LinearUnitType.Foot);
		test(m, LinearUnit.LinearUnitType.Yard);
		test(km, LinearUnit.LinearUnitType.Inch);
		test(cm, LinearUnit.LinearUnitType.Kilometer);
		test(km, LinearUnit.LinearUnitType.Mile);
		test(m, LinearUnit.LinearUnitType.Meter);
		test(i, LinearUnit.LinearUnitType.Kilometer);
		test(me, LinearUnit.LinearUnitType.Mile);
	}


}


This post has been edited by Python_4_President: 27 September 2012 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Help with converting awesome conversion classes from Python to Java

Posted 27 September 2012 - 11:49 AM

I'm glad you're having fun with it!

I feel the need to mention that I really don't like all those static methods. I used them to more closely approximate what you had.

You gave me an idea: Unit could be a class, but UnitType could be implemented in various enums.

Here's what I came up with:
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

interface UnitType { 
	double getFactor();
	String getName();
}

enum LinearUnitType implements UnitType {
	Centimeter(1.0), Inch(2.54), Foot(30.48),Yard(91.44),
	Mile(160934.4), Meter(100.0), Kilometer(100000);
	
	private final double factor;
	LinearUnitType(double factor) { this.factor = factor; }
	public double getFactor() { return this.factor; }
	public String getName() { return name(); }
}


enum AngularUnitType implements UnitType {
	MOA(1.0), MIL(3.438);
	
	private final double factor;
	AngularUnitType(double factor) { this.factor = factor; }
	public double getFactor() { return this.factor; }
	public String getName() { return name(); }
}

class Unit {
	private static final DecimalFormat fmt = new DecimalFormat("0.000");
	public final UnitType unitType;
	private final double value;
	public Unit(UnitType unitType, double value) {
		this.unitType = unitType;
		this.value = value * unitType.getFactor();
	}
	public String getName() { return unitType.getName(); }
	public double getValue() { return value / unitType.getFactor(); }
	
	public Unit convert(UnitType newType) {
		if (this.unitType.getClass().isInstance(newType)) {
			return new Unit(newType, value / newType.getFactor());
		}
		return null;
	}

	public String toString() { return fmt.format(this.getValue()) + " " + this.getName(); }
}


public class UnitTest {
	public static void main(String[] args){
		List<UnitType> allTypes = new ArrayList<UnitType>();
		for(UnitType i : LinearUnitType.values()) { allTypes.add(i); }
		for(UnitType i : AngularUnitType.values()) { allTypes.add(i); }
		for(UnitType t1 : allTypes) {
			Unit u1 = new Unit(t1,1);
			for(UnitType t2 : allTypes) {
				Unit u2 = u1.convert(t2);
				if (u2==null) {
					System.out.println("Can't convert " + t1.getName() + " to " + t2.getName());
				} else {
					System.out.println(u1 + " is " + u2);
				}
			}
		}
	}

}


Was This Post Helpful? 0
  • +
  • -

#14 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Help with converting awesome conversion classes from Python to Java

Posted 28 September 2012 - 01:26 AM

Oh, wow. I saw your latest post on my lunch break around noon. Been working with it ever since (It's 3:30AM now).

I tied it in with my app and now have almost exactly the behavior I was looking for. The remaining behavior deals with rounding to the nearest hundredth. Should be a perfectly attainable goal for tomorrow, along with the beginnings of the database.

I'm really curious to know how long it took you to create each of those examples and whether or not you did any actual testing (e.g., testing not executed solely in your noggin).

Effectively: How much brain power did you invest in this thread?
Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Help with converting awesome conversion classes from Python to Java

Posted 28 September 2012 - 05:27 AM

LOL. To be honest, I probably spent the most brain power on getting that little python table to print out the way I wanted. :P

I start in a text editor, usually gedit or scite. Sometimes I just stay there, but I almost always do a compile run test. For Java, I will often chuck it into Netbeans. I have a project called Scratch for such things.

Much of coding is simply reorganizing. If that's all that's need, then the process is fairly automatic. On that last one, I grabbed all your code and the code I'd posted. I then verified in a compiler that I could apply an interface to an enum. Once I had that, I pretty much just took the code I already had on my screen and moved it around. I did spend some time playing with the test loop, but that was mostly to make a good presentation. The meat of it probably took less than ten minutes, since I already had all the stuff in front of me.

Time worked is a function of expirience gained from prior time worked. I could bang out an array sort for you in the time it takes me to type, but my first bubble sort was many grueling hours of frustration. ( I still recall the idea of swap being a eureka momement. )

Time coding is never wasted, even when you're banging your head agaist a wall. The problem that takes a week to solve will find you again and you'll solve it in days, hours, or even minutes. The trick is to learn from your experience so you always find new and different walls to bang against.

I actually write a huge volume of code in response to questions on this board. If the problem is fun, I'll solve it. I probably post about 10% of what I write back to the board.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1