Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A task from H. Schildt - Java a beginner's guide 6th edition.  RSS feed

 
Yerlan Toleibergen
Greenhorn
Posts: 9
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi racnh!
Being an absolute beginner in Java, want to sorry for my interpretation of programming terms and sorry for my English
I read H.Schildt - Java A Beginner's Guide 6th edition. As I understood, we can not convert (cast) boolean types into another, like int or char for example. If I'm mistaken, please correct my mind ))
So, I need a help with a task# 6 on 85th page (I read russian translation of the book).

We have some code there:



The output of the code:
P Q AND OR XOR NOT
true true true true false false
true false false true true false
false true false true true true
false false false false false true

An the task says to rework the code thus the output should contain "1" instead of logical "true" and "0" instead of "false".
Please, give me the cue, how to solve this. Thanks in advance!

 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, let's look at the code you have. These four lines are repeated four times.

Do you know how to create methods other than main()? When you see repeated code, it should go in a method.

Now you need a method that will convert a boolean into an int. Is that something you could do or do you need help with that?
 
Yerlan Toleibergen
Greenhorn
Posts: 9
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Knute Snortum.
Previosly I tried to read Head First Java by K.Sierra. But it seemed a litlle bit difficult for me, without clarifying some terms, which suddenly appeared there. In Shieldt's book I like the description of all, just for novice in programming world. So while reading Head First, I met some examples with creation other methods, not only main. But i feel that I need some help with creating a method in this case. Thanks!
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a tutorial on creating methods. (That Oracle tutorial is a good one to go through complete.) Basically a method is defined like this:

The words in brackets need to be changed to some valid values. "printLogic" is a name I gave the method. It can be called anything but it should give some idea of what the method is doing. So you have these repeating lines:

Those go in the body. Except... p and q change, don't they? So they need to be parameters -- that is, we will pass in the values of p and q when we call the method like this:

But there's an easier way to do this. Just pass in the boolean values, not the variables:

So now back to the method definition. We need two parameter names. They could be called anything but p and q are fine. We also need to know the types of the parameters:

The last thing is the returnType. Since this method doesn't return anything, its return type is "void". If you have a return type, you need to have a "return" statement, but not if the method returns void.

So now create the method (I've done most of it for you) and call the method four time from the main() method. (There's a better way to do this, but we'll get to that later.) Post your code, and then we'll work on getting your program to print ones and twos.
 
Yerlan Toleibergen
Greenhorn
Posts: 9
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, you,ve done a great work, typing all this information here and explaining me methods "theory". I appreciate this. Have too read a little bit about methods, and then I'll try to represent to you my code. Thank you again!
 
Yerlan Toleibergen
Greenhorn
Posts: 9
Java MySQL Database Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Knute Snortum! How are you?
So, I read information about methods from your link, thought a little bit, and then realized, that I forgot to tell you, that on the stage, where I read Schildt's book, there is no any information about methods yet, it's gonna be a bit further. I tried to rewrite the code above like that:





If it's made wrong or somehow awfully, please let me know about it. But at least, it gives the task result.
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's certainly a way to do it. When you get to methods in your book, come back and rewrite your code with a method.
 
Yerlan Toleibergen
Greenhorn
Posts: 9
Java MySQL Database Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranch!
Hello Knute Snortum! How are you?!
Already half a month passed and I finally got to the methods )))
So, back to the ""TRUE FALSE table.
I tried to rewrite the very first code using a method, that you already explained me. So the code looks like this:



output gives the same solution that in original code. And it's fine! But I terribly can't understand, how to cast BOOLEAN into INT or conversely.
I suggest, that I should add some new method, that will do this job, but how?
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Yerlan Toleibergen, I'm fine.

I'm sorry to say it, but the code you posted is... well, not good. It doesn't compile and it has all sorts of errors. Can you post code that compiles and prints the table?

There is no way to cast a boolean to an int, but you can use this simple code that uses the ternary operator:
 
Yerlan Toleibergen
Greenhorn
Posts: 9
Java MySQL Database Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh, I see it! Posted another copy of the code. I tried to do it 3 times and this one was not my best
So, here is the code, that exactly compiles:



If there some errors, that you can find, could you pleasenotice them. Thanks!
 
Yerlan Toleibergen
Greenhorn
Posts: 9
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And as always, want to thank you for your support!
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the first error is that the code is not formatted. Use this Style Guide as a starting point.

There is no way to case a boolean into an int, but here is a quick way to convert them:
 
Bill foster
Ranch Hand
Posts: 63
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute:
There is no way to case a boolean into an int, but here is a quick way to convert them:


Hi Knute,

Can you concatenate the Boolean into a string then parse it into a int?

I was just wondering?

Bill
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill foster wrote:. . . Can you concatenate the Boolean into a string then parse it into a int? . . .
You would not want to catenate it to a String. You probably mean boolean, not Boolean, but with unboxing, you will hardly notice a difference.You cannot parse those Strings to ints. You can do this sort of thing:-…but I do not think you would ever want to. You should get out of thinking 0=false and 1=true or nonzero=true. That is why Java┬« supports a boolean datatype and does not use ints instead.
 
Bill foster
Ranch Hand
Posts: 63
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Richie,

in your code

You said that we need not to think of false/true as int numbers, why not it seems to me that the code above states either 0 or 1.
I think it is important to know if you can what type of numbers is false or true. Without this knowledge then the code below:




would not mean too much to me.


Thanks,

Bill
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill foster wrote:Hi Richie,

in your code . . .
You said that we need not to think of false/true as int numbers, . . .
Actually I said that after the code. The reason for saying that
true/false
is not a number is that true/false are not numbers. There is no a priori reason why 0 should mean false and 1 true. And some implementations use −1 for true. Why couldn't you use 1 for false and 0 for true? Why would
System.out.println(Boolean.parseBoolean(text) ? 123 : 456);
be any less valid? People used true/false for thousands of years without thinking of them as numbers; it was only when computers came along that people started thinking of 0 as false and 1 as true.

The Java┬« language has taken great pains to get away from 0=false, 1=true by clearly distinguishing between boolean values and numbers (except possibly with the %b tag in printf). So why go back to numbers?

I wrote that code because somebody wanted code to print 0 or 1 depending on a predicate. But I did say you wouldn't want to parse booleans to ints.
 
Bill foster
Ranch Hand
Posts: 63
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richie,

I think Knute said in the post above that you can convert booleans to numbers.
I misunderstood this, I thought the values were represented by numbers, the values 0 and 1 is what you as the programmer decides?

I hope I got it!

Thanks,

Bill
 
Bill foster
Ranch Hand
Posts: 63
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richie and Knute,

I found that the ternary operator is short hand for a if-then-else statement.

I read the tutorial on oracle: Ternary operators

Okay I see what you mean.

Thanks for the info.

Bill
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill foster wrote:. . . I misunderstood this, I thought the values were represented by numbers, the values 0 and 1 is what you as the programmer decides?
. . .
I think you have got it

Obviously in a computer a true/false must be represented by something in binary, and 0000_0000 is a good way to represent false and 1111_1111 is quite good for true. But that is an implementation detail. If I decide to print 999 for false and 666 for true, I can do that with the ?: operator, which as you noticed, is rather like squeezing an if‑else into a single expression. Yes, in the code shown in this thread we decided to print 1 for true and 0 for false. But that was our decision and does not necessarily reflect any implementation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!