It is currently Thu Mar 28, 2024 7:03 pm



Reply to topic  [ 54 posts ]  Go to page Previous  1, 2, 3, 4  Next
anyone here do Java? 
Author Message
King
User avatar

Joined: Wed Apr 16, 2003 2:18 pm
Posts: 1976
Location: Sexy Town
Reply with quote
Post 
mmm irish cream mocha....

_________________
Contrary to popular belief, America is not a democracy, it is a Chucktatorship.
Image


Thu Mar 24, 2005 3:14 pm
Profile ICQ YIM
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
I remember when i used to post stuff like that on a highly technical thread that i didnt understand.

Satis ended up knocking off something like 500 post count. :D

Ah the old days.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Fri Mar 25, 2005 12:14 pm
Profile
King
User avatar

Joined: Wed Apr 16, 2003 2:18 pm
Posts: 1976
Location: Sexy Town
Reply with quote
Post 
Man, thats tough, I guess its a good thing I dont give a poop about my post count. :)

<-- Drinking some java right now.

_________________
Contrary to popular belief, America is not a democracy, it is a Chucktatorship.
Image


Sun Mar 27, 2005 8:00 am
Profile ICQ YIM
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
New Problem!!!

Maybe this is something more for GFree...

I have a 5*5 2D array of integers, and i need to write some code that prints the column with the most odd integers.

I am totally stuck. Im using the code below and i get the following compile-time error: (sorry about indentation, the [code ][/ code] tags dont use spaces)

Code:
Ws2Ex2.java:86: cannot find symbol
symbol  : variable col
location: class Ws2Ex2
                        colPoint = col;
                                   ^


Here is the code im using: (Notice i have not yet written a System.out.println line yet because i first want it atleast to compile!

Code:
public class Ws2Ex2 {
    public static void main (String args [])
    {
        int[][] ex2Arr = {{2,1,4,6,8},{2,1,4,6,8},{2,1,4,6,8},{2,1,4,6,8},{2,1,4,6,8}};
            int colPoint;
            int prevTally = 0;
            int tally;
            for (int row = 0; row < ex2Arr.length; row++)
                {
                    tally = 0;
                    for (int col = 0; col < ex2Arr[row].length; col++)
                        if (ex2Arr[row][col] % 2 == 0) tally++;
               
                    if (tally > prevTally)
                    {
                        prevTally = tally;
                        colPoint = col;
                    }
                }
       
    }
}


Wed Mar 22, 2006 6:53 pm
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
The variable col does not exist on the place you use it. (I think Pig mentioned the rule)

Code:
// col lives in this for olny
   for (int col = 0; col < ex2Arr[row].length; col++)
            if (ex2Arr[row][col] % 2 == 0) tally++;
// col dies here

         if (tally > prevTally)
         {
            prevTally = tally;
            // col does not exist here
            colPoint = col;
         }


So, now it compiles. But I'm not sure what is an *odd* nr.


You do not have the required permissions to view the files attached to this post.


Thu Mar 23, 2006 10:11 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
'odd' numbers are 1, 3, 5, 7, 9, 11, 13, 15, etc. Ie, divide by 2 and see if there's a remainder. If there is, it's an odd number.

I could probably do this in php in 20 minutes, but since it's Java I can't be of much help.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu Mar 23, 2006 10:35 am
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Cheers, ill give it a look later tonight.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Thu Mar 23, 2006 10:45 am
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Ok. But let me do some additional notices here: First you are searching through the columns of one matrix, which would mean that you have to fix the second index and look up by first for the count of odd numbers. So the loop would look like:

Code:
for(i=0;i<arr[0].length;i++)
{
for(j=0;j<arr.length;i++)
{
// arr[j][i] ...
}
}


Second: what happens when there are no colmuns with odd numbers or when there are two colmuns with the came count of odd numbers?

Third: This one

Code:
ex2Arr[row][col] % 2 == 0


will return you true if the number is even.

See attachment if you'd like.


You do not have the required permissions to view the files attached to this post.


Thu Mar 23, 2006 12:04 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
G. Freeman wrote:
First you are searching through the columns of one matrix, which would mean that you have to fix the second index and look up by first for the count of odd numbers. So the loop would look like:

Code:
for(i=0;i<arr[0].length;i++)
{
for(j=0;j<arr.length;i++)
{
// arr[j][i] ...
}
}


Im too dumb to work out what that does in my mind.

Quote:
Second: what happens when there are no colmuns with odd numbers or when there are two colmuns with the came count of odd numbers?


I havent got that far yet. I did identify this problem, but i will solve it when i actually get there.

Quote:
Third: This one

Code:
ex2Arr[row][col] % 2 == 0


will return you true if the number is even.


Yeh, i know. I changed that yesderday with:
Code:
!=


Still havent looked at the files yet. Ive had no free time today.


Thu Mar 23, 2006 12:28 pm
Profile
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Elementary question:

What is x?

int[][] ex2Arr = {{2,1,4,6,8},{2,1,4,6,8}}
int i = 0;
x = ex2Arr[i].length;

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Thu Mar 23, 2006 4:05 pm
Profile
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
x=5.

doesn't the .length method give you the number of elements in the array? And since i is defined as being 0, it references the first array of the array, which is 5 elements big.

right? :P I don't know java, but this looks an awful lot like other programming languages I've used (kinda a fusion of javascript and visual basic .net and shit)

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu Mar 23, 2006 4:35 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Nah, its mainly just "shit".

You'd probably find Java a piece of piss Sat. I used to like it, now i fucking hate it with a vengeance. Its too low-level for me. I like to be on top! 8-)


You do not have the required permissions to view the files attached to this post.


Thu Mar 23, 2006 4:39 pm
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
1. Yes. But function do not get actually the whole array but the pointer to it.

2. Not beyound loops exactly. You can use them at any place in the function after the place where they are declared.

3. The value -1 is just an value which is not correct value of an index. In principle you could use -2, -3 etc. If function does not find any passing column, this parameter (it is return parameter as well) will stay set on -1 and the function will return it, so you can know that matrix consist of even numbers only. You can use one new boolean variable, which would be initialized to false on the start of the function, and assigned to true, when the first colmun with odd numbers is found.

4. Yes
5. Yes

6. If you mean, would (mat[0].length = 5) be correct, the answer is no. The variable length is declared as *final* which means that it cannot be changed after it is once assigned.

7. Nope... i is still just an counter.

8. The code int n = mostOddCol(ex2Arr); does the same as

int n;
n = mostOddCol(ex2Arr);

- So, the variable n will be declared.
- After that it will be assigned to a returned value from mostOddCol(ex2Arr).
- mostOddCol(ex2Arr) is the call of method mostOddCol with one argument - 2D array ex2Arr.


Fine manual: http://java.sun.com/docs/books/tutorial/java/TOC.html

_________________
++


Thu Mar 23, 2006 6:22 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Now im screwing around with inheritance. Im getting my head around it, slowly, but (hopefully) surely.

Any tips you got to offer GFree?

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Thu Apr 20, 2006 2:25 pm
Profile
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
children inherit from parents

parents don't inherit from children
:D

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu Apr 20, 2006 2:36 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 54 posts ]  Go to page Previous  1, 2, 3, 4  Next

Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.