Results 1 to 9 of 9
Thread: OO Advices
- 03-09-2009, 01:11 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
OO Advices
Hi all.
I'm quite new to java and I have made a little application to convert temperature and distance.
I tried to do it in OO but I don't know if there could be any adjustements that would make it more organized or more efficient?
Please tell me what you think of it.
Temperature.java and Distance.java are almost identical, exept for the names and the formulas so I only post Main.java and Temperature.java.
Thanks in advance.
Main.java
Temperature.javaJava Code:import javax.swing.JFrame; import javax.swing.JTabbedPane; public class Main { /** * @param args */ public static void main(String[] args) { Temperature Temperature = new Temperature(); Temperature.ConfigContent(); Distance Distance = new Distance(); Distance.ConfigContent(); JTabbedPane TabbedPane = new JTabbedPane(); TabbedPane.addTab("Temperature", Temperature.PTemperature); TabbedPane.addTab("Distance", Distance.PDistance); JFrame Window = new JFrame("Converter"); Window.setContentPane(TabbedPane); Window.setSize(400,400); Window.setLocationRelativeTo(null); Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Window.setVisible(true); } }
Java Code:import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Temperature implements ActionListener { JLabel LCelsius = new JLabel("Celsius:"); JLabel LKelvin = new JLabel("Kelvin:"); JLabel LFahrenheit = new JLabel("Fahrenheit:"); JTextField TCelsius = new JTextField(10); JTextField TKelvin = new JTextField(10); JTextField TFahrenheit = new JTextField(10); String CCelsius; String CKelvin; String CFahrenheit; JButton BConvert = new JButton("Convert"); JButton BReset = new JButton("Reset"); JPanel PTemperature = new JPanel(); GridLayout Row1 = new GridLayout(4,1); GridLayout Row2 = new GridLayout(4,1); JPanel PRow1 = new JPanel(Row1); JPanel PRow2 = new JPanel(Row2); BorderLayout BLayout = new BorderLayout(); JPanel Layout = new JPanel(BLayout); double DCelsius; double DKelvin; double DFahrenheit; public void ConfigContent() { BConvert.addActionListener((ActionListener) this); BReset.addActionListener((ActionListener) this); PRow1.add(LCelsius); PRow1.add(LKelvin); PRow1.add(LFahrenheit); PRow1.add(BReset); PRow2.add(TCelsius); PRow2.add(TKelvin); PRow2.add(TFahrenheit); PRow2.add(BConvert); Row1.setVgap(5); Row2.setVgap(5); Layout.add(PRow1, BorderLayout.WEST); Layout.add(PRow2, BorderLayout.CENTER); BLayout.setHgap(5); PTemperature.add(Layout, BorderLayout.NORTH); } public double GetNumber(JTextField A) { String ATemp = A.getText(); Double Result = Double.parseDouble(ATemp); return Result; } public void actionPerformed(ActionEvent e) { CCelsius = TCelsius.getText(); CKelvin = TKelvin.getText(); CFahrenheit = TFahrenheit.getText(); if(e.getSource() == BConvert) { if(!CCelsius.equals("")) { DCelsius = GetNumber(TCelsius); DKelvin = DCelsius + 273.15; DFahrenheit = ((DCelsius * 9) / 5) + 32; } else if(!CKelvin.equals("")) { DKelvin = GetNumber(TKelvin); DCelsius = DKelvin - 273.15; DFahrenheit = (DCelsius *(9/5))+32; } else if(!CFahrenheit.equals("")) { DFahrenheit = GetNumber(TFahrenheit); DCelsius = (DFahrenheit - 32.0) * 5.0 / 9.0; DKelvin = DCelsius + 273.15; } TCelsius.setText(Double.toString(DCelsius)); TKelvin.setText(Double.toString(DKelvin)); TFahrenheit.setText(Double.toString(DFahrenheit)); } else if(e.getSource() == BReset) { TCelsius.setText(""); TKelvin.setText(""); TFahrenheit.setText(""); } } }Last edited by bubbless; 03-09-2009 at 01:44 PM.
- 03-09-2009, 04:02 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Simple idea of the OO is doing the similar process separately and gives the safe interface to access those things safely. So your code is reusable. Keep in mind, this is very basic idea of the OO concepts.
- 03-09-2009, 06:18 PM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
such a project won't require too much concentration on object oriented programming. oop designs start taking place when, like eranga said, you need to re-use code (among more complex things), for example when you have different classes that share similarities or classes that need to frequently communicate with one another.
- 03-09-2009, 06:53 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
So this application is too small to write in OO?
Do you have any other suggestions for that program?
And could you give an example how it has to be done with a bigger application?
I just want to learn OO the right way, to prevent bad habits in the future.
- 03-09-2009, 07:43 PM #5
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Well, learning OOP concepts is a step-by-step process. There's no such thing as a project being too small, just that it doesn't require that much use of OOP concepts. I suggest re-writing either Temperature or Distance (no reason in doing both yet) while keeping the following in mind.
1. Do not use GUI classes yet. User interface is a separate part of a program from its logic and should be designed as such.
2. Create individual classes and think about what fields and methods you need, keeping in mind that your class is an object and will be used as a variable in another class.
3. Keep as much as possible private. The concept of classes revolves around modularity/encapsulation. A class is a black box where the only means of input/output should be public methods. Fields and operations should be practically invisible from the outside. This is why you should keep away from GUIs for now. When you learn to design classes well enough, you can create GUIs to use them smoothly.
I'm sure other people have more tips, but re-writing your classes should be enough to handle for now.
-
I think that small utility methods that don't change the state of a class can and perhaps should be static, as is demonstrated by the Math class in the Java standard library. Your conversion methods likely fit this bill, and so I'd just create a few static methods in a Conversion class that actually do the conversion. These can be considered the "model" for your GUI, and so the GUI can call whichever method from this library is appropriate depending on fields filled and button pressed.
- 03-10-2009, 02:41 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-10-2009, 11:09 AM #8
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
Thank you for your answers, all of you. :)
But I still don't get some things.
Wich parts should be individual classes?
I first had everything in one class and I seperated them to orden the program,
but I don't see why I should make more classes.
And what's the harm when all methods are public?
Edit.
I also don't see how I can seperate the GUI from the logic.
I've read a few tutorials about the MVC pattern but I don't really get it.Last edited by bubbless; 03-10-2009 at 02:43 PM.
- 03-11-2009, 04:22 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
As I said in simple word, normally a class contain common things, or perform a single task in your application. To access the class and set/get values use public class only. All the important process/calculation done on private(actually higher access levels than public) methods. Just read about Encapsulations on Java.
Similar Threads
-
need some advices!
By PureAwesomeness in forum New To JavaReplies: 9Last Post: 01-17-2009, 11:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks