Create add method to add feet and inches java -
i need create add method add feet , inches inputted user. user can input integer in feet or inches, , back, need add values. not sure how can approach that. great!
the following code:
import java.util.scanner; public class mixwithunit_jm { fraction_jm a; int first; int second; public string displaymixwithunit_jm() { string str=""; if (first == 0) str= first%second+"/"+second+"in"; else if (first%second == 0) str= ""+first/second+"ft"; else if (first < second) str=first+"/"+second+"in"; else str= first/second+"ft" +" "+ first%second+"/"+second+"in"; return str; }//display public mixwithunit_jm(string str) { int pos = str.indexof("ft"); int pos2 = str.indexof("in"); string a1 = ""; string a2 =""; string b1 = ""; string b2 =""; string b3 =""; if(pos == -1){ a2 = str.substring(0,pos2 +2).trim(); b2 = str.substring(0,pos2); b3 = b2; } else if(pos2 == -1){ a1 = str.substring(0,pos +2); b1 = str.substring(0, pos); b3 = b1; } else{ a1 = str.substring(0,pos +2); b1 = str.substring(0, pos); a2 = str.substring(pos +2).trim(); b2 = str.substring(pos+2, str.length()-2); b3 = b1 + b2; } int[] ia= parse (b3); int top=ia[0]*ia[2]+ia[1]; int bot= ia[2]; = new fraction_jm (top,bot); int gcd = a.gcd(top,bot); first=top/gcd; second =bot/gcd; }//mix public static string (){ scanner scan = new scanner (system.in); string userinput = scan.nextline(); userinput =userinput.trim(); return (userinput); } //get public static int[] parse (string userinput){ int inum = 0; int itop = 0; int ibot = 1; userinput = userinput.trim(); int pos = userinput.indexof(" "); int pos2 = userinput.indexof("/"); if (pos == -1 && pos2 != -1){ pos= userinput.indexof("/"); string stop=userinput.substring(0,pos); itop = integer.parseint(stop);//second integer string sbot=userinput.substring(pos+1); ibot = integer.parseint(sbot);//third integer //case 2 } else if (pos == -1 && pos2== -1) { inum = integer.parseint(userinput);//first integer //case 1 } else{ string snum=userinput.substring(0,pos); inum = integer.parseint(snum);//first integer string snum2=userinput.substring(pos+1); pos= snum2.indexof("/"); string stop=snum2.substring(0,pos); itop = integer.parseint(stop);//second integer string sbot=snum2.substring(pos+1); ibot = integer.parseint(sbot);//third integer } int[] sa = {inum,itop,ibot}; return (sa); } //parse public static void main(string[] args) { system.out.print("please enter mixed-format number :"); string userinput = mixwithunit_jm.get(); system.out.println("input is: "+userinput); mixwithunit_jm s = new mixwithunit_jm(userinput); s.displaymixwithunit_jm(); system.out.print("please enter mixed-format number :"); userinput = mixwithunit_jm.get(); system.out.println("input is: "+userinput); mixwithunit_jm s2 = new mixwithunit_jm(userinput); s2.displaymixwithunit_jm(); }//main }//class
the program runs, need add method.
you should use system.in.read() read standard input. can find more info in post: what system.in.read return?
Comments
Post a Comment