In: Computer Science
With the code that is being tested is:
import java.util.Random;
public class GVdate
{
private int month;
private int day;
private int year;
private final int MONTH = 1;
private final int DAY = 9;
private static Random rand = new Random();
/**
* Constructor for objects of class GVDate
*/
public GVdate()
{
this.month = rand.nextInt ( MONTH) + 1;
this.day = rand.nextInt ( DAY );
}
public int getMonth()
{return this.month;
}
public int getDay()
{return this.day;
}
public int getYear()
{return this.year;
}
public String toString()
{String monthString = "0" + this.month;
int monthLength = monthString.length();
String dayString = "0" + this.day;
int dayLength = dayString.length();
return (
"\t" +
monthString.substring( monthLength-2 ) + ":" +
dayString.substring( dayLength-2 ) + " " +
this.year
);
}
public boolean isMyBirthday()
{return month == 8 && day == 31 && year ==
2001;
}
public void setMonth( int m )
{
month=m;
}
public void setDay( int d )
{
day=d;
}
public void setYear( int y)
{
year=y;
}
public void setDate (int m, int d, int y)
{ year = y;
month = m;
day = d;
}
}
How do I fix the errors so that it runs?
int errors = 0; System.out.println ("Testing begings"); //********** phase 1 testing ************ // testing the default constructor GVdate today = new GVdate(); if (today.getMonth() != 10){ System.out.println("month should be 10"); errors++; } if (today.getDay() != 12){ System.out.println("day should be 12"); errors++; } // TO DO: test the year // testing constructor 2 GVdate theDay = new GVdate(1, 10, 1995); // TO DO: complete the checks for month, day and year // testing setter methods //testing setMonth theDay.setMonth(8); // TO DO: complete the code to check for month // TO DO: finish testing setDay and setYear // TO DO: test the toString method System.out.println("Errors: " + errors); System.out.println ("Testing ends"); } }
Corrected Java code
import java.util.Random;
public class GVdate
{
private int month;
private int day;
private int year;
private final int MONTH = 1;
private final int DAY = 9;
private static Random rand = new Random();
/**
* Constructor for objects of class GVDate
*/
public GVdate()
{
this.month = rand.nextInt ( MONTH) + 1;
this.day = rand.nextInt ( DAY );
}
public GVdate(int month , int day, int year)
{
this.month = month;
this.day = day;
this.year = year;
}
public int getMonth()
{return this.month;
}
public int getDay()
{return this.day;
}
public int getYear()
{return this.year;
}
public String toString()
{String monthString = "0" + this.month;
int monthLength = monthString.length();
String dayString = "0" + this.day;
int dayLength = dayString.length();
return (
"\t" +
monthString.substring( monthLength-2 ) + ":" +
dayString.substring( dayLength-2 ) + " " +
this.year
);
}
public boolean isMyBirthday()
{return month == 8 && day == 31 && year ==
2001;
}
public void setMonth( int m )
{
month=m;
}
public void setDay( int d )
{
day=d;
}
public void setYear( int y)
{
year=y;
}
public void setDate (int m, int d, int y)
{ year = y;
month = m;
day = d;
}
//How do I fix the errors so that it runs?
public static void main(String args[]){
int errors = 0;
System.out.println
("Testing begings");
//********** phase 1 testing ************
// testing the
default constructor
GVdate today = new
GVdate();
if (today.getMonth() !=
10){
System.out.println("month should be 10");
errors++;
}
if (today.getDay() !=
12){
System.out.println("day should be 12");
errors++;
}
// TO DO: test the
year
if (today.getYear() !=
1995){
System.out.println("year should be 1995");
errors++;
}
// testing
constructor 2
GVdate theDay = new
GVdate(1, 10, 1995);
// TO DO: complete the
checks for month, day and year
// testing setter
methods
//testing setMonth
theDay.setMonth(8);
theDay.setDay(10);
theDay.setYear(1995);
// TO DO: complete the
code to check for month
// TO DO: finish testing
setDay and setYear
// TO DO: test the
toString method
System.out.println(theDay);
System.out.println("Errors: " + errors);
System.out.println
("Testing ends");
}
}