Learning about classes

Hi,
I'm working my way through "The Java Programming Language, Fourth Editition" by Sun Microsystems. There are exercises through the book, but they don't give the solutions (which is helpful!!!).
They show a simple class, then ask you to produce an identical one to keep track of vehicles. Based on their sample code this should be the code:
class Vehicle {
     public int speed;
     public String direction;
     public String owner;
     public long idNum;
     public static long nextID = 0;
  }Then then ask you to write a class called LinkedList giving no coding examples 'that has a field type Object and a reference to the next LinkedList element in the list. Can someone help me to decipher what this means and the appropriate code?
They then show how to create objects linked to the vehicle class, which based on their sample code should be
Vehicle car = new Vehicle();
car.idNum = Vehicle.nextID++;
car.speed = 120
car.direction = "North"
car.owner = "Metalhead":
This is the bit that next confuses me. They ask you to write a main method for your Vehicle class that creates a few vehicles and prints their field values.
The next exercise is to write a main method for LinkedList class that creates a few objects of type vehicle and places them into successive nodes in the list.
Is'nt it correct that the above code starting 'Vehicle car' would be the code used to create the object which would go in the LinkerList calling on the Vehicle method which has already been defined? For example, successive entries could be for bus, bike etc in the LinkerList. What does it mean though to place these into successive 'nodes' on the list? And why would you create vehicles in the main method of the Vehicle class. Shouldn't they just be created in the LinkerList?
I'm only learning about classes and I'm confused already?
Any help (and code) would be great!!!!
Thanks for any help,
Tim
p.s. I know what I have written sounds vague, bu the book doesn't really give much extra helpful info.

First of all, the variables of the "Vehicle" class should all be private.
You should not be able to directly retrieve each variable from this class
Example:
Vehicle car = new Vehicle();
car.idNum = Vehicle.nextID++;
car.speed = 120
car.direction = "North"
car.owner = "Metalhead"You should have methods that can change this data as well as retrieve the data.
Example:
class Vehicle {
     private int speed;
     private String direction;
     private String owner;
     private static long nextID = 0;
        public Vehicle() {
            speed = 0;
            direction = "";
            owner = "";
            ++nextID;
        // These methods retrieve the attributes
     public int getSpeed() { return speed; }
        public String getDirection()  { return new String(direction);}
        public String getOwner() { return new String(owner);}
        public long getidNum() { return idNum; }
        // These methods change the attributes
     public int setSpeed(int i) { speed = i; }
        public String setDirection(String s)  { direction = new String(s); }
        public String setOwner(String s) { owner = new String(s); }
}Now, they want you to create another CLASS that shows what your Vehicle class can do.
Example:
public class SomeRandomExampleClass {
    public static void main(String[] args) {
        Vehicle v1 = new Vehicle();
        // Now let's fix up our vehicle
        v1.setSpeed(100);
        v1.setOwner("Zeus");
        v1.setDirection("North");
        // Do note that since idNum is a static variable, it will be increased everytime you create
        // an instance of a Vehicle, and works with the class rather than the object.
        // For instance, if you create Vehicle's v1, v2, and v3, then v1's idNum = 1, v2's idNum = 2;
        // and v3's idNum = 3.
        Vehicle v2 = new Vehicle() ;
        v2.setSpeed(500);
        v2.setOwner("Ricky Bobby");
        v2.setDirection("NorthEast");
        System.out.println(v1.getSpeed()); // prints v1's speed.
        System.out.println(v1.getOwner()); // prints Owner
        System.out.println(v1.getDirection); // Prints Direction
        System.out.println(v1.getidNum());  // prints idNum
        System.out.println(v2.getSpeed()); // prints v1's speed.
        System.out.println(v2.getOwner()); // prints Owner
        System.out.println(v2.getDirection); // Prints Direction
        System.out.println(v2.getidNum());  // prints idNum
}A linked list is what it sounds like. It can hold a list of (let's say vehicles) and you can print their
attributes from there.
For instance, let's say we have a list called list1.
list1.add(vehicle1);
list1.add(vehicle2);
Then you can iterate(move) through your list and print the attributes of each vehicle, instead of having to
call v1.doThis() v1.doThis(), v2.doThis().
I find it amusing they want you to create a LinkedList when you are first using/learning classes.
To understand how to create a linked list, you can visit:
http://leepoint.net/notes-java/data/collections/lists/simple-linked-list.html
Or you can use:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedList.html
*Note: I wrote the code from nothing so some of it could have syntax errors.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • Learn about classes and perfom in abap

    hallow
    i wont to learn how to use classes and perform
    in abap, any document will help
    regards

    I will send you the documents...
    please give your mail address...
    it's good to here about ABAP OOPS.
    Regards,
    Jayant
    Please award if helpful

  • Please suggest me the best way to start learning about Oracle Purchasing.

    Hi,
    I have started reading about Oracle Procurement. I have an over all idea of how ERP functions. Anyhow I am like a fresher to ORACLE APPS. Can you pleasesuggest me what is the best way to start learning about Oracle Purchasing.
    Is it is throuhg user guides or some thing else..... can you please specify the references also.......
    Thank you so much.

    If you have a sponsorship, I would recommend taking classes with Oracle University. The other resources are documentation, functional and technical depending on what your interests are.
    If you can load Vision database or access vision database (I think solutionbeacon offers a free account to login and play around), that will be another resource as well.
    As you learn, join as much user groups like this one, and review all questions and answers in the discussions to understand and learn more.
    Hope this helps.

  • Question about classes/Directories

    I've been using Java to learn programming for a while now, so this question may not belong here. It seems to be more of a general question, however, not code related.
    How does java work with directories? Say I had a project, and for some reason I wanted different java class files to be in different directories. How does java handle this? Say for example:
    1.) Main class is in C:/blahblah/Project/ and the other classes were in C:/blahblah/Project/src/?
    2.) Main class is in C:/blahblah/Project/src/ and the other classes were in C:/blahblah/Project/?
    3.) Main class is in C:/blahblah/Project/src/ and other classes were in C:/blahblah/Project/bin/?
    I know these may be kind of stupid examples, but I just wanted to know how such things worked for future reference. Thanks a ton!

    {color:#3D2B1F}It sounds like you need to learn about packages: [http://java.sun.com/docs/books/tutorial/java/package/index.html] {color}

  • Question while Learning about Thread

    I am learning about thread.
    I ran the sample producer/consumer programe in Java Thread Tutorial in which I added two lines of printing out codes in class CubbyHole to observe the runing sequence better.
    The result made me a bit confused. I imagine that the result "Producer #1 put: 1" should be printed out right after "put 1", why it appeared after "get 1" and "Consumer #1 got: 1"?
    Codes are listed below:
    public class ProducerConsumerTest {
    public static void main(String[] args) {
    CubbyHole c = new CubbyHole();
    Producer p1 = new Producer(c, 1);
    Consumer c1 = new Consumer(c, 1);
    p1.start();
    c1.start();
    public class Producer extends Thread {
    private CubbyHole cubbyhole;
    private int number;
    public Producer(CubbyHole c, int number) {
    cubbyhole = c;
    this.number = number;
    public void run() {
    for (int i = 0; i < 10; i++) {
    cubbyhole.put(i);
    System.out.println("Producer #" + this.number
    + " put: " + i);
    try {
    sleep((int)(Math.random() * 100));
    } catch (InterruptedException e) { }
    public class Consumer extends Thread {
    private CubbyHole cubbyhole;
    private int number;
    public Consumer(CubbyHole c, int number) {
    cubbyhole = c;
    this.number = number;
    public void run() {
    int value = 0;
    for (int i = 0; i < 10; i++) {
    value = cubbyhole.get();
    System.out.println("Consumer #" + this.number
    + " got: " + value);
    public class CubbyHole {
    private int contents;
    private boolean available = false;
    public synchronized int get() {
    while (available == false) {
    try {
    wait();
    } catch (InterruptedException e) { }
    available = false;
    System.out.println("get " + contents);
    notifyAll();
    return contents;
    public synchronized void put(int value) {
    while (available == true) {
    try {
    wait();
    } catch (InterruptedException e) { }
    contents = value;
    System.out.println("put " + contents);
    available = true;
    notifyAll();
    The result is as below:
    put 0
    Producer #1 put: 0
    get 0
    Consumer #1 got: 0
    put 1
    get 1
    Consumer #1 got: 1
    Producer #1 put: 1
    put 2
    get 2
    Consumer #1 got: 2
    Producer #1 put: 2
    put 3
    get 3
    Consumer #1 got: 3
    Producer #1 put: 3
    put 4
    get 4
    Consumer #1 got: 4
    Producer #1 put: 4
    put 5
    get 5
    Consumer #1 got: 5
    Producer #1 put: 5
    put 6
    get 6
    Consumer #1 got: 6
    Producer #1 put: 6
    put 7
    get 7
    Consumer #1 got: 7
    Producer #1 put: 7
    put 8
    get 8
    Consumer #1 got: 8
    Producer #1 put: 8
    put 9
    Producer #1 put: 9
    get 9
    Consumer #1 got: 9

    Hi alaska,
    The reason you got those results are because these are
    threads. The execution of the order of thread is
    always random (unless some explicit logic is applied)
    and CPU can execute any thread at any time within the
    process. So the results you got are perfectly right.
    Even the order of the output may change everytime you
    run the same program.
    -- manishI think you can change the predictability by setting the Producer's thread priority has higher than the Consumer's...

  • How can I learn about Row Set Navigator

    I would like to learn about Row Set Navigator and all DataWebBeans classes. Where do I find these informations.

    jdeveloepr has online documentation for all of this
    also review the tutorials ... provided there .

  • Learn about JVM

    I am looking for a job in Java, learning Java for interviews. I want to be through in the subject.
    What is the best book to learn about the inner workings on JVM..class loading, class execution etc.
    Also, any other suggestions for interview prep?
    Thanks
    Jed

    This may help you get some perspective [Teach Yourself Programming in Ten Years|http://norvig.com/21-days.html] Even with 10 years experience most experts only know a small fraction of all the Java APIs available.
    You could read the pages on the following web site. [http://java-sources.org/] I suggest you just try to learn that such APIs exist and what the most common terms are. No-one could hope to claim they know them all.

  • About Class Object  as Array

    Hello.. I'm beginner in Java Programming
    Now i learn about Array.. i'm not understand about use class object become array. for example i'm create class object Card.
    Then i call Card object to CardTest as main object and Card object called as array.
    can you tell me about how to work Card object as array in CardTest ?
    Edited by: 994075 on Mar 14, 2013 11:55 PM

    994075 wrote:
    yup..thanks guru :DI'm no guru, I just bought a decent Java book 10 years ago and here I am.
    so i guess Object Class can be array :).I don't get that statement. You can store objects in an array, if that's what you mean. An array itself is also an object.
    but i'm not understrand if static method in Card object can be called automatically when Card initialized in CardTest?a) research constructors
    b) you usually don't need static methods, its just something that people new to the language tend to overuse because then you don't have to create object instances. Forget about static methods, try to make it work using regular non-static methods.
    And you really should read and study more, you're coming to the forum way too soon. First get a solid knowledge base, the only thing you're doing now is asking questions that you could have answered for yourself by studying more.

  • Where to learn about formatting Wiki pages? - best way to have a consistent custom formatting?

    Hi,
    I'm a new user to SharePoint Wiki but have a lot of content that would work well in a wiki format that I want to input.  This is an enterprise SharePoint system and I am only a user.
    I am frustrated by the default formatting, and would like scalable way of altering the formatting.  I need to learn about formatting options in sharepoint but I don't know where to start. 
    What learning resources would you recommend?
    Is there a method for managing formatting that you would recommend?
    Many thanks in advance.

    Here are some resources
    Training classes: http://sharepointexperience.com/
    Website with how to 2003 and 2007:
    http://www.heathersolomon.com/blog/articles/resources.aspx
    Website with how to for 2010:
    http://blog.sharepointexperience.com/
    https://www.youtube.com/watch?v=k1v2xKnn50g
    Stacy Simpkins | MCSE SharePoint | www.sharepointpapa.com

  • I have a new iPhone 5S.  While trying to learn about it, I accidentally recorded a voice memo with no content.  I cannot now figure out how to get rid of it.  There is a banner across the top of my phone with this memo which I don't want.  Help!

    I have a new iPhone 5S.  While trying to learn about it, I accidentally recorded a voice memo with no content.  I cannot now figure out how to get rid of it.  There is a banner across the top of my phone with this memo which I don't want.  I have deleted it from iTunes but cannot get it off the phone.  Help!

    The banner usually indicates that the memo is "Paused." If you go back into voice memos, touch the word "Done" beside the big red pause button, give it a name, then it will show in a list. Touch the memo in the list then touch the trash can icon that should appear.

  • Network storage drive...where can I learn about these?

    I have so many questions, and frankly, the answers I've seen looking (briefly, admittedly) around these threads scare me.  Gee, Wally, what ever happened to Apple as the computer you didn't have to know computer programming to use?
    Now, before you all get your panties in a bunch telling me that I'm responsible for knowing how to use my hardware, I know...and I do want to know.
    So what I want to know now is how can I learn about the various types of "shares", and what they mean for hooking up a network drive for backup, etc.
    I have a WD 1TB drive which I believe uses "SMB", whatever that is.  It's got two folders on it currently, "Public" which I can access, and "WD Backup", which I can't.  I also can't seem to use it as a Time Machine drive, which is what I want to do with it.  And, or course, I can't re-format it, which I'd love to do as my wife hates the folder designations on it, and would like to have one folder called "iMac Backup" on it.
    Answers which don't involve Terminal will get extra points, but I've dealt with Terminal before, so take your best shot.  And ask for clarification if you need it.  Thanks.

    Unfortunately, networking and file-sharing aren't quite so simple in part because there's decades of history and dozens of companies involved in getting it into the current state. If you want dead-simple, you can get an Apple Time Capsule or AirPort with an external drive and be done with it. However, if you really want the most from network storage, you'll probably want to read up...
    WD make a number of drive, some network-attached, some not. We'll assume that you have one of their basic consumer network-attached drives.
    "SMB" stands for "Server Message Block" protocol and it was the local area network file-sharing protocol developed for Windows for Workgroups circa 1992 (it was later renamed CIFS - Common Internet File System - by Microsoft in 1996 because of the rising popularity of the Internet, despite the difficulty in getting it to work in anything but a small local network). SMB/CIFS is still widely used today for Microsoft Windows networks, though much of the equipment that serves those files are built on Linux or FreeBSD and not Windows.
    Access to shares on an SMB server (such as your hard disk) is controlled through a configuration utility provided with the drive or via a web application built into the device (that you access through your browser). Consult your manual. Generally speaking, however, SMB servers offer different ways of identifying whether or not you can access a share: 1) based on the computer connecting (in which case, everyone is considered the same user), or 2) based on a username/password combo. Generally a server will only operate in one mode. Shared directories can also allow or deny "guest" access (access without a password). All of this should be configurable through the drive utility or web application which you access with your web browser. Generally, an SMB server can only operate in one mode.
    It sounds like the Public directory is configured to allow guest access, and I'm guessing that "WD Backup" is a share that's password-protected and intended to be used with Western-Digital's own software of the same name. You should be able to add and create new directories as necessary through the provided utility or the web interface.
    You are correct, you cannot use Time Machine with an SMB file-sharing service. Time Machine will require AFP (Apple Filing Protocol) or NFS (Network File System) support. Western-Digital only has 3 network drives that support AFP and Time Machine: My Book Live, MyBook World, and WD ShareSpace. OS X also has some basic requirements about the performance and capabilities of network storage in order to use it with Time Machine, so you really want to look for drives that state up front that you can use Time Machine with them (for example, WD World Edition: http://www.wdc.com/wdproducts/updates/docs/en/appletimemachine.pdf )
    I'm not sure why you can't reformat the drive. This is supported on all WD products (though, if you've moved a bunch of data to it already, perhaps it would be a hassle).
    You can do backups of your Mac over SMB, but it's complicated by the fact that SMB is quite old and isn't capable of storing information about the file permissions, ownership, etc. It will be reliable for data files, but not for applications, etc. There's a work-around, of course. You can create a disk image on the SMB share using Disk Utility (make sure you create an HFS+ image) and backup to that. If you go that route, I would suggest either Carbon Copy Cloner or SuperDuper to perform your backups. They are true backup tools, not versioning tools like Time Machine.

  • Where can i learn about Boot Camp

    to all
    i would like to learn about boot camp and the risks of puttiong windows on my mac. some things i just need on windows.
    where can i read about it before i start to post dumb questions about virus software and the risk of virus, as i'm sure its been addressed somewhere..

    thanks for the reply The Hatter... but its not a case of not leaving my comfort zone, but getting the full facts first for what i want to do. I;m quite good at leaving my comfort zone thank you..
    currently trying to build a server at home with little experience
    teaching myself HTML and Javascript and writing 5 websites on the fly
    among many other things.. i just need to find out more about what i am doing before i do it so i understand what i am doing, and whether its the best use of my time.
    my laptop most certainly doesn't sit on a shelf. its been around the world, to far flung corners of jungles, remote islands in Thailand and never leaves my side. Its on al day working and on all night processing. infact it almost drowned with me when i was caught in a violent storm going between islands in Thailand. Naturally i put the laptop somewhere safe before saving myself from going overboard.

  • HT201250 My backup external hard drive hasn't work since I insalled Snow Leopard. Bad:(  I am still learning about my MAC and would just like to re-format the hard dive and set everything up again. I want to upgrade to Lion but can't untill I backup what

    My backup external hard drive hasn't work since I insalled Snow Leopard. Bad:(  I am still learning about my MAC and would just like to re-format the hard dive and set everything up again. I want to upgrade to Lion but can't untill I backup what I.

    Read through the links here, calm down and take your time.
    https://discussions.apple.com/community/notebooks/macbook_pro?view=documents

  • To learn about ABAP Mapping

    Hi,
    I would like to learn about ABAP Mapping.
    What is the importance of it and  where it is applied?
    Could you please guide me regarding this?
    Thanks,
    Raja Bala

    Hi,
    Go through the links,
    http://help.sap.com/saphelp_47x200/helpdata/en/cf/21f2f2446011d189700000e8322d00/frameset.htm
    http://help.sap.com/saphelp_47x200/helpdata/en/b1/8230a2d26611d4b2e90050dadfb92b/frameset.htm
    http://help.sap.com/saphelp_47x200/helpdata/en/d9/b84508bc9411d4b2e80050dadfb92b/frameset.htm
    Regards,
    Azaz Ali.

  • Questions about classes

    Hi,
    I finally upgraded from Flash 5 to CS3 - big difference :-)
    Reading the first half of an ActionScript 3 book left me
    quite confused about classes - what they are, how you handle
    multiple classes, how you use one class from within another etc.
    Are there any simple-speak tutorials out there that explain
    more about how to use classes?
    Specifically:
    I am trying to use classes to run multiple animations at the
    same time, i.e. using randomized movement for multiple objects on
    my stage.
    First issue is: If I do what I learnt in the book and simply
    create one .as file and have that be the document class, how can I
    prevent it from being run before all the elements on the stage have
    been loaded through a slow network connection?
    Second issue: I'd rather have "dormant" classes sitting
    around and call them once my stage is ready, but I have no idea how
    to handle multiple classes. I played with it for a while and then
    gave up and put my code into a frame, but that caused further
    issues. Again, any tutorials out there or any other advice you can
    give me? I am trying to start some class code at a certain time and
    use it to attach a Frame Event listener to a Sprite that I create
    inside the class.
    It looks easy in the book, but when I try to do my own stuff,
    it just won't do it...
    The book assumes (so far) that you want to run your code
    immediately once your movie starts, but I don't want that...
    Thanks!

    Yes, I realized later that _root etc. is actually as1 code
    from my days of Flash 5 :-)
    All I need is to know how to access a movieclip on the stage
    that was placed using the IDE from within a class, and how to
    access a class and call its methods from within another, and I can
    teach myself the rest...
    So, if I place something with the IDE, doesn't it show up in
    the display list? I should be able to control those elements, too,
    shouldn't I?
    Or do I have to use Actions rather than classes for those
    elements?
    I probably read 5 different articles/ebooks on the topic of
    classes and OOP in AS3 already, and none gave me the answer I
    needed.
    But I will have a look at the site you mentioned.
    Thanks!

Maybe you are looking for

  • Urgent: Problem opening a data form that uses a smart list

    Hi, we've got a problem with a data form that is quite unusual. The data form was working perfectly until yesterday when we put a smart list in it. The smart list is composed of 8 possible choices activated by the following member formula: IF (SML==1

  • Flash Builder 4 Beta 2 ---- Data Management Problem

    Flash Builder 4 Beta 2 ---- Data Management Problem I have a simple datagrid, a PHP service and a MySQL database. When I run the code I get multiple(7) records returned to the datagrid.  Once I enable Data Management and run the exact same code, I on

  • How to put outputText inside javascript?

    Hi guys, My javascript code is like below initially:                    var field = document.getElementById(theText).innerHTML;                    if(field=='Add'){                         document.getElementById(theText).innerHTML= 'Ignore';        

  • Having problem with mackeeper coming on when searching how do I rid of this or other sites coming on under my search site

    I tried to install mackeeper and thought it was free and because of popups and ads. Now I'm having more problem it comes up when searching. I would like to go back to week ago them the way my system works now to get rid of but don't know how and no o

  • Need Help Restoring Bookmarks

    I had to hard Reboot my mac after Finder had froze on me while coping files from one external saving device to the next (and has happened to me in the past week) now all of my bookmarks on safari have reset to it's defaults with the exception of my t