Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA . Each program is to be submitted in a separate file with the file name being the class name with extension .java as shown

JAVA . Each program is to be submitted in a separate file with the file name being the class name with extension .java as shown below. I only need the source file.

GeneralAverage.java

All answers will be posted on web site, and most will be reviewed in class.

  1. ISBNVerifier (40)

Background description:

An International Standard Book Number (ISBN) is a code that uniquely identifies an edition of a book. The ISBN consists of ten digits separated by dashes into groups. The groups are of various sizes, except for the last group. The last group is always a single character, '0' through '9' or 'X', and acts as a check on the rest of the digits. In this problem we look at 10 digit ISBNs

0-670-03441-X

0-201-48558-3

1-56592-262-X

0-06-027900-1

0-439-45695-9

0-470-84371-3

1-4000-3136-2

0-19-856453-8

1-85671-104-8

The last character is calculated from the other 9 digits:

Key Algorithm to calculate check digit.

Multiply the first digit by 10.

Multiply the second digit by 9.

Multiply the second digit by 8.

. . .

Multiply the ninth digit by 2.

Add up all these values.

Integer divide the sum by 11.

Find the remainder.

Subtract the remainder from 11. This is the check digit.

If the checkDigit is 10, use an 'X'

For example:

0-201-48558-3

0 * 10 = 0

2 * 9 = 18

0 * 8 = 0

1 * 7 = 7

4 * 6 = 24

8 * 5 = 40

5 * 4 = 20

5 * 3 = 15

8 * 2 = 16

-----

sum = 140

140 / 11 = 12 rem 8

11 - 8 = 3 <-- the check digit

Write a program that prompts the user for a file containing ISBN numbers and then checks if each the ISBN is correct. I have posted the isbnCodes.txt file in the Homework 7 folder and you may use it to test your results

Here is a complete description of what I want you to do:

In main:

Create a Scanner to read in the name of the input file as a String.

Call a method createScanner with the parameter of the String file. createScanner should return a reference to a scanner that allows you to read from the input file.

For each ISBN code in the file

Read in the potential ISBN codes one at a time as a StringBuilder.

Call checkISBN with the reference to StringBuilder object you have read in. checkISBN returns a String reference with a message telling whether this is a valid code, and if not why not.

Print the message to the screen , along with the ISBN number entered. (see sample output when using the file I have provided).

Close the Scanner used to read he file.

In createScanner:

Accept the reference to the file name as a String.

Create a Scanner object that reads from that file. Be sure to check for the files existence.

Return a reference to the created Scanner

In checkISBN:

Perform the following checks. If a check leads to an error, return a message listing the error. Below is my suggested order.

Here are steps you may follow:

Check that total length is 13.

Make sure the 12th position is a digit or X

Make sure that there are exactly three - in the code.

Check to make sure that each character except the last is a digit or a dash.

Extract the nine digits in order

Calculate the check digit.

If the check digit matches as described above return a message indicating a good ISBN, otherwise return a message indicating that a bad ISBN was created because the digit doesnt match

Helpful Hint:

Here is an example of how to convert the char to an int:

char x = '9';

int y = x - '0'; // gives 9 remember the subtraction will cast up to int and the int characters are sequential in UNICODE.

A check on the ISBN 0-670-03441-X yields: Good - final digit is X

A check on the ISBN 0-201-48558-3 yields: Good Digits Match

A check on the ISBN 1-56592-262-X yields: Good - final digit is X

A check on the ISBN 0-06-027900-1 yields: Good Digits Match

A check on the ISBN 0-439-45695-9 yields: Good Digits Match

A check on the ISBN 0-470-84371-3 yields: Good Digits Match

A check on the ISBN 1-4000-3136-2 yields: Good Digits Match

A check on the ISBN 0-19-856453-8 yields: Good Digits Match

A check on the ISBN 1-85671-104-8 yields: Good Digits Match

A check on the ISBN 92-9395 yields: Bad Length

A check on the ISBN 42-4920-223-Y yields: Final Character is Bad

A check on the ISBN 1-8906-456789 yields: There are not 3 dashes as required

A check on the ISBN 1-856X1-104-8 yields: Character not a digit or -

A check on the ISBN 1-4000-3136-5 yields: Final digit doesn't match check digit or X

  1. CompareTimes (30)

This problem can be done in main. Apply the increment operator (++) 10 million times, first to an Integer reference and then to a variable of type int, each set initially to 1. Compare the running times.

To compare running times we use the method long System.currentTimeMillis()

that returns the current time in milliseconds.

For both the Integer reference x and the primitive y, the main method

  • records the starting time in milliseconds,
  • increments with ++ a variable 10,000,000 times,
  • records the ending time in milliseconds, and
  • displays the elapsed time, ending time - starting time.
  1. ArrayListReversal (30)

Write a program where main reads in one word names from a file placing a reference to each new object into an ArrayList called names. (Be sure to ask for the name of the input file interactively.) You can use the namesforreversal.txt that is posted in HOMEWORK & PROGRAMS 2 ANSWERS.

Print out the list one element at a time to the screen. Then call a method reverseArrayList, passing it a reference to the ArrayList names. The method reverses the order of all the elements in names. The main method should then prints out names in reverse order to the screen as shown:

Finally, Ask the user to enter an index for the array and then print out that element. This should be implemented in a loop that exits if the number entered is greater than or equal to the size, or less than 0.

A sample output looks like this:

Please enter name of file to with names to reverse: namesforreversal.txt

Original List:

Jacob

Michael

Joshua

Matthew

Daniel

Christopher

Andrew

Ethan

Joseph

William

Anthony

Emily

Madison

Emma

Olivia

Hannah

Abigail

Isabella

Samantha

Elizabeth

Ashley

Alexis

Sarah

Sophia

Alyssa

Reversed List:

Alyssa

Sophia

Sarah

Alexis

Ashley

Elizabeth

Samantha

Isabella

Abigail

Hannah

Olivia

Emma

Madison

Emily

Anthony

William

Joseph

Ethan

Andrew

Christopher

Daniel

Matthew

Joshua

Michael

Jacob

Please enter the number of the index you want to find the value for.

Index must be between 0 and 24 inclusive, Enter a number outside of range to exit: 24

The name at index 24 is Jacob.

Please enter the number of the index you want to find the value for.

Index must be between 0 and 24 inclusive, Enter a number outside of range to exit: 0

The name at index 0 is Alyssa.

Please enter the number of the index you want to find the value for.

Index must be between 0

******* ibsncodes.txt *********

0-670-03441-X 0-201-48558-3 1-56592-262-X 0-06-027900-1 0-439-45695-9 0-470-84371-3 1-4000-3136-2 0-19-856453-8 1-85671-104-8 92-9395 42-4920-223-Y 1-8906-456789 1-856X1-104-8 1-4000-3136-5

******* namesforreversal.txt *********

Jacob Michael Joshua Matthew Daniel Christopher Andrew Ethan Joseph William Anthony Emily Madison Emma Olivia Hannah Abigail Isabella Samantha Elizabeth Ashley Alexis Sarah Sophia Alyssa

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Process Driven Database Design With Oracle PL SQL

Authors: Rajeev Kaula

1st Edition

1795532386, 978-1795532389

More Books

Students also viewed these Databases questions