Glasgow University Bioinformatics forum

Glasgow University Bioinformatics forum, for the MRes bioinformatics students of the University of Glasgow
 
HomeHome  ­FAQFAQ  ­SearchSearch  ­MemberlistMemberlist  ­UsergroupsUsergroups  ­RegisterRegister  ­Log inLog in  
Post new topic   Reply to topicShare | 
 

 code for lab8

View previous topic View next topic Go down 
AuthorMessage
Nico



Posts: 48
Join date: 2008-09-18

PostSubject: code for lab8   Mon Oct 13, 2008 7:04 pm

Here are my three different classes, including the extension.


Code:
import FormatIO.*;

public class MyDate {

   private int year=0,month=0,day=0;
   
   //first constructor
   public MyDate(int year,int month,int day)
   {
      this.year = year;          //class variable (using this) = parameter variable
      this.month = month;
      this.day = day;
   }
   
   //second constructor: takes only one parameter which represents day-month-year
   public MyDate(int ddmmyy)
   {
      year = ddmmyy%100;
      month = (ddmmyy%10000)/100; 
      day = ddmmyy/10000;
      
      if(year>=0 && year<=20) //converts the two digits into a year
         year+= 2000;
      else year+=1900;
   }
   
   public int getDay(){return day;}
   public int getMonth(){return month;}
   public int getYear(){return year;}
   public int getYearDdmmyy()
   {
      int ddmmyy2 =0;
      ddmmyy2 = day*10000; //for instance, it the day is 18, we obtain 180000
      ddmmyy2+=month*100; //month is 04, 180000 + 400 = 180400
      ddmmyy2+=year%100; // year is 1934, remainder is 34, 180400+34 = 180434
      return ddmmyy2;
   }

   public String printDate()
   {
      return "born on "+String.format("%02d/%02d/%02d",day,month,year); //it is important to use %02d to fill the empty fieldwith with 0, for instance for January it will write 01 instead of 1
   }
   
   public int differenceInYears(MyDate now)
   {
      if(this.year>now.year)
         return this.year - now.year;
      else return now.year - this.year;
   }
   
}


Code:
import FormatIO.*;

public class Person {

   private String firstName="",lastName="";
   private int dateOfBirth=0;
   MyDate dob;
   
   public Person(Console con) //I should take into account all the exceptions for this constructor, but I am waiting to know more about exceptions and specially how to create customize ones
   {
      con.print("Enter your first name: ");
      this.firstName = con.readWord();
      con.print("\nEnter your last name: ");
      lastName = con.readWord();
      con.println("Enter your date of birth(ddmmyy), the day cannot start by 0:");      
      dateOfBirth = con.readInt();
      dob = new MyDate(dateOfBirth);
   }
   
   public String getFirstName(){return firstName;}
   public String getLastName(){return lastName;}
   public MyDate getDateOfBirth(){return dob;}
   
   public String printDateAndName() {return firstName+" "+lastName+" "+dob.printDate();}
   
   public int age(MyDate now2)
   {
      return this.dob.differenceInYears(now2);
   }
}



Code:
import FormatIO.Console;


public class Ex2 {

   public static void main(String[]args)
   {
      Console con = new Console();
      
      Person per1 = new Person(con);
      MyDate date1 = new MyDate(1988,4,22); //for testing the difference in age
      MyDate nowadays = new MyDate(2008,10,13); //for calculating the age
      
      con.println("the first name is "+per1.getFirstName());
      con.println("the last name is "+per1.getLastName());
      MyDate checkDate = per1.getDateOfBirth();
      con.println("the day of birth is: "+checkDate.getDay());
      con.println("the month of birth is: "+checkDate.getMonth());
      con.println(per1.printDateAndName());
      con.println("the difference between the two date is: "+per1.dob.differenceInYears(date1));
       con.println("your age is: "+per1.age(nowadays));
   }
}
Back to top Go down
View user profile
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Re: code for lab8   Wed Oct 15, 2008 5:18 pm

Well done Nico! This exercise is quite hard for me. I could not understand what is constructor very well.

And I have not finish my program yet. Dont know how to get the current time(I tried Calendar, but seems not work).

Could you explain how you did it? I could not fully understand your solution on that.
Back to top Go down
View user profile
Nico



Posts: 48
Join date: 2008-09-18

PostSubject: Re: code for lab8   Wed Oct 15, 2008 6:13 pm

First, a constructor is what is going to create the object:

