Question

In: Computer Science

For this assignment, you will apply what you learned in analyzing Java™ code so far in...

For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following:

  • Accept user input that represents the number of sides in a polygon. Note: The code to do this is already written for you.
  • If input value is not between 3 and 5, display an informative error message
  • If input value is between 3 and 5, use a switch statement to display a message that identifies the correct polygon based on the number of sides matching the input number (e.g., triangle, rectangle, or polygon)

Complete this assignment by doing the following:

  1. Download tnd unzip ahe linked Week Two Coding Assigment Zip File.
  2. Read the file carefully, especially the explanatory comments of what the existing code does.
  3. Add your name and the date in the multi-line comment header.
  4. Refer to the following linked Week Two Recommended Activity Zip File to see examples of how to code all of the Java™ statements (i.e., switch, println(), and if-then-else) you will need to write to complete this assignment.
  5. Replace the following lines with Java code as directed in the file:
  • LINE 1
  • LINE 2
  1. Comment each line of code you add to explain what you intend the code to do.
  2. Test and modify your Java™ program until it runs without errors and produces the results as described above.

Program Summary: This program demonstrates these basic Java concepts:
* - defining variables of different types
* - if-then and if-then-else logic
* - constructing a string to display onscreen
* - switch logic
*
* To complete this assignment, you will add code where indicated. The
* behavior of your completed assignment should be to accept an input
* value for the number of sides of a two-dimensional figure. Based on that value,
* your code should display the type of figure that corresponds to the number of polygon angles
* indicated (3=triangle, 4=rectangle, etc.)
*
* Here are the specific requirements:
*
* After the user types in a value from 3 to 5 inclusive (i.e., 3, 4, or 5):
*
* 1. Your code determines whether the input value is out of range (less than 3 or more than 5)
* and, if so, displays a meaningful error message on the screen and ends the program.
*
* 2. Because you will be comparing a single expression (the input value) to multiple constants (3, 4, and 5),
* your code should use a switch statement to display the following message onscreen:
*
* If user inputs 3, onscreen message should say "A triangle has 3 sides."
* If user inputs 4, onscreen message should say "A rectangle has 4 sides."
* If user inputs 5, onscreen message should see "A pentagon has 5 sides."
*
* 3. Be sure to test your program. This means running your program multiple
* times with test values 3, 4, 5, as well as at least two values that fall outside that range
* (one lower than the lowest and one higher than the highest) and making sure
* that the correct message displays for each value you input. Also be sure
* that running your program does not cause any compiler errors.
***********************************************************************/
package week2codingassignment;

import java.util.Scanner;

public class PRG420Week2_CodingAssignment {

public static void main(String[] args) {

String userInputStringOfAngles; // Declare a variable of type String to capture user input
int numberOfAngles; // Declare a variable of type int to hold the converted user input

Scanner myInputScannerInstance = new Scanner(System.in); // Recognize the keyboard
System.out.print("Please type the integer 3, 4, or 5 and then press Enter: "); // Prompt the user
userInputStringOfAngles= myInputScannerInstance.next(); // Capture user input as string
numberOfAngles = Integer.parseInt(userInputStringOfAngles); // Convert the string to a number in case this will be useful later
  
// LINE 1. CODE TO DETERMINE WHETHER USER INPUT IS OUT OF BOUNDS GOES HERE
  
  
// LINE 2. SWITCH CODE TO PRINT CORRECT "SHAPE" MESSAGE BASED ON USER INPUT GOES HERE
  

}
}

Solutions

Expert Solution

package week2codingassignment;

import java.util.Scanner;

public class PRG420Week2_CodingAssignment {

public static void main(String[] args) {

String userInputStringOfAngles; // Declare a variable of type String to capture user input
int numberOfAngles; // Declare a variable of type int to hold the converted user input

Scanner myInputScannerInstance = new Scanner(System.in); // Recognize the keyboard
System.out.print("Please type the integer 3, 4, or 5 and then press Enter: "); // Prompt the user
userInputStringOfAngles= myInputScannerInstance.next(); // Capture user input as string
numberOfAngles = Integer.parseInt(userInputStringOfAngles); // Convert the string to a number in case this will be useful later
  
// LINE 1. CODE TO DETERMINE WHETHER USER INPUT IS OUT OF BOUNDS GOES HERE
// Checking on if condition that value shold not less than 3 or greater than 5
if(numberOfAngles <3 || numberOfAngles>5)
{
   // Printing message for invalid input
   System.out.println("The input value is not valid. Please input 3, 4 or 5.");
}
  
else {
   // LINE 2. SWITCH CODE TO PRINT CORRECT "SHAPE" MESSAGE BASED ON USER INPUT GOES HERE

   // Executing switch statement on user input
   switch(numberOfAngles)
   {
       // case 3 matches to user input =3 and likewise other cases and printing the relative message  
       case 3: System.out.println("A triangle has 3 sides.");
       //Break statement for stopping the switch case execution once any case condition is satisfied.
       break;
       case 4: System.out.println( "A rectangle has 4 sides.");
           break;
       case 5: System.out.println("A pentagon has 5 sides.");
           break;
   //default is used for displaying default message if it didn't matches any condition
       default:
           System.out.println("Default messsage"); // though the code will never reach here as we are validating user input in if condn.
   }
}
  }
}
// Output :


Related Solutions

For this assignment, you will apply what you learned in analyzing a simple Java™ program by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program. The Java™ program you write should do the following: Display a prompt on the console asking the user to type in his or her first name Construct the greeting string "Hello, nameEntered!" Display the constructed greeting on the console Complete this assignment by doing the following: Download and unzip the linked zip file. Add comments to the code by...
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze...
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze Assignment Zip File, and predicting the results. You will also examine both the code and the output for inconsistencies and clarity. This Java™ code includes examples of for, while, and do-while loops. Carefully read through the code line by line, then answer the following questions in a Microsoft® Word document: What is the output of the program as it is written? What improvement(s) could...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies on localization to format currencies and dates. In NetBeans, copy the linked code to a file named "Startercode.java". Read through the code carefully and replace all occurrences of "___?___" with Java™ code. Note: Refer to "Working with Dates and Times" in Ch. 5, "Dates, Strings, and Localization," in OCP: Oracle® Certified Professional Java® SE 8 Programmer II Study Guide for help. Run and debug...
Reflect on and relate to what you have learned so far in the course with respect...
Reflect on and relate to what you have learned so far in the course with respect to your ability to calculate gross domestic product and its impact on the economy’s business cycle, unemployment, and inflation; using gross domestic product formulate recommendations for a government’s role in achieving full employment in an economy.
What have you learned so far about what determines prices such as the rent for an...
What have you learned so far about what determines prices such as the rent for an apartment? If people cannot afford rent, what are they to do? What are the consequences of controlling rent and for not controlling it? Is the current system fair?
Using the information you have learned so far in this class, state what you think will...
Using the information you have learned so far in this class, state what you think will cause market fluctuations over the next few years as the economy struggles to recover.
Overview In this assignment you will apply what you have learned about standard deviation and normal...
Overview In this assignment you will apply what you have learned about standard deviation and normal curves to solve a number of problems, including finding the area under selected sections of a normal curve using z-scores, then solve a problem using normal distributions. Part 1: The weights of all one hundred (100) 9th graders at a school are measured, and it is found that the mean of all the measurements is 100 lbs., with a standard deviation of 15 lbs.  ...
For one important and interesting idea that you have learned so far in the course based,...
For one important and interesting idea that you have learned so far in the course based, write a one page essay beginning with the following statements: Although before this class I believed …….., in fact……..Or, Although it is tempting to believe……., in fact…..Or, Although it might seem that …………… is true, in fact…… Then explain why you or others could believe the idea that is fact incorrect. In other words, what is logical or compelling about this incorrect idea. Then...
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class: === 1) Basic (100' pts total) === Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates...
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class: === 1) Basic (100' pts total) === Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT