Results 1 to 20 of 20
Thread: Class and constructors question.
- 04-27-2010, 07:43 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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;
}
- 04-27-2010, 07:49 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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
- 04-27-2010, 07:58 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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
- 04-27-2010, 08:46 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
bumpsssssssss
- 04-28-2010, 02:48 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
please halp
-
- 04-28-2010, 03:26 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
i asked about the initialization
- 04-28-2010, 03:36 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
names of ALL constructors must exactly match the name of the class. so in your case,
public Lab4() {... }
public Lab4(int arg1, int arg2) { ... }
- 04-28-2010, 04:39 AM #9
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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!
- 04-28-2010, 08:06 PM #11
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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.
-
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
You can do both: initialize in the class with a default value, and then update these values in the constructor. Your choice.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.
- 04-29-2010, 01:32 AM #13
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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; } }
- 04-29-2010, 01:38 AM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 04-29-2010, 01:41 AM #15
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
but that looks exactly like my constructor i already made. should i just rename my second constructor to that one called setDate?
- 04-29-2010, 02:19 AM #16
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
What I posted were methods, not constructors.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 04-29-2010, 02:24 AM #17
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
but it does exactly what my overloaded constructor does. do i keep both of them?
- 04-29-2010, 02:36 AM #18
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
How about using the method setDate() in the constructor?
I really do think you should review the basics of classes, methods, overloading etc.Java Code:public Lab4Days(int d, int m, int y) { setDate(d,m,y); }Ever seen a dog chase its tail? Now that's an infinite loop.
- 04-29-2010, 02:42 AM #19
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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); } }
- 04-29-2010, 02:58 AM #20
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
Similar Threads
-
Define class and constructors
By HaDesT in forum New To JavaReplies: 2Last Post: 03-04-2010, 05:02 PM -
[SOLVED] [newbie] getting the constructors of a class (java.lang.reflect)
By jon80 in forum New To JavaReplies: 1Last Post: 05-19-2009, 11:03 PM -
java inner class question
By neo_in_matrix in forum New To JavaReplies: 9Last Post: 02-15-2009, 05:08 PM -
I have some question about jar and class??
By low224 in forum New To JavaReplies: 2Last Post: 01-01-2009, 04:02 AM -
question regarding class
By kavithas in forum New To JavaReplies: 4Last Post: 11-16-2007, 09:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks