{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Appendix - $SSHQGL $ $QVZHUV WR 6HOI&KHFN...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: $SSHQGL[ $ $QVZHUV WR 6HOI &KHFN 3UREOHPV &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS &KDSWHU &RPSXWHUV XVH ELQDU\ QXPEHUV EHFDXVH LW V HDVLHU WR EXLOG HOHFWURQLF GHYLFHV UHOLDEO\ LI WKH\ RQO\ KDYH WR GLVWLQJXLVK EHWZHHQ WZR HOHFWULF VWDWHV 0DNH WKH FRRNLH EDWWHU 0L[ WKH GU\ LQJUHGLHQWV &UHDP WKH EXWWHU DQG VXJDU %HDW LQ WKH HJJV 6WLU LQ WKH GU\ LQJUHGLHQWV %DNH WKH FRRNLHV 6HW WKH RYHQ IRU WKH DSSURSULDWH WHPSHUDWXUH 6HW WKH WLPHU 3ODFH WKH FRRNLHV LQWR WKH RYHQ $OORZ WKH FRRNLHV WR EDNH $GG IURVWLQJ DQG VSULQNOHV 0L[ WKH LQJUHGLHQWV IRU WKH IURVWLQJ 6SUHDG IURVWLQJ DQG VSULQNOHV RQWR WKH FRRNLHV 0\3URJUDP MDYD LV D VRXUFH FRGH ILOH W\SHG E\ WKH SURJUDPPHU DQG 0\3URJUDP FODVV LV D FRPSLOHG H[HFXWDEOH FODVV ILOH WKDW LV UXQ E\ WKH FRPSXWHU 7KH OHJDO LGHQWLILHUV VKRZQ DUH SULQWOQ $QQXDO6DODU\ $%& VXPBRIBGDWD BDYHUDJH DQG % "Quotes" Slashes \// How '"confounding' "\" it is! Shaq is 7'1" The string "" is an empty message. \'"" 747 Dear "DoubleSlash" magazine, Your publication confuses me. Is it a \\ slash or a //// slash that I should use? Sincerely, Susan "Suzy" Smith System.out.println("\"Several slashes are sometimes seen,\""); System.out.println("said Sally. \"I've said so myself.\" See?"); System.out.println("\\ / \\\\ // \\\\\\ ///"); System.out.println("This is a test of your"); System.out.println("knowledge of \"quotes\" used"); System.out.println("in 'string literals.'"); System.out.println("You're bound to \"get it right\""); System.out.println("if you read the section on"); System.out.println("''quotes.''"); 7KH NH\ZRUG FODVV LV PLVVLQJ RQ OLQH $ VHPLFRORQ LV PLVVLQJ RQ OLQH 7KH ZRUG 3ULQWOQ VKRXOG QRW EH FDSLWDOL]HG RQ OLQH 7KH NH\ZRUG YRLG LV PLVVLQJ RQ OLQH 7KH ZRUG VWULQJ VKRXOG EH FDSLWDOL]HG RQ OLQH $ FORVLQJ PDUN LV PLVVLQJ RQ OLQH $ FORVLQJ ` EUDFH LV PLVVLQJ RQ OLQH $ ^ EUDFH LV PLVVLQJ RQ OLQH $ FORVLQJ LV PLVVLQJ RQ OLQH 7KH FRPPHQW RQ OLQHV DFFLGHQWDOO\ FRPPHQWV RXW OLQHV FRPPHQWV ZRXOG IL[ WKH SUREOHP Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside first method third method first method second method first method second method first method third method first method second method first method RI WKH SURJUDP 8VLQJ 748 Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside Inside I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I am am am am am am am am am am am am am am am am am am am am am am am am am am am am am am am first method first method second method first method third method second method first method first method second method first method third method second method first method first method second method first method third method first method second method first method 1. 1. 2. 3. 1. 1. 2. 1. 2. 3. 1. 1. 1. 1. 2. 3. 1. 2. 1. 1. 2. 3. 1. 2. 1. 1. 2. 3. 1. 1. 2. method method method method method method method method method method method method method method method method method method method method method method method method method method method method method method method 2Q OLQH 2Q OLQH WKH FODVV QDPH VKRXOG EH /RWV2I(UURUV QR VSDFH WKH ZRUG YRLG VKRXOG DSSHDU DIWHU VWDWLF 749 2Q OLQH 2Q OLQH 2Q OLQH 2Q OLQH 2Q OLQH 2Q OLQH 2Q OLQH 2Q OLQH 2Q OLQH 6WULQJ VKRXOG EH 6WULQJ>@ 6\VWHP SULQWOQ VKRXOG EH 6\VWHP RXW SULQWOQ +HOOR ZRUOG VKRXOG EH +HOOR ZRUOG WKHUH VKRXOG EH D VHPLFRORQ DIWHU PHVVDJH WKHUH VKRXOG EH DIWHU PHVVDJH 6\VWHP RXW SULQWOQ VKRXOG EH 6\VWHP RXW SULQWOQ FDQQRW VKRXOG EH FDQQRW WKH SKUDVH HUURUV FDQQRW DSSHDU LQVLGH D 6WULQJ HUURUV ZRXOG ZRUN WKHUH VKRXOG EH D FORVLQJ ` EUDFH 6\QWD[ HUURU 7KH SURJUDP ZRXOG QRW FRPSLOH EHFDXVH LWV FODVV QDPH 'HPRQVWUDWLRQ ZRXOG QRW PDWFK LWV ILOH QDPH ([DPSOH MDYD 'LIIHUHQW SURJUDP RXWSXW 7KH SURJUDP ZRXOG QRW UXQ EHFDXVH -DYD ZRXOG EH XQDEOH WR ILQG WKH PDLQ PHWKRG 'LIIHUHQW SURJUDP RXWSXW 7KHUH ZRXOG QRZ EH D EODQN OLQH EHWZHHQ WKH WZR SULQWHG PHVVDJHV 6\QWD[ HUURU 7KH SURJUDP ZRXOG QRW FRPSLOH EHFDXVH WKH PDLQ PHWKRG ZRXOG EH FDOOLQJ D PHWKRG GLVSOD\5XOH WKDW QR ORQJHU H[LVWHG 1R HIIHFW 7KH SURJUDP ZRXOG VWLOO FRPSLOH VXFFHVVIXOO\ DQG SURGXFH WKH VDPH RXWSXW 'LIIHUHQW SURJUDP RXWSXW 7KH RXWSXW ZRXOG QRZ KDYH QR OLQH EUHDN EHWZHHQ 7KH ILUVW UXOH DQG RI -DYD &OXE LV LQ LWV RXWSXW 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 12 13 public class GiveAdvice { public static void main(String args) { System.out.println("Programs can be easy or difficult"); System.out.println("to read, depending upon their format."); System.out.println(); System.out.println("Everyone, including yourself, will be"); System.out.println("happier if you choose to format your"); System.out.println("Programs."); } } public class Messy { public static void main(String args) { message(); System.out.println(); message(); } public static void message() { System.out.println("I really wish that"); System.out.println("I had formatted my source"); System.out.println("code correctly!"); } } &KDSWHU DQG DUH OHJDO LQW OLWHUDOV 750 KHOOR int age; String gender; double height; int weight; String year; int numberOfCourses; double gpa; /DVW GLJLW number % 10 6HFRQG WR ODVW GLJLW (number % 100) / 10 RU (number / 10) % 10 7KLUG WR ODVW GLJLW (number % 1000) / 100 RU (number / 100) % 10 first: 19 second: 8 7KH FRGH VZDSV WKH YDOXHV RI WKH YDULDEOHV ILUVW DQG VHFRQG 751 int first = 8, second = 19; first += second; second = first - second; first -= second; a: 7 b: 10 c: 16 FRXQW FRXQW FRXQW FRXQW for (int i = 1; i < 6; i++) { // your code here System.out.println(18 * i - 22); } System.out.println("Twas brillig and the "); System.out.println(" slithy toves did gyre and"); System.out.println("gimble"); System.out.println(); System.out.println("in the wabe."); 7KH ORRS SULQWV HYHU\ WKLUG QXPEHU QRW HYHU\ RGG QXPEHU 7KH VWDWHPHQW count = count + 2 RQ OLQH VKRXOG EH PRYHG LQWR WKH ORRS KHDGHU LQVWHDG RI count++ 2Q OLQH WKH YDULDEOH FRXQW LV QR ORQJHU GHILQHG LWV VFRSH LV RQO\ ZLWKLQ WKH IRU ORRS DERYH ,W VKRXOG EH GHFODUHG EHIRUH WKH ORRS EHJLQV UDWKHU WKDQ LQVLGH WKH ORRS V KHDGHU 2Q OLQH WRR ODUJH D YDOXH LV SULQWHG IRU WKH ILQDO RGG QXPEHU count VKRXOG EH SULQWHG QRW FRXQW 2Q OLQH LW LV LOOHJDO WR WU\ WR DVVLJQ D QHZ YDOXH WR D FRQVWDQW VXFK DV 0$;B2'' 2QH ZD\ WR IL[ WKLV ZRXOG EH WR ZULWH WZR PHWKRGV RQH WR SULQW WKH RGGV XS WR DQG D VHFRQG WR SULQW WKH RGGV XS WR %XW WKLV LV DGPLWWHGO\ UHGXQGDQW 7KH EHWWHU VROXWLRQ WR WKLV NLQG RI SUREOHP LV FDOOHG SDUDPHWHU SDVVLQJ ZKLFK ZLOO EH VHHQ LQ ODWHU FKDSWHUV 4 2 The result is: 55 24 22 19 15 10 1 2 3 4 5 752 +----+ \ / / \ \ / / \ \ / / \ +----+ How How How are many lines many lines many lines printed? T-minus 5, 4, 3, 2, 1, Blastoff! 1 2 3 4 5 2 3 4 5 6 7 8 9 10 4 6 8 10 12 14 16 18 20 6 9 12 15 18 21 24 27 30 8 12 16 20 24 28 32 36 40 10 15 20 25 30 35 40 45 50 * *** ***** ******* ********* *********** ************* *************** ***************** ******************* ****!****!****! ****!****!****! ************! ************! *!*!*!*! *!*!*!*! *!*!*!*! *!*!*!*! *!*!*!*! *!*!*!*! 753 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class SlashFigure { public static void main(String args) { for (int line = 1; line <= 6; line++) { for (int i = 1; i <= 2 * line - 2; i++) { System.out.print("\\"); } for (int i = 1; i <= -4 * line + 26; i++) { System.out.print("!"); } for (int i = 1; i <= 2 * line - 2; i++) { System.out.print("/"); } System.out.println(); } } } public class SlashFigure2 { public static final int SIZE = 4; public static void main(String args) { for (int line = 1; line <= SIZE; line++) { for (int i = 1; i <= 2 * line - 2; i++) { System.out.print("\\"); } for (int i = 1; i <= -4 * line + (4 * SIZE + 2); i++) { System.out.print("!"); } for (int i = 1; i <= 2 * line - 2; i++) { System.out.print("/"); } System.out.println(); } } } &KDSWHU 1 3 5 1 3 5 7 9 11 13 15 1 3 5 7 9 11 13 15 17 19 21 23 25 1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 number = 8 three times two 1 times three = 1 times 1 = 42 three times 1 = 1 times eight = = 6 28 2 20 754 whom and who like it it and him like whom whom and him like him stu and boo like who her and him like who public static void printStrings(String s, int n) { for (int i = 1; i <= n; i++) { System.out.print(s); } System.out.println(); } 6\VWHP RXW SULQWOQ LV DQ RYHUORDGHG PHWKRG 7KH SURJUDP FKDQJHV WKH YDOXH RI LWV SDUDPHWHU WHPSF EXW WKLV GRHVQ W DIIHFW WKH YDULDEOH WHPSF LQ PDLQ 7KH LQFRUUHFW RXWSXW LV Body temp in C is: 0.0 3 1 4 5 8 5 0 2 4 3 2 4 1 9 4 public static int min(int n1, int n2, int n3) { return Math.min(n1, Math.min(n2, n3)); } public static int countQuarters(int cents) { return cents % 100 / 25; } 755 THG $5&785$1 0(*$'21.(< 7KH FRGH WKURZV DQ ,QGH[2XW2I%RXQGV([FHSWLRQ HJDG $UFWXUDQ 0HJDGRQNH\V E &\EHU PHJD &RUS quote.substring(5, 10).toUpperCase() quote.toLowerCase().substring(0, 4) + quote.substring(20, 26) p1: java.awt.Point[x=20,y=10] p2: java.awt.Point[x=46,y=4] p3: java.awt.Point[x=46,y=4] (5, 2) (6, -3) (5, 2) 7KHUH DUH WRNHQV +HOOR 6WULQJ WKHUH 6WULQJ 6WULQJ LV 6WULQJ LQW GRXEOH 6WULQJ DQG 6WULQJ GRXEOH 6WULQJ VTXDUHG 6WULQJ LV 6WULQJ 6WULQJ 7KH FRGH ZLOO UXQ VXFFHVVIXOO\ DQG WKH YDULDEOH PRQH\ ZLOO VWRUH WKH YDOXH 7KH FRGH ZLOO UXQ VXFFHVVIXOO\ DQG WKH YDULDEOH PRQH\ ZLOO VWRUH WKH YDOXH 7KH FRGH ZLOO FUDVK ZLWK DQ H[FHSWLRQ PLOOLRQ 7KH FRGH ZLOO FUDVK ZLWK DQ H[FHSWLRQ 7KH FRGH ZLOO FUDVK ZLWK DQ H[FHSWLRQ 7KH FRGH ZLOO FUDVK ZLWK DQ H[FHSWLRQ QRQH 7KH FRGH ZLOO FUDVK ZLWK DQ H[FHSWLRQ 7KH FRGH ZLOO UXQ VXFFHVVIXOO\ DQG WKH YDULDEOH PRQH\ ZLOO VWRUH WKH YDOXH Scanner console = new Scanner(System.in); System.out.print("Type an integer: "); int number = console.nextInt(); System.out.println(number + " times 2 = " + (number * 2)); 756 Scanner console = new Scanner(System.in); System.out.print("What is your phrase? "); String phrase = console.nextLine(); System.out.print("How many times should I repeat the phrase? "); int times = console.nextInt(); for (int i = 1; i <= times; i++) { System.out.println(phrase); } 6XSSOHPHQW * 2Q WKH VHFRQG OLQH WKH FDOO WR GUDZ/LQH VKRXOG EH PDGH RQ WKH *UDSKLFV REMHFW LQ WKH SDQHO QRW RQ WKH 'UDZLQJ3DQHO LWVHOI 2Q WKH VHFRQG OLQH WKH RUGHU RI WKH SDUDPHWHUV LV LQFRUUHFW 7KH\ VKRXOG EH DQG 7KH IROORZLQJ LV WKH FRUUHFWHG FRGH DrawingPanel panel = new DrawingPanel(200, 200); Graphics g = panel.getGraphics(); panel.drawLine(50, 86, 20, 35); 7KH EODFN UHFWDQJOH LV EHLQJ GUDZQ VHFRQG VR LW V FRYHULQJ XS WKH ZKLWH LQQHU FLUFOH 7KH IROORZLQJ FRGH IL[HV WKH SUREOHP DrawingPanel panel = new DrawingPanel(200, 100); Graphics g = panel.getGraphics(); g.setColor(Color.BLACK); g.fillRect(10, 10, 50, 50); g.setColor(Color.WHITE); g.fillOval(10, 10, 50, 50); 7KH SUREOHP LV WKDW WKH SDUDPHWHUV IRU WKH GUDZ5HFW DQG GUDZ/LQH PHWKRGV KDYH GLIIHUHQW PHDQLQJV ,Q GUDZ5HFW WKH SDUDPHWHUV DUH [ \ ZLGWK KHLJKW DQG LQ GUDZ/LQH WKH\ DUH [ \ [ \ 7R IL[ WKH SUREOHP WKH WKLUG DQG IRXUWK SDUDPHWHUV SDVVHG WR GUDZ5HFW VKRXOG EH FKDQJHG WR DQG VR WKDW WKH UHFWDQJOH V ERWWRP OHIW FRUQHU ZLOO EH DW 7KH IROORZLQJ FRGH IL[HV WKH SUREOHP DrawingPanel panel = new DrawingPanel(200, 100); Graphics g = panel.getGraphics(); g.drawRect(10, 20, 40, 20); g.drawLine(10, 20, 50, 40); 7KH 'UDZ SURJUDP GUDZV EODFN FLUFOHV WKDW JHW VPDOOHU DQG VPDOOHU HDFK FLUFOH ZLWK LWV ULJKW DQG ERWWRP HGJHV WRXFKLQJ WKH ULJKW DQG ERWWRP FRUQHUV RI WKH ZLQGRZ 757 &KDSWHU 7KH VXP YDULDEOH QHHGV WR EH GHFODUHG RXWVLGH WKH IRU ORRS 7KH IROORZLQJ FRGH IL[HV WKH SUREOHP public static int sumTo(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } return sum; } 7KH FRGH KDV D IHQFHSRVW SUREOHP D FRPPD ZLOO EH SULQWHG DIWHU WKH ODVW QXPEHU 7KH IROORZLQJ FRGH IL[HV WKH SUREOHP System.out.print(1); for (int i = 1; i <= n; i++) { System.out.print(", " + i); } System.out.println(); // end the line of output Scanner console = new Scanner(System.in); System.out.print("How many numbers? "); int count = console.nextInt(); int product = 1; for (int i = 1; i <= count; i++) { System.out.print("Next number --> "); int num = console.nextInt(); product *= num; } System.out.println("Product = " + product); z % 2 == 1 z <= Math.sqrt(y) y > 0 x % 2 != y % 2 y % z == 0 z != 0 758 Math.abs(y) > Math.abs(z) (x >= 0) == (z < 0) y % 10 == y z >= 0 x % 2 == 0 Math.abs(x - y) < Math.abs(z - y) true false true false true false false true true 7KH FRGH LQFRUUHFWO\ XVHV DQ LI HOVH LI HOVH LI SDWWHUQ ZKHQ LW VKRXOG UHDOO\ XVH DQ LI LI LI SDWWHUQ EHFDXVH WKH WKUHH FRQGLWLRQV DUH QRW PXWXDOO\ H[FOXVLYH RI HDFK RWKHU 7KH JLYHQ FRGH RQO\ DQVZHUV FRUUHFWO\ ZKHQ ]HUR RU RQH RI WKH QXPEHUV DUH RGG LI PRUH WKDQ RQH LV RGG WKH FRGH RQO\ HQWHUV RQH EUDQFK DQG GRHV QRW SURSHUO\ LQFUHPHQW WKH FRXQWHU PXOWLSOH WLPHV 7KH IROORZLQJ YHUVLRQ RI WKH FRGH IL[HV WKH SUREOHP if (n1 % 2 == 1) { count++; } if (n2 % 2 == 1) { count++; } if (n3 % 2 == 1) { count++; } 2U WKH IROORZLQJ YHUVLRQ DFKLHYHV WKH VDPH WKLQJ ZLWKRXW WKH QHHG IRU LI HOVH VWDWHPHQWV count = n1 % 2 + n2 % 2 + n3 % 2; Scanner console = new Scanner(System.in); System.out.print("Type a number: "); int number = console.nextInt(); if (number % 2 == 0) { System.out.println("even"); } else { System.out.println("odd"); } 7KH FRGH LQFRUUHFWO\ SULQWV WKDW HYHQ QXPEHUV QRW GLYLVLEOH E\ DUH RGG 7KLV LV EHFDXVH WKH HOVH VWDWHPHQW PDWFKHV WKH PRVW FORVHO\ QHVWHG LI VWDWHPHQW QXPEHU QRW WKH RXWHU LI VWDWHPHQW 759 7KH IROORZLQJ FKDQJH FRUUHFWV WKH SUREOHP 1RWH WKH EUDFHV DURXQG WKH RXWHU LI VWDWHPHQW if (number % 2 == 0) { if (number % 3 == 0) { System.out.println("Divisible by 6."); } } else { System.out.println("Odd."); } 7KH FRGH VKRXOGQ W UHWXUQ WKH IDFWRU L ZKHQ IRXQG LW VKRXOG LQVWHDG FRXQW KRZ PDQ\ IDFWRUV LW ILQGV 7KLV LV D FXPXODWLYH VXP VR WKH FRXQW YDULDEOH VKRXOG EH GHFODUHG RXWVLGH WKH IRU ORRS 7KH IROORZLQJ FRGH IL[HV WKH SUREOHP public static int countFactors(int n) { int count = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { // factor count++; } } return count; } public static void moneyMultiply(int sum, int total, int count1, int count2) { Scanner console = new Scanner(System.in); System.out.print("Is your money multiplied 1 or 2 times? "); int times = console.nextInt(); System.out.print("And how much are you contributing? "); int donation = console.nextInt(); sum += times * donation; total += donation; if (times == 1) { count1++; } else if (times == 2) { count2++; } } ,I WKH XVHU FRXOG W\SH DQ\ QXPEHU WKH FRGH PLJKW QHHG DGGLWLRQDO LI VWDWHPHQWV WR LQFUHPHQW WKH SURSHU FRXQW YDULDEOH ,I WKH XVHU FRXOG W\SH DQ\WKLQJ HYHQ D QRQ LQWHJHU WKH FRGH PLJKW QHHG WR XVH WKH hasNextInt PHWKRG RI WKH 6FDQQHU WR HQVXUH YDOLG LQSXW EHIRUH SURFHHGLQJ 760 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class Bills { public static void main(String args) { Scanner console = new Scanner(System.in); int numBills1 = getBills(console, "John"); int numBills2 = getBills(console, "Jane"); System.out.println("John needs " + numBills1 + " bills"); System.out.println("Jane needs " + numBills2 + " bills"); } public static int getBills(Scanner console, String name) { System.out.print("How much will " + name + " be spending? "); double amount = console.nextDouble(); System.out.println(); int numBills = (int) (amount/20.0); if (numBills * 20.0 < amount) { numBills++; } return numBills; } } 7KH FRGH ZRQ W HYHU SULQW 0LQH WRR EHFDXVH 6WULQJV FDQQRW EH FRPSDUHG ZLWK WKH RSHUDWRU 7KH IRXUWK OLQH RI WKH FRGH VKRXOG EH FKDQJHG WR WKH IROORZLQJ if (name.equals("blue")) { 7KH FRGH V RXWSXW LV WKH IROORZLQJ EHFDXVH WKH PHWKRG non-equal second third RSHUDWRU ZDV XVHG LQVWHDG RI WKH HTXDOV Scanner console = new Scanner(System.in); System.out.print("What color do you want? "); String choice = console.nextLine(); if (choice.equalsIgnoreCase("r")) { System.out.println("You have chosen Red."); } else if (choice.equalsIgnoreCase("g")) { System.out.println("You have chosen Green."); } else if (choice.equalsIgnoreCase("b")) { System.out.println("You have chosen Blue."); } else { System.out.println("Unknown color: " + choice); } 761 Scanner console = new Scanner(System.in); System.out.print("Enter a card: "); String rank = console.next(); String suit = console.next(); if (rank.equals("2")) { rank = "Two"; } else if (rank.equals("3")) { rank = "Three"; } else if (rank.equals("4")) { rank = "Four"; } else if (rank.equals("5")) { rank = "Five"; } else if (rank.equals("6")) { rank = "Six"; } else if (rank.equals("7")) { rank = "Seven"; } else if (rank.equals("8")) { rank = "Eight"; } else if (rank.equals("9")) { rank = "Nine"; } else if (rank.equals("10")) { rank = "Ten"; } else if (rank.equals("J")) { rank = "Jack"; } else if (rank.equals("Q")) { rank = "Queen"; } else if (rank.equals("K")) { rank = "King"; } else { // rank.equals("A") rank = "Ace"; } if (suit.equals("C")) { suit = "Clubs"; } else if (suit.equals("D")) { suit = "Diamonds"; } else if (suit.equals("H")) { suit = "Hearts"; } else { // suit.equals("S") suit = "Spades"; } System.out.println(rank + " of " + suit); 7KH H[SUHVVLRQ HTXDOV OHG WR D URXQGRII 7KH H[SUHVVLRQ JSD HTXDOV WR WHVW WKDW WKH YDOXH ZDV FORVH WR EHFDXVH RI WKH OLPLWHG SUHFLVLRQ RI WKH GRXEOH W\SH EHFDXVH RI GRXEOH URXQGRII $ IL[ ZRXOG EH UDWKHU WKDQ H[DFWO\ HTXDO WR LW double gpa = 3.2; if (Math.abs(gpa * 3 - 9.6) < 0.1) { System.out.println("You earned enough credits."); } 762 if (Character.isUpperCase(theString.charAt(0))) { ... } 7KH WR/RZHU&DVH PHWKRG FDQQRW EH FDOOHG RQ D FKDU YDOXH ZKLFK LV ZKDW WKH FKDU$W PHWKRG UHWXUQV $ EHWWHU VROXWLRQ ZRXOG EH WR FDOO WKH &KDUDFWHU WR/RZHU&DVH PHWKRG RQ WKH FKDUDFWHUV RI WKH VWULQJ int count = 0; for (int i = 0; i < s.length(); i++) { if (Character.toLowerCase(s.charAt(i)) == 'e') { count++; } } $QRWKHU VROXWLRQ ZRXOG EH WR ORZHUFDVH WKH HQWLUH VWULQJ RQFH EHIRUH WKH ORRS s = s.toLowerCase(); int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'e') { count++; } } String name = "Marla Singer"; int space = name.indexOf(" "); String lastName = name.substring(space + 1); String firstInitial = name.substring(0, 1); String lastNameFirstInitial = lastName + ", " + firstInitial + "."; System.out.println(lastNameFirstInitial); RU D VKRUWHU YHUVLRQ String name = "Marla Singer"; System.out.println(name.substring(name.indexOf(" ") + 1) + ", " + name.charAt(0) + "."); // assuming that the string is stored in variable 'str' int count = 0; for (int i = 0; i < str.length(); i++) { if (Character.toLowerCase(str.charAt(i)) >= 'n') { count++; } } System.out.println(count + " letters come after n."); 763 public static void printTriangleType(int s1, int s2, int s3) { if (s1 == s2) { if (s2 == s3) { System.out.println("equilateral"); } } else if (s2 == s3) { System.out.println("isoscel...
View Full Document

{[ snackBarMessage ]}