First off, I'm no Java programmer(but I do know the concepts of OO programming), I have this project I need to work on but I currently don't have a lot of time to spend in learning Java and then implementing it, I need to learn it on the fly. I appreciate the concern on letting me know that I need to learn the language first, and even though I want to, I really can't.
With that out of the way, I'm building an Android app in which I will implement thermodynamic equations of state. The basic outline of the app is as follows:
It has a menu in which you can choose the equation of your choice. (I will use the simple Ideal Gas Law to exemplify this); this is the MainActivity of the project.
Now that you have chosen the Ideal Gas Law, you will be presented with 3 options, to calculate temperature, pressure or volume, each of this will be implemented using a Button widget; this is the IdealGasActivity of the project, and each equation must be like this one (maybe I can inherit this class?).
Finally, once you have chosen a variable to calculate, we will use the temperature as an example, you are presented with a set of EditText fields where you can enter the values for the variables that you have and a Button to calculate the temperature, it also has some Spinner widgets to convert to different units; this would be the TemperatureActivity. Here is what the Temperature class looks like:
public class Temperature extends AppCompatActivity { Spinner spinner_pres; Spinner spinner_vol; TextView totalTextView; EditText pressure_value; EditText volume_value; EditText moles_value; final double consR = 0.0820574; // In atm*L/mol*K double pressure = 0.0, volume = 0.0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_temperature); // Can this declarations be implemented on a different method? // Gather values from text fields totalTextView = (TextView) findViewById(R.id.tempTxt); pressure_value = (EditText) findViewById(R.id.pressureTxt); volume_value = (EditText) findViewById(R.id.volumeTxt); moles_value = (EditText) findViewById(R.id.molesTxt); // Implement list de volume unit conversions spinner_pres = (Spinner) findViewById(R.id.units_pressure_temp); ArrayAdapter<CharSequence> adapter_pres = ArrayAdapter.createFromResource(this, R.array.arr_units_pressure, android.R.layout.simple_spinner_dropdown_item); spinner_pres.setAdapter(adapter_pres); spinner_pres.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 0: break; case 1: pressure = Float.parseFloat(pressure_value.getText().toString())/14.696; break; case 2: pressure = Float.parseFloat(pressure_value.getText().toString())/760.0; break; } } @Override public void onNothingSelected(AdapterView<?> parent) { } }); // Implement list de volume unit conversions spinner_vol = (Spinner) findViewById(R.id.units_volume_temp); ArrayAdapter<CharSequence> adapter_vol = ArrayAdapter.createFromResource(this, R.array.arr_units_volume, android.R.layout.simple_spinner_dropdown_item); spinner_vol.setAdapter(adapter_vol); //Can I implement this in a new method so I can override it? spinner_vol.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 0: break; case 1: volume = Float.parseFloat(volume_value.getText().toString())/0.001; break; case 2: volume = Float.parseFloat(volume_value.getText().toString())/1000; break; } } @Override public void onNothingSelected(AdapterView<?> parent) { } }); // Possible use of this.Calculate(); ?? // Calculate temperature Button calcBtn = (Button) findViewById(R.id.calcBtn); calcBtn.setOnClickListener(new View.OnClickListener() { @Override //Calculate temperature public void onClick(View view) { float moles = Float.parseFloat(moles_value.getText().toString()); double temperature = pressure*volume/(cteR*moles); totalTextView.setText(String.format(Locale.ENGLISH,"%.4f",temperature)); } }); } // Method to calculate temperature // I know this won't work if I call it inside OnCreate, but why? public void Calculate() { // Calculate temperature Button calcBtn = (Button) findViewById(R.id.calcBtn); calcBtn.setOnClickListener(new View.OnClickListener() { @Override //Calculate temperature public void onClick(View view) { float moles = Float.parseFloat(moles_value.getText().toString()); double temperature = pressure*volume/(cteR*moles); totalTextView.setText(String.format(Locale.ENGLISH,"%.4f",temperature)); } }); }
Ok, so my question is, how can I rearrange this class in order to make use of Object Oriented Programming? I want to be able to extend this class to calculate pressure and volume, and also for the other equations. I know it has a lot of errors and possible bad practices, but I would like to avoid just copying and pasting the code over and over again for the other buttons I plan on implementing.