Results 1 to 6 of 6
- 12-03-2010, 08:42 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Cant get past 1st if statement in driver
I have an assignment to write a driver to test class Date, using December 6, 1944. This is what appears in the console when I run the application:
I was born on 12/6/1944.
When were you born? 12161944
12/6/1944 and 12161944 are the same.
No matter what date I enter, it displays, “12/6/1944 and [DATE I ENTER] are the same.” It looks like its not evaluating past the the 1st if statement but I'm not sure why. Thanks in advance!
Here is the driver:
public static void main(String[] args) throws IOException {
Scanner myScanner = new Scanner(System.in);
Date myBdate = new Date();
System.out.print("I was born on ");
System.out.println(myBdate + ".");
System.out.print("When were you born? ");
birthDate2 = myScanner.nextInt();
if (myBdate.compareTo(birthDate2) == 0)
{
System.out.println(myBdate + " and " + birthDate2 + " are the same.");
}
else if (myBdate.compareTo(birthDate2) > 0)
{
System.out.println(myBdate + " is older than " + birthDate2);
}
else if (myBdate.compareTo(birthDate2) < 0)
{
System.out.println(myBdate + " is newer than " + birthDate2);
Here is the class:
package tripObjects;
import java.io.*;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Date {
// field declarations for instance variables
static int day;
static int year;
static int month;
private static int birthDate2;
// Constructor method
public Date(int newDay, int newMonth, int newYear)
{
day = newDay; // constructors. // syntax is 'field declaration = parameters;'
year = newYear;
month = newMonth;
}
public Date() // default constructor method. This line is the start of an instance method
{
// constructors that initialize
day = 06; // default constructors
year = 1944;
month = 12;
}
// Methods. statements that say "return day,etc are the method bodies
public int getDay()
{
return day;
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
public String toString() // converts the date from integer to string. returns content in good format for output. new content can b directly output to print or println
{
return month + "/" + day + "/" + year;
}
public void println(String toString) // allows the content to be directly output to print or println
{}
public String monthDate(String month) // month is the obj, Date is the meth
{
StringBuffer outDate = new StringBuffer(month);
outDate.append(" ");
outDate.append(month);
return outDate.toString();
}
public String dayDate(String day) // day is the obj, Date is the meth
{
StringBuffer outDate = new StringBuffer(day);
outDate.append(" ");
outDate.append(day);
return outDate.toString();
}
public String yearDate(String year) // year is the obj, Date is the meth
{
StringBuffer outDate = new StringBuffer(year);
outDate.append(" ");
outDate.append(year);
return outDate.toString();
}
public int compareTo(int birthDate2) {
return 0;
}
- 12-03-2010, 08:56 PM #2
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Java Code:public static void main(String[] args) throws IOException { Scanner myScanner = new Scanner(System.in); Date myBdate = new Date(); System.out.print("I was born on "); System.out.println(myBdate + "."); System.out.print("When were you born? "); birthDate2 = myScanner.nextInt(); if (myBdate.compareTo(birthDate2) == 0) { System.out.println(myBdate + " and " + birthDate2 + " are the same."); } else if (myBdate.compareTo(birthDate2) > 0) { System.out.println(myBdate + " is older than " + birthDate2); } else if (myBdate.compareTo(birthDate2) < 0) { System.out.println(myBdate + " is newer than " + birthDate2);I see that you have some initial values in your Date class constructor... does this ever change?? Hint: when your user enters their birthdate date...Java Code:package tripObjects; import java.io.*; import java.util.InputMismatchException; import java.util.Scanner; public class Date { // field declarations for instance variables static int day; static int year; static int month; private static int birthDate2; // Constructor method public Date(int newDay, int newMonth, int newYear) { day = newDay; // constructors. // syntax is 'field declaration = parameters;' year = newYear; month = newMonth; } public Date() // default constructor method. This line is the start of an instance method { // constructors that initialize day = 06; // default constructors year = 1944; month = 12; } // Methods. statements that say "return day,etc are the method bodies public int getDay() { return day; } public int getMonth() { return month; } public int getYear() { return year; } public String toString() // converts the date from integer to string. returns content in good format for output. new content can b directly output to print or println { return month + "/" + day + "/" + year; } public void println(String toString) // allows the content to be directly output to print or println {} public String monthDate(String month) // month is the obj, Date is the meth { StringBuffer outDate = new StringBuffer(month); outDate.append(" "); outDate.append(month); return outDate.toString(); } public String dayDate(String day) // day is the obj, Date is the meth { StringBuffer outDate = new StringBuffer(day); outDate.append(" "); outDate.append(day); return outDate.toString(); } public String yearDate(String year) // year is the obj, Date is the meth { StringBuffer outDate = new StringBuffer(year); outDate.append(" "); outDate.append(year); return outDate.toString(); } public int compareTo(int birthDate2) { return 0; }
Hope this helps!
ChrisLast edited by tashimoto; 12-03-2010 at 09:00 PM.
:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 12-03-2010, 08:56 PM #3
Look at the method for compareTo...
it always returns 0 and nothing else.
- 12-03-2010, 09:02 PM #4
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Oh yea, my mistake... Ignore my post: except for the code tags - they make reading code much easier! :)
Sorry!
Chris:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 12-03-2010, 10:53 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Thanks! I want to be able to enter in a birthdate then compare it to 12/06/1944. Should this line return something other than 0? This is where I'm confused.
return outDate.toString();
}
public int compareTo(int birthDate2) {
return 0;
}
- 12-04-2010, 01:39 AM #6
Similar Threads
-
New To Java Ive Done Some Coding In The Past!
By Noob Koer in forum New To JavaReplies: 1Last Post: 07-08-2010, 07:46 AM -
[SOLVED] Cut,Past,copy in edit menu
By smartsubroto in forum New To JavaReplies: 9Last Post: 07-03-2008, 10:15 AM -
TextArea additable and uneditable (copy/past problem)
By qwerty55 in forum Advanced JavaReplies: 0Last Post: 01-19-2008, 11:41 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM -
Help with JDBC driver
By Daniel in forum JDBCReplies: 2Last Post: 07-03-2007, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks