java - Weight Conversion - How to combine various Print Methods into 1 Print Method -


i novice programmer learning java , though homework complete want make neater. wanted know if can combine following print methods 1 , @ least combine kilograms pounds , pounds kilograms methods one.

note, can't use more advanced if statements , loops.

because first time posting , want make sure provide answerers adequate information have uploaded weight conversion java file here: weight conversion java file.

any other advice how simplify code, or following better code etiquette welcomed too.

here print statements:

/** * method below prints calculations calculatekg , calculatelbs */ public static void printresults1( double dresult1, double dresult2){ // prints result of pounds kilograms  system.out.print(dresult1 + " pounds " + dresult2 + " kilograms."); }// end method printresults1  /** * method below prints calculations calculatekg , calculatelbs */ public static void printresults2( double dresult1, double dresult2){ // prints result of pounds kilograms  system.out.print( dresult1 + " kilograms " + dresult2 + " pounds"); }// end method printresults2  /** * method below prints calculations calculateoz , calculatelbs */  public static void printresults3( double dresultoz, double dresultlbs){ // prints result of pounds kilograms  system.out.print( dresultoz + " ounces " + dresultlbs + " pounds"); }// end method printresults3   /** * method below prints calculations calculateoz , calculatelbs */  public static void printresults4( double dresultlbs, double dresultoz){ // prints result of pounds kilograms  system.out.print( dresultlbs + " pounds " + dresultoz + " ounces "); }// end method printresults4  

for start, consider this:

public static void printresults(     double dresultfrom,     string from,     double dresultto,     string to) {     system.out.print(dresultfrom + " " + + " " + dresultto + " " + to); } 

not sure whole context you're using , limitations. of course further refactoring steps possible. example:

public static void printresults(     double resultfrom,     string fromdescription,     double resultto,     string todescription) {     string formattedresult = formatresult(         resultfrom,         fromdescription,         resultto,         todescription);      system.out.print(formattedresult); }  public static string formatresult(     double resultfrom,     string fromdescription,     double resultto,     string todescription) {     return formatquantity(resultfrom, fromdescription)         + " "         + formatquantity(resultto, todescription); }  public static string formatquantity(double value, string description) {     return value + " " + description; } 

note less code duplication in example, , clear separation of responsibilities (formatting functions, , printing function). example, if had print results file, not console, design prove more flexible.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -