Starting from:
$35

$29

Project 2 The Shape Machine Solution

Background




You have been assigned to construct a mathematical shape machine. This shape machine can provide a variety of functions. The machine takes a radius/side and will compute both the area and the perimeter/circumference. However, most of the menu only functions with words. Other than the input for the radius/sides, the machine will fail if it detects numbers. It is your job to construct this machine, to ensure that everyone in the world can learn about shapes.




Description




This project will be utilizing Strings and Math methods to perform several operations with shapes. The user will create a simple menu with multiple options. The input should be done using Strings. For example, if you wanted to perform functions for the square, the user would type ‘Square.' There will also be an exit command within the menu that lets the user close the program by typing in the word ‘Exit.'







Your Assignment




The first part of the assignment will involve the user entering a password to have access to the machine. The password will be in the form of the date. The user will have to parse the date String and concatenate the elements of the date together to be allowed access into the machine.

Next, the user will create a piece of code that takes in input from the user about the shape's radius/side length and have the program compute both the area and the perimeter/circumference. For instance if the user chooses circle and gives the radius of the circle, the program should output both the area and the circumference.

Rules




 
The user MUST check for input validation at the menu step (See test cases below).




 
The program should only run on the day it is being tested. NO HARD CODING THE DATES.

User Input

Day ­ must be a positive 2 digit number between 01 and 31




Month ­ must be a positive 2 digit number between 01 and 12

Year ­ must be 2016




*Note: You do not have to check to ensure that valid dates for each month are entered (i.e. ­ Feb 31st)

Code Structure




 
Display login message “Shape Machine login”




 
Take input for the day (2 digits), the month (2 digits), and the year (4 digits) as Strings.

 
“What is today's day?”

 
“What is today's month?”

 
“What is today's year?


 
Compare it to the current date.

 
See notes at the end of this document how to get the exact date in the correct format

 
If it matches

 
Display “Correct date. Welcome!”


 
If input is not valid

 
Display “#ERROR Login attempt #[n] Invalid input. Please try again.”, where n is the login attempt number (message for 1st and 2nd login attempt)

 
Allow the user to input another date

 
If valid, display “Correct date. Welcome!” and continue program


 
If there are 3 total incorrect inputs, display an error message and terminate the program

 
“#ERROR 3rd invalid login attempt. Terminating program.” (message for 3rd login attempt)


 
Create the menu using String input as the only valid operation.

 
Make sure the input String matches exactly with the menu String for it to work (i.e. case sensitive).

 
The menu will have 4 options, “Circles”, “Rectangles”, “Triangles”, “Exit”

 
If a user inputs an option that is not valid, display an error message but do not terminate the program

 
Error message should read “#ERROR Invalid option. Please try again.”


 
If the user selects Circles

 
Display “Circles selected. Please enter the radius: ”

 
Take in user input

 
c. If the input is negative, display “#ERROR Negative input. Please input the radius again: ”

i. Check the new input and if it is correct then proceed. If not display the error message again and take in new input until it is valid




 
You do not need to worry about non­numerical input validation (the error should be generated only if the number is negative)

d. Using the circumference, calculate and display the area and perimeter of the circle (use Math.PI as a constant instead of 3.14159….)

 
“The circumference is: ____”

 
“The area is: ____”

e. Also display character length information for the perimeter and area. Total digits includes everything (for example for 23.0 the total number of digits is 3 because of 2, 3 decimal and the 0).




 
“Total number of digits in the circumference is: ____”




 
“Total number of digits in the area is: ____”




 
If the user selects Rectangles




 
Display “Rectangles selected. Please enter the 2 sides: ”

 
Take in the input for the 2 sides

 
If any of the 2 sides are negative or invalid, display “#ERROR Negative input. Please input the 2 sides again: ”

 
Continue to take in input until the 2 sides are valid

 
You do not need to worry about non­numerical input validation

d. Using the 2 input values, calculate the perimeter and the area

 
“The area is: ____”

 
“The perimeter is: ____”

e. Also display display character information for the perimeter and area




 
“Total number of digits in the area is: ____”




 
“Total number of digits in the perimeter is: ____”




 
If the user selects Triangles




 
Display “Triangles selected. Please enter the 3 sides: ”

 
Take in the input for the 3 sides

 
If any of the 3 input sides are negative, display “#ERROR Negative input. Please input the 3 sides again: ”

 
Continue to take in input until the 3 sides are valid

 
You do not need to worry about non­numerical input validation.

 
Once you have 3 positive inputs, determine if the 3 sides constitute a valid triangle

 
If the 3 sides to not generate a valid triangle, display the error message “#ERROR Triangle is not valid. Returning to menu.”, and return to the menu (Do not ask the user to reenter the 3 sides).

 
HINT: Look into the “continue” keyword in java and how it can be used to return to the start of a loop

 
Calculate the perimeter and the area of the triangle

 
Determine the type of triangle (options are: Equilateral, Isosceles, or Scalene)

 
. The definition of Isosceles is debated. We are using the strict definition of Isosceles wherein, a triangle is Isosceles only if there are EXACTLY 2 sides that are equal. (i.e. ­ an Equilateral triangle should not also be classified as Isosceles)

g. Display your results to the user

i. “The triangle is: “____”

 
“The perimeter is: “____”

 
“The area is: “____”

 
Also display display character information for the perimeter and area




 
“Total number of digits in the perimeter is: ____”




 
“Total number of digits in the area is: ____”




 
If the user selects Exit




 
Display message: “Terminating the program. Have a nice day!”

 
Terminate the program













Sample Run #1 ­ invalid input (user input is in green) (all tested on 21­09­ 2016)




Shape Machine login




What is today's day? 22




What is today's month? 09

What is today's year? 2016

#ERROR Login attempt #1 Invalid input. Please try again.




What is today's day? 21




What is today's month? 09

What is today's year? 2015

#ERROR Login attempt #2 Invalid input. Please try again.




What is today's day? 21




What is today's month? 9

What is today's year? 2016

#ERROR 3rd invalid login attempt. Terminating program.




Sample Run #2 ­ invalid input (user input is in green)




Shape Machine login




What is today's day? 21




What is today's month? 25

What is today's year? 2016

#ERROR Login attempt #1 Invalid input. Please try again.




What is today's day? 21




What is today's month? 09

What is today's year? 2016

Correct date. Welcome!




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit

(program continues…)










Sample Run #3­ invalid input (user input is in green)




Shape Machine login




What is today's day? 21




What is today's month? 09

What is today's year? 2016

Correct date. Welcome!




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Rectangles




Rectangles selected. Please enter the 2 sides: ­2 5

#ERROR Negative input. Please input the 2 sides again: 2 5

The area is: 10.0

The perimeter is: 14.0

Total number of digits in the area is: 3




Total number of digits in the perimeter is: 3




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Triangles




Triangles selected. Please enter the 3 sides: 2 2 5 #ERROR Triangle is not valid. Returning to menu.




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Triangles




Triangles selected. Please enter the 3 sides: 3 4 ­5




#ERROR Negative input. Please input the 3 sides again: 3 4 5




The triangle is: Scalene

The perimeter is: 12.0

The area is: 6.0

Total number of digits in the perimeter is: 3

Total number of digits in the area is: 2




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles




Triangles

Exit




Circles




Circles selected. Please enter the radius: 11

The circumference is: 69.11503837897544

The area is: 380.1327110843649

Total number of digits in the circumference is: 16

Total number of digits in the area is: 16




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Exit




Terminating the program. Have a nice day!







Sample Run #4­ valid input (user input is in green)




Shape Machine login




What is today's day? 21




What is today's month? 09

What is today's year? 2016

Correct date. Welcome!




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




circles




#ERROR Invalid option. Please try again.

­­­Welcome to the Shape Machine­­­

Available Options:

Circles

Rectangles

Triangles

Exit




Circles




Circles selected. Please enter the radius: ­4

#ERROR Negative input. Please input the radius again: 4

The circumference is: 25.132741228718345

The area is: 50.26548245743669

Total number of digits in the circumference is: 17




Total number of digits in the area is: 16




­­­Welcome to the Shape Machine

Available Options:




Circles

Rectangles

Triangles




Exit




Triangles




Triangles selected. Please enter the 3 sides: 13 16 25

The triangle is: Scalene

The perimeter is: 54.0

The area is: 91.19210492142398

Total number of digits in the perimeter is: 3




Total number of digits in the area is: 16




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Triangles




Triangles selected. Please enter the 3 sides: ­6 6 6

#ERROR Negative input. Please input the 3 sides again: 6 6 6




The triangle is: Equilateral

The perimeter is: 18.0

The area is: 15.588457268119896

Total number of digits in the perimeter is: 3

Total number of digits in the area is: 17




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Rectangles




Rectangles selected. Please enter the 2 sides: 0.5 0.75

The area is: 0.375

The perimeter is: 2.5

Total number of digits in the area is: 4

Total number of digits in the perimeter is: 2




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Triangles




Triangles selected. Please enter the 3 sides: 11.5 20 11.5

The triangle is: Isosceles

The perimeter is: 43.0

The area is: 56.789083458002736

Total number of digits in the perimeter is: 3




Total number of digits in the area is: 17




­­­Welcome to the Shape Machine

Available Options:




Circles

Rectangles

Triangles




Exit




Circles




Circles selected. Please enter the radius: 0.5

The circumference is: 3.141592653589793

The area is: 0.7853981633974483

Total number of digits in the circumference is: 16

Total number of digits in the area is: 17




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Exit




Terminating the program. Have a nice day!

Sample Run #5 ­ valid input (user input is in green)




Shape Machine login




What is today's day? 22




What is today's month? 09

What is today's year? 2016

#ERROR Login attempt #1 Invalid input. Please try again.




What is today's day? 21




What is today's month? 09

What is today's year? 2016

Correct date. Welcome!




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




TrianglesRectangles




#ERROR Invalid option. Please try again.

­­­Welcome to the Shape Machine­­­

Available Options:

Circles

Rectangles

Triangles

Exit




Circlez




#ERROR Invalid option. Please try again.

­­­Welcome to the Shape Machine­­­

Available Options:

Circles

Rectangles

Triangles

Exit




Triangles

Triangles selected. Please enter the 3 sides: 13.667 15 20.33

The triangle is: Scalene

The perimeter is: 48.997

The area is: 102.50179375946183

Total number of digits in the perimeter is: 5

Total number of digits in the area is: 17




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Rectangles




Rectangles selected. Please enter the 2 sides: ­1.75 1.75 #ERROR Negative input. Please input the 2 sides again: 1.75 1.75

The area is: 3.0625

The perimeter is: 7.0

Total number of digits in the area is: 5




Total number of digits in the perimeter is: 2




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Circles




Circles selected. Please enter the radius: 3.14

The circumference is: 19.729201864543903

The area is: 30.974846927333928

Total number of digits in the circumference is: 17

Total number of digits in the area is: 17




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Exit




Terminating the program. Have a nice day!




Sample Run #6­ valid input (user input is in green)




Shape Machine login




What is today's day? 22




What is today's month? 09

What is today's year? 2016

#ERROR Login attempt #1 Invalid input. Please try again.




What is today's day? 21




What is today's month? 09

What is today's year? 2016

Correct date. Welcome!

­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Rect0ngles




#ERROR Invalid option. Please try again.




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Circles




Circles selected. Please enter the radius: 0.275

The circumference is: 1.7278759594743864

The area is: 0.23758294442772815

Total number of digits in the circumference is: 17

Total number of digits in the area is: 18




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Triangles




Triangles selected. Please enter the 3 sides: 343 20 343

The triangle is: Isosceles

The perimeter is: 706.0

The area is: 3428.541964159109

Total number of digits in the perimeter is: 4




Total number of digits in the area is: 16




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Triangles




Triangles selected. Please enter the 3 sides: 70 25 30 #ERROR Triangle is not valid. Returning to menu.




­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Triangles




Triangles selected. Please enter the 3 sides: 2 ­2 5

#ERROR Negative input. Please input the 3 sides again: 2 2 5

#ERROR Triangle is not valid. Returning to menu.







­­­Welcome to the Shape Machine­­­




Available Options:

Circles

Rectangles

Triangles

Exit




Exit




Terminating the program. Have a nice day!




Note(s)




 
It is more helpful to use: use input.next() to read the selection from Shapes Menu as opposed to input.nextLine()




 
Please make sure to check your Eclipse settings so that you are using UTF­8 Encoding.




 
Eclipse ­ Preferences ­ General ­ Workspace : Text file encoding




 
You may see a different (rounded) result for area, depending on how you multiply to get the answer. Please use the following calculation (without parentheses) to avoid a difference in output:

 
double area = Math.PI * radius * radius;


 
You will need to use the following code when comparing the user's entered date to the current date:




import java.text.SimpleDateFormat;




import java.util.Date;




String date = new SimpleDateFormat(“dd­MM­ yyyy”).format(new Date());




new Date() ­ creates a new object with today's day




new SimpleDateFormat(“dd­MM­yyyy”).format(...) ­

converts the date it is given (...) to the dd­MM­yyyy String representation




 
Be careful to ensure that you do NOT create a new Scanner object within a loop. Aside from being inefficient and a waste of system resources, doing so will create multiple Scanner objects, which will DEFINITELY ensure that your code will not be graded as it should. You may call the scanner object (input.next*()) within a loop, but you should not create new objects on every loop iteration.




 
You may want to use double instead of int for all area, perimeter and circumference calculations in order to handle decimal values




 
To determine if a triangle is valid, 3 conditions must be met

 
side a + side b side c AND

 
side a + side c side b AND

 
side b + side c side a


 
You don't have to worry about very large exponential answers such as

1.2342343E13, all input will be small enough to avoid such answers

 
When validating input for the shape sides, you do not need to worry about string input (For example if you have to enter a side length, you do not need to worry about the input being a string such as “aJkfkFKASD” or some random sequence of characters




 
continue; can be used to stop executing the current loop iteration and begin the next




 
break; can be used to immediately break out of any loop




 
System.exit(0); can be used to immediately terminate the program from any point. Use it wisely and sparingly.




 
Submission Requirements




 
Name the project “Project2”

 
Name the class “ShapeMachine”. This will make the java file have the name: ShapeMachine.java

 
Zip the java file (right click the java file Send to Compressed (zipped) folder)

 
Name the zip file project2_ulfid.zip where uflID is the alphanumeric portion of your ufl email account that comes before the @ufl.edu part.




 
Submit using Canvas




If you submit the .java file without zipping, your project will not be graded. If your file is not named project2_ulfid.zip, your project will not be graded.




If you do not name the Project and class exactly as specified, your project will not be graded.

To help you test the output of your program, there will be a sample executable uploaded to Canvas.

It is highly recommended that you test your program piece by piece before assembling your final code. You will have a much easier time if you build your program piece by piece rather than trying to write the entire program. Pay very close attention to the order that you give inputs to the sample program. Your output should not differ in any way from the sample output. Using additional prompts, words, abbreviations, etc may result in the grading program taking unnecessary points off.




Sample test cases can be found under the Iteration Module on Canvas Grading




 
90% of your program will be graded on its ability to generate proper output




 
10% of your program will be graded on its adherence to the coding style standards.

More products