Java Homework HELP!

trance

Senior member
Oct 9, 1999
202
0
0
I"m getting so frustrated about this java course because the professor teachers absolutely nothing. WHich is ironic since the first exam was insanely simple!! So can someone give me a hand for a few problems? THey are basic loop problems:


4.
Consider the sequence 3, 4, 5, 6, 7. The sequence starts at 3 and ends at 7. Write a method called firstToLast, which is passed two integer values, first and last. The method then prints the integer sequence from first to last in a column.
For example, firstToLast(3,7) should print:
3
4
5
6
7

You may assume that first < last always holds.
----------------------------------
public void firstToLast(int first, int last){

CODE HERE
}


5.

Write a method called backwardsByTwos, which is passed an integer value, k. The method then counts backwards by 2, printing in a column the positive integer sequence k, k-2, k-4, and so forth.

If k <= 0, the method should print nothing.

For example, backwardsByTwos(6) should print:
6
4
2
------------------------
public void backwardsByTwos(int k){
CODE HERE

}

---

PSA - This is not the Software - Applications, Programming and Games forum.

AnandTech Moderator
 

Krk3561

Diamond Member
Jun 12, 2002
3,242
0
0
public int lastZero(int[] data){
for (int i=data.length-1; i>=0; i--){
if (Integer.toString(data[i(forum code messes it up)]).indexOf('0') != -1){
return i;
}
}
return -1;

}
 

cyclistca

Platinum Member
Dec 5, 2000
2,885
11
81
Dude if you can't figure these out your in big trouble. Wait until you have to pass an Object array and do stuff with it.
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
Do you have a specific question or do you just want us to do your homework for you?
 

EmperorIQ

Platinum Member
Sep 30, 2003
2,003
0
0
i'm usually theguy that tells everyone else to go easy on him since its his first time, but seeing how this comes up so many times i see how you guys get frustrated.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Read The Friendly Textbook.

Learn to think for yourself.

You can't become even a bad programmer by rote memorization.
 

Beattie

Golden Member
Sep 6, 2001
1,774
0
0
Acutally on second thought, learning this stuff can be hard. Here's some help

the first one goes something like this:

int i=0;
int index=-1;
for (i;i<data.length();i++) {
if (data{i} == 0) {
index = i; // or i+1 if you want the postition instead of index
}
}
return index;

those braces on the if data thing are square brackets... the forum code messes it up.
 

Beattie

Golden Member
Sep 6, 2001
1,774
0
0
Why am I in such a good mood?
2)
int i=0;
int count=0;

int (i;i<data.length();i++) {
if (data{i}.length() >= 10) {
count++;
}
}

Hopefully that will give you an idea how to do the rest. These may not be exactly right. The if data thing has braces instead of square brackets because of the forum code... you need to change that.

and I am not sure if the thing is .length() or .size(). You have to check that out. I dont remember off the top of my head.

 

BigPoppa

Golden Member
Oct 9, 1999
1,930
0
0
I've got to be one of the worst coders ever and I could program both of those in any language by...RTFM.
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
void firstToLast(int x, int y) {
while (x<=y) {
system.out.println(x);
x++;
}
}

void backwardsByTwos(int k) {
while (k!=0) {
system.out.println(k);
k-=2;
}
}

You really shouldn't have a problem with it, but with how screwed I'm getting from some of the stuff my teachers are giving me (second assignment in one class was calculate pi over range from 1-50000 in assembler...) I feel like throwing a bone to ya.

Edit: mmm pie....
2nd Edit: err off by one error :Q
 

Anonemous

Diamond Member
May 19, 2003
7,361
1
71
note to self:
1)build time machine, go back in time and post homework assignment on offtopic,
2)???
3)profit!

 

Chebago

Senior member
Apr 10, 2004
575
0
0
marcyes.com
this kinda reminds me of my day today, I taught a help seesion for the students today for a project that is coming up and they didn't seem to grasp any of it, I tried to simplify it as best I could but I don't think they even caught on to the easy parts...the really sad thing is is that the class is the last java class and is considered "advanced java"...it's really sad how far people can get in programming without ever learning to program.
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
What's question 6, integer x integer. If that's a programming assignment it makes most of the software engineers round here seem beyond guru's.