Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Making a calendar program?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jsnnyj




PostPosted: Wed Mar 15, 2006 12:21 pm   Post subject: Making a calendar program?

Hi, right now I need to make a program that you have to input the day that the first date of a month falls upon ( for example, Tuesday, Friday,etc) and then outputs the days of the month and the corresponding days.

I thought this would be easy but now I'm stuck. I have a date[] array of 31 elements, but how can I don't quite get how to assign the days for the dates, because it doesn't have to start from 1 to 7.

Any help is greatly appreciated, thx!
Sponsor
Sponsor
Sponsor
sponsor
Justin_




PostPosted: Wed Mar 15, 2006 2:01 pm   Post subject: (No subject)

Does this program have to address there are varying amounts of days in each month?

Try making a class to include information for each day, like name of day and number, then make an array of them. Then find an efficient way to initialize them with all the information. Then design a method you can call in a loop for all the objects in the array that will say what their information is.
jsnnyj




PostPosted: Wed Mar 15, 2006 2:30 pm   Post subject: (No subject)

Justin_ wrote:
Does this program have to address there are varying amounts of days in each month?

Try making a class to include information for each day, like name of day and number, then make an array of them. Then find an efficient way to initialize them with all the information. Then design a method you can call in a loop for all the objects in the array that will say what their information is.


No, it should assume that the month has 31 days. The parts I don't get is how to initialize it and output the information.
Justin_




PostPosted: Wed Mar 15, 2006 2:45 pm   Post subject: (No subject)

if it is always 31 days than you could make a day array that holds all the days of the week. (monday, tues etc..) then make an array of 31 Days. Day being the name of your class. Then make its day and number private, make a method called editDay(string day) and make the method set day to whatever day is. Same sort of thing for number.

Then to initialize it, in your main function you'd write
Java:

for (int i ; i <= 31 ; i++)
{
     Day[i].editDay(day[i % 7]);
}


the 1 % 7 incase you don't know what that is, dont' worry about it, just make sure you day array hold the seven days of the week [monday, tuesday etc..] and you can use i % 7 to keep the bounds within that array even though i gets much larger.

hint: since the user inputs the first day, you'll have to make the day array begin initializing on the day the user inputs.
Justin_




PostPosted: Wed Mar 15, 2006 4:06 pm   Post subject: (No subject)

Here, this should give you a good idea of what I am talking about. Remember this is just one way to do it, and I hope this is what you wanted. If not, than I misunderstood the guidelines.

Java:

import java.io.*;

public class Test
{
        public static void main(String[] args)
        {
                try
                {
               
                        String[] day = {"Mon","Tues","Wed","Thur","Fri","Sat","Sun"};
                        Day[] month = new Day[31];
                        String start_day;
                        int day_in_array = 0;
                        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
                       
                        System.out.println("What is the starting day of the month? (Mon,Tues,Wed, Thur, Fri, Sat, Sun");
                        start_day = input.readLine();
                        for (int i = 0 ; i <= 6 ; i++)
                        {
                                if (start_day.equals(day[i]))
                                {
                                        day_in_array = i;
                                        break;
                                }   
                        }
                        for (int i = 0; i <= 30 ; i++)
                        {
                                month[i] = new Day();
                        }
                        for (int i = 0; i <= 30; i++)
                        {
                                month[i].editDay(day[(i + day_in_array) % 7], i);
                                month[i].sayInfo();
                        }
               
                }
                catch (Exception e)
                {
                        System.out.println(e);
                }
               
        //      System.out.println("fuck");
        }
}
class Day
{
        private int number;
        private String day;
        public void Day()
        {
                day = "Mon";
                number = 1;          
        }
        public void editDay(String a_day,int i)
        {
                day = a_day;
                number = i + 1;
        }
        public void sayInfo()
        {
                System.out.println(day + " the " + number + "th");
        }
}   
jsnnyj




PostPosted: Wed Mar 15, 2006 7:57 pm   Post subject: (No subject)

thank you for the help Laughing !
Martin




PostPosted: Thu Mar 16, 2006 3:40 am   Post subject: (No subject)

Just for fun, in Ruby

Ruby:
#Everything except wtd's code is public domain. Have fun.

#wtd's enum code
def enum(*args)
    args.each_with_index do |arg, index|
        Object.const_set(arg,index)
    end
end

enum :SUNDAY,:MONDAY,:TUESDAY,:WEDNESDAY,:THURSDAY,:FRIDAY,:SATURDAY

#Draw a calandar of num_days days starting on day start_day
def draw_cal (num_days, start_day)
    puts "S  M  T  W  T  F  S"
    line = "   "*start_day
    index = start_day
    (1..num_days).each do |day|
        if index == 7
            puts line
            line=""
            index=0
        end
       
        line += day.to_s + " "*(3-day.to_s.length)
        index += 1

    end
    puts line

end

puts "   MARCH 2006"
draw_cal(31,WEDNESDAY)


The enum simply defines SUNDAY as 0, MONDAY as 1, TUESDAY as 2, etc.

And with colour!
Ruby:
#Everything except wtd's code is public domain. Have fun.

#wtd's enum code
def enum(*args)
    args.each_with_index do |arg, index|
        Object.const_set(arg,index)
    end
end

enum :SUNDAY,:MONDAY,:TUESDAY,:WEDNESDAY,:THURSDAY,:FRIDAY,:SATURDAY

#Draw a calandar of num_days days starting on day start_day
def draw_cal (num_days, start_day, holidays)
    puts "S  M  T  W  T  F  S"
    line = "   "*start_day
    index = start_day
    (1..num_days).each do |day|
        if index == 7
            puts line
            line="\e[32m"
            index=0
        end
        line += "\e[33m" if holidays.include? day
        line += day.to_s + " "*(3-day.to_s.length)
        line += "\e[0m" if index==0 or holidays.include? day

        index += 1

    end
    puts line

end

puts "\e[33m   MARCH 2006\e[0m"
draw_cal(31,WEDNESDAY,[4, 15, 21])
wtd




PostPosted: Thu Mar 16, 2006 12:04 pm   Post subject: (No subject)

Martin, this:

code:
    (1..num_days).each do |day|
         if index == 7
             puts line
             line=""
             index=0
         end


Is better written as:

code:
    1.upto num_days do |day|
         if index == 7
             puts line
             line = ""
             index = 0
         end
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Mar 16, 2006 12:41 pm   Post subject: (No subject)

Ruby:
#Everything except wtd's code is public domain. Have fun.
 
#wtd's enum code
def enum(*args)
   args.each_with_index do |arg, index|
      Object.const_set(arg,index)
   end
end
 
enum :SUNDAY,:MONDAY,:TUESDAY,:WEDNESDAY,:THURSDAY,:FRIDAY,:SATURDAY
 
#Draw a calandar of num_days days starting on day start_day
class Calendar
   attr_accessor :month, :year, :num_days, :start_day

   def initialize(month, year, num_days, start_day)
      @month, @year = month, year
      @start_day, @num_days = start_day, num_days
      @buffer = "#{month} #{year}".center(20)
      @buffer += "\nS  M  T  W  T  F  S"
      @buffer += "\n#{"   " * start_day}"
      1.upto num_days do |day|
         @buffer += "#{day.to_s.ljust(3)}#{"\n" if (day + start_day) % 7 == 0}"
      end
   end 
 
   def to_s
      @buffer
   end
end
 
puts Calendar.new("March", 2006, 31, WEDNESDAY)
McKenzie




PostPosted: Thu Mar 16, 2006 11:55 pm   Post subject: (No subject)

I hate to point out the obvious, but "Java Help" should result in Java code.
wtd




PostPosted: Thu Mar 16, 2006 11:58 pm   Post subject: (No subject)

Talk to Martin. Smile

Once it was said, though, I thought it worthwhile to have the best code possible available.

Ruby's pretty straightforward, and expressive. I don't think it'd be too hard to take that code and translate it to Java.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: