code errors - need some input.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: code errors - need some input.

  1. #1
    Join Date
    Mar 2009
    Posts
    78

    code errors - need some input.

    Hi.

    The following code snippets got some errors in there, Netbeans says. Can somebody tell me what, and why, so that I understand the errors, please?

    Database.Java

    The first "private person" gives me an error. Why is that?

    Code:
    package Test;
    
    public class Database {
    
        private person[] list;
        private int numberOfRecords;
    
        public Database() {
            
            throw new RuntimeException("Compiled Code");
        }
    
        public void add(String lastName, String firstName, int month, int day, int year) {
            //compiled code
            throw new RuntimeException("Compiled Code");
        }
    
        public int find(String firstName, String lastName, int month, int day, int year) {
            
            throw new RuntimeException("Compiled Code");
        }
    
        public void printList() {
         
            throw new RuntimeException("Compiled Code");
        }
    }
    Date.Java

    gives me an error for exit(1); and return month==theOtherDate.month && day==theOtherDate.day && year==year; ??

    Code:
    ackage Test;
    
    import java.language.*;
    
    public class Date
    {
        private int day;
        private int month;
        public int year;
    
        public Date(int m, int d, int y)
        {
            setDay(d);
            setMonth(y);
            setYear(y);
        }
    
        private int getDay()
        {
            return day;
        }
        public int getMonth()
        {
            return month;
        }
        public int getYear()
        {
            return month;
        }
        public void setDay(int d)
        {
            if( d>0 && d<=30) // assume there are 30 days in a month
            {
                day =d;
            }
            else
            {
                System.out.println("Improper user input. Day must be in [1,30]");
                exit(1);
            }
        }
        public void setMonth(int m)
        {
            if( m>=1 && m<=12)
            {
                month =m;
            }
            else
            {
                System.out.println("Improper user input. Month must be in [1,12]");
                System.exit(1);
            }
        }
        public void setYear(int y)
        {
            if( y>1900 && y<2050)
            {
                year =y;
            }
            else
            {
                System.out.println("Improper user input. Year must be in [1901,2049]");
                System.exit(1);
            }
        }
    
        public int equals(Date theOtherDate)
        {
            return month==theOtherDate.month && day==theOtherDate.day && year==year;
        }
        public String toString()
        {
            return month + "/" + day + "/" + year;
        }
    }
    Person.Java

    This gives me an error for the first 3 private strings ( firstname, lastname, birthdate) Any ideas?
    Code:
    package assignment2;
    
    class Person {
    
        private string firstName;
        private string lastName;
        private Date birthDate;
    
        public Person(String fN, String lN, int month, int day, int year) {
            //compiled code
            throw new RuntimeException("Compiled Code");
        }
    
        public boolean equals(Person theOtherPerson) {
            //compiled code
            throw new RuntimeException("Compiled Code");
        }
    
        public boolean equal(String fN, String lN, int month, int day, int year) {
            //compiled code
            throw new RuntimeException("Compiled Code");
        }
    
        public String toString() {
            //compiled code
            throw new RuntimeException("Compiled Code");
        }
    }
    I appreciate your help!

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: code errors - need some input.

    Hint: take a look at the return type of the method and take a good look at what you are trying to return.

  3. #3
    Join Date
    Feb 2011
    Posts
    27

    Re: code errors - need some input.

    Your Database class is in a different package than your Person class, so you have to import your Person class. Also 'private person' should be 'private Person'.

    In the Date class, I'm not sure why it would cause a problem, but I use System.exit(0) not System.exit(1) - maybe that will solve that problem. Also as was already mentioned about the second part of that problem, your equals() method says it returns an int, but you have it returning 'month==theOtherDate.month && day==theOtherDate.day && year==year' which is a boolean.

    In your Person class, the first two strings should be 'String' not 'string'. and the Data is giving you a problem for the same reason as the Person was in your Database class. They are in different packages, so you need to either put them in the same package, or import your Data class into your Person class.

  4. #4
    Join Date
    Feb 2011
    Posts
    27

    Re: code errors - need some input.

    Sorry in the 3rd part of that reply, when I said 'Data' I meant 'Date'

  5. #5
    Join Date
    May 2009
    Location
    Surrey, UK
    Posts
    285

    Re: code errors - need some input.

    Do you know that Java is case sensitive? Person != person, that's why you get the error in NetBeans.

    Do you know Java already has two Date classes (java.util.Date and java.sql.Date)?

    Your .equals methods are not properly defined (should be public boolean and take an Object, not any other type, as argument). Read the API to learn how to do it.

    There is no java.language package (unless it is one of your own). If you meant java.lang you don't have to import it.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center