Java Homework HELP!

trance

Senior member
Oct 9, 1999
202
0
0
Ok, you guys convinced me.

The text this course was assigned "Java, An Introduction to Computer Science & Programming 3rd Ed." by Walter Savitch. I've found the text to be convoluted and redundant.

Are there any books that would be recommended for basic introductory java course?


_____________________________________________________________________________________
_____________________________________________________________________________________
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

}

 

EpsiIon

Platinum Member
Nov 26, 2000
2,351
1
0
I have no idea how to help you without flat-out giving you the answers...

Write down (in English) how you'd do it if they were real objects. Then try to translate those to Jave code. That's the best I can come up with...

For instance, for the lastZero problem, I'd imagine that somebody actually gave me a bunch of boxes lined up and asked me to tell them the index of the box with the last zero. I'd start at the back, looking at each box to see if it has a zero in it. When I found a zero, I'd write down the number of the box. Then all I'd have to do is tell the person what number I wrote down. It's just that simple.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
1.
The method lastZero is passed an integer array, and returns the position of the last occurrence of 0 (zero) in the array. If no zeroes are present, the method should return -1.
---------------------------------------
public int lastZero(int[] data){

// Code copyright Tyler Karaszewski 2004
int lastZero = -1;
for ( int ii = 0; ii < data.length; ii++){
if (data[ii] = 0){
lastZero = ii;
}
}
return lastZero;
}


2.
The method longString is passed an array of strings, and returns the number of strings in the array that are longer than 10 characters.
------------------------------------
public int longString(String[] data){

// Code copyright Tyler Karaszewski 2004
int lastZero = 0;
for ( int ii = 0; ii < data.length; ii++){
if (data[ii].length() > 10){
lastZero ++;
}
}
return lastZero;
}


3.
The method trueCount is passed a boolean array, and returns the number of true values in the array.
--------------------------------
public int trueCount(boolean[] data){

// Code copyright Tyler Karaszewski 2004
int lastZero = 0;
for ( int ii = 0; ii < data.length; ii++){
if (data[ii] = true){
lastZero++;
}
}
return lastZero;
}

Come on, these are ridiculously simple, if you can't get them, drop the class.

BTW, all code I have written above is my intellectual property. Reproduction of it without permission is a criminal offense, and turning it in to your teacher is cheating.
 

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.

. . . and "my teacher didn't force me to learn it" is never an acceptable excuse. Did you ask questions? Did you see the TA if this isn't high school? Did you meet in a study group with other students? Did you try at all?
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: notfred
1.
The method lastZero is passed an integer array, and returns the position of the last occurrence of 0 (zero) in the array. If no zeroes are present, the method should return -1.
---------------------------------------
public int lastZero(int[] data){

// Code copyright Tyler Karaszewski 2004
int lastZero = -1;
for ( int ii = 0; ii < data.length; ii++){
if (data[ii] = 0){ <-- Should be == not =
lastZero = ii;
}
}
return lastZero;
}


2.
The method longString is passed an array of strings, and returns the number of strings in the array that are longer than 10 characters.
------------------------------------
public int longString(String[] data){

// Code copyright Tyler Karaszewski 2004
int lastZero = 0;
for ( int ii = 0; ii < data.length; ii++){
if (data[ii].length() > 10){
lastZero ++;
}
}
return lastZero;
}


3.
The method trueCount is passed a boolean array, and returns the number of true values in the array.
--------------------------------
public int trueCount(boolean[] data){

// Code copyright Tyler Karaszewski 2004
int lastZero = 0;
for ( int ii = 0; ii < data.length; ii++){
if (data[ii] = true){ <-- should be == not =
lastZero++;
}
}
return lastZero;
}

Come on, these are ridiculously simple, if you can't get them, drop the class.

BTW, all code I have written above is my intellectual property. Reproduction of it without permission is a criminal offense, and turning it in to your teacher is cheating.

I see you like to code errors into ridiculously simple problems to prevent people from taking them. :p:D

Anyway, == isn't the same as = in Java. == is equality and = is for assignment.

 

electified

Member
Sep 2, 2004
102
0
0
You should read the book and at least attempt to do the problems, they are not hard at all. If you still have problems post what you did and ask for help rather than just ask for people to do your homework.
 

klah

Diamond Member
Aug 13, 2002
7,070
1
0
If you can't code those after reading your textbook it must be poor quality. Read the Sam's book and these will be easy.
 

trance

Senior member
Oct 9, 1999
202
0
0
Ok, you guys convinced me.

The text this course was assigned "Java, An Introduction to Computer Science &amp; Programming 3rd Ed." by Walter Savitch. I've found the text to be convoluted and redundant.

Are there any books that would be recommended for basic /introductory java course?

Klah: Thanks, I'll check that out.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: trance
Ok, you guys convinced me.

The text this course was assigned "Java, An Introduction to Computer Science &amp; Programming 3rd Ed." by Walter Savitch. I've found the text to be convoluted and redundant.

Are there any books that would be recommended for basic /introductory java course?

Klah: Thanks, I'll check that out.

This one.
 

trance

Senior member
Oct 9, 1999
202
0
0
THe OReily book rerquires programming background in C++, which I don't have. So its really not the ideal text for me.
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
I really liked Bruce Eckel's "Thinking in Java".
Plus, it's FREE to download right here.

According to Eckel, sales of books have been even better since he started making them freely available in electronic format. His "Thinking in C++" book is also available as a free download.

EDIT:
"I've found the text to be convoluted and redundant."

I have to say that I'm not sure how you could find the text to be convoluted and redundant when you obviously haven't read any of it. Loops are so straightforward, that I'm not sure how any textbook could do a bad job of describing them.
 

imported_jediknight

Senior member
Jun 24, 2004
343
0
0
OK. I don't believe in giving answers flat out, but I can help you a bit. Consider #4.
Ask yourself:
What does the loop do? (Output numbers)
Where does it start?
When does it stop?

then,
int counter = whereIStart;
while (counter < whereIStop)
{
doWhatINeedTo;
incrementCounter;
}

Seriously, these are very trivial programs.. I suggest you seek help from your professor/TA if you don't understand how to do them on your own..