• Post Reply Bookmark Topic Watch Topic
  • New Topic

page 42 exercice help  RSS feed

 
jonny Alva
Greenhorn
Posts: 14
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the exercises on page 42 it give me this:
class DVDPlayer {
Boolean canRecord = false; <------------------------------------------------------------------------------
void recordDVD() {
System.out.println ("DVD recording");
}
}

class DVDPlayerTestDrive {
public static void main (String [] args) {

DVDPlayer d= new DVDPlayer();
d.canRecord=true; <----------------------------------------------------------------------------------
d.playDVD();

if (d.recordDVD ==true){ <-------------------------------------------------------------------------------
d,recordDVD();
}
}
}



Right here it says that the Boolean statement canRecord is "false", but on the bottom part it then says that the canRecord is "true", shouldn't the Boolean statement be "true" so the can bottom parts can be true also?

ANother question is: where you put "public static void main" that's always going to be the main class?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Page 42 of which book? Is it Head First =HFJ)?
Please check the code carefully; I can see at least one copying error.
You really should have indented that code, then you can see which part of the code is which.

I think you are confusing the assignment operator = and the == operator. There are historical reasons why = was used not to mean what it means everywhere else. Read = as, “becomes equal to” and == as, “is equal to”. Then read the whole code again.
In my copy of HFJ it says to look for compile‑time errors in that code. What errors did you find? That code is not presented as good working code, but as something for you to find errors in.
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make your code easier to read I decided to format it.



In the class DVDPlayer you are defining one instance variable for all objects of type DVDPlayer. This instance variable is declared as boolean canRecord = false;. This means for every object of type DVDPlayer you create they will all have a default instance variable of type boolean named canRecord that is set to false. You can, if need be, change this instance variable by using an object of type DVDPlayer. This exact process happens in your code. Where do you change it? Take a look at lines 15 and 16. At line 15 you create an object of type DVDPlayer and name it d. When this object is created it is automatically given the default instance variable canRecord that you defined. On line 16 you use the object d that you just created and change its instance variable canRecord from false to true by using d.canRecord = true. So that's how you change canRecord from false to true. If you wanted, you could bring it back by inserting a statement d.canRecord = false; and canRecord for object d is set back to false.

To answer you second question, there really is nothing as a main class. The main method is a very important piece to every Java program as it is the entry-point for the program. When you compile your program the Java compiler will look for your main method as a starting point. You could say that whatever class contained your main method is your main class, but that isn't a standard.

In addition, I would like to point out some errors in your code.

• Line 17, you invoke a method playDVD() on object d. Looking throughout the two classes, now playDVD() method has not been defined.
• Line 19, there is no instance variable defined as recordDVD.
• Line 21, you have made a typo. There is a comma where there should be a dot operator.

Best of luck, my friend. Feel free to post back with any questions you may have. Java Ranch is a friendly place for Java Greenhorns so soak all this constructive criticism in and learn lots from it all .
 
jonny Alva
Greenhorn
Posts: 14
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ok I understand, so on the top on the Boolean statement. It is false, But you can change it back to true when you invoke the d. statement. So if the Boolean statement were to be true, the d.canRecord=true would not be needed?
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, it would not need to be changed to true if it was already preset as that objects instance variables. What you should do is complete the exercise from that book. Take your time to go through the code. Get it to compile and then to run. Then once you get it to work you must study the code to know how it all works and most important of all... PLAY with the code like a new toy you were given when you were 5 years old. By experimenting you will ultimately answer any questions you stumble upon. If you hit a road block, though, feel free to post back and ask the Ranchers for their input!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:. . . PLAY with the code like a new toy you were given when you were 5 years old. . . .
What good advice, but it is not quite like a toy for a 5‑year‑old. Unlike a real toy you can't break your software (unless you do something really daft like deleting system files ‍)
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point! I digress, I may be the only one who turns into an excited 5 year old when I test theories in code. (Or am I?)
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tjhere are worse things than excited 5‑year‑olds. We have two excitable three‑year‑olds in the family
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:(Or am I?)

Oooooh no. Hi, I'm a 57-year old 5-year old. Nice to meet you.

Winston
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to see I'm not the only one! Greetings!
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

line 21 as above should be as below right(dot not comma)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!