Results 1 to 7 of 7
Thread: Java Constructor Question
- 03-08-2010, 03:29 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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.
- 03-08-2010, 07:19 AM #2
Member
- Join Date
- Jul 2009
- Posts
- 18
- Rep Power
- 0
Looks good to me.
- 03-08-2010, 07:27 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
- 03-08-2010, 07:40 AM #4
Member
- Join Date
- Jul 2009
- Posts
- 18
- Rep Power
- 0
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.
- 03-08-2010, 08:04 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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?
Thanks.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 !"); } } }
- 03-08-2010, 02:45 PM #6
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.
- 03-09-2010, 02:10 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
question about java rmi
By hakimade in forum Advanced JavaReplies: 1Last Post: 07-01-2009, 07:15 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Help with constructor in java
By mathias in forum New To JavaReplies: 1Last Post: 08-07-2007, 01:00 AM -
Java constructor method
By carl in forum New To JavaReplies: 2Last Post: 07-31-2007, 02:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks