Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Homework Assignment

 
Octavia Mondesir
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you looking for this:

[solution deleted]
[ April 04, 2008: Message edited by: Fred Rosenberger ]
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
K Aditi
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, should have been '||' instead of '|'.
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49447
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49447
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah. Thanks for the hint, I am so dumb, but I think I have it now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic