Results 1 to 7 of 7
  1. #1
    ashly is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default Java Constructor Question

    Hi,

    I am new to java. I need to define a class called Day with a default constructor.

    Below are my code, please let me know whether is correct or not.

    Java Code:
    public class Day
    {
    	private int dayNumber; 
    
    	public Day()
    	{
    		dayNumber = 1;
    	}//End constructor		
    }//End class Day

    Thanks.

  2. #2
    nolsen01 is offline Member
    Join Date
    Jul 2009
    Posts
    18
    Rep Power
    0

    Default

    Looks good to me.

  3. #3
    ashly is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by nolsen01 View Post
    Looks good to me.
    Does it really fulfill as a deault constructor?

  4. #4
    nolsen01 is offline Member
    Join Date
    Jul 2009
    Posts
    18
    Rep Power
    0

    Default

    A default constructor is a constructor that takes no arguments. Your constructor takes no arguments, so it is a default constructor.

    More technically, a default constructor is automatically created by the compiler when you do not specify any constructor, but I don't think that applies here.

  5. #5
    ashly is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    if I having a question on "an input function that reads the day as an integer", does that means get input from user?

    Below code suite the above requirement?

    Java Code:
    import java.io.*;
    
    public class IntDayDemo
    {
    	public static void main(String[] args)
    	{
    		int dayNum;
    
    		System.out.print("Please Enter Day in Integer: ");
    		try
    		{
                		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    	            	String input = reader.readLine();
                
                		dayNum = Integer.parseInt(input);
    
    			if ((dayNum>= 1) && (dayNum <=31))
    			{
    	        		System.out.println("Day entered: " + dyNum);
    			}
    			else
    			{
    				System.out.println("Invalid Day Entered !");
    			}
    		}
    
            	catch (IOException e) 
    		{
            		System.out.println("I did not get your day.");
            	}
    		
    		catch(NumberFormatException nFE) 
    		{ 
    			System.out.println("Day Entered is not an Integer !"); 
    		}
    	}
    }
    Thanks.

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    hi ashly

    your code above will not compile, because the variable dyNum is not declared!

    Java Code:
    System.out.println("Day entered: " + dyNum);

    but assuming that you mean the variable dayNum then your code will prompt for an integer input. so i would answer your question "does that means get input from user?" with yes.

  7. #7
    ashly is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    hi ashly

    your code above will not compile, because the variable dyNum is not declared!

    Java Code:
    System.out.println("Day entered: " + dyNum);

    but assuming that you mean the variable dayNum then your code will prompt for an integer input. so i would answer your question "does that means get input from user?" with yes.

    Sorry, typo error. Should be "dayNum".

    Is the above code meets the requirement "an input function that reads the day as an integer"?

Similar Threads

  1. question about java rmi
    By hakimade in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2009, 07:15 AM
  2. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  4. Help with constructor in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 01:00 AM
  5. Java constructor method
    By carl in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 02:15 PM

Posting Permissions

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