开发者

Trying to write a months class for a calendar class in java.

Ok so for a big lab I have to do the first part is to write a calendar class just like the one I've written here. This was pretty simple because we got really direct instructions on how to do this. I'll show it in case that helps with the problem:

import java.util.Scanner;

public class Calendar 
{ 
    static final int FEBRUARY        = 2;
    static final int MONTHS_PER_YEAR = 12;

    static int [] daysPerMonth = 
        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    static String [] months =  
        {"", "  Januray", "February", "   March", "   April",
             "      May", "    June", "    July", "  August", 
            "September", " October", "November", "December"};

    public static void main(String[] args) 
    { 
        Scan开发者_如何学JAVAner in = new Scanner(System.in); 
        System.out.print("Enter the year? (yyyy): "); 
        int year = in.nextInt(); 

        int firstDay = firstDayOfYear(year);

        // February: Check for leap year    
        daysPerMonth [FEBRUARY] = isLeapYear(year) ? 29 : 28; 

        for (int i=1; i <= MONTHS_PER_YEAR; i++) {
            Month month = new Month(daysPerMonth[i], firstDay);
            System.out.println("\n\n      "+months[i]+" "+year);
            month.printMonth();
            firstDay = month.getFirstDayOfNextMonth(); 
        }
    }

    private static boolean isLeapYear(int year) {
        return ((year%4 == 0) && (year%100 != 0)) || (year%400 == 0);
    }

    private static int firstDayOfYear(int year) {
        return (2 * (13) + (3 * (13+1)/5) + (year-1) + ((year-1)/4) 
                - ((year-1)/100) + ((year-1)/400) + 2) % 7;
    }
}

Ok so that's the first class. The part I'm really having trouble even getting started with is writing a "months" class. I've created the class and I keep trying to start setting up different methods and doing things like that but I really just can't seem to grasp the concept of how exactly to set up something in one class, so that it works for another class.

Here's the exact thing the months class has to do: The Month class that you have to define must contain the following state and behavior:

STATE

the number of days in the month (an integer between 28-31)

the first day of the month (an integer between 0-6, Sun=0, Mon=1, Tues=2, Wed=3, Thurs=4, Fri=5, Sat=6)

BEHAVIOR

A standard constructor with two parameters to set the state of the Month directly.

A method that returns the day that the NEXT month should start on.

A method that prints out the calendar for that month using a loop. That is, you should not just have 28 different sets of println() statements that print the calendar out directly.

I have a really hard time doing some things in java just because I'm just learning the language. Please don't suggest that I go read a java book or something like that because I've done that and this is still just a foreign language to me. ANY help with this would be greatly appreciated, I'm not saying answer it for me by any means, just maybe give me suggestions on how to do it.

Here's what my months class is so far

import java.util.*;
public class Month {
    Scanner sc = new Scanner(System.in);
    public static void Month(int daysPerMonth, int firstDay) {
        daysPerMonth = MONTHS_PER_YEAR
    }
}

And that's just completely wrong. I keep trying different things with the month method like static int, no static, and different things on the inside, but nothing will allow me to NOT get an error which is very frustrating. Sometimes java is very annoying because my professor is awful and doesn't actually teach how to write code at all, just things about how computers work.


Here is an example of a layout of a class, in this case the Month Class

Month <--------------- Class

daysPerMonth <---- State
firstDay <------------- State

Month <-------------- Behaviour
startDay <------------ Behaviour
printCal <------------ Behaviour

So from the definition we need,

  • two states
  • three behaviours (including the constructor)

This is all this class sees and knows about. So you must tell the class what it needs to know.

Currently you have the following

Month <----------- Class

sc <-- State

Month <----------- Behaviour

You made a Scanner object sc, as a state as it not held in any of the behaviours. Let's leave this out for now, so delete this. The states we need are above in the diagram. Let's try adding a state

public int daysPerMonth;

You can try adding the next one. And think of the above (should it be static or not ?)

Now your constructor does not need to be static, it is a modifier but we do not need it right now

A static modifier on a method means that we want to access a method without instantiating an object. But you do not want to do right ? We want to instantiate an object. So let's remove these identifiers. For now, let's say all constructors only need the following

public CLASSNAME , where CLASSNAME is the class that holds this method/constructor. So just remember for now that a constructor initializes an object upon creation and all it needs is to have the same name as the class and be public. So this is how your constructor should look.

public Month(){
}

How about we try adding the parameters they asked for ?

public Month(int numDaysPerMonth, int firstDay){
}

Okay so now we need to set the variables we made earlier on, left side gets assigned the value, right does the assigning

public Month(int daysPerMonth, int firstDay){
    this.daysPerMonth = daysPerMonth;
    // I will let you do the next one
    // Hmm what does the "this" do here?
}

This should get you on the right start for the first line

Month month = new Month(daysPerMonth[i], firstDay);

Now to take a break, look back over this section you should know how to use the following

  • constructors
  • static, final, public identifiers
  • this

On to the next stage, the first behaviour to match the method of this object

month.printMonth();

Things to notice,

  1. This is a method of the month instance, so it should not be a static method
  2. It does not take any parameters, so the behaviour in the class should have no parameters as well
  3. It is not assigned to anything, so the behaviour in the class should return void

Here is what it should look like knowing this information

public void printMonth() {
}

Pretty empty, so it's up to you to fill up inside. Some hints for this method.

Helping variables

 static String [] day = {"Sun","Mon","Tues","Wed","Thurs","Fri","Sat"};
 int nextDay;

Loop
Your loop should start from 1 and end on daysPerMonth

Changing the day
Just add 1 to traverse the helping variable above, you should use a local variable instead of firstDay to keep track of the current day

Resetting the days
There many ways to do this, I just used a conditional that checked if the current checked day reached "Sat" and then reset to "Sun"

Now when you are done the whole loop the last day should be your next day of the next month, yes ! we killed two behaviours with one. Assign the value to nextDay

So your next method just needs to return nextDay and since it needs to be assigned to an int (noted in the Calendar class) we need to match this within out class as well

public int getFirstDayOfNextMonth() {
    return nextDay;
}

And there you go, Java demystified.


Here is something to get you started. In particular notice the 2 parameters to the constructor (as specified). I've also tried to follow the style of your existing Calendar class.

public class Month {

    int numDays, startDay;
    String[] days {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

    public Month(int numDays, int startDay) {
        this.numDays = numDays;
        this.startDay = startDays;
    }

    //There will likely be a quicker way of doing this (and I haven't tested it, so this might not even work. But then again, I shouldn't do everything for you!
    public int getStartDayOfNextMonth() {
        int nextDay = startDay;
        for(int i = 0; i < numDays; i++) {
            if(nextDay + 1 > days.getLength() - 1) nextDay = 0;
            else nextDay++;
        }
        return nextDay;
    }    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