Browse Source

Added files

master
Raphael Roberts 6 years ago
commit
d45cae4ee7
  1. 2
      .gitignore
  2. 51
      prog/Date.java
  3. 23
      prog/IncDate.java
  4. 48
      prog/TestIncDate.java
  5. BIN
      prog/testDates.dat

2
.gitignore

@ -0,0 +1,2 @@
/HW1.zip
/HW1.pdf

51
prog/Date.java

@ -0,0 +1,51 @@
// The Date class that represents dates
// Do not make any changes to this file!
// Xiwei Wang
import java.io.*;
public class Date implements Serializable
{
// instance variables
protected int m_year;
protected int m_month;
protected int m_day;
// copy constructor
public Date(Date o)
{
m_year = o.m_year;
m_month = o.m_month;
m_day = o.m_day;
}
// constructor
public Date(int month, int day, int year)
{
m_year = year;
m_month = month;
m_day = day;
}
// observers
public int getYear()
{
return m_year;
}
public int getMonth()
{
return m_month;
}
public int getDay()
{
return m_day;
}
// return this date as a String
public String toString()
{
return (m_month + "/" + m_day + "/" + m_year);
}
}

23
prog/IncDate.java

@ -0,0 +1,23 @@
// The IncDate class
// Do not make changes to anything other than the body of increment() method
// Your name here
public class IncDate extends Date
{
// copy constructor
public IncDate(Date o)
{
super(o.m_month, o.m_day, o.m_year);
}
// constructor
public IncDate(int month, int day, int year)
{
super(month, day, year);
}
public void addDays(int numDays)
{
// TODO: implement this method
}
}

48
prog/TestIncDate.java

@ -0,0 +1,48 @@
// Test driver for the IncDate class
// Do not make any changes to this file!
// Xiwei Wang
import java.util.*;
import java.io.*;
public class TestIncDate
{
public static void main(String[] args)
{
try
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("testDates.dat"));
ArrayList<Date> oldDates;
ArrayList<Integer> daysList;
ArrayList<Date> newDates;
oldDates = (ArrayList<Date>)in.readObject();
daysList = (ArrayList<Integer>)in.readObject();
newDates = (ArrayList<Date>)in.readObject();
IncDate myDate;
int numCorrect = 0;
for (int i = 0; i < oldDates.size(); i++)
{
System.out.println("The current date is " + oldDates.get(i) + " and " + daysList.get(i) + " days are added.");
myDate = new IncDate(oldDates.get(i));
myDate.addDays(daysList.get(i));
System.out.println("The correct new date is " + newDates.get(i) + " and the one calculated by your program is " + myDate + ".");
if (myDate.toString().equals(newDates.get(i).toString()))
{
System.out.println("Correct!\n");
numCorrect++;
}
else
System.out.println("Wrong!\n");
}
System.out.println("Total test cases: " + oldDates.size() + "\nCorrect: " + numCorrect + "\nWrong: " + (oldDates.size() - numCorrect));
}
catch (Exception e)
{
System.out.println("Error occurred: " + e.getMessage());
}
}
}

BIN
prog/testDates.dat

Loading…
Cancel
Save