Easy Java Programing. Take about five minutes if you know what you are doing.

profilejardsh
java_homework.pdf

1

ITP 109: Introduction to Java Programming

Assignment 5

Goal In this assignment you will program a menu-based calculator and practice using…

 Switch statements

 Loops

Requirements Your program will perform one of two options repeatedly until the user quits your program.

 Create your program using Eclipse. Name your project Assignment5 and the Java class Loops.

 Use the Scanner class to get input from the user and use System.out to print to the console window.

 Use a while (or a do…while) to have the program loop until the user enter -1

 Inside the while, display a menu similar to the one below and use switch statement to respond to the user’s

choice

Please enter one of the following choices:

1 --> Largest Number

2 --> Factorial

-1 --> Quit

 For the Largest Number:

o Use ANOTHER while (or a do…while) loop ask the user to input an integer greater than or equal to 0

or -1 to quit.

Input an integer greater than or equal to 0 or -1 to quit:

o When the user enters -1, print out the largest number found

 For the Factorial:

o Ask the user for an integer that we will calculate the factorial.

o In math the factorial is notated as “n!”. It is the product of all positive integers less than or equal to n.

For example, 5! = 5 x 4 x 3 x 2 x 1 = 120. Meanwhile 3! = 3 x 2 x 1 = 6

o Here are the factorial rules

 If the integer is negative, display an error message

 Else if the integer is 0, then 0! = 1 so automatically print 1 as the answer

 Else calculate the factorial with a for loop and display the result.

o When finished, display the result

o Hint: use the factorial result as a long and not an int as the values can get quite large

 Display the result in a readable format.

 Be sure to comment your code.

Deliverables 1. A compressed Assignment5 folder containing a file named Loops. It must be submitted through Blackboard.

Here are the instructions for submission

a) Navigate to your project folder.

b) Create a compressed folder with all your code (include the src folder—if you don’t have a src folder, make

sure you include all your .java files that are within the project folder).

2

c) Name the folder as follows

ITP109_assignment#_lastname_firstname (replace # with this assignment number)

d) Upload zip file to Blackboard site for our course

Sample Output ================================================================= Please enter one of the following choices: 1 --> Largest Number 2 --> Factorials -1 --> Quit 1 Input an integer greater than or equal to 0 or -1 to quit: 4 3 2 15 -1 The Largest Number is 15 ================================================================= Please enter one of the following choices: 1 --> Largest Number 2 --> Factorials -1 --> Quit 2 Input an integer to calculate the factorial: 4 The factorial of 4 is 24 ================================================================= Please enter one of the following choices: 1 --> Largest Number 2 --> Factorials -1 --> Quit 2 Input an integer to calculate the factorial: 0 The factorial of 0 is 1 ================================================================= Please enter one of the following choices: 1 --> Largest Number 2 --> Factorials -1 --> Quit 2 Input an integer to calculate the factorial: -4

3

Invalid Input! ================================================================= Please enter one of the following choices: 1 --> Largest Number 2 --> Factorials -1 --> Quit -1 Have a great day!

Grading Item Points

Menu (while & switch) 10 Determining and displaying the largest number 10 Determining and displaying the factorial 10 Comments, style, and proper submission 5

Total* 35

* If you do not comment your code or use poor style points will be deducted