• 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

new to write code, pls help!

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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);
}

}
 
luk Hann
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic