Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > About java program.

Reply
Thread Tools

About java program.

 
 
rajbhaumik@gmail.com
Guest
Posts: n/a
 
      06-15-2013
import acm.program.*;
public class askYesNoQuestion extends ConsoleProgram {
public void run(){
String str=readLine("Would you like instructions");
println(askYesNoquestion(str));
}
7. private boolean askYesNoquestion (String str){
if(str.equals("yes"))return true;
if(str.equals("no")) return false;
}

}
the false is that , the program cannot be executed, because no 7 said , this method must return a type of boolean.
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      06-15-2013
On 6/15/2013 4:38 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> import acm.program.*;
> public class askYesNoQuestion extends ConsoleProgram {
> public void run(){
> String str=readLine("Would you like instructions");
> println(askYesNoquestion(str));
> }
> 7. private boolean askYesNoquestion (String str){
> if(str.equals("yes"))return true;
> if(str.equals("no")) return false;
> }
>
> }
> the false is that , the program cannot be executed, because no 7 said , this method must return a type of boolean.


Let meask you a question: what value will the method return if
the input string is "xxx"?

Arne


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      06-15-2013
On 15.06.2013 22:38, (E-Mail Removed) wrote:
> import acm.program.*;
> public class askYesNoQuestion extends ConsoleProgram {
> public void run(){
> String str=readLine("Would you like instructions");
> println(askYesNoquestion(str));
> }
> 7. private boolean askYesNoquestion (String str){
> if(str.equals("yes"))return true;
> if(str.equals("no")) return false;
> }
>
> }
> the false is that , the program cannot be executed, because no 7
> said, this method must return a type of boolean.


And, do you have a particular question or are you just informing us of
your compiler error?

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-16-2013
On Sat, 15 Jun 2013 13:38:48 -0700 (PDT), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

