classes, and constructors and instance variables . . . oh my!
wow, I am really struggling with the stuff we are doing in class for the last few days. I am going to post our assignment and point out specific things I don't understand. I don't understand some of the syntax and the overall logical flow of how parameters get passed back and forth, etc. I have been over our book and class examples and I am still confused.
Here is one part of the assignment:
"Create two instances of the MyDate class named begDate and endDate, by using the MyDate constructor after having prompted the user
3 times for each date for values of a valid month, day, and year."
Q: how do I create two instances using the constructor?
(from our assignment) "Within the MyDate class:
Include three private int variables, named month, day, and year.
Include a constructor that sets (sets them to what?!) the values of month, day, and year."
here is what I have:
publicclass MyDate{
privateint month, day, year;
public MyDate (int month,int day,int year){
month = 0;
day = 0;
year = 0;
}//MyDate Constructor
(from our assignment) "Create two instances of the MyDate class named begDate and endDate, by using the MyDate constructor after having prompted the user 3 times for each date for values of a valid month, day, and year."
and here is what I have in the executable class (it isn't much because I already have an error; it says I can't convert String to MyDate):
publicstaticvoid main(String[] args){
MyDate begDate = JOptionPane.showInputDialog("beg. month?");
}
I guess I don't know how to use the constructor correctly. Sorry, but I have tried to make this as clear as possible.
[2301 byte] By [
kalugaa] at [2007-11-26 22:21:15]

MyDate begDate = new MyDate() ;MyDate endDate = new MyDate() ;Two separate objects.PS.
The same way as you have created any object so far.
MyDate begDate = new MyDate(.....);
MyDate endDate = new MyDate(.....);
Just replace the dots with the approriate values.
(gazes into crystal ball and sees kaluga ask "What are the appropriate values?")
public class MyDate{
private int month, day, year ;
public MyDate(int m, int d, int y){
super() ;
month = m ;
day = d ;
year = y ;
}
// ... Rest of the code for MyDate
}
Message was edited by:
puckstopper31
OK, this should get you started without giving it all away. Make sure you put;
import javax.swing
at the top of your class so the JOptionPanes will work.
int month;
int day;
int year;
month = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Month"));
day = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Day"));
year = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Year"));
MyDate begDate = new MyDate(month, day, year);
month = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Month"));
day = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Day"));
year = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Year"));
MyDate endDate = new MyDate(month, day, year);
This is a very linear way to do this. The user gets three sequential dialog boxes asking them to enter the month, then day, and then year. Once those three are captured, a new myDate object can be instantiated using your paramatorized constructor. As long as you keep giveing the new objects new names, you can create as many myDate type objects as you want. So with that knowledge, we query the user again for a month, day, year combo and create our second myDate object.
Now, what this is missing; There is no error checking. The user could enter anything, including letters and punctuation and this code will try to use it. If it gets something other than a number as input, it will error.
Does this get you on your way?
Bernie
I get the feeling from your questions that you don't really grasp the concept of CLASS and OBJECT
"how do I create two instances using the constructor?"
A class is a blueprint to create an instance of an OBJECT. This OBJECT's structure will follow the CLASS' specification. Specifically, when the OBJECT is created, it will follow the instruction specified in the CLASS' constructor method.
e.g.
public class ClassA {
// by this specification, an Object created following ClassA blueprint will have a private instance
// variable of type int called value
private int value;
ClassA() {
// this is a constructor method that takes no argument
// and it sets the constructor object's value field to 0
value = 0;
}
}
So, when an object is instantiated, you construct it using the Class' constructor method:
ClassA objectA = new ClassA();
// objectA will have private field of type int with stored value of 0 as a result
I will give you a start:
public static void main(String[] args) {
MyDate begDate new MyDate(JOptionPane.showInputDialog("beg. month?"), JOptionPane.showInputDialog("beg. day?"), JOptionPane.showInputDialog("beg. year?"));
}
[code]
public class MyDate {
private int month, day, year;
public MyDate (int month, int day, int year){
this.month = month;
this.day = day;
this.year = year;
}//MyDate Constructor
}
/code]
Haven't tested it, maybe made some type mismatches, but at leasy try this one
OK, this should get you started without giving it all away. Make sure you put;
import javax.swing
at the top of your class so the JOptionPanes will work.
int month;
int day;
int year;
month = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Month"));
day = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Day"));
year = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Year"));
MyDate begDate = new MyDate(month, day, year);
month = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Month"));
day = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Day"));
year = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Year"));
MyDate endDate = new MyDate(month, day, year);
This is a very linear way to do this. The user gets three sequential dialog boxes asking them to enter the month, then day, and then year. Once those three are captured, a new myDate object can be instantiated using your paramatorized constructor. As long as you keep giveing the new objects new names, you can create as many myDate type objects as you want. So with that knowledge, we query the user again for a month, day, year combo and create our second myDate object.
Now, what this is missing; There is no error checking. The user could enter anything, including letters and punctuation and this code will try to use it. If it gets something other than a number as input, it will error.
Does this get you on your way?
Bernie
I get the feeling from your questions that you don't really grasp the concept of CLASS and OBJECT
"how do I create two instances using the constructor?"
A class is a blueprint to create an instance of an OBJECT. This OBJECT's structure will follow the CLASS' specification. Specifically, when the OBJECT is created, it will follow the instruction specified in the CLASS' constructor method.
e.g.
public class ClassA {
// by this specification, an Object created following ClassA blueprint will have a private instance
// variable of type int called value
private int value;
ClassA() {
// this is a constructor method that takes no argument
// and it sets the constructor object's value field to 0
value = 0;
}
}
So, when an object is instantiated, you construct it using the Class' constructor method:
ClassA objectA = new ClassA();
// objectA will have private field of type int with stored value of 0 as a result
Consequently, if ClassA also includes the following constructor method:
public class ClassA {
private int value;
ClassA() {
// this is a constructor method that takes no argument
// and it sets the constructed object's value field to 0
value = 0;
}
ClassA(int val) {
// this is a constructor method that takes one int argument
// and it sets the constructed object's value field to whatever
// the client code inputs into the argument
value = val;
}
}
So, when an object is instantiated, you construct it using the Class' constructor method:
ClassA objectA = new ClassA(5);
// objectA will have private field of type int with stored value of 5 as a result
Message was edited by:
Clem1986
ooo, thank you all, you have been very helpful; the explanation of class and object was good, too. I really liked the linear fashion that Bernie pointed out; I'll work on this and get back to ya'll
so, how would this next part work?
"Create an instance of the MyDate class named curDate, by using the MyDate constructor to set curDate抯 values to those entered for
begDate."
how can I pass begDate's values? is this right?
MyDate curDate = new MyDate(begDate.getMonth(), begDate.getDay(), begDate.getYear());
using these get methods?
return month;
}
public int getDay(){
return day;
}
public int getYear(){
return year;
}
also, what does this even mean? (either its poorly worded, or I just have no clue . . . probably the latter)
" (in the MyDate class) Include a method, named equals, which receives a MyDate variable as input and returns true if the input variable is equal to the
MyDate object and false otherwise. For example, if endDate and curDate are MyDate objects, then endDate.equals(curDate) will return
true if the date represented by endDate is equal to the date represented by curDate, and it will return false otherwise."
all I have so far is this, then I get stuck:
public boolean equals(MyDate k){
boolean test = false;
if (k == MyDate()){
}
return test;
}//equals
Message was edited by:
kaluga
"how can I pass begDate's values? is this right?MyDate curDate = new MyDate(begDate.getMonth(), begDate.getDay(), begDate.getYear());"You're getting it. That is correct.
"also, what does this even mean?"
It means that you have to implement an equal(MyDate) method that will return true if the specified MyDate object that is fed into the method is LOGICALLY equal to the calling MyDate object.
That is, suppose,
MyDate christmas = new MyDate(12, 25, 2007);
MyDate testA = new MyDate(12, 25, 2007);
MyDate testB = new MyDate(12, 25, 1997);
// suppose we have these three MyDate OBJECTS
// referenced by the above three variables, then
boolean a = christmas.equal(testA);// a would hold the value TRUE
boolean b = christmas.equal(testB);// b would hold the value FALSE
*NOTE*: christmas is the calling OBJECT, testA and testB are tested against christmas.
Now it's up to you to determine the logic behind determining if two dates are LOGICALLY equal. This should be easy enough.
Message was edited by:
Clem1986
okay, I think I got that last post figured out . . .
next thing: why isn't this get method working?
public class MyDate {
private int month, day, year;
private int countDay = 0;
void addDay(){
day++;
int monthDays = 31;
if (month == 4 || month == 6 || month == 9 || month == 11)
monthDays = 30;
if (month == 2){
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
monthDays = 29;
else
monthDays = 28;
}
if(day > monthDays){
day = 1;
month = month + 1;
}
if(month > 12){
month = 1;
year = year + 1;
}
countDay++;
}//addDay
public int getCountDay(){
return countDay;
}
public class C7E5 {
public static void main(String[] args) {
while (endDate.equals(curDate)){
curDate.addDay();
}
JOptionPane.showMessageDialog(null, "There are " + [b]countDay[/b].getCountDay() + " day(s) between " + begDate + " and " + endDate);
there are red error lines of death under countDay (bolded to show) in the JPane paramaters
in that context, countDay is seen by the compiler as a random variable it has no knowledge of what type it is. You hadn't declared it as a type, nor had you initialised it. Think what that variable is for a second and it'll come to you.