wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes new to write code, pls help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new to write code, pls help!" Watch "new to write code, pls help!" New topic
Author

new to write code, pls help!

luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Hi, I am new to write code and not feeling confident about the code I wrote. For the following programming excercise:
Design a Java class to represents a book which may be borrowed from a public library.
Some behaviors for the class are:
int getId( ) - gets the acquisition number of the book (unique for each title)
setBorrowed(boolean) - sets or clears the borrowed status of the book.// Do I need to initialize the setBorrowed "false"?
The acquisition number of this book is automatically generated by the constructor. The acquisition numbers are integers starting with 1 and increase by 1 for each new title generated.// How to express this in the code?
I tried to write the following code but not sure whether I did correct with the above two questions I commented. Please let me know if you think something is wrong with the code. Thanks.

huiying li
Ranch Hand

Joined: Feb 12, 2001
Posts: 68
Originally posted by luk Hann:
[B]Hi, I am new to write code and not feeling confident about the code I wrote. For the following programming excercise:
Design a Java class to represents a book which may be borrowed from a public library.
Some behaviors for the class are:
int getId( ) - gets the acquisition number of the book (unique for each title)
setBorrowed(boolean) - sets or clears the borrowed status of the book.// Do I need to initialize the setBorrowed "false"?
The acquisition number of this book is automatically generated by the constructor. The acquisition numbers are integers starting with 1 and increase by 1 for each new title generated.// How to express this in the code?
I tried to write the following code but not sure whether I did correct with the above two questions I commented. Please let me know if you think something is wrong with the code. Thanks.
[/B]

public class Book1 {
private static int nextId = 1;
private int id;
private boolean bBorrowed; // no need to intialize

public Book1() { // constructor
id = nextId++; // this should be in the constructor
}

public int getId () {
return id;
}

public void setBorrowed (boolean b) {
bBorrowed = b; // set the member variable
}
// test the class
public static void main(String[] args){
Book1 b1 = new Book1();
System.out.println("Book: " + b1.getId());
System.out.println("Borrowed: " + b1.bBorrowed);
// now set the Borrowed status
b1.setBorrowed(true);
System.out.println("Borrowed: " + b1.bBorrowed);
// try one more book
b1 = new Book1();
System.out.println("Book: " + b1.getId());
System.out.println("Borrowed: " + b1.bBorrowed);
}

}

SCJP
luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
huiying,
Thanks so much for your advice. It really cleared my problems. I appreciate very much!
BTW, what is the difference between having a different Test class and putting them together?
Luk
Scott Pedigo
Greenhorn

Joined: Feb 15, 2001
Posts: 15
The test part (main()) is an application which instantiates the class, not another class. The class by itself does nothing.
The following question is certainly beyond the scope of a beginning Java course, but you might as well be exposed to the general idea of protecting variables against concurrent access. What happens if - thinking of this as an analog - two librarians try to add a new book at the same time? Is is possible for one of them to get the nextId, and then before it is incremented, the other to also get the same nextId, resulting in two books with the same id?
It is definitely unlikely, but I'm not sure the possibility can be categorically excluded. For example, in a multi-threaded program where a thread is interrupted at exactly the wrong instant and then another thread gets control. You're probably thinking, 'what is a thread?'. Imagine the logic of your program as a track, with two runners running down the track at the same time. Or in this situation, two librarians. There is a way to deal with this. A method to get the nextId could be written, and declared 'synchronized'.
private synchronized int getTheNextId()
{
return nextId++;
}
The constructor would call this method instead of accessing the nextId variable directly. Only one thread is allowed to go through this method at a time. You may be wondering how nextId could be read twice without it being incremented between the first and the second access. It depends on what kind of machine instructions the CPU is capable of executing and what kind of Java byte code is generated and what the JVM does. For example, there *could* be an atomic instruction at the hardware level which does all of this - atomic meaning something that can't be divided into any more parts - an all or nothing instruction as it were. I have seen processors with special registers which increment automatically when they are read. But the above Java code might also be implemented as several separate machine code instructions - one to read the value from memory into a register, another to increment the register, and another to write the register back to memory. If a thread were interrupted just after executing the first instruction, another thread could run through the same code, and also read the memory into a register in its own context (a context is each thread's copy of the register contents).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: new to write code, pls help!
 
Similar Threads
Front controller and DAO, design question!
Having Trouble Referencing a Method from Anonther Method in Another Class
EJB3.0 example
Array List is Printing Out the Exact Same Thing
I want to use indexed properties