Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,000 members, 7,817,962 topics. Date: Sunday, 05 May 2024 at 12:04 AM

Help With A Java Project... - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help With A Java Project... (2661 Views)

Help With Java Project / Im A Java Expert Here To Teach Those Who Wants To Learn. / A Java J2me Application Developed By A Nigerian U Need To Have(must) (2) (3) (4)

(1) (Reply) (Go Down)

Help With A Java Project... by loveydud: 8:07am On Nov 17, 2014
PLEASE CAN ANYONE HELP ME WITHIS CODE..ITS A CLASS ASSIGNMENT..


You should write a program that does the following:

Create two arrays of type int[]. Both arrays should be the same size, and the size should be given by a constant in the program so that you can change it easily.

Fill the arrays with random integers. The arrays should have identical contents, with the same random numbers in both arrays. To generate random integers with a wide range of sizes, you could use (int)(Integer.MAX_VALUE * Math.random()).

Sort the first array using either Selection Sort or Insertion Sort. You should add the sorting method to your program; you can copy it from Section 7.4, if you want. (It is a good idea to check that you got the sorting method correct by using it to sort a short array and printing out the result.)

Time how long it takes to sort the array, and print out the time.

Now, sort the second (identical) array using Arrays.sort(). Again, time how long it takes, and print out the time.

You should run your program using array sizes of 1,000, 10,000, and 100,000. Record the sort times. Add a comment to the top of the program that reports the times. (You might be interested in applying Arrays.sort() to a million-element array, but don't try that with Selection Sort or Insertion Sort!)

Note: The general method for getting the run time of a code segment is:

long startTime = System.currentTimeMillis();
doSomething();
long runTime = System.currentTimeMillis() - startTime;

This gives the run time in milliseconds. If you want the time in seconds, you can use runTime/1000.0.


Part 2: Programming with Exceptions

In this part of the lab, you will write a program that fetches the information stored at a give URL on the web and saves that data to a file. This will also include networking and file operations and partly an exercise in using exceptions.

For doing I/O, Java has a pair of nice abstractions: InputStream and OutputStream. These are abstract classes in the package java.io. An InputStream is a place from which you can read data; an OutputStream is a place to which you can write data. For this lab, you will use an InputStream to represent the data read from the Web URL, and you will use an OutputStream to represent the file where you want to save a copy of the data. Once you have the streams, the data can be copied just by calling the following method, which you can copy into your program:

private static void copyStream(InputStream in, OutputStream out)
throws IOException {
int oneByte = in.read();
while (oneByte >= 0) { // negative value indicates end-of-stream
out.write(oneByte);
oneByte = in.read();
}
}

Aside from this method, you should have a main routine that does the following:

Declare variables to represent the InputStream and the OutputStream. It would be a good idea to initialize them to null to avoid uninitialized variable errors.

Read the URL and the file name as strings from the user.

To connect to the web, you need a variable -- say url -- of type URL (from package java.io). You can create the URL object with the constructor call url = new URL(urlString), where urlString is the string provided by the user. This constructor will throw a MalformedURLException if the string is not a legal URL. (Note: the string must be a complete URL, beginning with "http://".)

To get the input stream, you can simply call url.openStream(), which returns a value of type InputStream. This can throw an IOException, for example, if the web address that you are asking for does not exist.

To get the output stream, you can use the constructor new FileOutputStream(fileName), where fileName is the file name that was input by the user. This can throw a FileNotFoundException if it is not possible to open the specified file for reading (for example, if the user is trying to create a new file in a directory where they don't have write permission). Warning: If a file of the same name already exists, the old file will be erased and replaced by the new one, without giving the user any notice!

Now, copy the data from the web into the file by calling the above method. Note that this can throw an IOException.

Finally, use a finally to clause to make sure that both streams are closed (if they were successfully opened). Both InputStream and OutputStream have a close() method for closing the stream. Note that you can test whether the stream was opened by testing whether the value of the variable is still null.
Note that an exception should not crash your program. You should catch the exception and print out a reasonable error message before ending the program. It would be nice if the error message depends on the type of error that occurred (which means using several catch clauses).
Re: Help With A Java Project... by Sibrah: 4:19pm On Nov 17, 2014
You may want to try advanced sites like stackoverflow if Nairaland doesn't give u the result. Reason most people on Nairaland may not help is the fact that aspect like sorting and searching doesn't get implemented often in their day-to-day coding experiences.

1 Like

Re: Help With A Java Project... by Jeffahead: 12:27pm On Nov 18, 2014
I tire for some programmers on NL.

@OP, do you expect someone here to do your whole assignment for you?

I suggest that you narrow down your query to a specific problem you are having trouble with. Include, what code you have written and what line of code or outcome that isn't working for you.

If you are having trouble understanding a certain concept then specify how much of it you do understand what exactly it is about the concept that you don't understand.

If you structure your question this way, then you will probably get answers that will actually help you.

But posting your whole assignment here and saying "Help Me" doesn't give anyone a place to start.
Unless of course you are willing to pay a programmer here to do it all for you. In that case, I'll let you know my fee grin

5 Likes

Re: Help With A Java Project... by Bossman(m): 3:25pm On Nov 20, 2014
This is not an overly difficult assignment, even for a beginner. The assignment gave very useful pointers. You are better of trying to accomplish this yourself, and then if you get stuck, you can post here, and folks will try to help you out. Why - Because, in the "real" world, you will have to know it. You will be tested,and they will find out quickly if you do not know the stuff. I'd suggest writing out the algorithm/steps on paper, then start to implement/code one step at a time. Post back here and let us know what you have done and where you are having difficulty.

1 Like

Re: Help With A Java Project... by abenmariem: 11:04am On Dec 01, 2015
I suggest that you take a look at a good java tutorial to get the basics of the language before starting the project.
I suggest this core java tutorial to start with.
Re: Help With A Java Project... by danvery2k6(m): 9:06pm On Dec 01, 2015
Hi OP. Have you gotten a solution for this. If not, let me know here, I'll post the solution. But it will be nice to see what you have done.

(1) (Reply)

Become A Pro In Programming For Only N6000 / Desktop Application Vs Web Application / How To Become An Embedded Software Developer

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 30
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.