Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need some help testing created class.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need some help testing created class.

    I was assigned a problem where I have to create a class called Day. The program is to store the day such as Sun for Sunday, Mon for Monday, etc. The program then preform the following steps:
    1. Set the Day
    2. Print the Day
    3. Return the Day
    4. Return the Next Day
    5. Return the Previous Day
    6. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday.

    I have wrote some code for the Day class and the Daytester class but it isn't working very well and I can't figure out how I'm going to implement the part where it adds an integer and returns the Day.
    I'm just not very good with creating my own classes just yet.

    Here is the Day class that I have written so far:
    public class Day
    {
      private int Day;
      private String Sun,Mon,Tues,Wed,Thurs,Fri,Sat;
     
      public Day()
      {
        Sun = "";
        Mon = "";
        Tues = "";
        Wed = "";
        Thurs = "";
        Fri = "";
        Sat = "";
      }
     
      public Day(int Day)
      {
        if(Day == 1)
          Sun = "Sunday";
        else if(Day == 2)
          Mon = "Monday";
        else if(Day == 3)
          Tues = "Tuesday";
        else if(Day == 4)
          Wed = "Wednesday";
        else if(Day == 5)
          Thurs = "Thursday";
        else if(Day == 6)
          Fri = "Friday";
        else if(Day == 7)
          Sat = "Saturday";
       }
     
      public void setDay(int Day)
      {
        this.Day = Day;
      }
     
      public int getDay()
      {
        return this.Day;
      }
     
      public void printDay(int Day)
      {
        if(Day == 1)
          System.out.println("Today is " + Sun);
        else if(Day == 2)
          System.out.println("Today is " + Mon);
        else if(Day == 3)
          System.out.println("Today is " + Tues);
        else if(Day == 4)
          System.out.println("Today is " + Wed);
        else if(Day == 5)
          System.out.println("Today is " + Thurs);
        else if(Day == 6)
          System.out.println("Today is " + Fri);
        else if(Day == 7)
          System.out.println("Today is " + Sat);
        }
     
      public void printYesterday(int Day)
      {
        if(Day == 1)
          System.out.println("Yesterday was " + Sat);
        else if(Day == 2)
          System.out.println("Yesterday was " + Sun);
        else if(Day == 3)
          System.out.println("Yesterday was " + Mon);
        else if(Day == 4)
          System.out.println("Yesterday was " + Tues);
        else if(Day == 5)
          System.out.println("Yesterday was " + Wed);
        else if(Day == 6)
          System.out.println("Yesterday was " + Thurs);
        else if(Day == 7)
          System.out.println("Yesterday was " + Fri);
       }
     
      public void printTomorrow(int Day)
      {
        if(Day == 1)
          System.out.println("Tomorrow is " + Mon);
        else if(Day == 2)
          System.out.println("Tomorrow is " + Tues);
        else if(Day == 3)
          System.out.println("Tomorrow is " + Wed);
        else if(Day == 4)
          System.out.println("Tomorrow is " + Thurs);
        else if(Day == 5)
          System.out.println("Tomorrow is " + Fri);
        else if(Day == 6)
          System.out.println("Tomorrow is " + Sat);
        else if(Day == 7)
          System.out.println("Tomorrow is " + Sun);
      }
    }

    And here is the Daytester so far:
    import java.util.*;
     
    public class DayTester
    {
      public static void main(String[] args)
      {
        Scanner input = new Scanner(System.in);
        int Day,x;
        Day Dayweek;
     
        System.out.println("Enter a number 1-7 corresponding to the day of the week, for example, Sunday is 1, Monday is 2, .. Saturday is 7");
        Day = input.nextInt();
        Dayweek = new Day(Day);
        Dayweek.setDay(Day);
        System.out.println(Dayweek);
      }
    }
    I appreciate any help guys!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need some help testing created class.

    Do you know or can you use Arrays yet?

    Your constructor is a little twisted. You won't set the variables 'Mon' through 'Sun' based on the parameter Day (which should be called 'day'). In fact, there won't be variables 'Mon' through 'Sun'. You'll set the day object's name that will be a field, like:

    String name;

    This really isn't a bad start, but your thinking needs to be modified a bit on what the class Day is, what fields it needs, and how to set, get, and display those fields according to the requirements you've been given. And I really need to know if you know and can use Arrays. If not, the math part of the problem becomes a lot harder.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need some help testing created class.

    Quote Originally Posted by GregBrannon View Post
    Do you know or can you use Arrays yet?

    Your constructor is a little twisted. You won't set the variables 'Mon' through 'Sun' based on the parameter Day (which should be called 'day'). In fact, there won't be variables 'Mon' through 'Sun'. You'll set the day object's name that will be a field, like:

    String name;

    This really isn't a bad start, but your thinking needs to be modified a bit on what the class Day is, what fields it needs, and how to set, get, and display those fields according to the requirements you've been given. And I really need to know if you know and can use Arrays. If not, the math part of the problem becomes a lot harder.
    We started learning about arrays in our last class so I know the basics of an array.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need some help testing created class.

    Good. Then this is what I recommend you do:

    Create an array constant, something like:

    String[] DAY_OF_WEEK = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

    Then in your constructor,

    String name;
    int dayNumber;
     
    void Day( int dayNumber )
    {
        // set the name of the day to the element at the index dayNumber
        this.dayNumber = dayNumber;
        name = DAY_OF_WEEK[dayNumber]
     
        // the rest of the constructor . . .
    }
    And then the math part of the assignment becomes simple. The number of the next day is ( dayNumber + 1 ) % 7. You figure out the day before, 4 days from now, etc.

    Come back after you've made those changes and need more help.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    fpritt24 (November 16th, 2013)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need some help testing created class.

    Remember that indexes to arrays start at 0 and the index of the last element in the array is the length-1
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. scope of a class created as local variable
    By Samaras in forum Java Theory & Questions
    Replies: 6
    Last Post: August 25th, 2012, 06:01 PM
  2. Using charAt and other string methods in a created class
    By briankfmn in forum Object Oriented Programming
    Replies: 1
    Last Post: February 8th, 2012, 01:54 AM
  3. Diff between spring created been and instance of class
    By tcstcs in forum Web Frameworks
    Replies: 0
    Last Post: April 5th, 2011, 12:54 AM
  4. Running methods of a class that created you...
    By joestr in forum Java Theory & Questions
    Replies: 2
    Last Post: April 3rd, 2011, 01:59 PM
  5. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM