Question

In: Computer Science

e. Task 5 (3 pts): (This task uses Strings and an if..then..elseif cascade) A program that...

e. Task 5 (3 pts): (This task uses Strings and an if..then..elseif cascade) A program that prompts the user for their party affiliation (Democrat, Republican, or Independent) and responds accordingly with a Donkey,

Solutions

Expert Solution

SOLUTION IN JAVA:

import java.util.Scanner;

class Main

{

public static void main(String[] args) {

// creating scanner object for taking input

Scanner scan = new Scanner(System.in);

// taking user input

System.out.print("Enter your party affiliation: ");

String party_name = scan.next();

// if-then-else cascading - what it means basically is

// else part of each if is nested until all conditions are checked

if(party_name.equalsIgnoreCase("Republican")){

System.out.println("Elephant");

}

else{

if(party_name.equalsIgnoreCase("Independent")){

System.out.println("Eagle");

}

else{

System.out.println("Donkey");

}

}

}

}

SAMPLE OUTPUT:

SOLUTION IN PYTHON:

def main():
    # asking user for input
    party_name = input('Enter your party affiliation: ')
  
    # if-then-else cascading
    if party_name.lower() == 'republican':
        print('Elephant')
    else:
        if party_name.lower() == 'independent':
            print('Eagle')
        else:
            print('Donkey')

if __name__ == '__main__':
    main()

SCREENSHOT :


Related Solutions

Question 3: C-Strings and pointers (10 pts) [5] The following function appends C-string str2, to C-string...
Question 3: C-Strings and pointers (10 pts) [5] The following function appends C-string str2, to C-string str1. Complete the function using array access, i.e. str1[i], but no pointer access. Note: For both part a) and part b) you may not use any library function calls (e.g. you cannot use strlen, strcat, etc.) // Append strt2 to str1 void my_strcat(char str1[], char str2[]) { //YOUR CODE HERE   } // example of using my_strcat() #include <stdio.h> int main(void) { char my_str1[50] =...
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
The third task is to further refine the program so that it uses a loop to...
The third task is to further refine the program so that it uses a loop to keep asking the user for numbers until the user gets the correct number. HINT: You need functions eight from the sample code (see previous activity). Steps Please use Netbeans to revise your NumberGuessGame program to satisfy the above requirements. Please submit a print screen of your successful code including the Output window with the "Build Successful" message to this assignment.
Exercise 3 – Strings Using a function Write a program that prompts the user to enter...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter two inputs: some text and a word. The program outputs the starting indices of all occurrences of the word in the text. If the word is not found, the program should output “not found”. Example1: Input1: my dog and myself are going to my friend Input2: my Output: 0 11 31 Example 2: Input1: Programming is fun Input 2: my Output: not found
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for...
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for string in strings: st = "f'" + string + ": {" + string + "}'" print_stmt = 'print(' + st + ', end="; ")' # Uncomment the following print statement to see the statement to be executed. # Each will appear on a separate line. # print(f'\n{print_stmt}') exec(print_stmt) print() print_lv(['list(r)', 'r[-2:3:-1]', 'list(r[-2:3:-1])']) OUTPUT: list(r): [10, 13, 16, 19, 22, 25, 28, 31, 34, 37];...
Consider the decay process E+ -> p + pi0. (a)(5 pts) What is the Q value...
Consider the decay process E+ -> p + pi0. (a)(5 pts) What is the Q value of this reaction in MeV?(b)(5 pts) What is the energy of each particle (in MeV) if the E+ decays at rest? Use conserva-tion of energy and momentum.
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
3a) Let ?={????:0≤?≤?}. How many of the following strings are in ?∗? ?,??????,????,???,????,????,?????,???? 3 4 5...
3a) Let ?={????:0≤?≤?}. How many of the following strings are in ?∗? ?,??????,????,???,????,????,?????,???? 3 4 5 6 b) Let ? be any NFA with a single final state. If we convert ? into a right-linear grammar ?, the number of production rules in ? will be equal to number of transitions in ? number of states in ? plus 1 number of states in ? number of transitions in ? plus 1
(3 pts) Solve the initial value problem 25y′′−20y′+4y=0, y(5)=0, y′(5)=−e2. (3 pts) Solve the initial value...
(3 pts) Solve the initial value problem 25y′′−20y′+4y=0, y(5)=0, y′(5)=−e2. (3 pts) Solve the initial value problem y′′ − 2√2y′ + 2y = 0, y(√2) = e2, y′(√2) = 2√2e2. Consider the second order linear equation t2y′′+2ty′−2y=0, t>0. (a) (1 pt) Show that y1(t) = t−2 is a solution. (b) (3 pt) Use the variation of parameters method to obtain a second solution and a general solution.
An mRNA has the sequence 5’ GCCACCAUGGGGGGAAGGUGAAGACC 3’. (3 pts.) Write the sequence of template and...
An mRNA has the sequence 5’ GCCACCAUGGGGGGAAGGUGAAGACC 3’. (3 pts.) Write the sequence of template and nontemplate strands of the gene encoding this mRNA. Clearly indicate which is which and include 5’ and 3’ polarity. Change the font (where it says paragraph above) to "preformatted" to get a constant-width font the will make your sequence more readable. (3 pts) Use the genetic code to write the N to C sequence of the protein specified by this RNA. Remember that the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT