Presentation is loading. Please wait.

Presentation is loading. Please wait.

The 5 th and 6 th tutoring session - A case study about verifying a ISBN - Writing code for problems in HW1 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm.

Similar presentations


Presentation on theme: "The 5 th and 6 th tutoring session - A case study about verifying a ISBN - Writing code for problems in HW1 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm."— Presentation transcript:

1 The 5 th and 6 th tutoring session - A case study about verifying a ISBN - Writing code for problems in HW1 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm 9/18/2012 and 9/19/2012

2 CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue (You can call me Haydon) There are 2 sections: 1. Review 5:30pm – 6:30pm Code the ISBN verifying program Code the programs for the problems in your HW1 2. Q&A If you have any questions about Java programming, I am right here

3 ISBN Checking – problem definition An certain version of ISBN (International Standard Book Number) is a unique number assigned to a book when it’s published, such as 0–393–96945–2. The number at the end is a check digit that’s calculated from the other digits in the ISBN. Our goal is to write a program named CheckISBN that calculates the check digit for an ISBN entered by the user: Enter ISBN: 0-393-96945-2 Check digit entered: 2 Check digit computed: 2

4

5 ISBN Checking – to do list What is the to-do list?

6 ISBN Checking – to do list 1. Prompt the user to enter an ISBN. 2. Compute the check digit for the ISBN. 3. Display the check digit entered by the user. 4. Display the computed check digit. You can start from putting this list into your code as comments

7

8 ISBN Checking – The mathematics to verify the last digit 0-393-96945-2 987 65432 10

9 ISBN Checking – The mathematics to verify the last digit 0-393-96945-2 987 65432 10 total= 0  10 + 3  9 + 9  8 + 3  7 + 9  6 + 6  5 + 9  4 + 4  3 + 5  2 = 0 + 27 + 72 + 21 + 54 + 30 + 36 + 12 + 10 = 262 Check digit: 10 – ((262 – 1) mod 11) = 10 – (261 mod 11) = 10 – 8 = 2

10 ISBN Checking – coding Let code it step by step Please get your Eclipse ready Create a class named CheckISBN with main method If you are not very confident, please download and open this code first (it is the finished program): http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/C heckISBN.java

11 ISBN Checking – coding // 1. Prompt the user to enter an ISBN. Your code:

12 ISBN Checking – coding // 1. Prompt the user to enter an ISBN. System.out.print("Please enter a ISBN: "); Scanner s = new Scanner(System.in); String originalISBN = s.next(); s.close();

13 ISBN Checking – coding // 2. Compute the check digit for the ISBN. (About String: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html)http://docs.oracle.com/javase/7/docs/api/java/lang/String.html Your code:

14 ISBN Checking – coding // 2. Compute the check digit for the ISBN. int dashPos1 = originalISBN.indexOf('-'); // return the index of '-' int dashPos2 = originalISBN.indexOf('-', dashPos1 + 1); // return the index of '-', //only scan the string //from dashPos1+1 to //the end String reducedISBN = originalISBN.substring(0, dashPos1) + originalISBN.substring(dashPos1 + 1, dashPos2) + originalISBN.substring(dashPos2 + 1, 11); //remove '-' (….)

15 ISBN Checking – coding // 2. Compute the check digit for the ISBN. (…) int total = 10 * Integer.parseInt(reducedISBN.substring(0, 1)) + 9 * Integer.parseInt(reducedISBN.substring(1, 2)) + 8 * Integer.parseInt(reducedISBN.substring(2, 3)) + 7 * Integer.parseInt(reducedISBN.substring(3, 4)) + 6 * Integer.parseInt(reducedISBN.substring(4, 5)) + 5 * Integer.parseInt(reducedISBN.substring(5, 6)) + 4 * Integer.parseInt(reducedISBN.substring(6, 7)) + 3 * Integer.parseInt(reducedISBN.substring(7, 8)) + 2 * Integer.parseInt(reducedISBN.substring(8, 9)); int checkDigit = 10 - ((total - 1) % 11);

