LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple java question (https://www.linuxquestions.org/questions/programming-9/simple-java-question-484627/)

newbz 09-18-2006 09:07 AM

simple java question
 
I am new to java and not quite proficient in programming . I need some help with a java program that would generate 1000 random integers and send them to a file with no duplicates. Thanks a lot your assistance is much appreciated.

95se 09-18-2006 06:42 PM

Here's an easy way if your just looking to get it done,
Code:

import java.util.*;

public class Blah {
    public static void main(String args[]) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>(1250);
        Random rand = new Random(System.currentTimeMillis());

        while(map.size() < 1000) {
            Integer num = new Integer(rand.nextInt());
            map.put(num, num);
        }

        for(Iterator i = map.values().iterator(); i.hasNext(); System.out.println(((Integer)i.next()).intValue()));
       
    }
}

It just uses the fact that the HashMap will only allow one value per key, so you'll never get a duplicate.

Also, if this is for homework, you may not get full marks for this solution, so you would be better off to think of your own solution.

nadroj 09-19-2006 10:05 PM

sorry nothing to contribute to the thread starter.

hey 95se i see your 4th year at the U. i graduate next month from st clair [:(] with 3yr programming and a 2yr networking diplomas. im really considering transfering the credits over, and ill start there in 3rd semester and graduate in 5 from the honours program (if i choose to go through with this).

anyways, how tough is it? what are the averages? if you dont mind, thanks

95se 09-27-2006 11:30 AM

I sent an e-mail, but, basically, it's as tough as you make it. Most classes are suppose to have a C average (60%). Some are higher, some are lower. If you really enjoy Comp. Sci., you'll do fine I believe.

Also, how is the 3 yr. programming course? I have a friend who just started his first year in St. Clair in programming.

nadroj 09-27-2006 01:04 PM

thanks for the response, i didnt get the email sorry. maybe it was sent to my junkmail which i dont check and automatically got deleted after X days.

i do really enjoy it, but i hear that the CompSci (and eng) are just as much math degrees as the majors. let me tell you im terrible at math so thats why i think i wont enjoy it or have a smooth time there. my friend whos at the U let me borrow his calculus book which im going to try and review before the winter semester when i hope to enroll.

the st clair diplomas were honestly easy. the first year is a joke, programming wise (unless you had ZERO programming knowledge). second year we did 2 java courses, an asp course, 3rd year we did another java course and 2 c++ courses, as well as the 'technical project'--go out find a company and write something for them, pretty interesting it was.

i just feel i dont know enough to go start workign, after getting these diplomas. i think they emphasized java too much, i feel very comfortable with it (didnt teach any socket stuff though), but i wish they spent more time on c++ or even one intro c course before c++.

good luck to your friend.. if hes like yourself or myself, who really enjoy it and really want to know the ins and outs, tell him to consider switching after first year, rather than be like me and spent 7 semesters there and have them only transfer to 2 at the U.

cya

95se 09-27-2006 06:23 PM

If you want C++, you won't really get it at the U :| It's mostly C or Java with the odd course using some other language (C++, Lisp, Assembly). Do note, 3rd year+ are more theoretical, so the programming is really secondary to the main goals of the course or non-existant.

As for math, there is a decent amount of math, but it's not too bad. I'm in the software eng. stream which is slightly more math-oriented than say, a regular CS Honours. Total Math courses for me: Differential Calculus, Integral Calculus, Linear Algebra, Mathematical Foundations (Set theory and proofs), Statistics, and Numerical Methods. It's basically just the introductory courses for each "type" of mathematics, but no more than that. Some courses are also more math oriented, or use the same principles (proofs, induction, etc..). It's hard to separate the 2 (Comp. Sci. and Math), since mathematicians really laid most of the ground work and foundations (and still are) for computer science and programming. I'm not bad at math, but I know some people who, according to them, suck at math. They just make up for the bad math grades, with good grades in other courses. Basically, math is a part of it, but not so much that if you aren't fond of straight math, it'll ruin your experience. Definately check out the text book your friend gave you; even try just auditing a course if you have time.

indienick 09-28-2006 09:44 AM

Quick side note: I find it best to learn/practice Lisp apart from learning and practicing any other programming language. Lisp is miles from "ordinary" when it comes to any other language. For example, Java vs. Lisp, creating a limit from the first argument, and storing all values in a list:

Java:
Code:

import java.util.ArrayList;
public class Crapalicious {
 public static void main(String[] args) {
  int limit = Integer.parseInt(args[0]);
  ArrayList<Integer> arrayList = new ArrayList<Integer>();
  for (int x = 1; x <= limit; x++) {
  System.out.printf("%d ", x);
  arrayList.add(x);
  arrayList.trimToSize();
  }
 }
}

Lisp:
Code:

(defun foo (limit)
  "Yay for docstrings."
  (loop for i from 1 to limit collecting i))

:) They both do exactly the same thing.

Lisp is a neat language to learn, and makes quick work of most tasks, but it is completely different from any other language I've seen.

EDIT: Quick question 95se, how come you have Lisp highlighted in red?

jlliagre 09-28-2006 11:19 AM

Quote:

Originally Posted by indienick
Quick side note: I find it best to learn/practice Lisp apart from learning and practicing any other programming language. Lisp is miles from "ordinary" when it comes to any other language. For example, Java vs. Lisp, creating a limit from the first argument, and storing all values in a list:

Java:
Code:

import java.util.ArrayList;
public class Crapalicious {
 public static void main(String[] args) {
  int limit = Integer.parseInt(args[0]);
  ArrayList<Integer> arrayList = new ArrayList<Integer>();
  for (int x = 1; x <= limit; x++) {
  System.out.printf("%d ", x);
  arrayList.add(x);
  arrayList.trimToSize();
  }
 }
}

Lisp:
Code:

(defun foo (limit)
  "Yay for docstrings."
  (loop for i from 1 to limit collecting i))

:) They both do exactly the same thing.

You took an example of program which manage lists, where Lisp excels by design.

It may be more complex to use other computing objects, but your point on Lisp conciseness is correct.
Quote:


Lisp is a neat language to learn, and makes quick work of most tasks, but it is completely different from any other language I've seen.
Indeed, I never manage to be confortable with the syntax though.

I prefer C and its sequels ...

Quote:

EDIT: Quick question 95se, how come you have Lisp highlighted in red?
That's not him, nor is it highlighted in red for the rest of us.
My guess: It's just you who find this thread by searching the forums for the Lisp keyword.

indienick 09-28-2006 01:20 PM

thanks jlliagre. wierd thing is, I don't recall posting a search for lisp. *shrug* no biggie.

nadroj 09-28-2006 02:06 PM

Quote:

If you want C++, you won't really get it at the U :| It's mostly C or Java with the odd course using some other language (C++, Lisp, Assembly). Do note, 3rd year+ are more theoretical, so the programming is really secondary to the main goals of the course or non-existant.
ya someone that i went to st clair with is now in 3rd semester for CS honours. since we did no C at college, thats what he recommended--'go out and buy yourself a good C book', which is fine, id be interested to learn C and more C++. are 3rd and 4th year more theoretical as in learning more advanced OO techniques? or how more theoretical, in general (not that this will change anything as i probably WILL be enrolling in the winter :))

Quote:

It's basically just the introductory courses for each "type" of mathematics, but no more than that.
i hope so, because, as ive already said, im not comfortable with advanced math and have NEVER taken calc or algebra (not even in high school, just basic math).

Quote:

It's hard to separate the 2 (Comp. Sci. and Math),
very true.

and i do think that since ive already taken 7 semesters in the field, at least the start of my career at the U (CS-wise) should be easier than the other students, id imagine... so i could focus more on the mathematical side since thats where i wouldnt be strong.

thanks for the tips, and good luck to you

Dan04 09-28-2006 03:16 PM

Quote:

Originally Posted by nadroj
i do really enjoy it, but i hear that the CompSci (and eng) are just as much math degrees as the majors.

At my school, majoring in Computer Engineering put you one class away from getting a math minor.

xhi 09-28-2006 06:46 PM

not to butt in on your conversation guys, but 60% is a C. thats awesome! i sure could have used that scale in some of my english classes. ha.

95se 09-29-2006 09:52 AM

Quote:

Originally Posted by nadroj
ya someone that i went to st clair with is now in 3rd semester for CS honours. since we did no C at college, thats what he recommended--'go out and buy yourself a good C book', which is fine, id be interested to learn C and more C++. are 3rd and 4th year more theoretical as in learning more advanced OO techniques? or how more theoretical, in general (not that this will change anything as i probably WILL be enrolling in the winter :))

Yes, OO techniques are part of it, but there is really only one big class on that; OO Analysis & Design. I'm in Software Engineering, so I also get all the software engineering courses that deal w/ software development cycle, cost estimation, V & V, etc.. We also get courses that deal w/ computational theory, programming languages (design), algorithm design and cost (computation wise), etc.. Those are the mainly theoretical ones. Some are mixed, like Operating Systems Design, which, while it introduces a lot of "theory" behind OSes, has a large practical project as well. And definately learn C if you don't plan on taking any specific courses in it at the U. There are many classes where you'll use it... However, you'd be suprised at how many people don't know C for poop here, yet still manage to do OK. Probably because I end up w/ them in my groups :mad:

95se 09-29-2006 09:56 AM

Quote:

Originally Posted by xhi
not to butt in on your conversation guys, but 60% is a C. thats awesome! i sure could have used that scale in some of my english classes. ha.

When I first started, the classes actually chose their own scale, so some saw 60% as a D-. In my second year the school standardised it, so now C- = 60% :)

nadroj 09-29-2006 12:49 PM

ya we did afew Systems analysis + design in college using OO methodoligies.

Quote:

And definately learn C if you don't plan on taking any specific courses in it at the U.
i havent seen any that (in the description) focus entirely on c, except this one, which states that CS honours students cant enroll in it lol.

i dont know how my courses are going to transfer over but hopefully id be able to do afew first year courses that introduce C. is '60-100 Key Concepts in Computer Science' C-oriented? or '60-256 Systems Programming'? id like to take the intro to algorithms 1 and 2, but dont think i would, id probably be tossed into 2nd year with afew 1st year classes.

my friend that has the same college diploma as me, as ive mentioned, is at the U in 3rd semester and these are his courses:
Code:

Differential Calculus                                03 62-140
Principles and Methods of Sociology                02 48-101
Object-Oriented Programming Using Java                03 60-212
Key Concepts in Computer Science                03 60-100
System Programming                                03 60-256

who should i go talk to about deciding what courses ill get? the Dean? registrar? i hear ill be able to get 15 non-technical courses transfered, but since my friend here doesnt have the intro to algorithms course its logical to say i wont either, but id like to do them (1 and 2) since itll reinforce what i learned afew semesters ago and maybe some C programming that i didnt get to learn.
thanks

ps-do you think youll go for graduate studies after your next semester?


All times are GMT -5. The time now is 09:42 AM.