Results 1 to 20 of 20
  1. #1
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default Class and constructors question.

    I have a program to do that i make my own class, with constructors and methods.

    im going to call it Lab4. when i do this part:

    public class Lab4

    do i have to name my default and overload constructor lab4?

    or can i do

    public Time() //constructor with default values
    {
    hrs = 0;
    min = 0;
    sec = 0;
    }
    public Time(int h, int m, int s) //overloaded constructor
    {
    hrs=h;
    min=m;
    sec=s;
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    The Java Language Specification demands that the constructor(s) has/have exactly the same name as the class (upper and lowercase matter)

    kind regards,

    Jos

  3. #3
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    this program allows a user to enter a date and it will add 1 day to it and display it. but im confused on how i should declare and initialize days, month, and year. which of these should be arrays. should i make month an array and fill it with strings of the month, or should i fill it with the number of days it has in the month(appropriate index)

    1. constructor - called to declare/initialize date with default & initial values
    2. setdate - called to set a date to specific values
    3. inputtdate - called to input a date from a user
    4. advancedate - add 1 to the date to find the date 1 day later
    5. outputdate - called to output a date or toString to return output string

  4. #4
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    bumpsssssssss

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

    Default

    please halp

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Meta View Post
    please halp
    Your initial question has been answered by Jos and while you've posted your assignment requirement, you've yet to ask another specific question. Much luck.

  7. #7
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    i asked about the initialization

  8. #8
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    names of ALL constructors must exactly match the name of the class. so in your case,

    public Lab4() {... }
    public Lab4(int arg1, int arg2) { ... }

  9. #9
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    im asking specifically how should i declare and initialize my days, month, and year variables.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Meta View Post
    im asking specifically how should i declare and initialize my days, month, and year variables.
    You've got an example of how it's done with the second Time sample constructor above:

    Java Code:
    public Time(int h, int m, int s) //overloaded constructor
    {
       hrs=h;
       min=m;
       sec=s;
    }

    Likely your constructor will be structured very similar to this, although of course its name and the parameters will be different.

    Let's see your attempt at solving this first and then we can work from there. Also, you may want to have a look at my links in my signature below, especially the one on how to ask questions as it may help you ask a question that can get answered right away without our having to request multiple clarifications from you. The information in the link helped me greatly when I was starting out here and will likely help you too. Much luck!

  11. #11
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    well. if im going to have variables for day, month, and year. 1) i dont know which ones i should initialize with certain values. Like i think i should have an array for month and fill it with the max days of the month in the correct index. Like array[] = {0, 31, 28, 31, 30, ...}. but im not sure about that, nor what i should do for day and year.

    2) idk if i should initialize these values under the class, or if i should do it in the default constructor, or the overloaded constructor.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Meta View Post
    well. if im going to have variables for day, month, and year. 1) i dont know which ones i should initialize with certain values. Like i think i should have an array for month and fill it with the max days of the month in the correct index. Like array[] = {0, 31, 28, 31, 30, ...}. but im not sure about that, nor what i should do for day and year.
    You may be over-thinking this. How about just keeping it very simple and using int variables to hold the month, day, and year. Y


    2) idk if i should initialize these values under the class, or if i should do it in the default constructor, or the overloaded constructor.
    You can do both: initialize in the class with a default value, and then update these values in the constructor. Your choice.

  13. #13
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    ok. im starting my program. these are what i need:

    1. constructor - called to declare/initialize date with default & initial values
    2. setdate - called to set a date to specific values
    3. inputtdate - called to input a date from a user
    4. advancedate - add 1 to the date to find the date 1 day later
    5. outputdate - called to output a date or toString to return output string

    im confused on the difference between setdate and inputdate. they seem the same the way im comprehending it. this is what i have so far. i think i only have the constructors. do u see any immediate mistakes?

    import java.text.*; // needed for formatting
    import java.util.Scanner; // needed for input

    Java Code:
    public class Lab4Days
    {
    	// class variable declarations
    	private int day;
    	private int month;
    	private int year;
    	
    	public Lab4Days() //constructor with default values
    	{
    		day = 0;
    		month = 0;
    		year = 0;
    	}
    	
    	public Lab4Days(int d, int m, int y) // overloaded constructor
    	{
    		day = d;
    		month = m;
    		year = y;
    	}
    	
    }

  14. #14
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    setDate() modifies the values of the date, while inputDate() asks for the values.
    Java Code:
    void setDate(int d, int m, int y) {
      day = d;
      month = m;
      year = y;
    }
    
    void inputDate() {
      Scanner sc = new Scanner(System.in);
      int d = sc.nextInt();
      int m = sc.nextInt();
      int y = sc.nextInt();
      setDate(d, m ,y);
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

  15. #15
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    but that looks exactly like my constructor i already made. should i just rename my second constructor to that one called setDate?

  16. #16
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    What I posted were methods, not constructors.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  17. #17
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    but it does exactly what my overloaded constructor does. do i keep both of them?

  18. #18
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    How about using the method setDate() in the constructor?
    Java Code:
    public Lab4Days(int d, int m, int y) {
      setDate(d,m,y);
    }
    I really do think you should review the basics of classes, methods, overloading etc.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  19. #19
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    this is what im basing it off of:

    Java Code:
    public class Time
    {
    	//class variable declaration section
    	private int hrs;
    	private int min;
    	private int sec;
    
    	//class implementation section
    	public Time()  //constructor with default values
    	{
    		hrs = 0;
    		min = 0;
    		sec = 0;
    	}
    	public Time(int h, int m, int s)	//overloaded constructor
    	{
    		hrs=h;
    		min=m;
    		sec=s;
    	}
    
    	public void showmil() //
    	{
    		DecimalFormat df = new DecimalFormat("00"); //force 2 digits (leading 0's)
        		System.out.println("The military time is: " + df.format(hrs)
                           + ':' + df.format(min) + ':' 
                           + df.format(sec)); 
    	}
    
    	public void showstan()
    	{
    		DecimalFormat df = new DecimalFormat("00"); //force 2 digits (leading 0's)
    		if(hrs>12)
    			System.out.println("The standard time is: " + df.format(hrs-12) + ":" + df.format(min) + ":" 
    			+ df.format(sec) + " pm");
    		else
    			System.out.println("The standard time is: " + df.format(hrs) + ":" + df.format(min) + ":" 
    			+ df.format(sec) + " pm");
    	}
    
    
    	public void advance()
    	{
    		if(sec<59)
    			sec++;
    		else{
    			sec=0;
    			if(min<59)
    				min++;
    			else{
    				min=0;
    				if(hrs<24)
    					hrs++;
    				else
    					hrs=0;}
    		}
    		if(hrs==24&&sec==1)
    			hrs=0;
    	}
    
    	public void inTime()
    	{
    		Scanner scan = new Scanner (System.in);
    		System.out.print("Enter time as hours minutes seconds: ");
    		hrs = scan.nextInt();
    		min = scan.nextInt();
    		sec = scan.nextInt();		
    	
    	}
    
    	public static void main (String[] args)
    	{
    		Time a = new Time();		//call constructor with default values (0.0.0)
    		Time b = new Time(11,59,59);	//call constructor with argument list
    
    		int ans;
    		Scanner scan = new Scanner (System.in);
    		System.out.println("Testing constructor with default values (0,0,0): ");
    		a.showmil();	//call function for to show military time for object a
    		System.out.println("Testing the constructor with initialized values of (11,59,59): ");
    		b.showstan();	//call functions for object b
    //		System.out.println("Testing setTime with (10,40,0)");
    //		b.setTime(10,40,0);
    //		b.showstan();
    	
    		do
    		{
    			a.inTime();
    			a.showstan();
    			a.advance();
    			System.out.println("**1 second later**");
    			a.showmil();
    			a.showstan();
    			System.out.println("more? (1=yes, 2=no)");
    			ans = scan.nextInt();
    		}
    		while(ans==1);
    	}
    }

  20. #20
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    ignore this thread. its gotten to messy. im going to make a new one, with a diffeent question

Similar Threads

  1. Define class and constructors
    By HaDesT in forum New To Java
    Replies: 2
    Last Post: 03-04-2010, 05:02 PM
  2. Replies: 1
    Last Post: 05-19-2009, 11:03 PM
  3. java inner class question
    By neo_in_matrix in forum New To Java
    Replies: 9
    Last Post: 02-15-2009, 05:08 PM
  4. I have some question about jar and class??
    By low224 in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 04:02 AM
  5. question regarding class
    By kavithas in forum New To Java
    Replies: 4
    Last Post: 11-16-2007, 09:12 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
  •