So I don\'t even know where to start. I need to create class code without any ja
ID: 3658412 • Letter: S
Question
So I don't even know where to start. I need to create class code without any java api imports. write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date (int year, int month, int day) // constructs a new Date object to represent the given date. public void addDays(int days) //Moves this date object forward in time by given number of days public void addWeeks(int weeks) //Moves this date object forward in time by given number of seven-day weeks public int daysTo(Date other) // returns number of days from one date to another and here what the client code looks like public class Assign4 { public static void main(String[] a) { Date Date(1753,1,1); Date two = new Date(2012,1,28); one.addDays(1); // advance one day one.addWeeks(1); // advance one week System.out.println(one.daysTo(two)); // 94616 positive //System.out.println(one.equals(two)); // checks day, month, year (false here) //one.nextDay(); // simply advance one day //System.out.println(one.getDayOfWeek()); // a Wednesday if all above works // Finally, let's understand what static methods are most commonly used for: //System.out.println(Date.daysTo(one, two)); // still 94,616 days } }Explanation / Answer
class Date{ private : int year ; int month; int day; public: Date(int a,int b,int c){ this.year = a; this.month = b; this.day = c; } void addDays(int a){ this.day= (this.day + a)%30; this.month += (this.day + a)/30; return; } void addWeeks(int a){ addDays(7*a); return; } void nextDay(){ addDays(1); } int daysTo(Date other){ return (other.date-this.date + (other.month-this.month)*30 + (other.year-this.year)*365); } } class assign4{ public static void main(String[] a){ Date one = new Date(1753,1,1); Date two = new Date(2012,1,28); one.addDays(1); System.out.println(one.daysTo(two)); System.out.println(one.equals(two)); one.nextDay(); //System.out.println(one.getDayOfWeek()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.