1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Passing an Object to a function

Discussion in 'Java' started by cyrow, Jan 31, 2008.

  1. cyrow

    cyrow New Member

    Joined:
    Nov 19, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    import java.io.*;
    import java.util.*;
    
    class Time1{
    	private int day;
    	private int month;
    	private int year;
    	
    	Time1(int d, int m, int y){
    		day =d;
    		month =m;
    		year = y;
    	}
    	
    	public void setDay(){
    		
    	}
    		public void setDay(int d){
    			if(d < 0 || d > 31){
    				System.out.println("Day cannot be less than 0 or greater than 31");
    			}else{
    				day = d;
    			}
    			
    	}
    		public void setMonth(int m){
    			if(m <0 || month > 12){
    				System.out.println("Month cannot be less than 0 or greater than 12");
    			}else{
    				month = m;
    			}
    		
    	}
    		public void setYear(int y){
    		year = y;
    	}	
    		public int getDay(){
    			return day;
    			
    		}
    			public int getMonth(){
    			return month;
    		}
    			public int getYear(){
    			return year;
    		}
    		public void print(){
    			 System.out.println("Day  :" + day);
    			 System.out.println("Month:" + month);
    			 System.out.println("Year :" + year);
    		} 
    }
    public class Date2{
    	public static void main(String[] args){
    		Time1 d1 = new 	Time1(12,4,2003);
    		Time1 d2 = new 	Time1(23, 12,2006);
    		
    
    		// call function
    		int result = checkDate(d1, d2);
    		
    		if(result  < 1){
    			System.out.println("d1 comes before d2");
    		}else if(result == 0){
    			System.out.println("d1 equals to d2");
    		}else{
    			System.out.println("d2 is greater than d1");
    		}
    	}//end main
    public static int checkDate(d1 x, d2 y){//[B]Cannot find symbol class d1; Cannot find symbol class d2[/B]	if(x.day < y.day || x.month < y.month || x.year < y.year){
    		return -1;
    	}else if (x.day == y.day || x.month == y.month || x.year == y.year){
    		return 0;
    	}else{
    		return 1;
    	}
      }
    }
     

Share This Page