CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2010
    Posts
    161

    Classes implementation Problem

    Hello everyone, I really hope someone can help me on this as I am trying to learn java and I know classes are really important but I am not being able to understand how it works.

    Basically I made two classes,one which does the calculations,sets data and gets data and I called it carDealership,the other class is called carInterface and,as the name suggests,is the interface from where data is sent to be set by the carDealership class.

    This is the carDealership class
    public class carDealership {

    /**
    * @param args
    */
    //Initializing public variables
    String regNumber,make;
    int year;
    double value;

    public carDealership(String setRegNum,String setMake,int setYear,
    double setValue)
    {

    this.regNumber=setRegNum;
    this.make=setMake;
    this.year=setYear;
    this.value=setValue;


    }
    public void printInfo()
    {
    System.out.println(regNumber + "-" + make + "-" +
    year + "-" + value );

    }

    public String getRegNo()
    {

    return regNumber;
    }
    public String getMake()
    {
    return make;
    }

    public int getYear()
    {
    return year;

    }
    public double getValue()
    {
    return value;
    }

    public void setValue(double setValue)
    {
    this.value=setValue;
    }


    }


    after making this class I did not have any error but now I needed something to interact with this class and I used carInterface
    --------------------------------------------------
    import java.io.*;
    public class carInterface extends carDealership {

    /**
    * @param args
    */
    String rNames="saghar",rNumb="08455";
    int yr=2010;
    double price=20000;

    public static void main(String[] args) {
    // TODO Auto-generated method stub





    }

    public carInterface(String rName,String rNum,int year,double price)
    {

    super(rName,rNum,year,price);

    }

    }

    I know this class might be a bit confusing but it's imcomplete as I don't know how to go further.
    What I am trying to do,for testing the class,is set some variables to be sent to the carDealership class in order to set them and then retrive them through other methods and print them out to see if they have been stored properly.
    Once i know the two classes are interacting with each other I can then use user input code to let the user set the data and decide what data he/she wants to retrive.

    I hope someone can help me on how to set these classes.

    Thank you

  2. #2
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Classes implementation Problem

    If I understand correctly, the carInterface (You should call it CarInterface with capital letter), is only a test class for the carDealerShip.
    For this simple test, you don't even need to create an instance of carInterface. Just create an instance of carDealerShip, with new operator and play with it.

    BTW, calling super at your code was irrelevant because that class wasn't extending anything. By default it extended Object class, so a compilation error should occur because there isn't a constructor of Object class that receives those parameters.

    I think you should take a simple Java book and try to read it. It will explain more clearly the concept of oop.

  3. #3
    Join Date
    Jan 2010
    Posts
    161

    Re: Classes implementation Problem

    Hi, thank you for the answer but can you please tell me how to do this?
    I mean what do I need to write in carInterface to send and receive data from carDealership?
    and
    why do I need to call it CarInterface(with capital C)?
    I have a book which I am reading but I don't quiete understand this part of sending and receiving data between two classes and wanted to have some clarification.
    Last edited by cpu2007; October 25th, 2010 at 10:25 AM.

  4. #4
    Join Date
    Jan 2010
    Posts
    161

    Re: Classes implementation Problem

    Ok thank you very much for the help, I tried to do something like this before, as you suggested,create an instance of carDealership but didn't know the syntax properly or I was setting up something wrong.
    Now I did it and I can interact with my carDealership class through carInterface
    here is what I did, if you think there is a better way to do the same thing please let me know.
    import java.io.*;
    import java.util.*;
    public class carInterface {

    /**
    * @param args
    */



    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String rNames="saghar",rNumb="08455";
    int yr=2010;
    double price=20000;
    Scanner sc= new Scanner(System.in);

    carDealership cr= new carDealership(rNames,rNumb,yr,price);

    System.out.println("Please insert the new price: ");

    cr.setValue(sc.nextInt());

    cr.printInfo();


    }


    }

    thank you

  5. #5
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Classes implementation Problem

    why do I need to call it CarInterface(with capital C)?
    There is a convention in Java that Classes' names start with capital letters. See this link.

    I suggest that for start, don't get input from console, but hard-code several set statements and just print them to screen. This only for sanity to check if all your methods are written correctly.
    If you are confused with your book, try another one, or google any java tutorial.
    I don't know your programming background, but maybe you should start with basic programming before working on objects and classes.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Classes implementation Problem

    As guyafe has quite rightly said get yourself a good tutorial. I always recommend Thinking in Java by Bruce Eckel to beginners which, luckily for you, there is a free on-line version of on the codeguru site. It is a little out of date now (it's missing the Java 5 enhancements etc) but is still an excellent book for beginners. Also try the Java tutorials.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Jan 2010
    Posts
    161

    Re: Classes implementation Problem

    Thank you very much for the replies.
    I actually have the knowledge of the basic of java and other programming languages, the thing was that while I was trying to implement the classes,as you can see from the first code in the first post,i was making some syntax and other mistakes on how to interact and inherit a class in another class.

    As you can see,the way to implement and interact with another class is to make another instance of that class
    carDealership cr= new carDealership(values here);
    thanks to this I can now send and get data from the other class and It was really easy to implement the user input to it ,by allowing the user to submit data and make it set by the subclass and use this base class to retrieve data from the subclass.

  8. #8
    Join Date
    Jan 2010
    Posts
    161

    Re: Classes implementation Problem

    Thank you very much for the replies.
    I actually have the knowledge of the basic of java and other programming languages, the thing was that while I was trying to implement the classes,as you can see from the first code in the first post,i was making some syntax and other mistakes on how to interact and inherit a class in another class.

    As you can see,the way to implement and interact with another class is to make another instance of that class
    carDealership cr= new carDealership(values here);
    thanks to this I can now send and get data from the other class and It was really easy to implement the user input to it ,by allowing the user to submit data and make it set by the subclass and use this base class to retrieve data from the subclass.
    the book I am using is called Java in Two semester 3rd edition and I can say is a really good book but maybe it was me who didn't quiete get syntax part.

  9. #9
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Classes implementation Problem

    Maybe it can help you to think about an object as a unique and independent entity, that from the moment it was instantiated, you can call its methods and use it anyway you want. Having several objects and calling methods of each of them, will cause each method to be executed, but it will have no influence on any other function method not belongs to that object.
    This is opposed to simple functions (Like in C language) that you just call, but all the functions are actually the same.
    This is a bit garbled but I think the meaning is understandable.

    Guy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured