6
COMP 1010 Fall 2013 Assignment 2 Page 1 of 6 COMP 1010 Fall 2013 Assignment 2 Due date: Wednesday, October 23, 2013, before midnight Material Covered long integers if statements input validation using Scanner "has" methods loops (while, dowhile, for) File input using Scanner class String processing Notes and Instructions – Please follow exactly Name your Java files as follows: <LastName><FirstName>A2Qn.java (use Q1, Q2 and Q3 for Questions 1, 2 and 3, of course). For example, SmithJohnA2Q1.java is a valid name. Your classes need to be named to match. Follow the programming standards posted on the course website to avoid losing marks. You must submit the “Blanket Honesty Declaration”, also on the course website, before any assignment in COMP 1010 will be marked. To submit the assignment you will upload your .java files to the Dropbox for Assignment 2. Note this is different than the procedure for Assignment 1 (the Dropbox will accept .java files). You will also copy and paste the output of your test runs from Dr Java into .txt files, and upload them, in the same way. Name your output files <LastName><FirstName>A2Qnoutput.txt If you use JOptionPane for input, nothing will appear in the Interactions panel. For that reason, you must echo all such input using System.out, so that it will appear in the output that you hand in. Do NOT manipulate your output in any way. We will demonstrate the assignment handin procedure in class. To be eligible to earn full marks, your Java programs must compile and run upon download, without requiring any modifications. In every assignment this year, 90% of the marks will be assigned to normal question(s) with a moderate degree of difficulty. The other 10% will be assigned to "challenge" question(s) which will be difficult for many students, but will be welcomed by the more experienced students. It is no coincidence that 10% is the normal difference between "A" and "A+". Question 3 is the “challenge question” for Assignment 2.

Assignment2-Fall2013V2

Embed Size (px)

DESCRIPTION

yeah

Citation preview

Page 1: Assignment2-Fall2013V2

COMP  1010  Fall  2013  Assignment  2     Page  1  of  6  

COMP  1010  Fall  2013  Assignment  2  Due  date:  Wednesday,  October  23,  2013,  before  midnight  

Material  Covered  • long  integers  • if  statements  • input  validation  using  Scanner  "has-­‐"  methods  • loops  (while,  do-­‐while,  for)  • File  input  using  Scanner  class  • String  processing  

Notes  and  Instructions  –  Please  follow  exactly  • Name  your  Java  files  as  follows:  <LastName><FirstName>A2Qn.java  (use  Q1,  Q2  and  Q3  for  

Questions  1,  2  and  3,  of  course).  For  example,  SmithJohnA2Q1.java  is  a  valid  name.  Your  classes  need  to  be  named  to  match.  

• Follow  the  programming  standards  posted  on  the  course  website  to  avoid  losing  marks.  • You  must  submit  the  “Blanket  Honesty  Declaration”,  also  on  the  course  website,  before  any  

assignment  in  COMP  1010  will  be  marked.  • To  submit  the  assignment  you  will  upload  your  .java  files  to  the  Dropbox  for  Assignment  2.  

Note  this  is  different  than  the  procedure  for  Assignment  1  (the  Dropbox  will  accept  .java  files).  

• You  will  also  copy  and  paste  the  output  of  your  test  runs  from  Dr  Java  into  .txt  files,  and  upload  them,  in  the  same  way.  

• Name  your  output  files  <LastName><FirstName>A2Qn-­‐output.txt  • If  you  use  JOptionPane  for  input,  nothing  will  appear  in  the  Interactions  panel.  For  that  

reason,  you  must  echo  all  such  input  using  System.out,  so  that  it  will  appear  in  the  output  that  you  hand  in.  

• Do  NOT  manipulate  your  output  in  any  way.  We  will  demonstrate  the  assignment  hand-­‐in  procedure  in  class.  

• To  be  eligible  to  earn  full  marks,  your  Java  programs  must  compile  and  run  upon  download,  without  requiring  any  modifications.  

• In  every  assignment  this  year,  90%  of  the  marks  will  be  assigned  to  normal  question(s)  with  a  moderate  degree  of  difficulty.  The  other  10%  will  be  assigned  to  "challenge"  question(s)  which  will  be  difficult  for  many  students,  but  will  be  welcomed  by  the  more  experienced  students.  It  is  no  coincidence  that  10%  is  the  normal  difference  between  "A"  and  "A+".  Question  3  is  the  “challenge  question”  for  Assignment  2.  

 

Page 2: Assignment2-Fall2013V2

COMP  1010  Fall  2013  Assignment  2     Page  2  of  6  

Question  1  -­‐  Credit  Card  Validator  (45%)  1  In  this  question  you  will  implement  an  algorithm  for  checking  a  16-­‐digit  credit  card  number  for  validity.  This  algorithm  is  useful  for  checking  for  common  errors  such  as  transposing  two  digits  when  entering  a  credit  card  number  on  a  web  page.  (Of  course,  it  can't  tell  whether  or  not  the  number  is  a  legitimate  account.)  You  can  try  it  on  your  own  credit  card  numbers  if  you  like.  

The  algorithm  works  as  follows:  

• Each  of  the  16  digits  is  in  either  an  even  or  odd  position;  the  leftmost  digit  is  even,  the  next  digit  is  odd,  etc.  

• From  each  digit  we  calculate  a  check  value:  for  a  digit  in  an  odd  position,  the  check  value  is  the  digit  itself;  for  a  digit  in  an  even  position  we  multiply  it  by  2  to  get  the  check  value,  and  if  the  check  value  is  greater  than  9  we  subtract  9.    

• The  check  values  are  totalled.  If  the  total  is  divisible  by  10,  the  credit  card  number  is  valid.  

For  example,  performing  the  algorithm  on  the  credit  card  number  5048  4801  4229  6497  gives  the  following  results:  

digit   position   check  value  5   even   5*2  =  10  -­‐  9  =  1  0   odd   0  4   even   4*2  =  8  8   odd   8  4   even   4*2  =  8  8   odd   8  0   even   2*0  =  0  1   odd   1  4   even   4*2  =  8  2   odd   2  2   even   2*2  =  4  9   odd   9  6   even   6*2  =  12  -­‐  9  =  3  4   odd   4  9   even   9*2  =  18  -­‐  9  =  9  7   odd   7  

Total 80  (valid)  

Create  a  Java  program  that  accepts  as  input  a  16-­‐digit  number  and  implements  the  above  algorithm  to  check  for  a  valid  credit  card  number.  Since  a  16-­‐digit  number  is  too  large  to  fit  into  an  int,  you  will  need  to  work  with  long  integer  values.  

 

                                                                                                               1  All  images  Microsoft  ClipArt  

Page 3: Assignment2-Fall2013V2

COMP  1010  Fall  2013  Assignment  2     Page  3  of  6  

Input:  Ask  the  user  for  a  credit  card  number  with  16  digits  (Note:  leading  zeros  are  NOT  permitted).  Use  System.in  (using  the  Scanner  class)  for  input  and  echo  the  input  using  System.out.println.  You  must  validate  the  input  by  checking  that  1)  the  input  is  a  valid  long  integer  (hint:  use  one  of  the  Scanner  "has-­‐"  methods),  and  2)  that  the  input  has  16  digits  (hint:  the  number  must  be  in  the  range  1,000,000,000,000,000  ≤  input  <  10,000,000,000,000,000).  Use  a  loop  to  accept  and  validate  input  so  that  the  user  must  enter  valid  input  before  proceeding  to  the  calculation  step.  Print  meaningful  messages  when  invalid  input  is  entered  (see  the  sample  output  below).  

Calculate:  calculate  the  check  value  using  the  algorithm  described  above.  You  will  use  a  loop  to  perform  this  calculation.  (Which  is  the  most  appropriate  type?)  

Output:  Print  (using  System.out)  the  total  check  value  and  a  message  indicating  whether  the  credit  card  number  is  valid  or  not.  

Here  is  sample  output  from  the  program.  Scanner  input  is  shown  in  bold  italic.  Enter a 16-digit credit card number: something else Input of 'something else' is not valid. Enter a 16-digit credit card number : 0000111122223333 Input of 111122223333 is not 16 digits. Enter a 16-digit credit card number: 12345678901234567 Input of 12345678901234567 is not 16 digits. Enter a 16-digit credit card number: 5048480142296497 Your card number is 5048480142296497 Checksum is 80 Card number is valid. Programmed by Stew Dent ** End of processing. **

Extra  feature  (not  required):  Echo  the  credit  card  number  as  four  groups  of  4  digits.  

Hand  in:  Your  Java  source  code,  according  to  the  instructions  on  page  1.  Also,  submit  two  sets  of  output  using  the  following  input  values:  5048  4801  4229  6497  and  1234  5678  9009  8765.  Your  output  should  also  show  that  invalid  input  is  detected  and  an  appropriate  message  is  printed  by  entering:  1)  non-­‐numeric  input,  2)  a  number  with  less  than  16  digits  and  3)  a  number  with  more  than  16  digits.  Put  all  your  output  into  one  file.  (Submit  exactly  2  files,  one  .java  file  and  one  .txt  file,  for  this  question.)  

 

Page 4: Assignment2-Fall2013V2

COMP  1010  Fall  2013  Assignment  2     Page  4  of  6  

Question  2  -­‐  File  Input  and  String  processing  (45%)  One  of  the  reasons  we  use  computers  is  their  ability  to  quickly  process  large  data  sets.  In  this  question  you  will  get  a  chance  to  do  just  that,  by  reading  information  from  a  text  file  and  processing  it.  Boilerplate  code  for  the  file  input  using  the  Scanner  class  is  provided;  you  will  provide  the  required  processing.  We  will  define  something  called  an  ascending  word  for  the  purposes  of  processing.  An  ascending  word  is  one  where  each  letter  is  either  equal  to  or  occurs  later  in  the  alphabet  than  the  preceding  letter.  That  is,  the  letters  in  the  word  are  found  in  order  in  the  alphabet.  For  example,  the  word  "envy"  is  an  ascending  word.  The  word  "invest"  is  not  ascending,  since  the  letters  'v'  and  'e'  are  not  in  order.  

Input:  You  will  read  data  from  the  file  dictionary.txt,  which  contains  20,069  words,  one  per  line.  Download  this  file  and  place  it  in  the  same  folder  as  your  Java  program.  The  boilerplate  code  for  performing  file  input  is  shown  highlighted  in  red  below:  

import java.io.*; // defines FileNotFoundException

public static void main(String[] args) throws FileNotFoundException{ String theWord;

Scanner inputFile = new Scanner(new File("dictionary.txt")); while(inputFile.hasNextLine()){ theWord = inputFile.nextLine();

// your code goes here }

Calculate:  Find  all  the  ascending  words  that  are  at  least  2  letters  long  (the  single  letter  words  are  not  interesting),  count  them  and  calculate  the  average  length  and  longest  length.  

Output:  Print  the  list  of  ascending  words.  Group  the  list  by  first  letter,  separating  each  group  with  a  blank  line.  Print  as  many  words  on  each  line  as  will  fit  into  an  80-­‐character  width,  but  do  not  go  over  80  characters.  Separate  the  words  with  a  single  blank  (but  don't  put  a  blank  at  the  beginning  of  a  line).  Print  the  total  number  of  ascending  words  found,  their  average  length  and  the  longest  length.  

Hand  in:  Your  Java  source  code,  according  to  the  instructions  on  page  1.  Also,  submit  the  output  from  running  your  program.  (Submit  exactly  2  files,  one  .java  file  and  one  .txt  file,  for  this  question.)  

Here  is  sample  output  from  the  program,  with  some  values  obscured.  Since  everyone’s  output  should  be  identical,  we  don’t  want  to  give  away  the  correct  values.  Searching for ascending words from the input file. abbe abbey abbot abc abet abort accent accept access accost ace act ad add adept ado adopt aegis affix afoot aft ago ah ahoy ail aim air airy all allot allow // more output here Found *** ascending words. Average word length is ******. Longest word length is *.

Page 5: Assignment2-Fall2013V2

COMP  1010  Fall  2013  Assignment  2     Page  5  of  6  

Question  3  -­‐  Password  Validator  (10%)  In  the  Challenge  question  you  will  implement  a  program  to  check  whether  a  password  meets  the  U  of  M  Identity  Management  System  (iridium)  validity  rules,  listed  below.  

1. Must  be  between  6  and  8  characters  in  length  2. Letters  and  numbers  only  3. Must  begin  with  a  letter  4. Must  contain  at  least  2  letters  5. Must  contain  at  least  1  number  6. Maximum  Occurrence  of  same  character:  4  7. Maximum  Repetitive:  2  (i.e.  you  cannot  have  a  sequence  of  3  or  more  of  the  same  

character)  8. Must  not  contain  your  userID  

Write  a  series  of  8  static  boolean  methods  that  implement  each  of  the  checks  listed  above.  You  may  also  write  other  methods  that  you  find  useful.  You  may  use  the  String  class  methods  length()  and  charAt(),  but  no  others,  and  you  may  not  use  any  Character  class  methods.  

Your  program  will  accept  two  inputs  from  the  user,  a  userID  and  password.  You  may  use  either  JOptionPane  or  Scanner  for  input.  Echo  the  input  regardless  of  which  technique  is  used  (we  can't  see  the  JoptionPane  dialog,  or  what  was  typed  into  the  Scanner  text  box).  It  will  then  perform  the  password  checks  in  the  order  above,  printing  a  pass/fail  message  for  each  check.  It  will  also  print  a  summary  message,  indicating  either  that  the  password  passes  all  tests,  or  the  number  of  tests  failed.  Sample  output  is  shown  below  (using  Scanner  for  input).  

Hand  in:  Your  java  source  program  and  enough  test  runs  showing  all  8  checks  failing  (you  will  need  more  than  1  but  probably  less  than  8)  and  one  test  where  the  password  passes.  

 > run PasswordChecker Enter your userid and password: umdent24 Bif44buT Checking userID: umdent24 and password: Bif44buT password passes length check. password passes letters and numbers only check. password passes begins with letter check. password passes minimum letter check. password passes minimum number check. password passes same character check. password passes maximum repetitive check. password passes userid check. Password 'Bif44buT' passes all tests, congratulations. > run PasswordChecker Enter your userid and password: umdent2 passswd Checking userID: umdent2 and password: passswd

Page 6: Assignment2-Fall2013V2

COMP  1010  Fall  2013  Assignment  2     Page  6  of  6  

password passes length check. password passes letters and numbers only check. password passes begins with letter check. password passes minimum letter check. password fails minimum number check. password passes same character check. password fails maximum repetitive check (maximum 2 allowed). password passes userid check. Password 'passswd' failed 2 tests. > run PasswordChecker Enter your userid and password: ng245 sing2456 Checking userID: ng245 and password: sing2456 password passes length check. password passes letters and numbers only check. password passes begins with letter check. password passes minimum letter check. password passes minimum number check. password passes same character check. password passes maximum repetitive check. password fails userid check. Password 'sing2456' failed 1 test. Enter your userid and password: umdent24 1BadPswd Checking userID: umdent24 and password: 1BadPswd password passes length check. password passes letters and numbers only check. password fails begins with letter check. password passes minimum letter check. password passes minimum number check. password passes same character check. password passes maximum repetitive check. password passes userid check. Password '1BadPswd' failed 1 test. > run PasswordChecker Enter your userid and password: umdent24 aa1aa2aa Checking userID: umdent24 and password: aa1aa2aa password passes length check. password passes letters and numbers only check. password passes begins with letter check. password passes minimum letter check. password passes minimum number check. password fails same character check (maximum 4 allowed). password passes maximum repetitive check. password passes userid check. Password 'aa1aa2aa' failed 1 test.