Results 1 to 2 of 2
  1. #1

    Default user defined class in java [help]


    guys pa help unta ko. . medyo na confused lng ko ani

    create a program for a class named MyDate that contains data members and a contructor that meet the criteria in the following lists. This class is used to initialize instance variables
    - the non-static integer data members named month,day,year should be private members.
    - the non-static boolean data member named good should be a public member.
    - the constructor MyDate() should be assign to the member varialbles the value 1 to month, 1 to day, and 2006 to year and the value true to good. .

    unsa iya code ani kng e compile na?

    medyo confuse sad ko anang constructor . . mka explain mo?? tnx daan ^_^

  2. #2
    Elite Member
    Join Date
    Jun 2010
    Gender
    Male
    Posts
    1,018
    This code is good for newer Java where it has auto cast.

    Constructors are code to be executed when instantiated by "new". Its syntax is
    public/private ClassName

    public constructors are accessible by users of the class, while private constructors can only be used within the class.

    So wala kaayo ko kasabot unsa gyud ang imong kahinanglan...but this code will compile and work.

    Please confirm that you don't need setter/getter methods for your private variables.

    Code:
    public class MyDate{
         private Integer month;
         private Integer day;
         private Integer year;
         public Boolean good;
         public MyDate(){
              month = 1; // can also be new  month = new Integer(1);
              day = 1;
              year = 2006;
              good = true; // can also be good = Boolean.TRUE;
         }
    
    }
    To further explain... you can add another constructor...

    Code:
           public MyDate(int month, int day, int year){
                 this.month = month;
                 this.day = day;
                 this.year = year;
                 good = true;
           }
    Calling this constructor allows you to initialize the class with user inputted values.

  3.    Advertisement

Similar Threads

 
  1. loop in java. help
    By jairoh_ in forum Programming
    Replies: 15
    Last Post: 02-14-2012, 08:21 PM
  2. Need help in JAVA plsss.. help 25problems..
    By FrozenBoi in forum Programming
    Replies: 89
    Last Post: 02-09-2010, 02:26 PM
  3. NEED HELP in JAVA EXCELAPI
    By rastaman81 in forum Programming
    Replies: 1
    Last Post: 05-11-2009, 09:17 PM
  4. need help in java programming: creating a folder
    By rastaman81 in forum Programming
    Replies: 4
    Last Post: 03-11-2009, 08:51 AM
  5. Replies: 5
    Last Post: 05-28-2005, 07:50 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
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top