ITSS3311 Introduction to Programming Project 3
1
Part 1: Random Numbers and Average (7 points)
(5 points for code and 2 points for comments)
Problem Description: Your task is to use a loop to generate n random integer between 0 and 20 and calculate
average of these n numbers. You can use either for or while loop.
1. Use Scanner sc = new Scanner(); int n = sc.nextInt(); to read n. 2. Use for or while loop 3. Within the loop create a random integer between 0 to 20. 4. Use Math.round(20×Math.random()) to generate a random integer between
0 to 20.
5. After the loop ends, calculate the average of all random numbers generated by the loop.
Write a program with class name AverageRandom that prompts the user to enter value of
n and display the average of n random numbers generated using a loop.
Delivery: Upload .java file and with sample run with n = 5, 20, 50, 1000, 1000000
Demo calculation of average:
Average of n numbers = (x1 + x2 + x3+ …+ xn)/n, where x1, x2, …, xn are n numbers.
For example, x1= 2, x2 =3, x3= 10, x4=5, x5=3 and n is 5
Average of these five numbers is = (2+3+10+5+3)/5 = 22/5 = 4.4
Help: Follow the example code in week 6 folder. You need first calculate sum of n
random integers and then divide the sum by n.
Sample run:
Enter value of n: 5
Average is: 11.3
Enter value of n: 20
Average is: 9.2
Enter value of n: 50
Average is: 9.45
Enter value of n: 1000
Average is: 10.2
Total Points (15 pts)
Due: 6:00pm on July 21, 2020
ITSS3311 Introduction to Programming Project 3
2
Enter value of n: 1000000
Average is: 10.0000001
Part 2: Check Password (8 Points)
(6 points for code and 2 points for comments)
Problem Description:
Some websites impose certain rules for passwords. Suppose password rules are as
follows:
1. A password must have at least eight characters. 2. A password consists of only letters and digits.
Write a program with class name CheckPassword that prompts the user to enter a
password and displays Valid password if the rules are followed or Invalid password
otherwise. Create two methods (headers given below) to check the rules.
public static boolean checkLength(String p)
public static boolean checkDigitLetter(String p)
For example, method checkLength will return true if there are at least eight characters in
the password otherwise it will return false.
Here are four sample runs:
Sample 1:
Enter your password: My password20
Invalid password
Sample 2:
Enter your password: pass18
Invalid password
Sample 3:
Enter your password: password@
invalid password
Sample 4:
Enter your password: password20
Valid password
You can use the following steps in your code:
System.out.print(“Enter your password: “);
1. Store the user entry in String variable named password using nextLine() method.
ITSS3311 Introduction to Programming Project 3
3
2. Create two boolean variables isAtLeast8, and isLetterDigi and assign the output of the corresponding methods to these boolean variables. For example,
boolean isAtLeast8 = checkLength(password);
boolean isLetterDigi = checkDigitLetter (password);
3. If both boolean variables are true then display Valid password otherwise display Invalid password.
//Method checkLength
4. Create a method checkLength outside the main method with the header provided in the problem description. The password is passed to the string variable p in this
method. Inside the method do the following.
i) If the length of p is less than 8 then return false, otherwise return true.
//Method checkDigitLetter
5. Create a method checkDigitLetter outside the main method with the header provided in the problem description. The password is passed to the string variable
p in this method. Inside the method do the followings.
i) Create a loop that checks whether each character in p is a letter or a digit. To check this use the in-built methods
Character.isLetterOrDigit(p.charAt(index)). The method isLetterOrDigit
returns true if p.charAt(index) is a letter or digit.
ii) If a character is not found to be a letter or digit then return false inside the loop, otherwise return true outside the loop.