Haidong (Haydon) Xue

Preview:

DESCRIPTION

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 . CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue (You can call me Haydon) There are 2 sections: - PowerPoint PPT Presentation

Citation preview

The 5th and 6th tutoring session- A case study about verifying a ISBN- Writing code for problems in HW1

Fall, 2012

Haidong(Haydon) Xue

5:30pm—8:30pm9/18/2012 and 9/19/2012

• CSc2310 Tutoring• Time: 5:30pm-8:30pm• Tutor: Haidong Xue (You can call me Haydon)

There are 2 sections:1. Review 5:30pm – 6:30pmCode the ISBN verifying programCode the programs for the problems in your HW1

2. Q&AIf you have any questions about Java programming, I am right here

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-2Check digit entered: 2Check digit computed: 2

ISBN Checking – to do list

What is the to-do list?

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

ISBN Checking – The mathematics to verify the last digit

• A ISBN: 9 digits and 1 check digite.g. 0-393-96945-2• For the 9 digits there is a weight for each of them

• The last digit is computed:total = check digit = 10 – ((total – 1) mod 11)

0-393-96945-2 987 6543210

ISBN Checking – The mathematics to verify the last digit

0-393-96945-2 987 6543210

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

ISBN Checking – codingLet 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/CheckISBN.java

ISBN Checking – coding

// 1. Prompt the user to enter an ISBN.Your code:

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();

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)

Your code:

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 '-'

(….)

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);

ISBN Checking – coding

// 3. Display the check digit entered by the user.Your code:

ISBN Checking – coding

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

ISBN Checking – coding

// 4. Display the computed check digit.Your code:

ISBN Checking – coding

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

ISBN Checking - testing

Run your finished code and let me know your questions.

HW1 – Problem #1

• X = (-b ± Sqrt(b2 – 4ac))/2a• Use the following values 2, 6 and 2 for

variables a, b and c respectively .Your code:

HW1 – Problem #1

• X = (-b ± Sqrt(b2 – 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);

HW1 – Problem #2

• Print:

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

HW1 – Problem #2My code: String java = " _____ _____ _____ _____\n" +" /\\ \\ /\\ \\ /\\ \\ /\\ \\ \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

HW1 – Problem #2Result:

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

Recommended