I am trying to output how many students were processed, how many of the students had a February 29th birth date and the birthdate of the oldest student. I figured out how to output the first two except how to output the oldest student.

is the ckOldestBirthDate
and updateoldestBirthDate in profile class wrong?

or the argument that i made in main in line 17 is wrong?

import java.util.Scanner;
public class StudentBirthdate
{
        public static void main (String[] args)
    {
        Profile p = new Profile();
        Profile oldestStudent = new Profile(1999,12, 31);
        Profile base = new Profile (02, 29);
        p.readData();
        while (!(p.getStudentId().equals("000")))
        {
            if (p.dataValid(p))
            {
                p.totalStudents();
                if (base.ckBirthDate(p))
                    p.updateBirthDateTotal();
                if (base.ckOldestBirthDate(p,oldestStudent))
                    oldestStudent.updateOldestBirthDate(p, oldestStudent);;
                p.formatMonth();
                p.writeStudentData(p);
                p.readData();
            }
        }
        p.writeOutcomes();
    }
}



import java.util.Scanner;
public class Profile
{
    public String studentId;
    private String birthDateFormattedMon;
    private int birthDateMonth;
    private int birthDateDay;
    private int birthDateYear;
    private static int totalStudents;
    private static int birthDateTotal;
    private static int NumStudents;
    Profile()
    {NumStudents++;}
    Profile (int bdayDay, int bdayMonth, int bdayYear, String studId)
    {
        birthDateDay = bdayDay;
        birthDateMonth = bdayMonth;
        birthDateYear = bdayYear;
        studentId = studId;
        NumStudents++;
    }
    public Profile(int year, int month, int day)
    {
        birthDateYear=year;
        birthDateMonth=month;
        birthDateDay=day;
        NumStudents++;
    }
    public Profile(int month, int day)
    {
        birthDateMonth=month;
        birthDateDay=day;
        NumStudents++;
    }
    public int getBirthDateDay()
    {
        return birthDateDay;
    }
    public int getBirthDateMonth()
    {
        return birthDateMonth;
    }
    public int getBirthDateYear()
    {
        return birthDateYear;
    }
    public String getStudentId()
    {
        return studentId;
    }
    public void readData()
            {
                Scanner input = new Scanner(System.in);
                System.out.print("Enter a 3-digit student id:");
                studentId = input.next();
                        if(studentId.equals("000")) return;
                System.out.print("Enter month of birth date: ");
                birthDateMonth = input.nextInt();
                System.out.print("Enter day of birth date: ");
                birthDateDay = input.nextInt();
                System.out.print("Enter year of birth date: ");
                birthDateYear = input.nextInt();
         }
        public boolean dataValid (Profile stud)
        {
            boolean validate = true;
            if ((birthDateMonth < 01)||(birthDateMonth > 12))
            {
                validate = false;
                System.out.println("Month must be b/w the range 01-12 inclusive.");
            }
            if ((birthDateDay < 01) || (birthDateDay > 31))
            {
                validate = false;
                System.out.println("Day must be b/w the range 01-31 inclusive.");
            }
            if ((birthDateYear < 1900) || (birthDateYear > 1999))
            {
                validate = false;
                System.out.println("Year must be b/w the range 1900-1999 inclusive.");
            }
            return validate;
        }
            public void totalStudents()
            {
                totalStudents++;
            }
            public void updateBirthDateTotal()
            {
                birthDateTotal++;

            }

        public void formatMonth()
        {
            switch (birthDateMonth)
            {
                case 1: birthDateFormattedMon = "January";
                        break;
                case 2: birthDateFormattedMon = "February";
                        break;
                case 3: birthDateFormattedMon = "March";
                        break;
                case 4: birthDateFormattedMon = "April";
                        break;
                case 5: birthDateFormattedMon = "May";
                        break;
                case 6: birthDateFormattedMon = "June";
                        break;
                case 7: birthDateFormattedMon = "July";
                        break;
                case 8: birthDateFormattedMon = "August";
                        break;
                case 9: birthDateFormattedMon = "September";
                        break;
                case 10:birthDateFormattedMon = "October";
                        break;
                case 11:birthDateFormattedMon = "November";
                        break;
                case 12:birthDateFormattedMon = "Decemember";
            }
        }
        public boolean ckBirthDate (Profile stud)
        {
            return((birthDateMonth == stud.birthDateMonth) && (birthDateDay == stud.birthDateDay))?true:false;
        }
        public static void writeOutcomes()
        {
            System.out.println(totalStudents + "students were processed.");
            System.out.println(birthDateTotal + "students had a February 29th birth date.");
            System.out.println(oldestStudent + "oldest.");

        }
        public boolean ckOldestBirthDate(Profile p, Profile oldestStu)
        {
            boolean birthDate = false;
            if (p.birthDateYear < oldestStu.birthDateYear)
                birthDate = true;
            else if (p.birthDateYear == oldestStu.birthDateYear)
                if (p.birthDateMonth < oldestStu.birthDateMonth)
                    birthDate = true;
                else if (p.birthDateMonth == oldestStu.birthDateMonth)
                    if (p.birthDateDay < oldestStu.birthDateDay)
                        birthDate = true;
                    else
                        birthDate = false;
                else
                    birthDate = false;
            else
                birthDate = false;
            return birthDate;
        }
        public void updateOldestBirthDate(Profile oldestStudent, Profile p)
        {
            oldestStudent = p;
        }
        public static void writeStudentData(Profile p)
        {
            System.out.println("Student Id: " + p.studentId);
            System.out.println("Birth Date: " + p.birthDateFormattedMon + p.birthDateDay + "," + p.birthDateYear);
        }
        public static int getNumStudents()
        {
            return NumStudents;
        }
}

Recommended Answers

All 46 Replies

don't use ints for a date, use an instance of Date.

also: this
public boolean ckOldestBirthDate(Profile p, Profile oldestStu)
is bad coding. either make the method static, and call it through the class, or
make it only take one parameter. remember, since you already call it on an instance, you already have the info about one.

also, don't directly access the variables. so, you'll have your code a bit like this:

public Profile{

  private Date birthDate;

  //... rest of your code

  public Date getBirthDate(){
    return this.birthDate;
  }

  public boolean isOlderThan(Profile oldestSoFar){  
    return getBirthDate().before(oldestSoFar.getBirthDate());
  }
}

this is just a draft ill try to fix the other mistake late.
Im more focus on outputting the oldest student....
this is the only way i can think of writting it.

Main

  if (base.ckOldestBirthDate(p,oldestStudent))
                    oldestStudent.updateOldestBirthDate(p, oldestStudent);

profile class

  public boolean ckOldestBirthDate(Profile p, Profile oldestStu)
        {
            boolean birthDate = false;
            if (p.birthDateYear < oldestStu.birthDateYear)
                birthDate = true;
            else if (p.birthDateYear == oldestStu.birthDateYear)
                if (p.birthDateMonth < oldestStu.birthDateMonth)
                    birthDate = true;
                else if (p.birthDateMonth == oldestStu.birthDateMonth)
                    if (p.birthDateDay < oldestStu.birthDateDay)
                        birthDate = true;
                    else
                        birthDate = false;
                else
                    birthDate = false;
            else
                birthDate = false;
            return birthDate;
        }
        public void updateOldestBirthDate(Profile oldestStudent, Profile p)
        {
            oldestStudent = p;
        }

I already explained why you should not have your method like that.
the method I showed you: about three lines, is all you need.

im sorry i am really new to java

but our professor said that we need to prompt 3 integers in the order month, day, and year.

im supposed to output the birth date of the oldest student that were processed.

we have also not learn the syntax .before

ok. but then still, you only need to pass one Profile element as argument to the method.

but, just a question. when you test it, does it return the value you expect, yes or no ?

yes it return the value i expect. it output everything i need except the birth date of the oldest student.

Do you know some other way to like figure out how to output the birth date of the oldest student?

  1. have you already corrected your method, that it is either static or takes only one Profile instance as parameter?
  2. when do you expect this birthday to be printed ?

i have not and to be honest i have no idea how to cuz i'm just following the example that our professor gave us as an example. wrote it without understanding it.

i expect this birth day to be printed when the loop end where the user enter the student ID "000"

so, you expect that to be printed by code you haven't shown us yet? can't really comment on that, then.
but, again: you are calling it on an instance

for instance:

Profile p = new Profile(/* enter parameters */ );
Profile oldest = getCurrentOldestPerson(); 

so, you want to check whether or not p is older then oldest, because, if it is, oldest should be overwritten by a reference to p.

what you do is:

if ( p.chkOldestBirthdate(p, oldest)){ //

but why on earth are you passing p as a parameter ? you call the method on p, so you already have all the variables of p at your disposal

I showed you how to use them in an earlier reply, just do it so.

and, if the call returns true, just re-reference oldest:

if ( p.checkOldestBirthdate(oldest)){
  oldest = p;
}

as for the printing part, I assume that's in your main method, but since you haven't shown that ...

I just looked over your code a bit more...
this method:

public void updateOldestBirthDate(Profile oldestStudent, Profile p)
        {
            oldestStudent = p;
        }

makes absolutely no sense at all.
1. you should not keep the value for the oldestStudent for each instance seperately, that's insane. in the end, they'll all contain different values.
2. you're working with local variables here, so your "changes" are kind of lost

EDIT: sorry, missed your main method in there. actually, you print this after that moment:

public static void writeOutcomes()
        {
            System.out.println(totalStudents + "students were processed.");
            System.out.println(birthDateTotal + "students had a February 29th birth DATE.");
            System.out.println(oldestStudent + "oldest.");
        }

just look at my above explanation to see why this doesn't produce the correct output.

so lets do this. forget the

Profile oldestStudent = new Profile(1999,12, 31);

and

public boolean ckOldestBirthDate(Profile p, Profile oldestStu)
    {
        boolean birthDate = false;
        if (p.birthDateYear < oldestStu.birthDateYear)
            birthDate = true;
        else if (p.birthDateYear == oldestStu.birthDateYear)
            if (p.birthDateMonth < oldestStu.birthDateMonth)
                birthDate = true;
            else if (p.birthDateMonth == oldestStu.birthDateMonth)
                if (p.birthDateDay < oldestStu.birthDateDay)
                    birthDate = true;
                else
                    birthDate = false;
            else
                birthDate = false;
        else
            birthDate = false;
        return birthDate;
    }
        public void updateOldestBirthDate(Profile oldestStudent, Profile p)
        {
            oldestStudent = p;
        }

here is my new code

import java.util.Scanner;
public class StudentBirthdate
{
        public static void main (String[] args)
    {
        Profile p = new Profile();
        Profile base = new Profile (02, 29);
        p.readData();
        while (!(p.getStudentId().equals("000")))
        {
            if (p.dataValid(p))
            {
                p.totalStudents();
                if (base.ckBirthDate(p))
                    p.updateBirthDateTotal();
                p.formatMonth();
                p.writeStudentData(p);
                p.readData();
            }
        }
        p.writeOutcomes();
    }
}



import java.util.Scanner;
public class Profile
{
    public String studentId;
    private String birthDateFormattedMon;
    private int birthDateMonth;
    private int birthDateDay;
    private int birthDateYear;
    private static int totalStudents;
    private static int birthDateTotal;
    private static int NumStudents;
    Profile()
    {NumStudents++;}
    Profile (int bdayDay, int bdayMonth, int bdayYear, String studId)
    {
        birthDateDay = bdayDay;
        birthDateMonth = bdayMonth;
        birthDateYear = bdayYear;
        studentId = studId;
        NumStudents++;
    }
    public Profile(int year, int month, int day)
    {
        birthDateYear=year;
        birthDateMonth=month;
        birthDateDay=day;
        NumStudents++;
    }
    public Profile(int month, int day)
    {
        birthDateMonth=month;
        birthDateDay=day;
        NumStudents++;
    }
    public int getBirthDateDay()
    {
        return birthDateDay;
    }
    public int getBirthDateMonth()
    {
        return birthDateMonth;
    }
    public int getBirthDateYear()
    {
        return birthDateYear;
    }
    public String getStudentId()
    {
        return studentId;
    }
    public void readData()
            {
                Scanner input = new Scanner(System.in);
                System.out.print("Enter a 3-digit student id:");
                studentId = input.next();
                        if(studentId.equals("000")) return;
                System.out.print("Enter month of birth date: ");
                birthDateMonth = input.nextInt();
                System.out.print("Enter day of birth date: ");
                birthDateDay = input.nextInt();
                System.out.print("Enter year of birth date: ");
                birthDateYear = input.nextInt();
         }
        public boolean dataValid (Profile stud)
        {
            boolean validate = true;
            if ((birthDateMonth < 01)||(birthDateMonth > 12))
            {
                validate = false;
                System.out.println("Month must be b/w the range 01-12 inclusive.");
            }
            if ((birthDateDay < 01) || (birthDateDay > 31))
            {
                validate = false;
                System.out.println("Day must be b/w the range 01-31 inclusive.");
            }
            if ((birthDateYear < 1900) || (birthDateYear > 1999))
            {
                validate = false;
                System.out.println("Year must be b/w the range 1900-1999 inclusive.");
            }
            return validate;
        }
            public void totalStudents()
            {
                totalStudents++;
            }
            public void updateBirthDateTotal()
            {
                birthDateTotal++;

            }

        public void formatMonth()
        {
            switch (birthDateMonth)
            {
                case 1: birthDateFormattedMon = "January";
                        break;
                case 2: birthDateFormattedMon = "February";
                        break;
                case 3: birthDateFormattedMon = "March";
                        break;
                case 4: birthDateFormattedMon = "April";
                        break;
                case 5: birthDateFormattedMon = "May";
                        break;
                case 6: birthDateFormattedMon = "June";
                        break;
                case 7: birthDateFormattedMon = "July";
                        break;
                case 8: birthDateFormattedMon = "August";
                        break;
                case 9: birthDateFormattedMon = "September";
                        break;
                case 10:birthDateFormattedMon = "October";
                        break;
                case 11:birthDateFormattedMon = "November";
                        break;
                case 12:birthDateFormattedMon = "Decemember";
            }
        }
        public boolean ckBirthDate (Profile stud)
        {
            return((birthDateMonth == stud.birthDateMonth) && (birthDateDay == stud.birthDateDay))?true:false;
        }
        public static void writeOutcomes()
        {
            System.out.println(totalStudents + "students were processed.");
            System.out.println(birthDateTotal + "students had a February 29th birth date.");

        }
        public static void writeStudentData(Profile p)
        {
            System.out.println("Student Id: " + p.studentId);
            System.out.println("Birth Date: " + p.birthDateFormattedMon + p.birthDateDay + "," + p.birthDateYear);
        }
        public static int getNumStudents()
        {
            return NumStudents;
        }
}

now tell me how to check the birthdate of the oldest student in Profile p

several options:
1. keep a static instance in your class.
2. iterate over all the different profiles until you have the oldest
3. store your instances in a sorted list, and make sure the sorting is done on age.

i meant to say output the birthdate of the oldest student in Profile p

if it is not to much to you stultuske, write me the static instance that output the birthdate of the oldest. i've been working with this oldest birthdate thing for 5 straight hour and im about to give up. or atleast show me how to start it first step by step

public class Profile{

  private static Profile oldestStudent;

}

isnt it support to be

public class Profile{
  private static int oldestStudent;
}

this would be impossible.
1. a Student is not an int, but an instance of Profile
2. if you keep it as an int, you can' t use it to compare birthdates.

this would require you to iterate over all the students you have, and you don't necessarily have them all in the same place.

i see

public class Profile
{
    public String studentId;
    private String birthDateFormattedMon;
    private int birthDateMonth;
    private int birthDateDay;
    private int birthDateYear;
    private static int totalStudents;
    private static int birthDateTotal;
    private static int NumStudents;
    private static Profile oldestStudent;

public Profile oldestStudent()
{ 
    return oldestStudent;

}   

correct me if im wrong please

public static Profile getOldestStudent(){
  return oldestStudent;
}

//
Profile theOldestSoFarIs = Profile.getOldestStudent();

where did you get the Profile theOldestSoFarIs?

that's the easy part.
each time you instantiate a Profile, or when you set the day, month or year of birth, you check the birthdate of the current instance with that of oldestSoFar.

if oldestSoFar -> null : replace it by the current (new) instance
otherwise:
if current birthdate before birthdate of oldestSoFar : replace it by the current (new) instance

I see what you mean
But how would you convert that into a method and put it into main as a code cuz I have no idea how to

and why should that be 'converted into a method' ? don't really know what you mean, by that.

Same here huhuhu
Please right the whole code then maybe I will be able to get where you getting this and that please please

"right the whole code" ?
we're not coders on demand. I told you (repeatedly) how to do what you need to do, taking into account I don't know enough of your analysis to actually work on it.

I don't know the details of what you need, so even if I wanted to do the work for you, I couldn't, not while being sure what I do is correct

what i need is to output the oldest birthDate that was entered in in Profile p

import java.util.Scanner;
public class StudentBirthdate
{
        public static void main (String[] args)
    {
        Profile p = new Profile();

for example:

The users enter their birthmonth, birthday, birthyear in class Profile object p(Profile p= new Profile();

february 29, 19991
march 22, 1992
june 12, 1993
february 13, 1990

how would i output the oldest birth date?

Output:

february 13, 1990

:P

Anyway, for what reason would you return Profile object from oldestStudent()? Is it in your requirement???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.