• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Homework Assignment

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) {

}
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you looking for this:

[solution deleted]
[ April 04, 2008: Message edited by: Fred Rosenberger ]
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, should have been '||' instead of '|'.
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79174
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ||.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79174
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. Thanks for the hint, I am so dumb, but I think I have it now
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic