Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

e Courses Organizations Help QUESTION 1 Let s be a String of length 1 consisting of only characters 'O' and 1. We define a parity

image text in transcribed
e Courses Organizations Help QUESTION 1 Let s be a String of length 1 consisting of only characters 'O' and 1. We define a parity character p to be a 0 or '1' such that when p is appended to the end of s to form a String t, the number of 1' characters in t will be even. ek) For example, let s be the String "00101". p can be either 0' or '1'.Suppose we let p be '1. Then when we append p onto the end of s to form t we will arrive at t "001011". The number of '1' characters in t is odd, but is supposed to be even. Therefore, we made a wrong choice: p should be '0' so t would be "001010, which now contains two (an even number) '1' characters. The parity character can be determined thusly. Note: to get the character at index n in a string s we call the charAtfn) method on s, e.g. to get the first character of s we would write s.charAt(0). 1. If the length of s is 1, then p is just the first character of s. For example, if s is "0 then p is 'O' and if s is "1* then p is '1 2. If the first character of s matches the parity character of s.substring 1) then p is 'o' 3. If the first character of s does not match the parity character of s.substring(1) then p is '1 Note that s.substringl1) is the Java String class method that returns a substring of s starting at index 1 and continuing to the end of s. For example, if s is "Fred" then s.substring(1) returns the string "red" For this question, complete the code below which is a recursive method named char parity(String s) which determines and returns the parity character p for String s. Note: we are not asking you to return the String t, we want you to return the parity character p. For example, if we call parity0 with the specified strings, we will get back the associated parity character char pl-parity(") char p2 parity("i) char p3-parity("0011001010m): 17 Rule 2: t would be "00110010100" so p3 is assigned 0 char p4 parity("1011011") Rule 3: t would be 10110111" so p4 is assigned 1 // Rule 1: t would be -00" so pl is assigned ". /I Rule 1: t would be 1" so p2 is assigned 1" char parity (String s) if ( #) { // Implement Rule 1 ) else iE #} { // Implenent Rule 2 Save All Answers

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

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

2 points

Answered: 1 week ago

Question

2. Identify the purpose of your speech

Answered: 1 week ago