GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Homework Assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Homework Assignment" Watch "Homework Assignment" New topic
Author

Homework Assignment

Octavia Mondesir
Greenhorn

Joined: Apr 03, 2008
Posts: 2
I am taking an online class with no formal direction from the professor. I have been working on the following for over an hour and have not been able to make any headway. Please help!
*********************
Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more.

firstLast6({1, 2, 6}) → true
firstLast6({6, 1, 2, 3}) → true
firstLast6({3, 2, 1}) → false

public boolean firstLast6(int[] nums) {

}
K Aditi
Ranch Hand

Joined: Mar 17, 2008
Posts: 89
Are you looking for this:

[solution deleted]
[ April 04, 2008: Message edited by: Fred Rosenberger ]

Aditi
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi
Originally postd by K Aditi
[solution deleted]

No doubut the above code solves the problem, but to keep things clear let me make a correction,
the if statement should be

because putting brackets around expressions makes it more readable and | is a bitwise OR operation not a logical OR, that may be a typo, but makes much difference.

Hope this helps
[ April 04, 2008: Message edited by: Fred Rosenberger ]

SCJP, SCWCD.
|Asking Good Questions|
K Aditi
Ranch Hand

Joined: Mar 17, 2008
Posts: 89
Yes, should have been '||' instead of '|'.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Octavia Mondesir,
Welcome to JavaRanch.

Quick Tip: As the discussion on "|" and "||" has come up. So, please visit this opertors link to have more details.
[ April 04, 2008: Message edited by: Anubhav Anand ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39076
    
  23
Please don't simply give answers to homework questions, and the answer given is, I am fraid, not at all elegant.

Octavia, welcome to the Ranch, and your mission, should you wish to accept it, is to get that method down to a single line, and explain whether you agree with the points about () and | or ||.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Not that I can think of a more elegant solution, its too early.
However, given the wording of the question, the 6 is the first OR last item in the array.
So surely
-> is false, not true.

Or having a very well defined meaning in English, that of, one of two choices not both.

however I believe for the given code it would return true. I would mark down for that *shrugs*.
K Aditi
Ranch Hand

Joined: Mar 17, 2008
Posts: 89
Please don't simply give answers to homework questions, and the answer given is, I am fraid, not at all elegant.

Hi Campbell,
Could you please give an elegant solution for the problem.Now I am not asking for it as a homework solution.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3603
    
  15
Originally posted by K Aditi:

Hi Campbell,
Could you please give an elegant solution for the problem.Now I am not asking for it as a homework solution.


Hint : you are using the result of a boolean statement (can be true or false) to decide whether to return true or false.


Joanne
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi Joanne Neal

Hint : you are using the result of a boolean statement (can be true or false) to decide whether to return true or false.


I am unclear about the above statement, will you please explain.
If its regarding ternary operator ?:, then I think its not a good choice for beginners.

Thank you.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3603
    
  15
Originally posted by Amit Ghorpade:
Hi Joanne Neal


I am unclear about the above statement, will you please explain.
If its regarding ternary operator ?:, then I think its not a good choice for beginners.

Thank you.


It's nothing to do with the ternary operator. In order to avoid giving the OP the answer, I've sent you a private message.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39076
    
  23
Do you mean either-or-but-not-both? There is a list of all operators in the Java Tutorial, here. See if you can't find the correct operator there.

Hint: Its name is a bit confusing until you are used to it.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Ah. Thanks for the hint, I am so dumb, but I think I have it now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Homework Assignment