Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Main.java expects Person.java behavior to work. How can I solve this?

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Main.java expects Person.java behavior to work. How can I solve this?

    Good afternoon, everybody. The Main class expects the Person class to have a specific behavior, so it will only work if I implement that behavior. What should I put in the Person.java file to make Main.java to run?

    Main.java code:
    package br.xxx.unitOne.exOne;
     
    import br.xxx.unitOne.exOne.lib.Person;
     
    public class Main{
        public static void main(String args[]){
            Person myName = new Person("My Name");
     
            System.out.println(myName.helloWorld());
     
        }
    }

    I would appreciate so much if someone could help me. Thanks.
    Last edited by joelpub; September 30th, 2021 at 09:11 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    What error messages are you getting with this code?
    If there are no compiler errors, what happens when you execute the code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    Quote Originally Posted by Norm View Post
    What error messages are you getting with this code?
    If there are no compiler errors, what happens when you execute the code?
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Person cannot be resolved to a type
    	Person cannot be resolved to a type
     
    	at br.xxx.unitOne.hello_person/br.xxx.unitOne.exOne.Main.main(Main.java:7)

    This is part of a simple exercise but I'm a beginner and don't know how to analyze the code and find out what is missing. I need to write something in the Person.java class (it's empty) and I don't know what it is.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    Person cannot be resolved to a type
    The compiler is looking for a class named Person. The Person class's class file must be on the class path so that the compiler can find it.
    Do you have a Person class? Where is it located? Did you compile it so that there is a Person.class file somewhere?

    The compiler needs to find the Person.class file when the Main.java file is compiled.

    How are you compiling the java file? What command are you using?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    Quote Originally Posted by Norm View Post
    The compiler is looking for a class named Person. The Person class's class file must be on the class path so that the compiler can find it.
    Do you have a Person class? Where is it located? Did you compile it so that there is a Person.class file somewhere?

    The compiler needs to find the Person.class file when the Main.java file is compiled.

    How are you compiling the java file? What command are you using?
    I have created a Person.java file in the exact path (br.xxx.unitOne.exOne.lib.Person), the compiler finds it. But I don't know what to put inside Person.java to make Main.java work.

    For exemple, when I write the code below inside Person.java
    package br.xxx.unitOne.exOne.lib;
     
    public class Person {
     
    }

    it returns

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	The constructor Person(String) is undefined
    	The method helloWorld() is undefined for the type Person
     
    	at br.xxx.unitOne.hello_person/br.xxx.unitOne.exOne.Main.main(Main.java:7)

    I think I should define the constructor Person(String) and the method helloWorld(). I don't have a clue how to do that. I have been reading about it but it seems impossible for me to do it.

    Note: it is an exercise where I should be able to write the Person.java behavior just by looking to the Main.java code. It will make Main.java work after that. Can you give me some light? (The Main.java code is in the first message of the topic)

    Thank you so much for your patience.
    Last edited by joelpub; September 30th, 2021 at 09:16 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    I should define the constructor Person(String) and the method helloWorld()
    Yes that sounds right.
    Look at the tutorial on how to create a constructor:
    https://docs.oracle.com/javase/tutor...structors.html
    and how to create methods:
    https://docs.oracle.com/javase/tutor...O/methods.html

    For more information on how to do different things see the items in the tutorial via the links on this page:
    https://docs.oracle.com/javase/tutor...ybigindex.html
    Save that last link for future reference.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    Create a person class and add a parameterized constructor to accept the value passing from the main method at the time of creating person object.then run it.

  8. #8
    Junior Member
    Join Date
    Sep 2021
    Location
    India
    Posts
    20
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    To make the Main.java code work with the Person class, you should define the Person class in a way that it has a method named helloWorld(). Here's how you can modify the Person class to meet this requirement:

    package br.xxx.unitOne.exOne.lib;
     
    public class Person {
        private String name;
     
        public Person(String name) {
            this.name = name;
        }
     
        public String helloWorld() {
            return "Hello, my name is " + name;
        }
    }

    In this modified Person class, the constructor takes a name as a parameter, and it has a helloWorld() method that returns a greeting message with the person's name.

    With this implementation, your Main.java code should work as expected, and it will print the "Hello, my name is My Name" message when executed.

  9. #9
    Junior Member
    Join Date
    Jan 2024
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    To get Main.java to run, just make sure that Person.java does what Main.java expects it to do. If Main.java needs certain things from Person.java to work, you'll need to add those things in Person.java. Don't hesitate to reach out to them for additional support if needed. if you're still getting doubt about something, feel free to get in touch with programming assignment experts available 24/7 at https://www.programminghomeworkhelp....va-assignment/. They have experienced tutors having more than 10+ years of experience in this field.

Similar Threads

  1. Is there any intelligent person in the Java programming? plz
    By alaseier in forum What's Wrong With My Code?
    Replies: 17
    Last Post: April 4th, 2024, 06:06 AM
  2. [SOLVED] Java class Person: with friend_data.txt issue:
    By Mvxexty1001 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: May 7th, 2014, 05:41 AM
  3. Java strange thread behavior
    By Bingo90 in forum Threads
    Replies: 7
    Last Post: October 19th, 2013, 10:53 AM
  4. Replies: 1
    Last Post: September 27th, 2012, 03:55 AM