JAVA - have written the programs but want to modify it now. Please help me get r
ID: 3770290 • Letter: J
Question
JAVA - have written the programs but want to modify it now.
Please help me get rid of the private fields.
RESTRICTONS:
Every method and constructor in your code must interact with the super class. No new Class fields should be created. Variables with local scope inside methods are OK.
Here's the start of your code:
import java.util.*;
public class Date extends GregorianCalendar {
// no new fields allowed this time
// constructors, accessors, modifiers, methods follow
My Code:
/*
* Programming Assignment #4
*/
public class Date {
private int day; private int month; private int year;
public Date(){
day = 1; month = 1; year = 1970; // This is default
}
public Date(int new_year, int new_month, int new_day){
day = new_day;
month = new_month;
year = new_year;
if(day<1 || day>31){
throw new IllegalArgumentException("Pleas correct your day = "+ day);
}
if(month<1 || month>12){
throw new IllegalArgumentException("Pleas correct your month = "+ month);
}
if(month==2 && day==29){
if(!(this.isLeapYear()))
throw new IllegalArgumentException("the year "+year+" is not a leap year, so February can not be "+day);
}
if(year<0){
throw new IllegalArgumentException("Pleas correct your year = "+ year);
}
}
public Date(Date other){
day = other.day;
month = other.month;
year = other.year;
}
public void addDays(int days){
day+=days;
if(day<1){//start main if block
while(day<1){
month--;
day+=getDaysInMonth();
if(month<1){
month = 12- month;
year--;
}
}//end of while loop
}else{
if(day>getDaysInMonth()){
while(day>getDaysInMonth()){
day-= getDaysInMonth();
month++;
if(month>12){
month=month-12;
year++;
}
}//end of while loop
}
}//end of main if block
}//end of addDays method
public int getDaysInMonth(){
if(month ==9 || month ==4 || month ==11){
return 30;
} else if(month == 2){
if(isLeapYear()){
return 29;
}else{
return 28;
}
}else{
return 31;
}
}//end of getDaysInMonth method
public void addWeeks(int weeks){
addDays(7*weeks);
}//end of addWeeks method
public boolean isLeapYear(){
if( year % 4 ==0 && year % 100 !=0){
return true;
}else {
return false;
}
}//end of isLeapYear method
public int daysTo(Date other){
int yearsto=0; int monthto = 0; int daysto = 0; int new_day=0;
Date temp = new Date(this);
Date year1 = new Date(this);//copy values from this
Date year2 = new Date(other);//copy values from other
if(year1.year <= year2.year){//starting main if block
if(year1.year == year2.year){
yearsto=0;
}else{
for(int i = year1.year; i< year2.year; i++){
temp.year = i;
if(temp.isLeapYear()){
yearsto += 366;
}else{
yearsto += 365;
}
}//end of year loop
}//end of year compare
if(year1.month<=year2.month){
if(year1.month == year2.month){
monthto=0;
}else {
for(int j= year1.month; j< year2.month; j++){
temp.month = j;
if(j==4 || j==6 ||j==9 ||j==11){
monthto += 30;
}else if (j==2){
if(temp.isLeapYear()){
monthto +=29;
}else{
monthto +=28;
}
}else{
monthto +=31;
}
}
}// end of month for
if(year1.day<year2.day){
daysto = year2.day - year1.day;
}else{
daysto = (year2.day - year1.day);
}// end of day compare
}else if(year1.month>year2.month){
for(int j= year1.month-1; j>= year2.month; j--){
temp.month = j;
if(year1.month == year2.month){
monthto=0;
}else {
if(j==4 || j==6 ||j==9 ||j==11){
monthto -= 30;
}else if (j==2){
if(temp.isLeapYear()){
monthto -=29;
}else{
monthto -=28;
}
}else{
monthto -=31;
}
}
}// end of month for
if(year1.day<year2.day){
daysto = year2.day - year1.day;
}else{
daysto = year2.day - year1.day;
}// end of day compare
} // end of year1.month and year2.month compare
}// end of if year1.year < year2.year
else {// it means if(year1.year > year2.year)
for(int i = year1.year-1; i>= year2.year; i--){
temp.year = i;
if(temp.isLeapYear()){
yearsto -= 366;
}else{
yearsto -= 365;
}
}//end of year loop
if(year1.month<=year2.month){
if(year1.month == year2.month){
monthto=0;
}else {
for(int j= year2.month-1; j>= year1.month; j--){
temp.month = j;
if(j==4 || j==6 ||j==9 ||j==11){
monthto += 30;
}else if (j==2){
if(temp.isLeapYear()){
monthto +=29;
}else{
monthto +=28;
}
}else{
monthto +=31;
}
}
}// end of month for
if(year1.day==year2.day){
daysto = 0;
}else{
daysto = (year2.day - year1.day);
}// end of day compare
}else if(year1.month>year2.month){
for(int j= year1.month-1; j>= year2.month; j--){
temp.month = j;
if(j==4 || j==6 ||j==9 ||j==11){
monthto -= 30;
}else if (j==2){
if(temp.isLeapYear()){
monthto -=29;
}else{
monthto -=28;
}
}else{
monthto -=31;
}
}
}// end of month for
if(year1.day==year2.day){
daysto = 0;
}else{
daysto = (year2.day - year1.day);
}// end of day compare
} // end of main if block
new_day =yearsto + monthto + daysto;
return new_day;
}// end of daysTo method
public String toString(){
int today = this.day; int thismonth = this.month;
String standard_day =""; String standard_month = ""; String standard_year =""+this.year;
if(today<10){
standard_day ="0"+today;
}else{
standard_day =""+today;
}
if(thismonth<10){
standard_month="0"+thismonth;
}else{
standard_month=""+thismonth;
}
String s = standard_year+"/"+standard_month+"/"+standard_day;
return s;
}// end of toString method
public static int daysTo(Date one, Date two){
int result = one.daysTo(two);//refer to daysTo (Date other) method
return result;
}// end of daysTo method
public int getDay(){
return day;
}// end of getDay method
public int getMonth(){
return month;
}// end of getMonth method
public int getYear(){
return year;
}// end of getYear method
}//end of Date class
My code main method class:
/*
* from Building Java Programs text
* by Reges and Stepp (Pearson pub.)
*
* Programming Project #2, Chapter 8, p. 571
*/
public class Assign4 {
// Part of the main method I'll use to test your class
// NO imports allowed from the JAVA API
public static void main(String[] a) {
Date Date(1753,1,1); // well know Monday
Date two = new Date(2012,1,28); // 2012 was a leap year
one.addDays(1); // advance one day (negative subtracts days)
one.addWeeks(1); // advance one week (negative allowed, yes)
System.out.println(one.daysTo(two)); // 94616 positive days
System.out.println(one.getDay()); // now day is 9
System.out.println(one.getMonth()); // January is 1
System.out.println(one.getYear()); // still 1753 (SQL based)
System.out.println(one.isLeapYear()); // false for 1753
System.out.println(one.toString()); // style is 1753/01/09
try {
Date three = new Date(99,99,99); // obviously illegal
Date four = new Date(2013,2,29); // illegal leap year
// Default use zero time in UNIX it's January 1, 1970
Date five = new Date();
} catch (IllegalArgumentException e) {
System.out.println("Illegal Date attempted");
}
// 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
Protected variables in GregorianCalendar, makes availabe the fiedls to Date class.
package cheg;
public class DateAssign4 {
public static void main(String[] a) {
Date Date(1753, 1, 1); // well know Monday
Date two = new Date(2012, 1, 28); // 2012 was a leap year
one.addDays(1); // advance one day (negative subtracts days)
one.addWeeks(1); // advance one week (negative allowed, yes)
System.out.println(one.daysTo(two)); // 94616 positive days
System.out.println(one.getDay()); // now day is 9
System.out.println(one.getMonth()); // January is 1
System.out.println(one.getYear()); // still 1753 (SQL based)
System.out.println(one.isLeapYear()); // false for 1753
System.out.println(one.toString()); // style is 1753/01/09
try {
Date three = new Date(99, 99, 99); // obviously illegal
Date four = new Date(2013, 2, 29); // illegal leap year
// Default use zero time in UNIX it's January 1, 1970
Date five = new Date();
} catch (IllegalArgumentException e) {
System.out.println("Illegal Date attempted");
}
// Finally, let's understand what static methods are most commonly used
// for:
System.out.println(Date.daysTo(one, two)); // still 94,616 days
}
}
class GregorianCalendar {
protected int day;
protected int month;
protected int year;
public GregorianCalendar() {
}
}
class Date extends GregorianCalendar {
public Date() {
day = 1;
month = 1;
year = 1970; // This is default
}
public Date(int new_year, int new_month, int new_day) {
day = new_day;
month = new_month;
year = new_year;
if (day < 1 || day > 31) {
throw new IllegalArgumentException("Pleas correct your day = "
+ day);
}
if (month < 1 || month > 12) {
throw new IllegalArgumentException("Pleas correct your month = "
+ month);
}
if (month == 2 && day == 29) {
if (!(this.isLeapYear()))
throw new IllegalArgumentException("the year " + year
+ " is not a leap year, so February can not be " + day);
}
if (year < 0) {
throw new IllegalArgumentException("Pleas correct your year = "
+ year);
}
}
public Date(Date other) {
day = other.day;
month = other.month;
year = other.year;
}
public void addDays(int days) {
day += days;
if (day < 1) {// start main if block
while (day < 1) {
month--;
day += getDaysInMonth();
if (month < 1) {
month = 12 - month;
year--;
}
}// end of while loop
} else {
if (day > getDaysInMonth()) {
while (day > getDaysInMonth()) {
day -= getDaysInMonth();
month++;
if (month > 12) {
month = month - 12;
year++;
}
}// end of while loop
}
}// end of main if block
}// end of addDays method
public int getDaysInMonth() {
if (month == 9 || month == 4 || month == 11) {
return 30;
} else if (month == 2) {
if (isLeapYear()) {
return 29;
} else {
return 28;
}
} else {
return 31;
}
}// end of getDaysInMonth method
public void addWeeks(int weeks) {
addDays(7 * weeks);
}// end of addWeeks method
public boolean isLeapYear() {
if (year % 4 == 0 && year % 100 != 0) {
return true;
} else {
return false;
}
}// end of isLeapYear method
public int daysTo(Date other) {
int yearsto = 0;
int monthto = 0;
int daysto = 0;
int new_day = 0;
Date temp = new Date(this);
Date year1 = new Date(this);// copy values from this
Date year2 = new Date(other);// copy values from other
if (year1.year <= year2.year) {// starting main if block
if (year1.year == year2.year) {
yearsto = 0;
} else {
for (int i = year1.year; i < year2.year; i++) {
temp.year = i;
if (temp.isLeapYear()) {
yearsto += 366;
} else {
yearsto += 365;
}
}// end of year loop
}// end of year compare
if (year1.month <= year2.month) {
if (year1.month == year2.month) {
monthto = 0;
} else {
for (int j = year1.month; j < year2.month; j++) {
temp.month = j;
if (j == 4 || j == 6 || j == 9 || j == 11) {
monthto += 30;
} else if (j == 2) {
if (temp.isLeapYear()) {
monthto += 29;
} else {
monthto += 28;
}
} else {
monthto += 31;
}
}
}// end of month for
if (year1.day < year2.day) {
daysto = year2.day - year1.day;
} else {
daysto = (year2.day - year1.day);
}// end of day compare
} else if (year1.month > year2.month) {
for (int j = year1.month - 1; j >= year2.month; j--) {
temp.month = j;
if (year1.month == year2.month) {
monthto = 0;
} else {
if (j == 4 || j == 6 || j == 9 || j == 11) {
monthto -= 30;
} else if (j == 2) {
if (temp.isLeapYear()) {
monthto -= 29;
} else {
monthto -= 28;
}
} else {
monthto -= 31;
}
}
}// end of month for
if (year1.day < year2.day) {
daysto = year2.day - year1.day;
} else {
daysto = year2.day - year1.day;
}// end of day compare
} // end of year1.month and year2.month compare
}// end of if year1.year < year2.year
else {// it means if(year1.year > year2.year)
for (int i = year1.year - 1; i >= year2.year; i--) {
temp.year = i;
if (temp.isLeapYear()) {
yearsto -= 366;
} else {
yearsto -= 365;
}
}// end of year loop
if (year1.month <= year2.month) {
if (year1.month == year2.month) {
monthto = 0;
} else {
for (int j = year2.month - 1; j >= year1.month; j--) {
temp.month = j;
if (j == 4 || j == 6 || j == 9 || j == 11) {
monthto += 30;
} else if (j == 2) {
if (temp.isLeapYear()) {
monthto += 29;
} else {
monthto += 28;
}
} else {
monthto += 31;
}
}
}// end of month for
if (year1.day == year2.day) {
daysto = 0;
} else {
daysto = (year2.day - year1.day);
}// end of day compare
} else if (year1.month > year2.month) {
for (int j = year1.month - 1; j >= year2.month; j--) {
temp.month = j;
if (j == 4 || j == 6 || j == 9 || j == 11) {
monthto -= 30;
} else if (j == 2) {
if (temp.isLeapYear()) {
monthto -= 29;
} else {
monthto -= 28;
}
} else {
monthto -= 31;
}
}
}// end of month for
if (year1.day == year2.day) {
daysto = 0;
} else {
daysto = (year2.day - year1.day);
}// end of day compare
} // end of main if block
new_day = yearsto + monthto + daysto;
return new_day;
}// end of daysTo method
public String toString() {
int today = this.day;
int thismonth = this.month;
String standard_day = "";
String standard_month = "";
String standard_year = "" + this.year;
if (today < 10) {
standard_day = "0" + today;
} else {
standard_day = "" + today;
}
if (thismonth < 10) {
standard_month = "0" + thismonth;
} else {
standard_month = "" + thismonth;
}
String s = standard_year + "/" + standard_month + "/" + standard_day;
return s;
}// end of toString method
public static int daysTo(Date one, Date two) {
int result = one.daysTo(two);// refer to daysTo (Date other) method
return result;
}// end of daysTo method
public int getDay() {
return day;
}// end of getDay method
public int getMonth() {
return month;
}// end of getMonth method
public int getYear() {
return year;
}// end of getYear method
}// end of Date class
----------output--------------
94615
9
1
1753
false
1753/01/09
Illegal Date attempted
94615
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.