In this assignment, you will write a recursive method.This is yet another version of Assignment 1. You may use your own solution, or use the one provided under Week 1. You are to re-write...

1 answer below »
In this assignment, you will write a recursive method.This is yet another version of Assignment 1. You may use your own solution, or use the one provided under Week 1. You are to re-write theisCorrectmethod so that it isrecursive. The code will not be long but there will be a lot of planning and thinking that should come before coding. The first thing you need to remember is that there should be no loop in this method anymore. The loop is replaced by recursive calls to itself. Think of its base case(s) and what/how recursion can be worked in.No credit shall be given if yourisCorrectmethod is not recursive.There is no change to the user interface of your code. From the user's perspective, the program is the same. However, yourisCorrectmust be implemented using recursion. Be sure to focus on the correctness of yourisCorrectmethod in your video.Comments:

  • Add Javadoc to each class, and each method. Each class must have, at the least, a purpose, @author, @version (date). Each method must have, at the least, a purpose, @param(s) as needed, and @return if needed.

  • Add inline or block comments and non-trivial parts of your code.





package assignment1mastermain; import java.util.Random; /** * * @author CPR * @version 05.29.20 */ public class Password { private int[] code; //the encoding digits 0 to 9 private int max; //the max value for each digit's encoding /** * Fills the code array with random numbers between 1 and max inclusive * @param mac the max value in the code array */ public Password (int max) { this.max = max; code = new int [10]; Random rand = new Random(); for (int i = 0; i < code.length;="" i++)="" code[i]="rand.nextInt(max)" +1;="" }="" **="" *="" returns="" the="" string="" representation="" of="" the="" object="" *="" @return="" includes="" the="" max="" value="" and="" the="" code="" array,="" *="" appropriately="" labeled="" with="" its="" indices="" */="" public="" string="" tostring(){="" string="" result="max =" +="" max="" +="" "\n="" pin:="" ";="" for="" (int="" i="0;">< code.length;="" i++)="" result="" +="i" +="" "="" ";="" result="" +="\nCode: " ;="" for="" (int="" val="" :="" code)="" result="" +="val" +="" "="" ";="" return="" result;="" }="" **="" *="" determines="" weather="" encoded="" pin="" matched="" the="" original="" pin="" based="" on="" the="" code="" array="" *="" @param="" origpin="" the="" original="" pin="" un-encoded="" *="" @param="" endcodedpin="" the="" encoded="" pin="" being="" tested="" for="" correctness="" *="" @return="" true="" if="" the="" encoded="" pin="" is="" correct,="" false="" otherwise="" */="" public="" boolean="" iscorrect="" (int="" origpin,="" int="" encodedpin){="" boolean="" correct="true;" int="" orig_digit;="" int="" encoded_digit;="" while="" (origpin="" !="0" &&="" encodedpin="" !="0){" extract="" the="" ith="" digit="" orig_digit="origPIN" %="" 10;="" encoded_digit="encodedPIN" %="" 10;="" system.out.println="" (orig_digit="" +="" "="" "="" +="" encoded_digit);//="" for="" debugging="" drop="" that="" ith="" digit="" for="" the="" next="" cycle="" origpin="origPIN" 10;="" encodedpin="encodedPIN" 10;="" if="" (code[orig_digit]="" !="encoded_digit)" correct="false;" }="" return="" correct;="" }="" }="" package="" assignment1mainweek5;="" import="" java.util.random;="" import="" java.util.scanner;="" import="" java.io.file;="" **="" *="" *="" @author="" cpr="" stiglianos="" *="" @version="" july282021="" *="" */="" public="" class="" assignment1mainweek5="" {="" public="" static="" void="" main(string[]="" args)="" {="" int="" actual_pin,="" encoded_pin;="" random="" rand="new" random();="" scanner="" scan="new" scanner(system.in);="" password="" pw;="" system.out.print("enter="" the="" actual="" pin:="" ");="" actual_pin="scan.nextInt();" while="" (actual_pin="" !="-1)" {="" pw="new" password="" (rand.nextint(6)="" +="" 3);="" system.out.println("actual="" pin=" + actual_PIN + " ;="" "="" +="" pw);="" ask="" user="" for="" the="" encoded="" pin="" system.out.print("please="" enter="" your="" encoded="" pin="" :="" ");="" encoded_pin="scan.nextInt();" check="" whether="" the="" encoded="" pin="" matches="" the="" actual="" pin="" if="" (pw.iscorrect(actual_pin,="" encoded_pin))="" system.out.println("you="" are="" right!");="" else="" system.out.println("not="" correct,="" sorry!");="" ask="" user="" for="" the="" next="" actual="" pin="" system.out.print("\n\nenter="" the="" next="" actual="" pin="" :="" ");="" actual_pin="scan.nextInt();" }="" }="" }="" assignment="" 1="" solution/.ds_store="" __macosx/assignment="" 1="" solution/._.ds_store="" assignment="" 1="" solution/password.java="" assignment="" 1="" solution/password.java="" package assignment1mastermain;="" import java.util.random;="" **=""  *=""  * @author cpr=""  * @version 05.29.20=""  */="" public class password {=""     private int[] code; //the encoding digits 0 to 9=""     private int max; //the max value for each digit's encoding=""     =""     /**=""      * fills the code array with random numbers between 1 and max inclusive=""      * @param mac the max value in the code array=""      */=""     =""     public password (int max) {=""         this.max =" max;"         code =" new int [10];"         random rand =" new Random();"         =""         for (int i ="">< code.length; i++)                code[i] =" rand.nextInt(max) +1;"     }=""     =""     /**=""      * returns the string representation of the object =""      * @return includes the max value and the code array, =""      * appropriately labeled with its indices=""      */=""     public string tostring(){=""         string result =" "max =" + max + "\n PIN: ";"         for (int i ="">< code.length; i++)             result += i + " ";         result += "\ncode: ";         for (int val : code)             result += val + " ";         return result;              }     /**      * determines weather encoded pin matched the original pin based on the code array      * @param origpin the original pin un-encoded      * @param endcodedpin the encoded pin being tested for correctness      * @return true if the encoded pin is correct, false otherwise      */     public boolean iscorrect (int origpin, int encodedpin){         boolean correct = true;         int orig_digit;         int encoded_digit;                  while (origpin != 0 && encodedpin !=0){             //extract the ith digit             orig_digit = origpin % 10;             encoded_digit = encodedpin % 10;             //system.out.println (orig_digit + " " + encoded_digit);// for debugging                         //drop that ith digit for the next cycle             origpin=origpin /10;             encodedpin = encodedpin / 10;                          if (code[orig_digit] != encoded_digit)                 correct = false;                      }         return correct;     }             } __macosx/assignment 1 solution/._password.java             result +=" i + " ";"         result +=" "\nCode: ";"         for (int val : code)=""             result +=" val + " ";"         return result;=""         =""     }=""     /**=""      * determines weather encoded pin matched the original pin based on the code array=""      * @param origpin the original pin un-encoded=""      * @param endcodedpin the encoded pin being tested for correctness=""      * @return true if the encoded pin is correct, false otherwise=""      */=""     public boolean iscorrect (int origpin, int encodedpin){=""         boolean correct =" true;"         int orig_digit;=""         int encoded_digit;=""         =""         while (origpin !=" 0 && encodedPIN !=0){"             //extract the ith digit=""             orig_digit =" origPIN % 10;"             encoded_digit =" encodedPIN % 10;"             //system.out.println (orig_digit + " " + encoded_digit);// for debugging=""            =""             //drop that ith digit for the next cycle=""             origpin="origPIN /10;"             encodedpin =" encodedPIN / 10;"             =""             if (code[orig_digit] !=" encoded_digit)"                 correct =" false;"             =""         }=""         return correct;=""     }=""        =""     }="" __macosx/assignment="" 1="">
Answered 2 days AfterAug 04, 2021

Answer To: In this assignment, you will write a recursive method.This is yet another version of Assignment 1....

Kshitij answered on Aug 07 2021
145 Votes
Assignment1MainWeek5.java
Assignment1MainWeek5.java
/*   Created by IntelliJ IDEA.
 *   Author: Kshi
tij Varshney (kshitijvarshne1)
 *   Date: 06-Aug-21
 *   Time: 11:39 AM
 *   File: Assignment1MainWeek5.java
 */
package August.aug06_21;
import java.util.Random;
import java.util.Scanner;
/**
 * @author CPR // StiglianoS
 * @version July282021
 */
public class Assignment1MainWeek5 {
    public static void main(String[] args) {
        int actual_PIN, encoded_PIN;
        Random rand = new Random();
        Scanner scan = new Scanner(System.in);
        Password pw;
        System.out.print("Enter the actual PIN: ");
        actual_PIN = scan.nextInt();
        while (actual_PIN != -1) {
            pw = new Password(rand.nextInt(6) + 3);
            System.out.println("Actual PIN = " + actual_PIN + "; " + pw);
            //ask user for the encoded PIN
            System.out.print("Please enter your encoded PIN : ");
            encoded_PIN = scan.nextInt();
            //check whether the en...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here