Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-02-2009, 04:31 PM
Member
 
Join Date: Feb 2009
Posts: 12
Rep Power: 0
pinkdiamondgail is on a distinguished road
Question Help! I have homework in arrays
1.write code that sets each element of an array called nums to the value of the constant INITIAL.
2.Write code that prints the values stored in an array called names backwards.
3.Write code that sets each element of a boolean array called flags to alterning values (true at index 0. false at index 1, etc)
4.Write a method called sumArray that accepts an array of floating point values and returns the sum of the values stored in the array.
5.Write a method called switchThem that accepts two integer arrays as parameters and switched the contents of the arrays. Take into account that the arrays may be of different sizes.

If anyone can help me you totally rock.I am so dumb when it comes to java I don't know how I got this far. Please help me I think I have number 1 let me know if this is right?

1.// init
int[] intArray = new int[10];
for (int i=0; i<intArray.length; i++)
intArray[i] = constant INITIAL;

// print in revers
for (int i=intArray.length-1; i>=0; i--)
System.out.println(intArray[i]);
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-03-2009, 02:59 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default
First examples look good. Have you tried compiling and running them to see if they do what you want?
for #3, just have a loop aternate between assigning true or false dependign on previous boolena value (pseudo code):

Code:
boolean boolValue = false
start loop
if boolValue == false
boolValue = true
else boolValue = false
assign boolean value to array
end loop
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-03-2009, 03:35 PM
Member
 
Join Date: Aug 2008
Location: The Netherlands
Posts: 25
Rep Power: 0
Rooneyz is on a distinguished road
Thumbs up
An alternate approach for #3 is the following:

Code:
Boolean[] array = new Boolean[10];
for(int i = 0;i<10;i++)
{
if(i % 2 == 0)
{
   //Number is even
array[i] = true;
}else
{
   //number is odd
array[i] = false;
}
}
Number 4# (In a generous mood, looking forward to the weekend ):

Code:
    public Float addUp(Float[] floats)
    {
        Float returnFloat = new Float(0);
        for(Float floatValueInArray : floats)
        {
            //Array might containt null values
            if(floatValueInArray != null)
            {
            //Add up the value in the array to the current value of variable returnFloat
            returnFloat += floatValueInArray;
            }
        }
        //return the result
        return returnFloat;
    }

Last edited by Rooneyz; 04-03-2009 at 03:43 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-04-2009, 07:51 AM
Member
 
Join Date: Feb 2009
Posts: 12
Rep Power: 0
pinkdiamondgail is on a distinguished road
Default You guys rock
thank you so much for helping me.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-17-2009, 04:44 AM
Member
 
Join Date: Apr 2009
Posts: 9
Rep Power: 0
chapiforme is on a distinguished road
Default
Great answers!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP Homework exercise albi_87m Java 2D 3 04-04-2009 06:10 AM
Java homework please Indulgence New To Java 1 11-03-2008 03:48 AM
LF: Homework help with Java excurssion New To Java 2 10-17-2008 07:00 AM
Homework help... robrobrob New To Java 4 10-17-2008 05:24 AM
Need help with homework. JavaNewbie0000 New To Java 2 07-31-2008 04:06 PM


All times are GMT +2. The time now is 03:08 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org