Question

In: Computer Science

Unit 4: Discussion - Part 1 Question Read the lecture for Chapter 4, and then answer...

Unit 4: Discussion - Part 1

Question

Read the lecture for Chapter 4, and then answer the following:

Think of a brief example where you can use conditional statements and write it in correct Java syntax. You may use if-else-if statements or the switch statement. You may also use the example you wrote in pseudo code for the discussion thread 2 during week 2 and convert it to Java, if you think it is appropriate. Be mindful that we are not talking about homework here. Do not post homework in this or any other discussion thread. As always, show a screen capture of the program running.
After writing your example, please explain if you think your example could also be written in the other conditional statement you did not chose. Explain why you think so, or why not.

Solutions

Expert Solution

If you have any problem with the code feel free to comment

Program

import java.util.Scanner;

public class Test {
   public static void main(String[] args) {
       //basic calculator app
      
       Scanner sc = new Scanner(System.in);
       int num1, num2, choice;
      
       System.out.print("Enter the first number: ");
       num1 = sc.nextInt();
       System.out.print("Enter the second number: ");
       num2 = sc.nextInt();
      
       System.out.println("1. Add\n2. Subtract\n3. Multiply\n4. Divide");
       System.out.print("Enter your choice: ");
       choice = sc.nextInt();
      
       switchOperation(num1, num2, choice);
       ifelseOperation(num1, num2, choice);
      
       sc.close();
   }
  
   /*Switch case is more preferred as depending upon the choice the calculation is performed
   *and it will be faster than if/else staetemnet in this case
   *It also improves the clarity and readability of the program*/

   private static void switchOperation(int num1, int num2, int choice) {
       System.out.println("********The Switch Statement********");
       switch(choice) {
       case 1:
           System.out.println("The sum is "+(num1+num2));
           break;
       case 2:
           System.out.println("The difference is "+(num1-num2));
           break;
       case 3:
           System.out.println("The multiplication result is "+(num1*num2));
           break;
       case 4:
           System.out.println("The division result is "+(num1/num2));
           break;
       default:
           System.out.println("Invalid choice!");
           break;
       }  
   }
  
   /*The calculation can also be represented in if/else statement but
   * if else is used for checking boolean expressions which makes it slower here */

   private static void ifelseOperation(int num1, int num2, int choice) {
       System.out.println("********If Else Statement********");
       if(choice == 1)
           System.out.println("The sum is "+(num1+num2));
       else if(choice == 2)
           System.out.println("The difference is "+(num1-num2));
       else if(choice == 3)
           System.out.println("The multiplication result is "+(num1*num2));
       else if(choice == 4)
           System.out.println("The division result is "+(num1/num2));
       else
           System.out.println("Invalid choice!");      
   }
}

Output


Related Solutions

Unit 4: Discussion - Part 2 No unread replies.No replies. Question Read the lecture for Chapter...
Unit 4: Discussion - Part 2 No unread replies.No replies. Question Read the lecture for Chapter 4, and then answer the following: This week covered the many different types of loops in Java: while, do, and for. Think of a brief example where you can use loops and write it in correct Java syntax. You may use the loop example you wrote in pseudo code for the discussion thread 2 during week 2 and convert it to Java, if you...
Read the lecture for Chapter 7, the following text and then answer the question at the...
Read the lecture for Chapter 7, the following text and then answer the question at the end. Notice the grading criteria this week is different than other weeks Constructors Constructor methods inside a class are methods that determine how an object is going to be created when some program instantiate one of them using the “new” clause. Their main use is to initialize the object’s instance variables. For example, last week I presented the Chair class that represented chairs. This...
Unit 1 Discussion Forum 2: Transforming Leaders. Read the case at the end of Chapter 1...
Unit 1 Discussion Forum 2: Transforming Leaders. Read the case at the end of Chapter 1 on Transforming Leaders. Respond to the discussion questions at the end of case to frame your post. You don't need to answer every question, but use them to guide your responses in the case analysis. The case is also listed below: Cameryn is the vice president for professional development at a large metropolitan hospital in the Midwest. She has been in her role for...
please don't copy from previous post and answer all Chapter 4 - Discussion Question (DQ) #...
please don't copy from previous post and answer all Chapter 4 - Discussion Question (DQ) # 2, 7, 11, 13, 21, 25, 30, 34 (DQ2) What is the difference between purchasing and strategic sourcing? (DQ7) In what ways is corporate social responsibility different from business ethics? (DQ11) What are the benefits of sustainable sourcing? Can firms actually make money from sustainable sourcing? Do you think it is a good practice? Why ? (DQ13) How could you apply sustainability to a...
Chapter 4 Lecture Assignment (part 1) Hide or show questions eBook Calculator Appendix: Completing an End-of-Period...
Chapter 4 Lecture Assignment (part 1) Hide or show questions eBook Calculator Appendix: Completing an End-of-Period Spreadsheet Alert Security Services Co. offers security services to business clients. Complete the following end-of-period spreadsheet for Alert Security Services Co. If a box does not require an entry, leave it blank. Alert Security Services Co. End-of-Period Spreadsheet (Work Sheet) For the Year Ended October 31, 2019 Adjusted Trial Balance Income Statement Balance Sheet Account Title Dr. Cr. Dr. Cr. Dr. Cr. Cash 373...
Consumer Decision Process,, Read Chapter 5 or listen to the lecture video on Chapter 5 (under...
Consumer Decision Process,, Read Chapter 5 or listen to the lecture video on Chapter 5 (under Course Content for this week). The lecture video discusses the consumer decision model first.   Think of a product that you purchased within the past six (6) months. Answer these questions relative to the purchased product. Your answers should align with how these stages are done based on the consumer decision model in the text.  Your submission should be attached, NOT typed in the submission box....
Use the data in BUSI1013 Bank Dataset.xlsx (Unit 1 Question 2) to answer this question. (4...
Use the data in BUSI1013 Bank Dataset.xlsx (Unit 1 Question 2) to answer this question. (4 points for each part; 8 points total) Perform a statistical test to see whether the average loans of customers before the change at the Brock and Chase branch are different. Construct a 95% confidence interval for the difference of average loans of customers before the change at the Brock and Chase branch. Loans After Increase in Loans 48.1 12.9 62.4 8.1 51.1 12.6 63.3...
By now, you should have listened to the audio lecture, read the chapter, and taken the...
By now, you should have listened to the audio lecture, read the chapter, and taken the self-assessment and quiz. For this discussion activity, I want you to respond to the following question in the Discussion area in this learning module. Tower cranes have tall vertical, latticed masts or towers and horizontal booms called "jibs". There are several different types of jibs available for tower cranes. List at least three jibs and briefly describe each jib type.
By now, you should have listened to the audio lecture, read the chapter, and taken the...
By now, you should have listened to the audio lecture, read the chapter, and taken the self-assessment and quiz. For this discussion activity, I want you to respond to the following question in the Discussion area in this learning module. Clamshells are one of the special excavator types and sometimes clamshells are compared with draglines. List and discuss at least three different points between the two (Dragline vs. Clamshell) in terms of operations, productivity, etc. Do research on the internet,...
There is no answer to the Chapter 3 discussion question 13 for Pearsons Federal Taxation 2020....
There is no answer to the Chapter 3 discussion question 13 for Pearsons Federal Taxation 2020. I want to make sure the answer I have is correct. Here is the question: Crane Corporation incurs a $75,000 NOL in the current year. In which years can Crane use this NOL? What limitations might apply?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT