1. Introduction The assignment includes 2 programming tasks to give you practice in a...

Pepe

New member
...number of areas: Writ? 1. Introduction
The assignment includes 2 programming tasks to give you practice in a number of areas:
Write complete running programs (main method)
Read input from the keyboard and from a file
Output to the screen and to a file
Practice using loops and nested loops
Practice using APIs

2. Details of Each Task
2.1 Task 1
Prime numbers are natural numbers which have exactly two distinct natural number divisors: 1 and themselves. Therefore, the first 10 prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. Write an application that checks whether a natural number is a prime number. Your code should continue to ask the user whether another natural number would be checked (y/n) until the user answers „n" or „N". Your code should include a simple input validation: if the user input is not a positive integer, an error message (“not natural numbers”) should be printed out and the user is asked to input again.

Hints:
o You must use only three loops to complete the task. One main loop that keeps asking the user whether to continue to check another natural number. Within the main loop are two loops: one does input validation and the other checks whether the number has natural number divisors other than 1 and itself.
o Note 1 is not a prime number.
o Consult the API of the java.lang.String, use equalsIgnoreCase(String anotherString), toLowerCase(), or toUpperCase() in the main loop control condition to ensure comparisons are case insensitive.

Examples (Bold italic represents user inputs):
-=- Prime Number Examination -=-
Please enter a natural number to examine: 2847
2847 is NOT a prime number
Another natural number to examine(y/n)? y
Please enter a natural number: -65521
Not a natural number, please enter again: -655
Not a natural number, please enter again: 65521
65521 is a prime number
Another natural number to examine (y/n)? Y
Please enter a natural number: 999979
999979 is a prime number
Another natural number to examine(y/n)? y
Please enter a natural number: 2147483647
2147483647 is a prime number
Another natural number to examine (y/n)? Y
Please enter a natural number: 4294967291
4294967291 is a prime number
Another natural number to examine (y/n)? Y
Please enter a natural number: 9223372036854775783
9223372036854775783 is a prime number
Another natural number (y/n)? n

Bye-bye.

***********************************************************************
In these examples, 2147483647 is the largest prime, also the largest positive integer, you can represent with int (32 bits) data type in Java. To represent 4294967291 you have to use long (64 bits) data type, and 9223372036854775783 is the argest prime you can represent with long data type. To examine larger prime, extra facility is needed. You do not have to test your program on the prime in the last example take very long time.
***********************************************************************

2.2 Task 2
Write a program that first asks the user to enter a computer term, then goes through a file named task2.txt (downloadable as supporting file beside the assignment4) to count the number of times the term appears in the file (0 if the term does not appear in the file). Once you know the count, output the result to a file named task2output.txt in the following format (as an example)

Hard drive 2 // Use two tabs between the term and the count
Your code should keep asking for a term until the user enters -1. Comparison between terms inputted and words in task2.txt should be case insensitive. However, the terms printed in the output file should be the same as originally entered by users (i.e., case sensitive).


Hints:
o You can use two loops to complete the task. The main loop keeps asking the user for a computer term and prints the results in the output file. Another loop, within the main loop, goes through the file task2.txt and keeps track of the count.

o Both the computer terms and -1 can be read as a String.
o You will have to reset the cursor in the file task2.txt before reading a new term from the user. To do this, close the file and re-open it again.
o Use the toLowerCase(), toUpperCase(), or equalsIgnoreCase(Streing anotherString) to ensure comparisons are case insensitive.
o Do not forget to reset count to zero before reading a new term from the user.
o Do not forget to close both files at the end of the program.


Example (Bold italic represents user inputs):
-=- Computer Term counting -=-
Please enter a computer term (-1 to exit): Hard Drive
Please enter a computer term (-1 to exit): CPU
Please enter a computer term (-1 to exit): hard drive
Please enter a computer term (-1 to exit): IP address
Please enter a computer term (-1 to exit): -1

task2output.txt:


Hard Drive 2
CPU 0
hard drive 2
IP address 4
 
Back
Top