> private boolean askYesNoquestion (String str){
> if(str.equals("yes"))return true;
> if(str.equals("no")) return false;


what should it return if I typed "maybe"?
--
Roedy Green Canadian Mind Products http://mindprod.com
Getting information off the Internet is
like taking a drink from a fire hydrant.
~ Mitch Kapor 1950-11-01
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-16-2013
On 16.06.2013 15:21, lipska the kat wrote:
> On 16/06/13 00:12, Robert Klemme wrote:
>> On 15.06.2013 22:38, (E-Mail Removed) wrote:
>>> import acm.program.*;
>>> public class askYesNoQuestion extends ConsoleProgram {
>>> public void run(){
>>> String str=readLine("Would you like instructions");
>>> println(askYesNoquestion(str));
>>> }
>>> 7. private boolean askYesNoquestion (String str){
>>> if(str.equals("yes"))return true;
>>> if(str.equals("no")) return false;
>>> }
>>>
>>> }
>>> the false is that , the program cannot be executed, because no 7
>>> said, this method must return a type of boolean.

>>

>
> One possible solution (not compiled or tested)
>
> private boolean askYesNoquestion (String str){
> boolean response = false;
> if(str.equals("yes")){
> response = true;
> }
> return response;
> }


Why are you making it so complicated? Just do

private static boolean isStringYes(String str) {
return str.equals("yes");
}

Or, for NPE safety:

private static boolean isStringYes(String str) {
return "yes".equals(str);
}

Btw, I also changed the name because the method did not ask a question
at all.

Cheers

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      06-16-2013
On 6/16/2013 2:52 PM, lipska the kat wrote:
> On 16/06/13 16:28, Robert Klemme wrote:
>> On 16.06.2013 15:21, lipska the kat wrote:
>>> On 16/06/13 00:12, Robert Klemme wrote:
>>>> On 15.06.2013 22:38, (E-Mail Removed) wrote:
>>>>> import acm.program.*;
>>>>> public class askYesNoQuestion extends ConsoleProgram {
>>>>> public void run(){
>>>>> String str=readLine("Would you like instructions");
>>>>> println(askYesNoquestion(str));
>>>>> }
>>>>> 7. private boolean askYesNoquestion (String str){
>>>>> if(str.equals("yes"))return true;
>>>>> if(str.equals("no")) return false;
>>>>> }
>>>>>
>>>>> }
>>>>> the false is that , the program cannot be executed, because no 7
>>>>> said, this method must return a type of boolean.
>>>>
>>>
>>> One possible solution (not compiled or tested)
>>>
>>> private boolean askYesNoquestion (String str){
>>> boolean response = false;
>>> if(str.equals("yes")){
>>> response = true;
>>> }
>>> return response;
>>> }

>>
>> Why are you making it so complicated? Just do
>>
>> private static boolean isStringYes(String str) {
>> return str.equals("yes");
>> }

>
> This is the age old argument about the use and abuse of the return
> statement. I can't see us coming to a MAC so there's little point
> arguing about it here really.
>
> But OK, it's another solution.


How can it be misuse of return to have a non-void method
with a single return statement as the last statement returning
what should be returned?

Arne


 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      06-17-2013
On 06/16/2013 12:28 PM, Robert Klemme wrote:
> On 16.06.2013 15:21, lipska the kat wrote:
>> On 16/06/13 00:12, Robert Klemme wrote:
>>> On 15.06.2013 22:38, (E-Mail Removed) wrote:
>>>> import acm.program.*;
>>>> public class askYesNoQuestion extends ConsoleProgram {
>>>> public void run(){
>>>> String str=readLine("Would you like instructions");
>>>> println(askYesNoquestion(str));
>>>> }
>>>> 7. private boolean askYesNoquestion (String str){
>>>> if(str.equals("yes"))return true;
>>>> if(str.equals("no")) return false;
>>>> }
>>>>
>>>> }
>>>> the false is that , the program cannot be executed, because no 7
>>>> said, this method must return a type of boolean.
>>>

>>
>> One possible solution (not compiled or tested)
>>
>> private boolean askYesNoquestion (String str){
>> boolean response = false;
>> if(str.equals("yes")){
>> response = true;
>> }
>> return response;
>> }

>
> Why are you making it so complicated? Just do
>
> private static boolean isStringYes(String str) {
> return str.equals("yes");
> }
>
> Or, for NPE safety:
>
> private static boolean isStringYes(String str) {
> return "yes".equals(str);
> }
>
> Btw, I also changed the name because the method did not ask a question
> at all.
>
> Cheers
>
> robert
>
>

To some extent I agree with you, Robert.

A little side note about the NPE-safe form of doing "equals": this can
sometimes bite because of the very liberal method signature. Just last
week I was doing some CXF+JAXB work with SOAP, and for a few hours I was
perplexed as to why a a path was not being taken. Anyone who has worked
with generated code for SOAP knows how verbose and encapsulated and
boilerplate it is; lots of commenting and good white space needed, and
copy & paste is to be avoided (albeit tempting).

Anyway, come to find out that I was doing a String equals (had to be
equals, not equalsIgnoreCase), and the argument was not a String but
through a typo an MXString. equals() of course happily accepted that and
always returned false.

That's not to say it wasn't my mistake, it was. But I've found that
equals() is a relatively fertile source of program defects.

With your method it's not a concern, you're controlling the type of the
parameter. But in wider expanses of code then it's important to nail
down that the 2 objects being compared are of the expected types. Code
reviews, maybe even an instanceof if correctness at this point is
essential (more so than we usually desire but often don't achieve )

For String in particular, I wouldn't actually mind if there was an
equals(String). In some cases I'll use String#compareTo(String) to
achieve that.

To the original point, I agree to an extent. But the actual idiom that
lipska used is not bad in general, if you're used to single return. It's
sort of like *always* providing braces for conditionals, even for one
liners, defensive coding in part, but also adhering to personal coding
style.

AHS

--
When a true genius appears, you can know him by this sign:
that all the dunces are in a confederacy against him.
-- Jonathan Swift
 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      06-17-2013
On 06/17/2013 10:46 AM, lipska the kat wrote:
> On 17/06/13 13:49, Arved Sandstrom wrote:
>> On 06/16/2013 12:28 PM, Robert Klemme wrote:
>>> On 16.06.2013 15:21, lipska the kat wrote:
>>>> On 16/06/13 00:12, Robert Klemme wrote:
>>>>> On 15.06.2013 22:38, (E-Mail Removed) wrote:
>>>>>> import acm.program.*;
>>>>>> public class askYesNoQuestion extends ConsoleProgram {
>>>>>> public void run(){
>>>>>> String str=readLine("Would you like instructions");
>>>>>> println(askYesNoquestion(str));
>>>>>> }
>>>>>> 7. private boolean askYesNoquestion (String str){
>>>>>> if(str.equals("yes"))return true;
>>>>>> if(str.equals("no")) return false;
>>>>>> }
>>>>>>
>>>>>> }
>>>>>> the false is that , the program cannot be executed, because no 7
>>>>>> said, this method must return a type of boolean.
>>>>>
>>>>
>>>> One possible solution (not compiled or tested)
>>>>
>>>> private boolean askYesNoquestion (String str){
>>>> boolean response = false;
>>>> if(str.equals("yes")){
>>>> response = true;
>>>> }
>>>> return response;
>>>> }

>
> [snip]
>
>> To the original point, I agree to an extent. But the actual idiom that
>> lipska used is not bad in general, if you're used to single return. It's
>> sort of like *always* providing braces for conditionals, even for one
>> liners, defensive coding in part, but also adhering to personal coding
>> style.

>
> ... and what is your opinion of defensive coding?


Not far different from yours, apparently.

> Anyone who has ever written software to interact with a user via a
> device interface knows (or should know) that defensive coding is de-riguer.
>
> If you don't want the device to crash every time someone does something
> unexpected you need to anticipate no only the unexpected but the
> unanticipated. There is *no* room for error.
>
> This discipline is hard to learn and once learned even harder to
> un-learn. This is why I don't like things like
>
> return str.equals("yes");
>
> It's lazy coding and harder to understand than my example and yes, I
> realize that it's only a one liner but the next stage in the
> 'development' of this style is
>
> if(something){
> return something;
> }
> else if(something else){
> return something else;
> }
> else{
> return some other value
> }
>
> It is much easier to understand and debug
> the following
>
> type var;
>
> if(something){
> var = something;
> }
> else if(something else){
> var = something else;
> }
> else{
> var = some other value
> }
>
> return var;
>
> If I was doing a proper job and not just providing a simple example I
> would have had a pre-condition on the method *and* exception handling
> *and* braces around every conditional, even one liners,
> *and* a fallback position if the operation was mission critical. So yes,
> it's my personal style learned from painful experience.
>
> Thank you for understanding
>
> lipska
>

Generally speaking all of this boils down to one thing: errors happen
when the same developer - you, me, anyone - has white-box knowledge of,
and maybe responsibility for, both called *and* calling code. This
extends even to method lexical scope where a developer may write some
stuff that makes assumptions about his or her own code in the same
method 50 lines previous.

I daresay that this state of affairs is the norm. Sure, a whole bunch of
developers work on NASA or defense or avionics or Big Healthcare type
projects where you've got a great deal of imposed rigor. But very likely
most developers own a whole bunch of code, and I'm sure that a large
number of us write entire apps. For example, as a software consultant I
often do analyze, design and code entire applications.

So, given this, it's necessary to be somewhat schizophrenic - when
working on method X in class A, and it's called by method Y in class B
and method Z in class C, and you are writing *all* of them, you need to
wear 3 hats. And by extension N hats as you implement the entire app,
and even more because you're also doing design and analysis.

God forbid that the developer also does the functional and integration
and system testing, but this is not rare. But let's ignore that aspect
of real life.

If there actually is a good sized team the same considerations apply.
The nature of the beast these days is that often developers have
visibility into calling code that they don't have responsibility for,
because they do git or svn or hg, and they inevitably see way more than
they ought to.

I've rarely seen it done, because it adds substantial cost, but a better
approach would be to have each developer assigned (a) method(s) in a
class just write the class, and also be given time to write necessary
scaffolding in the form of tests that exercises their code. They never
see the complete codebase. The advantage of this is that they write to a
written design spec and not to knowledge of other code, and that they
have also necessarily written useful tests.

Robert's method, the NPE-safe one that also types the method parameter,
is not objectionable to me precisely because the equals is encapsulated
in a method that constrains the type. Trivial methods of this sort are
quite handy as private methods and make the code more robust, since
equals() by itself is too Wild West.

AHS

--
When a true genius appears, you can know him by this sign:
that all the dunces are in a confederacy against him.
-- Jonathan Swift
 
Reply With Quote
 
Joerg Meier
Guest
Posts: n/a
 
      06-17-2013
On Mon, 17 Jun 2013 14:46:57 +0100, lipska the kat wrote:

> return str.equals("yes");


> It's lazy coding and harder to understand than my example and yes, I
> realize that it's only a one liner but the next stage in the
> 'development' of this style is


Personally, I found your example considerably harder to read than the one
liner, as well as harder to read than what would have been my solution:

private boolean askYesNoquestion (String str){
if(str.equals("yes"))
return true;

return false;
}

Of course, YMMV. Obviously, I find code written the way I would write it
easier to read, so I can't be objective. The problem I have with this code:

> type var;


> if(something){
> var = something;
> }


is that I can't tell what is returned if 'something' is true. Unless I look
at all the following code, var could easily be assigned a second time to
something different before it is returned.

Liebe Gruesse,
Joerg

--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-17-2013
On 6/17/2013 2:32 PM, Joerg Meier wrote:
>[...]
> Personally, I found your example considerably harder to read than the one
> liner, as well as harder to read than what would have been my solution:
>
> private boolean askYesNoquestion (String str){
> if(str.equals("yes"))
> return true;
>
> return false;
> }


Or, for even greater clarity:

private boolean askYesNoquestion(String str) {
boolean yes;
boolean no;
if (str != null && str.equals("yes")) {
yes = true;
no = false;
} else if (str != null && str.equals("no")) {
no = true;
yes = false;
} else {
yes = no = false;
}
return (no != true && yes != false) == true;
}

(If we put our minds to it, I bet we can turn the one-liner
into an entire class hierarchy with runtime-selected plug-in
provider implementations!)

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need your advices about C prg. Dogukan Bayraktar C Programming 76 06-16-2013 08:54 AM
Survey about Software Integration Martin Dias C Programming 0 04-29-2013 03:23 PM
Quesion about running a exe file in Python(Not enough memory) yuyaxuan0@gmail.com Python 5 04-26-2013 06:30 AM
silly question about Running a script from the command line A.Rock Python 0 04-10-2013 11:21 AM
newbie question about confusing exception handling in urllib cabbar@gmail.com Python 6 04-09-2013 07:11 PM



Advertisments