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

• (2 Pages)
• 1
• 2

## 26 Replies - 7095 Views - Last Post: 16 October 2011 - 12:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=251415&amp;s=6ffda2ddfcebb2997ad2eeb0d866cfa4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kendav

Reputation: 0
• 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

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• 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.

### #3 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• 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.

### #4 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,804
• 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?

### #5 kendav

Reputation: 0
• 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!

### #6 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• 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

kendav, 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.

### #7 kendav

Reputation: 0
• 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.

### #8 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,804
• 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

kendav, 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....

### #9 kendav

Reputation: 0
• 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

### #10 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,804
• 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.

### #11 immeraufdemhund

• D.I.C Regular

Reputation: 79
• 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.

### #12 kendav

Reputation: 0
• 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.

### #13 kendav

Reputation: 0
• 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.

### #14 kendav

Reputation: 0
• 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);

}
}

### #15 kendav

Reputation: 0
• 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);

}