16 ISBN Checking – coding // 3. Display the check digit entered by the user. Your code:

17 ISBN Checking – coding // 3. Display the check digit entered by the user. System.out.println("Check digit entered: " + originalISBN.charAt(12));

18 ISBN Checking – coding // 4. Display the computed check digit. Your code:

19 ISBN Checking – coding // 4. Display the computed check digit. final String DIGITS = "0123456789X"; System.out.println("Check digit computed: " + DIGITS.charAt(checkDigit));

20 ISBN Checking - testing Run your finished code and let me know your questions.

21 HW1 – Problem #1 X = (-b ± Sqrt(b 2 – 4ac))/2a Use the following values 2, 6 and 2 for variables a, b and c respectively. Your code:

22 HW1 – Problem #1 X = (-b ± Sqrt(b 2 – 4ac))/2a Use the following values 2, 6 and 2 for variables a, b and c respectively. My code: double a =2; double b=6; double c=2; double doubleTempSqrt = Math.sqrt(b*b-4*a*c); double x1 = (-b+ doubleTempSqrt )/(2*a); double x2 = (-b- doubleTempSqrt )/(2*a);

23 HW1 – Problem #2 Print:

24 HW1 – Problem #2 Solution from Haidong: Find a ASCII art generator like: http://patorjk.com/software/taag/#p=display&f =Alpha&t=Java Copy it to java line by line For each ‘\’, replace it with ‘\\’, and put ‘\n’ at the end of each line Print it

25 HW1 – Problem #2 My code: String java = " _____ _____ _____ _____\n" +" /\\ \\ /\\ \\ /\\ \\ /\\ \\ \n" +" /::\\ \\ /::\\ \\ /::\\____\\ /::\\ \\\n" +" \\:::\\ \\ /::::\\ \\ /:::/ / /::::\\ \\\n" +" \\:::\\ \\ /::::::\\ \\ /:::/ / /::::::\\ \\\n" +" \\:::\\ \\ /:::/\\:::\\ \\ /:::/ / /:::/\\:::\\ \\\n" +" \\:::\\ \\ /:::/__\\:::\\ \\ /:::/____/ /:::/__\\:::\\ \\\n" +" /::::\\ \\ /::::\\ \\:::\\ \\ |::| | /::::\\ \\:::\\ \\\n" +" _____ /::::::\\ \\ /::::::\\ \\:::\\ \\ |::| | _____ /::::::\\ \\:::\\ \\\n" +" /\\ \\ /:::/\\:::\\ \\ /:::/\\:::\\ \\:::\\ \\ |::| | /\\ \\ /:::/\\:::\\ \\:::\\ \\\n" +"/::\\ /:::/ \\:::\\____\\/:::/ \\:::\\ \\:::\\____\\ |::| | /::\\____\\/:::/ \\:::\\ \\:::\\____\\\n" +"\\:::\\ /:::/ \\::/ /\\::/ \\:::\\ /:::/ / |::| | /:::/ /\\::/ \\:::\\ /:::/ /\n" +" \\:::\\/:::/ / \\/____/ \\/____/ \\:::\\/:::/ / |::| | /:::/ / \\/____/ \\:::\\/:::/ / \n" +" \\::::::/ / \\::::::/ / |::|____|/:::/ / \\::::::/ / \n" +" \\::::/ / \\::::/ / |:::::::::::/ / \\::::/ / \n" +" \\::/ / /:::/ / \\::::::::::/____/ /:::/ / \n" +" \\/____/ /:::/ / ~~~~~~~~~~ /:::/ / \n" +" /:::/ / /:::/ / \n" +" \\::/ / \\::/ / \n" +" \\/____/ \\/____/ \n" +" \n"; System.out.println(java); http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/PrintJava.java

26 HW1 – Problem #2 Result:

27 If you have any questions about java programming, I will be here till 8:30pm


Download ppt "The 5 th and 6 th tutoring session - A case study about verifying a ISBN - Writing code for problems in HW1 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm."

Similar presentations


Ads by Google