4 Replies - 815 Views - Last Post: 26 August 2009 - 05:07 PM Rate Topic: -----

#1 liliac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-June 09

I need some opinions :D

Posted 23 August 2009 - 03:23 PM

Hi,
i just finished my first year in software engineering so I'm a noobie :D. Tonight I was playing with some code and I need an opinion from some specialist.
What do you think about my code (for my level of course), is the way I think about development and design ok?
(is just that I need some opinions to know if what I'm doing it's alright or not, :D thanx )

public class Converter {
	public static void main(String[] args) {
		char choice;
		do{
			System.out.println("Choose a converter: ");
			System.out.println("1 - for kilo to pounds");
			System.out.println("2 - for pounds to kilo");
			choice = UserInput.readChar();
			while (choice!='1' && choice!='2'){
				System.out.println("Invalid Character!");
				System.out.println("1 - for kilo to pounds");
				System.out.println("2 - for pounds to kilo");
				choice = UserInput.readChar();
			}//end while(choice!='1' && choice!='2') statement
			switch (choice){
				case '1': kiloToPounds();//call kiloToPounds method 
				break;
				case '2': poundsToKilo();//call poundsToKilo method
				break;
			}//end switch statement
				System.out.println("Restart Converter?");
				System.out.println("Y/N");  
				choice = UserInput.readChar();
				while (choice!='Y' && choice!='y' && choice!='N' && choice!='n'){
					System.out.println("Invalid Character!");
					System.out.println("Restart Converter?");
					System.out.println("Y/N");
					choice = UserInput.readChar();
				}//end while(choice!='Y' || choice!='y' && choice!='N' || choice!='n') statement
		}while (choice=='Y' || choice=='y');//end do-while statement
		
	}//end main

	private static void kiloToPounds() {
		double kg;
		System.out.println("Input the number of kg:");
		kg = UserInput.readDouble();
		System.out.println(kg+" kg = "+kg*2.2+" pounds");
	}//end kiloToPounds method

	private static void poundsToKilo() {
		double pound;
		System.out.println("Input the number of pounds :");
		pound = UserInput.readDouble();
		System.out.println(pound+" pounds  = "+pound/2.2+" kg");
	}//end poundsToKilo method
	
}//end Converter class



cheers :^:

Is This A Good Question/Topic? 0
  • +

Replies To: I need some opinions :D

#2 ts230   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 225
  • Joined: 11-July 09

Re: I need some opinions :D

Posted 23 August 2009 - 04:39 PM

You should wrap all of the I/O from and to the console in a try/catch block. I also think you should use a small Swing GUI maybe? But other than that, it looks OK. And where is "UserInput" defined?

You should wrap all of the I/O from and to the console in a try/catch block. I also think you should use a small Swing GUI maybe? But other than that, it looks OK. And where is "UserInput" defined?
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: I need some opinions :D

Posted 23 August 2009 - 08:14 PM

and you should remove all the stuff from your main() method and all your static method

[code]
class Converter {

// constructor
Converter() {
// here everyting happens
}

void kiloToPounds() {
....
}
....
public static void main(String[] args) {
new Converter(); // <---- and that's it
}
Was This Post Helpful? 0
  • +
  • -

#4 liliac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-June 09

Re: I need some opinions :D

Posted 26 August 2009 - 03:59 PM

View Postpbl, on 23 Aug, 2009 - 07:14 PM, said:

and you should remove all the stuff from your main() method and all your static method

[code]
class Converter {

// constructor
Converter() {
// here everyting happens
}

void kiloToPounds() {
....
}
....
public static void main(String[] args) {
new Converter(); // <---- and that's it
}


hi,
Can you please give a more detailed example, with my code....I understand what you said but I cannot figure it out....
Regards,
soso
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: I need some opinions :D

Posted 26 August 2009 - 05:07 PM

The static main() method in a class is used to:
- unit test that class
- start a big application

the kiloToPound() method is part of the Converter part it should not be a static method

Actually, beside some exceptions in more complicated situations, at your level (no offense) the only static method that you should have in a class is the main() method that is used to test your Converter class

it should call the Converter class constructor (and method) the same way another completly different class will call it

class Converter {

	 // Constructor
	 Converter() {
	 }

	 float fromKiloToPound(int kilo) {
		....
	 }


	 // to test Converter
	 public static void main(String[] args) {
		  Converter c = new Converter();
		  // test methof fromKiloToPounds
		  float pound = c.fromKiloToPound(5);
		  System.out.println("5 kilo is " + pound + " pounds");
	 }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1