Results 1 to 4 of 4
  1. #1
    Join Date
    May 2014
    Posts
    1
    Rep Power
    0

    Default object programing

    /**
    * NAME:
    * DATE:
    * FILE:
    * COMMENTS:
    */

    public class Pet Class_main
    {
    public static void main(String[] args)
    {
    // declare variables
    ?? Dog = ??;

    Dog = new Pet();
    Pet.setName("");
    Pet.setType("Dog");
    Pet.setAge(3);
    printLine("His name is " + Pet.getName());
    printLine("He is a " + Pet.getType());
    printLine("He is " + Pet.getAge() + " years old");
    } // close main
    } // close Pet Class

    I using raptor in object mode with UML and I keep getting a error "Can't call dispatching method setName without object" if you need I can upload the .rap file any help would be great thanks

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: object programing

    Maybe I'm misreading what you're doing with the class names there, but if you intend to name your class "Pet Class_main", you aren't supposed to have spaces in your class name.


    As for your error:

    You're calling setName() on Pet for some reason. You have no instance called Pet. You named your instance of Pet "Dog."

    Therefore, you need to be doing
    Java Code:
    Dog = new Pet();
    Dog.setName("Woof");
    Dog.doThis(_);
    Dog.doThat("");

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: object programing

    I don't know what 'raptor' is and when I look at that code though my Java classes, it doesn't make much sense ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: object programing

    "Raptor" - are you referring to ** RAPTOR - Flowchart Interpreter? If not, please clarify what you're referring to. In fact always provide such clarification in your posts, and not assume that others would know the application that you use.

    Also, please wrap your code with [code] tags so that it is easier to read, e.g.,
    [code=java]
    // your code here
    [/code]

    It preserves formatting and provides syntax highlighting, e.g.,
    Java Code:
    // your code here
    Quote Originally Posted by Bradley.lucas1047 View Post
    public class Pet Class_main
    {
    public static void main(String[] args)
    {
    // declare variables
    ?? Dog = ??;

    Dog = new Pet();
    Pet.setName("");
    ...
    } // close main
    } // close Pet Class
    Alex is right that you cannot have spaces in your class name, and therefore "public class Pet Class_main" is wrong. See Declaring Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects) to learn the right way to declare a class. Note that by convention class names start with an uppercase letter.

    Quote Originally Posted by Bradley.lucas1047 View Post
    I using raptor in object mode with UML and I keep getting a error "Can't call dispatching method setName without object" if you need I can upload the .rap file any help would be great thanks
    The error message states that you need to first have an object before you can call the setName method.

    When you instantiate a class using the new operator, you create an object of that class, and you assign the object to a reference variable in order to be able to use the object via the variable. See Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects) for details. By convention, both primitive and reference variable names start with a lowercase letter so that you do not confuse them with class names. Therefore instead of
    Java Code:
    // declare variables
    ?? Dog = ??;
    
    Dog = new Pet();
    Pet.setName("");
    ...
    you should write
    Java Code:
    // declare variables
    Pet dog = new Pet();
    
    dog.setName("");
    ...
    where "Pet" is the class name, and "dog" is the reference variable name. "new Pet()" creates a Pet object, and the object is assigned to the "dog" variable using the "=" sign.

    Btw, "printLine("His name is " + Pet.getName());" will not work unless you have written a static method called "printLine". The usual way to print some text to the console is to use "System.out.println("your_text_here");". Remember also that you'll need to fix "Pet.getName()" to use the reference variable name.

Similar Threads

  1. Help, new to programing
    By P@Java in forum New To Java
    Replies: 4
    Last Post: 02-01-2014, 06:12 PM
  2. Help with basic programing
    By gem132 in forum New To Java
    Replies: 2
    Last Post: 03-18-2012, 01:17 PM
  3. Regarding OS for Java Programing
    By Sanjeeva in forum Advanced Java
    Replies: 3
    Last Post: 01-19-2011, 06:57 PM
  4. Hi, I am new to programing!
    By Zrob in forum Introductions
    Replies: 1
    Last Post: 09-14-2008, 05:38 AM

Posting Permissions

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