Exercise 4
Implement and run the following code snippet. This program generates ten random integers. Note that the numbers range between a very large negative number and very large positive number. This might not be that useful for many applications. Often we want to generate uniformly distributed random numbers between two limits/bounds.
Random rand = new Random();
for(int i=0;i<10;++i)
{
[login to view URL]([login to view URL]());
}
Modify, run and test the program as follows:
a) Generate random integers between -100 and +100 (inclusive).
b) Generate random integers between limits (pre-specified) a and b.
c) Generate random doubles between zero and one.
d) Generate random doubles between limits (pre-specified) a and b.
Make sure your programs cater for any error conditions, e.g. where b > a.
Exercise 5: Reading Data
Copy-paste the text in Appendix A, to create a text file called “the art of [login to view URL]”. Write a program that takes as a parameter the name of the file and returns an integer containing the number of words in that file. The text in Appendix A contains 558 words.
Test your program on several of your own test files and the file you have created from the Appendix.
Note that you can use Microsoft Word to count the number of words in a text file.
Exercise 6: Writing Data
a) Complete the following code that takes as input a number n and a text string filename and writes n lines to the file where each line is of the form: i: sqrt(i).
i should take all values from 1 to n.
Follow the example below:
public static void main(String args[]) throws IOException
{
writefile(5,"c:\\temp\\[login to view URL]" );
}
public static void writefile(int n, String filename)throws IOException{
FileWriter writehandle = new FileWriter(filename);
//complete the rest of the code to write in the file
}
The contents of the file should look like the following after running your code:
1 : 1.0
2 : 1.4142135623730951
3 : 1.7320508075688772
4: 2.0
b) Now modify the writefile method so that the contents of the files will look like that:
That is, the first square root is to one decimal place, the second is to two decimal places and the third is to three decimal places.
1: 1.0
2: 1.41
3: 1.732
4: 2.0000
Exercise 7: Searching for the Longest Word
Write a Java program to find all of the longest word in a list of words entered by the user assuming that the user finishes entering the words in the list when s/he enters the word “done”.
Example-1:
{
"cat",
"flag",
"green",
"country",
"resource"
}
Result: resource
Example-2:
{
"cat",
"dog",
"red",
"is",
"am"
}
Result: cat, dog, red
The code in the main method will ask the user to enter the list of words.
Then, the code responsible about finding the longest word/s in the list should be written in a user-defined method named (Longest_Word) that is called from the main method and returns the longest word to the main method
In case there is more than one word has the same length as the longest word (e.g. example 2 above, then the user-defined method will return all the words separated by a comma)
Hello, I am a computer science grad (Master's degree) and a Java expert with 5 years of experience.
I can confidently help you with your task in less than 6 hours but for 30usd.
I have a track record of delivering satisfactory and excellent jobs to my clients within a limited timeline.
I look forward to discussing your project with you via chat.
Thanks.