somjit{} 60 Junior Poster in Training Featured Poster

What is a Thread ? I understand it to be an instance of a class that implements Runable , and thus has a run() method , which is executed when the jvm works the thread ? Inside that run() method there will be some lines of code , let's suppose at any particular line , a synchronized method bar() of object foo is invoked , Now , when we say

First, it is not possible for two invocations of synchronized methods on the same object to interleave

i suppose it means another thread cannot call foo.bar() untill the 1st thread has finished. Thus , the object being mentioned is foo , and the invocation being mentioned is of the bar() method ?

and then , when its said :

When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

it means that : If one thread is executing the bar() method for foo , foo's intrinsic lock is currently owned by that particular thread , and all the other threads are waiting in blocked state for the lock held by the running thread to be made free , ie , the current thread to stop executing ?

somjit{} 60 Junior Poster in Training Featured Poster

in 1985 , maybe i was a giant transparent space squid swimming through the void of a parallel universe. i sure wasnt born as a human :P

<M/> commented: ikr... +0
somjit{} 60 Junior Poster in Training Featured Poster

How long did amiga exist ? When did you make the shift @Jim ?

somjit{} 60 Junior Poster in Training Featured Poster

Thanks for that answer , its really good ! just for the record i would like to add that class methods are nothing but the static methods. ( just in case it confuses someone , which is probable i guess when the same thing has two different names. )

somjit{} 60 Junior Poster in Training Featured Poster

I'm a bit confused about the use of "for" in the above statement.( i read the line from the oracle tutorials ) I suppose a thread has its run() method , inside which there is some code. That code might call a method of an object... and by this point of time , my mind has locked on to firing "method of an object" but i keep reading "for an object". What does "for" signify here ? Can someone please help me get untangled from this grammatical nuance ?

EDIT: This should be in the java forum!! I messed up and posted it here ! i'm hoping the mods could move this there.. apologies for my stupidity.

somjit{} 60 Junior Poster in Training Featured Poster

good to know :)

somjit{} 60 Junior Poster in Training Featured Poster

you want to do that ? or you want us to do that ? the later one isnt really an option here.

Also , this is two words , one english , and another java . You say copy and then you say this . that confuses me a bit.

somjit{} 60 Junior Poster in Training Featured Poster

and while your at it as stultuske suggests , would be good if you can learn about StringBuilders too. google it up.

somjit{} 60 Junior Poster in Training Featured Poster

if stack is empty , you should return some kind of error or throw some exceptions imo.

 revertStack.push(pop());

i dont know what those are , but assuming they do the work they are supposed to , AND its an in-place operation , you are returning a reversed version of stack , but its not a copy of the original stack , because the stack itself has been reversed. here is something you can do :

  1. make FancyStack iterable
  2. in your else() condition , have a for() loop that takes in each member of FancyStack() and put that in a new copy.
  3. apply revertStack.push(pop()) to this newly made copy , and return it.
somjit{} 60 Junior Poster in Training Featured Poster

apart from the typo's in code , which you can correcteasily by pasting it into an IDE , look at your last for() loop

        for (i = 0; i < rowa; i++) {
            for (j = 0; j < colb; j++) {

                    JOptionPane.showMessageDialog(null,
                            " the resultant matrix is" + mul[i][j] + "\n",
                            "result", JOptionPane.PLAIN_MESSAGE);

            }

        }

it creates a new window for each entry in your result matrix.

somjit{} 60 Junior Poster in Training Featured Poster

I agree with @stuugie , that part doesn't make much sense to me.

somjit{} 60 Junior Poster in Training Featured Poster

@James : i thought that applied to only overriden methods ? and also that it happens at runtime...

somjit{} 60 Junior Poster in Training Featured Poster

The question you should be asking is that why does the dog-move() method gets called even when i'm calling the move on an animal object. Java rule : methods that can be called depend on the reference type and not the object type , so whats going on here ?

somjit{} 60 Junior Poster in Training Featured Poster

My main complaint with sublime-text is , as mike pointed out , its learning curve. I needed to download a youtube playlist of tutorials to get going with it!

Advantages with notepad++ :

  1. easy to understand
  2. easy to set up and use custom key-bindings
  3. color schemes can be easily modified.
  4. i dont know if its me , but notepad++ offers better code-completion. Sublime text just doesn't feel right in that department.
  5. its just FREE. Sublime on the other hand keeps asking me for licensing even though its an "untimed trial" . whatever that means.

But , when it comes to looks , SublimeText is just (imo) such an eye-candy! So much so that i spend a good number of days learning how to use it ! However , apart from the visual charm , a few functional advantages that i feel with sublime are :

  1. side-bar that acts as a project-manager.
  2. it has a console
  3. it can build things.

I think notepad++ can do the build-part too, but the fact that i never needed it says a bit about how easy it is for actual programming.

Assembly Guy commented: Don't know why you got downvoted, this is just the lounge +0
somjit{} 60 Junior Poster in Training Featured Poster

09620f64f2003269704fe694244654f6

it looks quite modern to me !

somjit{} 60 Junior Poster in Training Featured Poster

... and this link for those who are wondering what surrogate pairs are. The accepted answer is short and sweet :)

OK, maybe I'm just being pedantic

An eye to detail when explaining stuff to young people, imo only helps in helping them learn more. :) i didn't know about surrogate pairs before your post. thank you .

somjit{} 60 Junior Poster in Training Featured Poster

So anyone here who loves sublime text ? I do , and i use it on windows as my sql editor and compiler. (i have a batch set up which complies the code on mysql , and the output is tee'd back to another open text file)

somjit{} 60 Junior Poster in Training Featured Poster

I mean ofcourse its not ! a string is a class , char is a primitive type... what i meant was , in terms of number of characters for this particular problem , a char[][] equates to a string array ?

somjit{} 60 Junior Poster in Training Featured Poster

That's a really big post !

anyway , a thing i'd like to ask for clariication :

a) Write code that transforms the string array provided above into a 2 dimensional character matrix, (e.g., char[][] charMatrix;).
b) Once in the two dimensional array form, use insertion sort to sort the characters of each row, from a-z.

isn't this like going round in circles ? a 2-D character matrix is at the end of the day a string array right ?

now then , if i understand your question correctly , here is something you can do :

  1. have a count variable
  2. run a for() loop , taking in each string , and adding the size to count , at the end , count should refer to total number of characters in the string array.
  3. create a character array of size count , populate it with 'count'-many characters , sort it.
  4. take 4 entries at a time from the sorted char array , and knock yourself out with whatever you want to do with them !

ps : personally , i feel silly homeworks like these do more harm than good when it comes to developing a sound programming logic. sadly , most students have to deal with them. ( i know i have , and still do )

somjit{} 60 Junior Poster in Training Featured Poster
final int SIZE=this.size();

can you explain this part a little ?

somjit{} 60 Junior Poster in Training Featured Poster

Thanks there Pritaes :)

somjit{} 60 Junior Poster in Training Featured Poster

I like watching stuff like google tech-talks , josh bloch seminars/talks etc for Java. Can somebody provide something similar in nature for C# ?

somjit{} 60 Junior Poster in Training Featured Poster

This thread is a bit old , but i'm having the same questions (under different circumstances) as the OP. Googling for "CLR versions for Mac and Linux" led me to mono and wine ( can i say respectively here ? ). Now in one stackoverflow thread , i read that the MSIL is only a part of an EXE for a windows program , and the other parts include native windows stuff , and MONO will just take the MSIL and do that part. So it doesn't work the other native stuff inside the EXE ? What does it all mean ?

apologies for being vague , but i really do not know enough to be more specific.

somjit{} 60 Junior Poster in Training Featured Poster
somjit{} 60 Junior Poster in Training Featured Poster

google up "sieve of eratosthenes" . the algorithm results in a small but powerful code for finding primes.

somjit{} 60 Junior Poster in Training Featured Poster

you can figure out the first word relatively easily using a "compare character read with ascii value of space " strategy. The last word would be a bit tricky. Keep a variable that points to the last position at which a delimiter was read , now while reading a character at a time , if you meet the end of the string , take a substring from that last delim index to String.length - 1 . I havent tested this , but it should get you started.

somjit{} 60 Junior Poster in Training Featured Poster

Hey thanks for the location of the api codes! As retarded as it may sound , i searched a lot for them on the net thinking it may be there , but didnt find anything. All the time it was under my noses it seems.. !

somjit{} 60 Junior Poster in Training Featured Poster

Version 1 had quite a few problems that i didnt think about. A lot of them were pointed out in the replies. I tried to correct them as best as i could to make this code.

However , I'm having some doubts regarding what to do while implementing the Collection interface. Mainly in that if i implement Collection , a lot of the methods in it are similar to what i'v already coded up. like add() vs enqueue() . In such cases , i suppose the best way out will be to make my methods private , and call them from inside their respective Collection implementations ?

Also, I was using System.arraycopy() previously , but in my tests , i found that it wasnt ideal for a circular array. Case in point :

adding : e
head : 5 tail : 5 , size : 12
a b c d e f g h i j k l 

adding : f
resizing array to size: 24
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at ResizingCircularArray.resize(ResizingCircularArray.java:16)
    at ResizingCircularArray.enqueue(ResizingCircularArray.java:33)
    at ResizingCircularArray.main(ResizingCircularArray.java:105)

This came up because : head + size > source length , and resulted in IndexOutOfBounds exception. So i made something similar that works with cicular array.

// Modified version of System.arraycopy() to work with circular array.
    private void arrayCopy(E[] srcArr , int srcpos , E[] destArr , int destpos , int length){
        for(int index = 0 ; index < length ; index++){
            destArr[index] = …
somjit{} 60 Junior Poster in Training Featured Poster

An update : As sos pointed out , although i call it a circular buffer , its really a linear one. But lets compare what i do , and what a circular array would have done :

Similarities: A cicular array would keep check of size via the difference between the location of Head and tail , I'm doing basically the same i think , perhaps in a different way.

Dis-similarities: I didnt provide any way for the tail to move past the last position in the array , and occupy the null positions left off where pops occured from the head. If i do that part , I think i'll make it circular ?

anything else i'm missing something ?

somjit{} 60 Junior Poster in Training Featured Poster

Collection<String> myData = new ResizingCircularArray<>{};

So i can pass it anywhere a Collection<E> is expected .... nice !

somjit{} 60 Junior Poster in Training Featured Poster

@ ~s.o.s~

head is unnecessary in the second statement.

I kept that extra head to make it easier to understand later on , or for someone else viewing my code.

maybe NoSuchElementException?

Eclipse auto generation :| i intended it to be NSEE.

Your sample method is missing a check wherein head+offset can go beyond tail.

Wow ! i didnt even think of that.

Regarding the circular part , right again ! I felt i was making it circular by virtue of the pop from head , and push to tail , and keeping a tab on the size constantly , But now that you mention it , I really dont have any circular part in it. I'll post again with the 3 corrections. Thanks a lot !

somjit{} 60 Junior Poster in Training Featured Poster

implement the whole Collection interface

  1. i dont know how to do that
  2. i dont really know what that will result in. If you could provide some examples , would be great.

Frankly I think your code is very good.

Thank you ! :)

somjit{} 60 Junior Poster in Training Featured Poster

Since the underlying data-structure used here is an array , which is itself iterable via for-each() , i'm wondering how much benefit implementing Iterable will provide here.
I would appreciate if someone could review my code and suggest any further improvements.

Code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class ResizingCircularArray<E> {

    private int head = 0;
    private int tail = 0;
    private int size = 0; // a measure of non-null elements in the array
    private E[] arr;

    private void resize() {
        System.out.println("resizing array to size: " + 2 * size);
        @SuppressWarnings("unchecked")
        E[] tempArr = (E[]) new Object[2 * size];
        System.arraycopy(arr, head, tempArr, 0, size);
        head = 0;
        tail = head + (size - 1);
        arr = tempArr;
    }

    @SuppressWarnings("unchecked")
    public ResizingCircularArray() {
        arr = (E[]) new Object[5];
    }

    public void enqueue(E item) {
        if (item == null)
            throw new java.lang.NullPointerException(
                    " adding null values is not allowed ");
        if (size == arr.length) {
            resize();
        }
        arr[tail++] = item;
        size++;
        System.out.println("head : " + head + " tail : " + tail + " , size : "
                + size);
    }

    public E dequeue() {
        if (size == 0)
            throw new java.lang.NullPointerException("size = 0");
        if (size == arr.length / 4) {
            resize();
        }
        E item = arr[head];
        arr[head++] = null;
        size--;
        System.out.println("head : " + head + " tail : " + tail + " , size : "
                + size);
        return item;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public E sample(int offset) { …
somjit{} 60 Junior Poster in Training Featured Poster

@rubberman : I should have asked you back then , how does C# or python compare to C++ in terms of what you suggested? C# has the .net overhead i suppose which might not make it a suitable option i suppose ? Then what about python ? I read real good words about python everywhere i look.

somjit{} 60 Junior Poster in Training Featured Poster

are you sure that the code you posted is exactly the one your working with ? If thats the case , then i see a lot of spelling mistakes in your code , Starting from the 1st line. Maybe taking care of them will bring you better luck.

Your code is painful to read. Why not break things up in your class in segments like

fetchDOB(/* arguments */);
fetchName(/* arguments */);
.
.

each will have their own code inside them , and makes things easy to follow. It will also be easier for you to debug aswell. It would help out both you and us if you do that first.

somjit{} 60 Junior Poster in Training Featured Poster

@diafol : what i agree upon is that the education system here is quite bad.

Theres nothing to hide regarding that. As AncientDragon had mentioned , a phd can be bought in the US as well as in India. These things hardly change in any part of the world. I'v seen people assume that indian student learning C must be programming with Turbo-C , I'v read words like "code-monkeys" in low wage countries , and many others . code-monkeys exist in any place where there's programming getting done. statements like

It's overall cultural as well, unwillingness to admit lack of knowledge to foreigners leading to misinterpreted specs, bugs that could have been avoided by asking a few questions,

really ? i mean it all made sense untill you add the cultural part. Even then , are there are no arogant know-it-all programmers in the west ?

It was sad to see such ideas about us developing amongst people from other countries. And though i would love to be angry on those who say such stuff, i cant because i know some "dumbasses" are always out there , making a bad name for more than just themselves.
In this case , the OP , and because of this and what i wrote above , i hoped that the thread could be deleted if possible. But i understand that its a bit too late for that.

somjit{} 60 Junior Poster in Training Featured Poster

I assume that there's a frequency based check where the time period is one day. If its not too much code , how about adding a nested check with a smaller time-period of one or two minutes or so ? That would check if a user is going on a downvote spree.

somjit{} 60 Junior Poster in Training Featured Poster

i cleaned the cache using ccleaner , but wasnt working. Now , after a restart , seems to be working again ! they come like odd , and they go like odd :/

somjit{} 60 Junior Poster in Training Featured Poster

Thanks for the vote of confidence guys :)
however , something even worse is happening rightnow - I can't log in from my own laptop into daniweb. Forget log-in , i cant even open any daniweb links !! chrome is giving an ACCESS_DENIED error when i try to open any daniweb links. I didnt do anything to any settings in either chrome or my antivirus software , so im not sure where to look into if its a case of firewall blocking daniweb.
Last i logged into daniweb from my own laptop was the time i made this post , and read "his" private message. ( yes , i did downvote two people last night with a comment , i doubt one of them , but no , i dont think he has a hand into the chrome heck happening right now)
Anyways , right now , my biggest concern is getting back into daniweb. Some help/suggestions would be a lifesaver !

ps : i'm on my room-mate's laptop right now.

somjit{} 60 Junior Poster in Training Featured Poster

last night , the downvote counter was at 6.
it hasnt been 8 hours , and my downvote counter rockets upto 27! all with just 2 members who have downvoted.

one point though : when i click the "posts voted down" , shows only 3 posts.

ps : i dont get any "upload" or "insert into editor" options after choosing a image for upload

somjit{} 60 Junior Poster in Training Featured Poster

think about something :
what is it that you read from a console ?
ans : you read strings.
what are strings ? they are (C-wise) an array of characters.
Yes , this is a java forum , but there are consederable amount of hints "a string is basically a character array" can give you regarding different methods you can use to do what it is that your trying. Think about it.

somjit{} 60 Junior Poster in Training Featured Poster

For some reason , when i tried for (Double s : dmap.values()) { eclipse was giving me a red dot indicating an error.Hence the long version. Perhaps it needed a refersh or something. but i tried again now, works just fine.

I like your commenting style.

thank you :)

The normal convention is that class names should start with a Capital letter.

i guess i got lazy there.

a question : how can i sort a hashmap ? i searched a bit , everyone suggested using a treemap , which is the reason i used them here. but TreeMap sorts the map in every entry , which i feel is a waste for this particular application. i'd rather sort it all at the end. Is that a good option ? if so , how to do it ?
ps : there were a few complicated looking solutions to this , but i really didnt feel at home with for some reason. Any simple solution ?

somjit{} 60 Junior Poster in Training Featured Poster

Have been studying Reservoir sampling for a couple of days , What I'v tried here is to draw a uniformly random sample of size 3 from a bigger data (the English alphabet) via reservoir sampling.
Below is what i'v come up with. if anyone could review my code and offer some suggestions , would be great.

here's the code :

import java.util.TreeMap;

public class test1 {
    public test1(){
        String[] data = "A B C D E F G H I J K L M O P".split(" ");
        double[] freq = new double[data.length];
        int k = 3;
        String[] sample = new String[k];

        // the tree map stores the count of appearance of each letter during
        // the total sampling procedure
        TreeMap<String, Double> dmap = new TreeMap<String, Double>();
        for (int index = 0; index < data.length; index++) {
            dmap.put(data[index], 0.0);
        }

        for (int loop = 0; loop < 1000000; loop++) {
            int i = 0;
            // initiate the sample
            while (i < k) {
                sample[i] = data[i++];
            }
            // start sampling from the reservoir
            for (; i < data.length; i++) {
                int r = (int) (Math.random() * (i + 1));
                if (r < k) {
                    sample[r] = data[i];
                }
            }
            // update the count of each entry in the map
            for (String s : sample) {
                dmap.put(s, dmap.get(s) + 1);
            }
        }
        int index = 0;
        for (Double s : dmap.values().toArray(new Double[dmap.size()])) {
            freq[index++] = s;
            // System.out.println(s);
        }
        // calculate statistics
        double mean, stddev , cov;
        mean …
somjit{} 60 Junior Poster in Training Featured Poster

generally , not much. but its good to have the option i suppose. Unless your doing something really big , or doing some testing , doesnt matter much.
you may like reading this .

somjit{} 60 Junior Poster in Training Featured Poster

I feel sad that for a few dumbass people with no sense of what they are saying , students from india get such a bad rep. Unfortunately , the rotting education system in the country keeps feeding off peoples hopes that getting their child to an engineering institution would guarantee a better future , and keeps churning out more such dumbasses by the year.

what is more unfortunate is that , these people get heard the most.

i would request the mods to delete this thread.

somjit{} 60 Junior Poster in Training Featured Poster

answer to my original question is : yes . which i kinda guessed will bethe case anyway.
im reading into this . turns out to be what i need here.

Thread solved i suppose.

somjit{} 60 Junior Poster in Training Featured Poster

Thanks to both of you for replying.
i tried asking a lot of people , but didnt get any answers.
@rubberman : i was really starting to think whatever programming iv done would go to waste. hope restored :)

somjit{} 60 Junior Poster in Training Featured Poster

writting this :

N = args[1].split("\\s+").length;

with commandline argument like : echo "A B C D E F G H I" | java Subset 3 , will this consume similar memory as would have been the case if i parsed the "A B C D E F G H I" string using .split() into a String array ?

my assingment states that (as a challenge) the student (me) can try to display K strings ( K = 3 in the above cmd line ) uniformly at random , from the N string ( N = 9 above , A to I ) input , while consuming memory proportional to K , and not N.
so thats basically what im trying to do.

somjit{} 60 Junior Poster in Training Featured Poster

are you trying to add/remove something to/from your arraylist while your program is running ? like read something from the console , parse it as a string and add it to your string type arraylist ? if so , then yes. that is quite a normal usage.

somjit{} 60 Junior Poster in Training Featured Poster

i had previously worked with the GNS3 and cisco packet tracer console as a part of college lab work. It involved setting up and simulating LAN and VLAN topologies.
Iv seen people in the CS dept , as part of their course , simulate various TCP/IP functions using C and other programming languages . I like coding , and had spent a considerable amount of time learning C , Java ,Databases and Embedded Stuff , So is there any career path i can take up in the networking domain which will also lead me to a work environment where i can extend the skills i have learnt in programming with the above languages?

ps : How much will a ccna help in all of this ?

pps : i have a probable job offer as a hardware support guy. And im thinking of joining it if it helps with what i have in mind.