im trying to learn java on my own and need a bit of help.

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 3394 Views - Last Post: 16 October 2011 - 12:57 PM Rate Topic: -----

#1 kendav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 11

im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 09:02 AM

Passing 3 numbers call a function that will print these numbers in ascending order, if you pass (98, 234, 6)

(print 6 98 234)

i declared them:

[ int m = 98;]
[ int l = 234;]
[ int s = 6;]

but this is where im stuck.
Is This A Good Question/Topic? 0
  • +

Replies To: im trying to learn java on my own and need a bit of help.

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 09:04 AM

Are you familiar with arrays? You should take a look at arrays to handle sorting elements.

If you haven't gotten to arrays yet, simple if statements can help you determine the ordering of the numbers.
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 09:06 AM

Don't use parentheses and square brackets unnecessarily. It's confusing and messy.

You must have more than that. If not, you need to turn back a few pages and learn about basic Java program structure; classes, the main() method, etc.

Have you done the "HelloWorld.java" program?

Put what you have into a bare bones, skeleton source file including a class and a main() method, and we can go from there.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,138
  • Joined: 19-March 11

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 09:10 AM

Am I correct in assuming that your assignment is to write a method to sort three integers?

If so, you should be familiar with writing methods, assuming your teacher isn't a complete psychopath, and assuming you've been paying attention in class. So, why don't you start by giving us a method signature for the method you're going to write. What does the method look like?
Was This Post Helpful? 0
  • +
  • -

#5 kendav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 11

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 09:18 AM

dont have a teacher learning on my own!

Im finding different task and trying to complete them. My way of learning is doing!
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 09:18 AM

View Postkendav, on 15 October 2011 - 12:17 PM, said:

dont have a teacher learning on my own!

Then . . . you're the teacher, and we're here to help.
Was This Post Helpful? 0
  • +
  • -

#7 kendav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 11

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 09:20 AM

Yes, and thank you.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,138
  • Joined: 19-March 11

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 09:58 AM

View Postkendav, on 15 October 2011 - 11:18 AM, said:

dont have a teacher learning on my own!

Im finding different task and trying to complete them. My way of learning is doing!



So I'll assume your teacher is a pretty decent sort, then, but not very well qualified. :)

For writing methods, you might take a look here.

They don't have a lot of exercises in that particular tutorial. If you want some things to try to do, write these methods:

- a method called returnTrue that takes no argument and returns a boolean true
- a method called return argument that takes a boolean argument and returns that argument
- a method called logicalAnd that takes two boolean arguments and returns true if they are both true (you can use the logical and operator if you want, or you can write it with if statements)
- a method called sum that takes two ints and returns their sum
- a method called isGreater that takes two ints and returns true if the first is greater than the second
- a method called areSorted that takes three ints and returns true if they are in order from smallest to largest.

Once you've written each of those and tested them, you should have a sense of how to write a method that returns a value.

Tip: to call a method from the main method of a class, you'll need to declare the method "static". This will be covered in the tutorial I linked to, and there will be plenty of material found by googling "java static methods"...

Second tip: to test a function, call it with a known set of values and print the result. If the result suits, the values, it's passed at least one test.


since you're teaching yourself, I'm going to let you struggle with this for at least a day before I answer any questions on this....
Was This Post Helpful? 1
  • +
  • -

#9 kendav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 11

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 10:41 AM

I think i got it:      


public static void partOne(int m, int l, int s)
    {

        int temp = 0;
        if (m>l)
        {
        temp = m;
         m = l;
         l = temp;
        }
         if (l>s)
        {
         temp = l;
         l = s;
         s = temp;
        }
         if (m>l)
        {
         temp = m;
         m = l;
         l = temp;
        }

This post has been edited by jon.kiparsky: 15 October 2011 - 11:05 AM
Reason for edit:: added code tags, deleted duplicated code

Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,138
  • Joined: 19-March 11

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 11:11 AM

That looks reasonable. There's one minor bit of efficiency that you might be able to squeeze out of it. In the last step, you compare l and m, but you only would need to do this if you've done the swap in step 2. (if you don't swap in step two, you know that l < m still holds, right? So you could move step three into the last if.

But this looks right to me.
Was This Post Helpful? 1
  • +
  • -

#11 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 11:33 AM

This would seem like a good time to check out Math.max() and Math.min(). Are you familiar with any other languages? if so how would you go about finding the solution. That might give you some more options.
Was This Post Helpful? 1
  • +
  • -

#12 kendav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 11

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 11:35 AM

OKay cool "jon.k" ill go back and look.
Was This Post Helpful? 0
  • +
  • -

#13 kendav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 11

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 11:42 AM

In found a new task to try:

while(1) loop, print the upper case alphabeth and its corresponding ascii values, from Z to A

But not fully understanding.

i was reading and seen that i hade to import.KB and Z - A is 90 - 65.
Was This Post Helpful? 0
  • +
  • -

#14 kendav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 11

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 11:51 AM

this is what i just did:


public static void ascii( int Z, int A);

int Z = 90
int A = 65

int main()
{

printf("ASCII values from Z to A are :\n");
for(i=90;i<=65;i++)
{
printf("%c=%d\n",Z,A);

}
}
Was This Post Helpful? 0
  • +
  • -

#15 kendav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 11

Re: im trying to learn java on my own and need a bit of help.

Posted 15 October 2011 - 11:58 AM

public static void ascii( int Z, int A);
{

printf("ASCII values from Z to A are :\n");
for(Z=90;A<=65;++)
{
printf("%d, %d\n", Z, A);

}
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2