3 Replies - 438 Views - Last Post: 01 August 2013 - 01:08 PM Rate Topic: -----

#1 manuelma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-July 13

Adding days and subtracting days using an interface

Posted 01 August 2013 - 09:15 AM

Hey,

I'm working on a calendar interface where the user can specify how many days to add or subtract. For some reason, my 'days' won't exist - it would require me to convert the addDays or subtractDays method into a static, which would negate the whole point of using an interface (which is required of the assignment. I feel like this may be the last step to completing the assignment and the semester (thank God).

Any help?

import java.text.SimpleDateFormat;
import java.util.*;

public class calendar implements elapsed {
	
	@Override
	public Date addDays(Date date, int days) {
		GregorianCalendar addCalc = new GregorianCalendar();
		addCalc.setTime(date);
		addCalc.add(Calendar.DATE, days);
		return addDays(date, days);
	}

	@Override
	public Date subtractDays(Date date, int days) {
		GregorianCalendar subCalc = new GregorianCalendar();
		subCalc.setTime(date);
		subCalc.add(Calendar.DATE, days);
		return subtractDays(date, days);
	}

	public static String toddMMyy(Date day){
	    SimpleDateFormat properFormat = new SimpleDateFormat("MM-dd-yyyy");
	    String date = properFormat.format(day);
	    return date;
	}
public void main(String[] args)
{
	Scanner in = new Scanner(System.in);
	Date date = new Date();
    System.out.println("Today is " + toddMMyy(date));
    
    System.out.println("Would you like to add or subtract days? Type 'add' or 'subtract'.");
    String answer = in.next();
    
    if (answer.equals("add") || answer.equals("ADD"))
	{
    	System.out.println("Add how many days?");
    	days = in.nextInt();
    	Date newDate = elapsed.addDays(date, days);
        System.out.println("Date after adding "+days+" days: "+newDate.toString());
	}
    
    if (answer.equals("subtract") || answer.equals("SUBTRACT"))
	{
    	 Date newDate = elapsed.subtractDays(date, days);
         System.out.println("Date after subtracting "+days+" days: "+newDate.toString());
         
	}
    
}
}



and what it's implementing:

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public interface elapsed {

	public static final int DAYS_WEEK = 7;
	public static final int WEEKS_YEAR = 52;
	
	public abstract Date addDays(Date date, int days);
	 
	public abstract Date subtractDays(Date date, int days);	
	
	
}



Can't thank you guys enough!!

Is This A Good Question/Topic? 0
  • +

Replies To: Adding days and subtracting days using an interface

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Adding days and subtracting days using an interface

Posted 01 August 2013 - 09:33 AM

Quote

return addDays(date, days);
That's not right and will result in a recursive call

Should be

return addCalc.getTime();

This post has been edited by g00se: 01 August 2013 - 09:35 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#3 manuelma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-July 13

Re: Adding days and subtracting days using an interface

Posted 01 August 2013 - 09:50 AM

View Postg00se, on 01 August 2013 - 09:33 AM, said:

Quote

return addDays(date, days);
That's not right and will result in a recursive call

Should be

return addCalc.getTime();

Oh. So then what would my

    	 Date addDate = elapsed.addCalc(date, days);



be in this case? Something like elasped.getTime(___);?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Adding days and subtracting days using an interface

Posted 01 August 2013 - 01:08 PM

Quote

elapsed.addCalc


Your interface 'elapsed' (btw interface and class names begin upper case in Java) doesn't have a method addCalc ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1