Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following Java code is based on Liang's: public class Switch { public static String zodiak(final int year) { switch (year % 12) { case

The following Java code is based on Liang's:

public class Switch { public static String zodiak(final int year) { switch (year % 12) { case 0: return "monkey"; case 1: return "rooster"; case 2: return "dog"; case 3: return "pig"; case 4: return "rat"; case 5: return "ox"; case 6: return "tiger"; case 7: return "rabbit"; case 8: return "dragon"; case 9: return "snake"; case 10: return "horse"; case 11: return "sheep"; } // switch throw new IllegalArgumentException("year cannot be negative"); } // zodiak() public static void main(String[] args) { final int year = Integer.parseInt(args[0]); System.out.println(year + " is year of the " + zodiak(year) + "."); } // main() } // class Switch

Rewrite the above code in Python replacing the Java switch statement with a Python dictionary as described at https://stackoverflow.com/questions/11479816/what-is-the-pythonequivalent-for-a-case-switch-statement# This means you will have a dictionary where the keys are the integers 0 through 11, and each key maps to a function that takes no arguments and returns a single string, e.g., 'rabbit' or 'sheep.'

As an example, my solution includes this function: def option6(): return "tiger" The dictionary may be global.

When I run my program, a few times, I can see this output: 1269:hw/03/src> ./zodiak.py 1970 1970 is the year of the dog. > ./zodiak.py 1980 1980 is the year of the monkey. > ./zodiak.py 1990 1990 is the year of the horse. > ./zodiak.py 2000 2000 is the year of the dragon.

Your output should be similar, though you have the option of prompting for the year and then entering it from the keyboard, like this: > ./zodiak.py Please enter a birth year: 2010 2010 is the year of the tiger. > ./zodiak.py Please enter a birth year: 2011 2011 is the year of the rabbit. Also, the Java code may throw an exception. We can ignore this for the Python version, since if an invalid key is passed to a dictionary, that throws an exception. It is fine not to catch this exception in this assignment. Your solution must use at least one function other than the twelve different switch options. Any function of more than one loine must have a docstring. You must have your name and a brief description of the program at the top of the source code file, following the shebang (if there is a shebang).

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

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions

Question

Question What is the advantage of a voluntary DBO plan?

Answered: 1 week ago

Question

Question How is life insurance used in a DBO plan?

Answered: 1 week ago