Code:
//first constructor
  public MyDate(int year,int month,int day)
  {
      this.year = year;          //class variable (using this) = parameter variable
      this.month = month;
      this.day = day;
  }
 
  //second constructor: takes only one parameter which represents day-month-year
  public MyDate(int ddmmyy)
  {
      year = ddmmyy%100;
      month = (ddmmyy%10000)/100;
      day = ddmmyy/10000;
     
      if(year>=0 && year<=20) //converts the two digits into a year
        year+= 2000;
      else year+=1900;
  }


As you see, we do not care about who can read or not this function because it is not one!!! So we do not need to put "private" or "public", etc.

Code:
  public MyDate(int year,int month,int day)

public MyDate(int ddmmyy)



So here are two different constructors to create the object MyDate. You can do it in two different ways. Let's take an example:
if you have a class "car", this class will enable you to: 1) create a car 2)get the colour of the car 3)get the maximum speed of the car

For creating a car, you can write different constructors to enable the user to choose different parameters. for instance, he could create a car by choosing only the color:

Car ferrari = new Car("red");

or maybe he would like also to say how many people can sit in the car, so.., you will have to write a second constructor and your input will be:

Car ferrari = new Car("red",2); //if it is a two seats car



Concerning the current date: You need a special package for that, I do not think they want us to use it (regarding what they said about FormatIO). Just put it in hard in your program: MyDate datenow = new MyDate(15,10,2008) for the 15th of October 2008.


Nico

ps: I think the assessor (get function) can be static, I have to test it.
Back to top Go down
View user profile
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Re: code for lab8   Wed Oct 15, 2008 6:27 pm

Nico, thanks very much.
I now understand better about the constructor now.

here attaches my code for exercise 1 and 2. I will do the extension exercise later in the week.

Code:
public class MyDate {
   private static int day=0;
   private static int month=0;
   private static int year=0;
   
   //
   public MyDate(int d, int m, int y){
      day=d;
      month=m;
      year=y;
      
      if(year<=20){
         year=2000+year;
      }
      else{
         year=1900+year;
      }
   }
   
   public MyDate(int date){
      day=date/10000;
      month=(date%10000)/100;
      year=date%100;
      
      if(year<=20){
         year=2000+year;
      }
      else{
         year=1900+year;
      }
   }
   
   public static int getDay(){
      return day;
   }
   
   public static int getMonth(){
      return month;
   }
   
   public static int getYear(){
      return year;
   }
   
   public static String getDate(){
      int date=10000*day+100*month+year%100;
      String s=String.format("%06d",date);
      return s;
   }
   
   public static void printDate(Console con){
      con.print(String.format("%02d/%02d/%04d",day,month,year));
   }
}


Code:

public class Person {
   private static String firstName;
   private static String lastName;
   private static int dateOfBirth;
   
   public Person(String fN,String lN,int dOB){      
      firstName=fN;
      lastName=lN;
      dateOfBirth=dOB;
   }
   
   public static String getFirstName(){
      return firstName;
   }
   
   public static String getLastName(){
      return lastName;
   }
   
   public static String getDateOfBirth(){
      MyDate md=new MyDate(dateOfBirth);
      return md.getDate();
   }
   
   public static void printPerson(Console con){
      con.print(firstName+" ");
      con.print(lastName+": born on ");
      MyDate md=new MyDate(dateOfBirth);;
      md.printDate(con);
   }
}


Code:
public class Lab8 {
   public static void main(String arg[]){
      MyDate a=new MyDate(15,9,07);
      Console con=new Console();
      
      MyDate b=new MyDate(160935);
      
      a.printDate(con);
      con.println();
      con.println(a.getDay());
      con.println(a.getMonth());
      con.println(a.getYear());
      b.printDate(con);
      con.println();
   
      Person p=new Person("Wei","Jiao",260984);
      p.printPerson(con);

   }
}
Back to top Go down
View user profile
Nico



Posts: 48
Join date: 2008-09-18

PostSubject: Re: code for lab8   Wed Oct 15, 2008 9:46 pm

markj wrote:



public class MyDate {
private static int day=0;
private static int month=0;
private static int year=0;


private int year=0,month=0,day=0; //shorter is better




About the question of static, you get this message:

"The static method printDate(Console) from the type MyDate2 should be accessed in a static way" in the lab8 class, but the program is working, so I do not know if we can use static or not.
Back to top Go down
View user profile
headache_aspirin



Posts: 1
Join date: 2008-09-30

PostSubject: code 8   Fri Oct 24, 2008 8:45 pm

import FormatIO.Console;
public class Ex1 {


public static void main(String[] arg){



Console con = new Console();
MyDate date = new MyDate(01,02,03);
Person per = new Person(con);
MyDate now = new MyDate(18,10,06);
MyDate now2 = new MyDate (24,10,07);


System.out.println(date.getday());
System.out.println(date.getmonth());
System.out.println(date.getyear());
System.out.println(date.getddmmyy());
date.printDate(con);

System.out.println(per.getfName());
System.out.println(per.getlName());
System.out.println(per.getDob());

per.printPerson(con);

con.println("the difference between the two dates is: "+date.differenceInYears(now));

con.println("your age is: "+per.age(now2));


end of Ex1 class.

import FormatIO.Console;
public class MyDate {


private int day, month, year, ddmmyy, nYear;



public MyDate(int day, int month,int year){

this.day = day;
this.month = month;
this.year = year;
ddmmyy =(( day*10000) + (month*100)+ year);

}

public MyDate(int ddmmyy){

this.ddmmyy =ddmmyy;
day = ddmmyy/10000;
month = ddmmyy/100% 100;
year = ddmmyy%100;
}



public int getday(){
return day;
}
public int getmonth(){
return month;
}
public int getyear(){
return year;
}
public int getddmmyy(){
return ddmmyy;
}


public void printDate(Console con){
if ( year >0 && year < 20)
year = 2000 + year;

else year = 1900 + year;
con.println( String.format("%4d/%02d/%2d", year , month, day));
return;
}


public int differenceInYears(MyDate now){

if ( now.year >0 && now.year < 20){
now.year = 2000 + now.year;
}
else{ now.year = 1900 + now.year;
return now.year;
}

if(this.year>now.year){
nYear = this.year - now.year;
return nYear;
}
else{ nYear = now.year - this.year;
return nYear;

}
}
}
end of MyDate class

import FormatIO.Console;
public class Person {

private int dob, now2;

MyDate date1;

String fName ="";
String lName = "";

public Person(Console con){

con.println("What is your First Name?");
fName = con.readWord();
System.err.println(fName);

con.println( (fName) +" What is Your Last Name?");
lName = con.readWord();
System.err.println(lName);

con.println( (fName) +" What is Your Date of Birth?");
dob = con.readInt();
System.err.println(dob);

date1 = new MyDate(dob);
}


public String getfName(){
return fName;
}
public String getlName(){
return lName;
}
public int getDob(){
return dob;

}

public void printPerson(Console con){
con.print( (fName)+" " +(lName)+": born on " );
date1.printDate(con);
return;
}

public int age(MyDate now2)
{
return this.date1.differenceInYears(now2);
}

}

end of person class
Back to top Go down
View user profile
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Re: code for lab8   Sat Oct 25, 2008 1:04 pm

Thanks (Nico?) Razz

I didn't figure the red part out when I was in the lab.

By the way, if anyone want to know how to get current time of the system, the following code which is given by Dr Cooper might help:
Code:

   public MyDate(){
      Calendar c=Calendar.getInstance();
      day=c.get(Calendar.DAY_OF_MONTH);
      month=c.get(Calendar.MONTH)+1;
      year=c.get(Calendar.YEAR);
   }


Quote:

import FormatIO.Console;
public class Person {

private int dob, now2;

MyDate date1;

String fName ="";
String lName = "";

public Person(Console con){

con.println("What is your First Name?");
fName = con.readWord();
System.err.println(fName);

con.println( (fName) +" What is Your Last Name?");
lName = con.readWord();
System.err.println(lName);

con.println( (fName) +" What is Your Date of Birth?");
dob = con.readInt();
System.err.println(dob);

date1 = new MyDate(dob);
}


public String getfName(){
return fName;
}
public String getlName(){
return lName;
}
public int getDob(){
return dob;

}

public void printPerson(Console con){
con.print( (fName)+" " +(lName)+": born on " );
date1.printDate(con);
return;
}

public int age(MyDate now2)
{
return this.date1.differenceInYears(now2);
}

}

end of person class
Back to top Go down
View user profile
 

code for lab8

View previous topic View next topic Back to top 
Page 1 of 1

Permissions of this forum:You cannot reply to topics in this forum
Glasgow University Bioinformatics forum :: IT Strand :: Programming-
Post new topic   Reply to topic