Question

Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.


Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.

1. Increment a Date to the next day (for example, 1-31-2011 increments to 2-1-2011; 12-31-2010 increments to 1-1-2011).

2. Decrement a Date to the previous day (for example, 2-1-1953 decrements to 1-31-1953; 1-1-1954 decrements to 12-31-1953).

. Extra Credit (up to 5 points) Re-write the Date member functions Input() and Output() to usedialog boxes to accomplish input and output of Date objects.

//---------------------------------------------------------
// Date.java
//---------------------------------------------------------

package problem14;

import java.util.Scanner;

public class Date
{
private String name;
private int MM;
private int DD;
private int YYYY;
private char separator;

//----------------------------------------------------
public Date(String name,char separator)
//----------------------------------------------------
{
this.name = name;
MM = 12; DD = 1; this.YYYY = 1993;
this.separator = separator;
System.out.print("Date construction#1 of "); Output();
}

//----------------------------------------------------
public Date(String name,int MM,int DD,int YYYY,char separator)
//----------------------------------------------------
{
this.name = name;
this.MM = MM; this.DD = DD; this.YYYY = YYYY;
this.separator = separator;
System.out.print("Date construction#2 of "); Output();
}

//----------------------------------------------------
public void finalize()
//----------------------------------------------------
{
System.out.print("Date destruction of "); Output();
}

//-------------------------------------------------
public void Input()
//-------------------------------------------------
{
Scanner IN = new Scanner(System.in);

System.out.print(name + " MM? "); MM = IN.nextInt();
System.out.print(name + " DD? "); DD = IN.nextInt();
System.out.print(name + " YYYY? "); YYYY = IN.nextInt();
}

//----------------------------------------------------
public void Output()
//----------------------------------------------------
{
System.out.printf("%s = %2d%c%2d%c%4d\n",name,MM,separator,DD,separator,YYYY);
}

//-------------------------------------------------
public void SetMM(int MM)
//-------------------------------------------------
{
this.MM = MM;
}

//-------------------------------------------------
public void SetDD(int DD)
//-------------------------------------------------
{
this.DD = DD;
}

//-------------------------------------------------
public void SetYYYY(int YYYY)
//-------------------------------------------------
{
this.YYYY = YYYY;
}

//-------------------------------------------------
public String GetName()
//-------------------------------------------------
{
return( name );
}

//-------------------------------------------------
public int GetMM()
//-------------------------------------------------
{
return( MM );
}

//-------------------------------------------------
public int GetDD()
//-------------------------------------------------
{
return( DD );
}

//-------------------------------------------------
public int GetYYYY()
//-------------------------------------------------
{
return( YYYY );
}

//-------------------------------------------------
public int GetSeparator()
//-------------------------------------------------
{
return( separator );
}

//-------------------------------------------------
public void incrementDate()
//-------------------------------------------------
{
int[] daysInMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// If leap year than Feb will contain 29 days
if (isLeapYear(this.YYYY))
{
daysInMonth[2] = 29;
}
/*
* increment days if no of days if more that the days of a month increment a
* month , if no of months ig greater that 12 then set it to 1 and increment a
* year
*/
this.DD++;
if (this.DD > daysInMonth[this.MM])
{
this.DD = 1;
this.MM++;
if (this.MM > 12)
{
this.MM = 1;
this.YYYY++;
}
}
}

//-------------------------------------------------
public void decrementDate()
//-------------------------------------------------
{
//missing code
}

public boolean isLeapYear(int year)
{
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}

}

//---------------------------------------------------------
// Problem14.java
//---------------------------------------------------------

package problem14;

import javax.swing.JOptionPane;

public class Problem14
{
public static void main(String[] args)
{
Date date1 = new Date("date1",'-');
Date date2 = new Date("date2",1,5,1953,'.');

date1.SetMM(1);
date1.SetDD(30);
date1.SetYYYY(1979);
date1.Output(); System.out.println();

date2.Input();

// OMG! An example of an attempted violation of the principle of information hiding!!!
// System.out.printf("%s = %2d%c%2d%c%4d\n\n",date2.name,date2.MM,date2.separator,
// date2.DD,date2.separator,date2.YYYY);
// generates a syntax error that complains about name having private access in the class Date.

// This is the correct way to access private instance variables using accessor methods.
System.out.printf("%s = %2d%c%2d%c%4d\n\n",date2.GetName(),date2.GetMM(),date2.GetSeparator(),date2.GetDD(),date2.GetSeparator(),date2.GetYYYY());
  
date2.incrementDate();
System.out.println("Next Day is ");
date2.Output();
  
date1 = null;
date2 = null;

System.out.println("Just before System.gc() call...");
System.gc();

JOptionPane.showMessageDialog(null,"Pause execution...");

}
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Suggest using simple java.util.lib

SimpleDateFormat Dateformat1= new SimpleDateFormat("dd-mm-yyyy");

Calender cal=Calender.getinstance();

//print the user input date

cal.set(31,Calender.JANUARY,2011);

System.out.println(Dateformat1.format(cal.getTime()));

//increment the date to next day and print the date

cal.add(Calender.DATE,1);

System.out.println(Dateformat1.format(cal.getTime()));

//Decrement the date by one day and print the date

cal.add(Calender.DATE,-1);

System.out.println(Dateformat1.format(cal.getTime()));

 
Add a comment
Know the answer?
Add Answer to:
Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; //...

    Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; // Scanner class to support user input public class TestPetHierarchy { /* * All the 'work' of the process takes place in the main method. This is actually poor design/structure, * but we will use this (very simple) form to begin the semester... */ public static void main( String[] args ) { /* * Variables required for processing */ Scanner input = new Scanner( System.in...

  • For Java please.Artwork. javaimport java.util.Scanner;public class ArtworkLabel {public static void main(String[] args)...

     Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...

  • Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void...

    Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void main(String[] args) { int currentVertex, userChoice; Scanner input = new Scanner(System.in); // create graph using your WeightedGraph based on author's Graph WeightedGraph myGraph = new WeightedGraph(4); // add labels myGraph.setLabel(0,"Spot zero"); myGraph.setLabel(1,"Spot one"); myGraph.setLabel(2,"Spot two"); myGraph.setLabel(3,"Spot three"); // Add each edge (this directed Graph has 5 edges, // so we add 5 edges) myGraph.addEdge(0,2,9); myGraph.addEdge(1,0,7); myGraph.addEdge(2,3,12); myGraph.addEdge(3,0,15); myGraph.addEdge(3,1,6); // let's pretend we are on...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the fo...

    I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the form (month day year): 2 28 2018  Ask user to enter Birthday in the form (month day year): 11 30 1990  Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...

  • public class Pet {    //Declaring instance variables    private String name;    private String species;...

    public class Pet {    //Declaring instance variables    private String name;    private String species;    private String parent;    private String birthday;    //Zero argumented constructor    public Pet() {    }    //Parameterized constructor    public Pet(String name, String species, String parent, String birthday) {        this.name = name;        this.species = species;        this.parent = parent;        this.birthday = birthday;    }    // getters and setters    public String getName() {        return name;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT