lundi 1 février 2016

how to test the class Day I created

I have seen questions regarding this assignment, but none really answer my question. I have tried to create the class Day that can set the day, print and return the day, return the next day and the previous day, and calculate and return the day if a given number of days are added to the original day. I also have to make a test program to test the class Day. I'm very new at this, and I finally got it to compile with no errors, but now when I run the program and enter the day, I get this:

    Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at testDay.main(testDay.java:13)

I know it is because the user is going to input a day and that doesn't agree with the ' int storeDay = keyboard.nextInt();' command, but I'm not sure how to fix this. I was reading about switch structures, but have never used one and am not sure if it would work. I have also seen examples of this assignment where the creator didn't use each day as a separate variable so I'm wondering if I am on the right path with what I have so far. I'm not trying to get anyone to fix this or do it for me, but I'm just wondering if someone can point me in the right direction in terms of if I am on the right path, or if I need to change my variables, or if a switch structure might help me get this to work. Any input would be greatly appreciated. Thank you!

    public class Day
     {  
      final static int sunday = 0;
      final static int monday = 1;
      final static int tuesday = 2;
      final static int wednesday = 3;
      final static int thursday = 4;
      final static int friday = 5;
      final static int saturday = 6;

      private int oneDay;

      public Day()
       {
        setDay(0);
       }
      public Day(int oneDay)
       {
       setDay(oneDay);
       }
      public void setDay(int oneDay)
       {
        this.oneDay = oneDay;
       }
      public void printDay()
       {
        System.out.print("" + oneDay);
       }
      public int returnDay()
       {
        return oneDay;
       }
      public int returnNextDay()
       {
       return (oneDay + 1)%7;
       }
      public int returnPreviousDay()
       {
        return (oneDay - 1)%7;
       }
      public int returnAddDay(int addDays)
       {
        return (oneDay + addDays)%7;
       }        
     }

Test Code:

    import java.util.Scanner;

    public class testDay
     {
      static Scanner keyboard = new Scanner(System.in);

      public static void main(String[] args) 
       {
        Day testDay = new Day();

        System.out.print("Enter the day: ");

        int storeDay = keyboard.nextInt();
        testDay.setDay(storeDay);
        System.out.println("Your day is: " + testDay.returnDay());
        System.out.println("The previous day is: " + testDay.returnPreviousDay());
        System.out.println("The next day is: " + testDay.returnNextDay());

          }
        }

Note: I know my test code isn't finished but I want to get the first few tasks right before moving to the rest. Thank you!

Aucun commentaire:

Enregistrer un commentaire