Question

In: Computer Science

With the code that is being tested is: import java.util.Random; public class GVdate { private int...

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");
    }  

}

Solutions

Expert Solution

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");
    }

}


Related Solutions

import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r = new Random(seed); }    public static String numberToDirection(int a){ if(a==0) return "North";    if(a==1) return "NorthEast"; if(a==2) return "East"; if(a==3) return "Southeast"; if(a==4) return "South"; if(a==5) return "Southwest"; if(a==6) return "West";    if(a==7) return "Northwest";    return "Invalid Direction" ; } public String randomDirection(){ return numberToDirection(r.nextInt()% 4 + 1); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter seed: ");...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private E[] queue;    private int front = 0, rear = 0;    private static final int DEFAULT_CAPACITY = 5;       public CircularQueue(int capacity)    {    queue = (E[]) new Object[capacity + 1];    }       public CircularQueue()    {        this(DEFAULT_CAPACITY); }       //Add a method that will determine if the queue is empty. Recall that the queue is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT