Please Help Me, Basic Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Minor
    New Member
    • Apr 2007
    • 8

    Please Help Me, Basic Java

    I am in a beginner java class and am having trouble getting a program to work. We are asked to create a calendar type application to display a calendar. It uses a application and a worker class I am stuck and have been working on it all day. Here is the code i have, Please help if you can i have been working on this for way too long.

    Code:
     public class Calendar { 
     
    	String tempStr;
    	public int day;
    	public int year;
     
     
    	public Calendar(int nday, int nyear, int fmonth){
    			day = nday;
    			year = nyear;
    	}
    	public String displayJanuary(){
     
    		String display = "";
    		display = "January of year " + year +"\n" +
    				 "S M T W T F S"+"\n" +
    				 space();
     
    		for(int i=1;i<32;i++){
    			display += day(i);
    		}
     
     
    		return display;
    	}
     
     
     
    	private String space(){
    		String space = "";
    		for(int i=0; i<day-1;i++){
    			space += " ";
    		}
     
     
    		return space;
    	}
     
    	private String day(int currentday){
    		String printday = ""+currentday;
     
    		if(currentday<10){
    			printday+=" ";
    		}
    		else{
    			printday+=" ";
    		}
     
    		boolean isWeekend = weekend(currentday);
    		if(isWeekend==true){
    			printday+="\n";
    		}
     
    		return printday;
    	}
     
    	private boolean weekend(int currentday){
    		int result = (day+currentday-1)%7;
    		if(result==0){
    			return true;
    		}
    		else{
    			return false;
    		}
    	}
    	public String showFebruary(){
     
    		String show = "";
    		show = "February of year " + year +"\n" +
    				 "S M T W T F S"+"\n" +
    				 space();
     
    		for(int j=1;j<day;j++){
    			show += day(j);
    		}
     
     
    		return show;
    	}
    	private boolean isleap(int leapyear){
    		int isleap = (year%4);
    		if (isleap==0){
    			return true;
    		}
    		else{
    			return false;
    		}
    	}
    	private int dayinfeb(int days){
    		if (true) {
    			return 29;
    		}
    		else{
    			return 28;
    		}
    	}
    	private String area(){
    		String area="";
    		for(int j=0; j<day-1;j++){
    			area +=" ";
    		}
    		return area;
    	}
    	private String today(int currentday){
    		String printtoday = ""+currentday;
     
    		if(currentday<10){
    			printtoday+=" ";
    		}
    		else{
    			printtoday+=" ";
    		}
     
    		boolean isWeekend = weekend(currentday);
    		if(isWeekend==true){
    			printtoday+="\n";
    		}
     
    		return printtoday;
    	}
    	private boolean endweek(int currentday){
    		int result = (day+currentday-1)%7;
    		if(result==0){
    			return true;
    		}
    		else{
    			return false;
    		}
    	}
    }
    Below is the worker.

    Code:
     import javax.swing.JOptionPane; 
     
    public class TestCalendar {
     
    public static void main(String[] args) {
     
    		String tempStr = " ";
    		int yearNum;
    		int dayNum;
     
    tempStr = JOptionPane.showInputDialog("Enter the year");
    yearNum = Integer.parseInt(tempStr);
    tempStr = JOptionPane.showInputDialog("Enter the day of the week for January 1");
    dayNum = Integer.parseInt(tempStr);
    tempStr = JOptionPane.showInputDialog("Enter the year");
    yearNum = Integer.parseInt(tempStr);
    tempStr = JOptionPane.showInputDialog("Enter the day of the week for February 1");
    dayNum = Integer.parseInt(tempStr);
     
     
    	}
     
    }
  • BSCode266
    New Member
    • Jan 2007
    • 38

    #2
    It would be nice if you would tell us what the problem is.

    BSCode266

    Comment

    • Minor
      New Member
      • Apr 2007
      • 8

      #3
      [QUOTE=BSCode266]It would be nice if you would tell us what the problem is.

      BSCode266[/QUOTE

      The months are messed up in this paste, but you get the idea. They are supposed to display like regular calendar months. These are my instructions.


      This assignment is designed to solidify the concepts learned in the classroom about loops in Java. In this assignment, you are going to use for or while loop statements to implement the repetitions.

      The problem statement:
      Write a program to generate a calendar for a year. The program should accept the year and the day of the week for January 1 of that year (1 = Sunday, 7 = Saturday) by using JOptionPane.sho wInputDialog. Remember, February has 29 days if the year is divisible by 4. You are required to output two months (January and February) for each year. You are going to have two runs. The following is the inputs and outputs (use the following as your test data).

      First run:
      inputs:
      year = 1999
      The day of the week for January 1 = 6

      Outputs:
      January of year 1999
      S M T W T F S
      1 2
      3 4 5 6 7 8 9
      10 11 12 13 14 15 16
      17 18 19 20 21 22 23
      24 25 26 27 28 29 30
      31

      February of year 1999
      S M T W T F S
      1 2 3 4 5 6
      7 8 9 10 11 12 13
      14 15 16 17 18 19 20
      21 22 23 24 25 26 27
      28

      Second run:
      inputs:
      year = 2000
      The day of the week for January 1 = 7

      Outputs:
      January of year 2000
      S M T W T F S
      1
      2 3 4 5 6 7 8
      9 10 11 12 13 14 15
      16 17 18 19 20 21 22
      23 24 25 26 27 28 29
      30 31

      February of year 2000
      S M T W T F S
      1 2 3 4 5
      6 7 8 9 10 11 12
      13 14 15 16 17 18 19
      20 21 22 23 24 25 26
      27 28 29


      The design:
      Use two classes to do the job. The two classes are the worker class and the application class. The worker class is problem specific and the application class is generic. The application class will take care of the setup overhead and manage the interface with users. Finally, the application class will create an instance of the worker class, initialize it and run it. The relationship between the worker class and the application class is an association relationship. You should name the worker class “Calendar” and the application class “TestCalendar”.

      Comment

      • Minor
        New Member
        • Apr 2007
        • 8

        #4
        Update:
        This code works much better but i am having trouble with the application class. both are pasted below.


        Code:
        /*
         * Calendar.java
         * Class that can generate a calendar.
         */
        
        
        
         public class Calendar {
        
        
        
        
           private int year;
        
           private int day;
        
        
        
        
           public  Calendar(int ayear, int aday){
        	   year = ayear;
        	   day = aday;
           }
        
           public void date(){
        
          String week = "";
          String blank =" ";
        
        
        
        
            System.out.println("Jannuary of the "+ year );
            System.out.println("S  M  T  W  T  F  S");
        
        
          for( int j = 1; j<day; j++)
                week = week + blank + blank + blank;
        
        
           for( int date = 1; date <32; date++) {
        
        		if((date == 1) || (date == 2) || (date == 3) || (date == 4) ||
        			(date == 5) || (date == 6) || (date == 7) || (date == 8) || (date == 9))
        			week = week + date + blank + blank;
        		else
        			week = week + date + blank;
        
        
        		if (week.length() == 21) {
               		System.out.println(week);
               		week = "";
        		}
        
        	}
        
            System.out.println(week);
        
            int len = week.length(); // 0, 3. 6. 9. 12. 15. 18. 21
            if(len == 0) day = 1; // last day of January was a Saturay --- February 1st is a Sunday
        	else if (len == 3) day = 2;
        	else if (len == 6) day = 3;
        	else if (len==9) day = 4;
        	else if(len== 12) day = 5;
        	else if(len== 15) day = 6;
        	else if (len== 18) day = 7;
        
        
        	week = "";
        
        
        
        	System.out.println("February of the "+ year );
        	System.out.println("S  M  T  W  T  F  S");
        
        
        	  for( int j = 1; j<day; j++)
        	        week = week + blank + blank + blank;
        
        
                   int daysInFebruary;
        
                   boolean leapYear = false;
        
                   // if leap year,then daysInFebruary is 29
        
                   if (year%4==0)
                       leapYear = true;
        
        
                   if (leapYear == true) { daysInFebruary = 29; }
                   else { daysInFebruary = 28; }
        
        		   for( int date = 1; date <= daysInFebruary; date++) {
        
        			if((date >= 1) && (date <= 9))
        				week = week + date + blank + blank;
        			else
        				week = week + date + blank;
        
        
        			if (week.length() == 21) {
        	       		System.out.println(week);
        	       		week = "";
        			}
        
        		}
        	System.out.println(week);
           }
        }

        AND
        Code:
        import javax.swing.JOptionPane;
        
        public class TestCalendar {
        
        	public static void main(String[] args) {
        		 String tempStr = " ";
        		 String yearNum;
        		 String dayNum;
        
        
        			tempStr = JOptionPane.showInputDialog("Enter the year");
        			yearNum = Integer.parseInt(tempStr);
        			tempStr = JOptionPane.showInputDialog("Enter the day of the week for January 1");
        			dayNum  = Integer.parseInt(tempStr);
        
        
        Calendar calendar1 = new Calendar(yearNum,dayNum);
        
        
        System.out.println(calendar1.toString());
        
        	}
        
        }
        Probably a simple fix at this point.
        Last edited by JosAH; Apr 13 '07, 07:29 AM. Reason: added [code] ... [/code] tags

        Comment

        • Minor
          New Member
          • Apr 2007
          • 8

          #5
          I have successfully fixed my errors and the program now works. No further response is necessary. Thanks to all who looked at my code.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Minor
            I have successfully fixed my errors and the program now works. No further response is necessary. Thanks to all who looked at my code.
            Well, one more response: great, that's the attitude; you have solved the thing
            all by yourself. I'm sure you've learned quite a bit from it. Take care.

            kind regards,

            Jos

            Comment

            • BSCode266
              New Member
              • Jan 2007
              • 38

              #7
              I am glad that you worked it out all by yourself. And sorry if i just looked like a smart-@ss. I truly wanted to help you out, just couldnt find the thread.

              BSCode266.

              Comment

              • LordHungwell
                New Member
                • Jul 2007
                • 5

                #8
                Can anyone tell me how this code was fixed? I am a newbie myself and am trying to learn code by studying mistakes.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by LordHungwell
                  Can anyone tell me how this code was fixed? I am a newbie myself and am trying to learn code by studying mistakes.
                  Which part do you need help with most?
                  I suggest you try the code on your machine and try to correct the mistakes. Then you can post if you get stuck with some of the errors so we know where to start.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by LordHungwell
                    Can anyone tell me how this code was fixed? I am a newbie myself and am trying to learn code by studying mistakes.
                    Well, the OP didn't tell us but I would've taken the lazy path and used a
                    GregorianCalend ar class for all the nitty gritty hard work.

                    In the Java Articles section (see the blue menu bar on top of this page), there's
                    an 'index' article that contains links to the entire API documentation; download
                    it and keep it as a bookmark/favourite in your browser. You'll need it more than
                    you realize now ;-)

                    kind regards,

                    Jos

                    Comment

                    • LordHungwell
                      New Member
                      • Jul 2007
                      • 5

                      #11
                      Well I was just browsing and did put the code into my machine. The application class is the one that won't compile so I was working on that and trying to make it work. Because of no luck with trying, I thought I would ask if anyone has or knows how to fix that piece of code to make his calendar project work. I am not looking for another calendar lol.
                      I think there is a problem with the object creation according to my editor, but not sure.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by LordHungwell
                        Well I was just browsing and did put the code into my machine. The application class is the one that won't compile so I was working on that and trying to make it work. Because of no luck with trying, I thought I would ask if anyone has or knows how to fix that piece of code to make his calendar project work. I am not looking for another calendar lol.
                        I think there is a problem with the object creation according to my editor, but not sure.
                        Well the GregorianCalend ar really is best one around for most purposes, believe me it's worth the click.
                        If you want to get the one you have to work, just post it here (under code tags) together with the error messages that you are getting.

                        Comment

                        • LordHungwell
                          New Member
                          • Jul 2007
                          • 5

                          #13
                          Ok when I compile the application Class listed below and previously posted above, I get the error messages below. I also re-posted the worker class below that. The worker class appears to compile with no problems.

                          Application Class


                          [CODE=java] import javax.swing.JOp tionPane;
                          public class TestCalendar {
                          public static void main(String[] args) {

                          String tempStr = " ";
                          String yearNum;
                          String dayNum;

                          tempStr = JOptionPane.sho wInputDialog("E nter the year");
                          yearNum = Integer.parseIn t(tempStr);
                          tempStr = JOptionPane.sho wInputDialog("E nter the day of the week for January 1");
                          dayNum = Integer.parseIn t(tempStr);

                          Calendar calendar1 = new Calendar(yearNu m,dayNum);
                          System.out.prin tln(calendar1.t oString());
                          }
                          }

                          [/CODE]

                          error messages


                          Code:
                           TestCalendar.java:24: incompatible types
                          found   : int
                          required: java.lang.String
                          dayNum  = Integer.parseInt(tempStr);
                                                    ^
                          TestCalendar.java:26: cannot find symbol
                          symbol  : constructor Calendar(java.lang.String,java.lang.String)
                          location: class Calendar
                          Calendar calendar1 = new Calendar(yearNum,dayNum);
                                               ^
                          3 errors
                          1 error(s), 0 warning(s)




                          worker class in same folder follows:

                          [CODE=java] /*

                          * Calendar.java

                          * Class that can generate a calendar.

                          */
                          public class Calendar {

                          private int year;
                          private int day;


                          public Calendar(int ayear, int aday){
                          year = ayear;
                          day = aday;
                          }


                          public void date(){
                          String week = "";
                          String blank =" ";

                          System.out.prin tln("January of the "+ year );
                          System.out.prin tln("S M T W T F S");


                          for( int j = 1; j<day; j++)
                          week = week + blank + blank + blank;

                          for( int date = 1; date <32; date++) {


                          if((date == 1) || (date == 2) || (date == 3) || (date == 4) ||
                          (date == 5) || (date == 6) || (date == 7) || (date == 8) || (date == 9))

                          week = week + date + blank + blank;

                          else
                          week = week + date + blank;

                          if (week.length() == 21) {

                          System.out.prin tln(week);

                          week = "";

                          }

                          }

                          System.out.prin tln(week);



                          int len = week.length(); // 0, 3. 6. 9. 12. 15. 18. 21

                          if(len == 0) day = 1; // last day of January was a Saturay --- February 1st is a Sunday

                          else if (len == 3) day = 2;

                          else if (len == 6) day = 3;

                          else if (len==9) day = 4;

                          else if(len== 12) day = 5;

                          else if(len== 15) day = 6;

                          else if (len== 18) day = 7;


                          week = "";

                          System.out.prin tln("February of the "+ year );

                          System.out.prin tln("S M T W T F S");


                          for( int j = 1; j<day; j++)

                          week = week + blank + blank + blank;

                          int daysInFebruary;
                          boolean leapYear = false;

                          // if leap year,then daysInFebruary is 29
                          if (year%4==0)
                          leapYear = true;
                          if (leapYear == true) { daysInFebruary = 29; }
                          else { daysInFebruary = 28; }


                          for( int date = 1; date <= daysInFebruary; date++) {
                          if((date >= 1) && (date <= 9))
                          week = week + date + blank + blank;
                          else
                          week = week + date + blank;


                          if (week.length() == 21) {

                          System.out.prin tln(week);

                          week = "";

                          }
                          }

                          System.out.prin tln(week);
                          }

                          }[/CODE]
                          Last edited by r035198x; Jul 31 '07, 05:14 PM. Reason: added the missing code tags

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            You forgot those code tags that I talked about. Please don't forget them next time.
                            Now the trick to removing errors is to read the error messages. They tell you exactly (in most cases) what to do to get your code running.

                            P.S Have you downloaded the API docs and Sun's tutorial yet?

                            Comment

                            • LordHungwell
                              New Member
                              • Jul 2007
                              • 5

                              #15
                              Ok, sorry I am really new to a lot of this and don't really know what you mean by posting in code tags. I also don't understand the errors so that is why I am asking. If no one knows how to fix it no big deal. I am just trying to learn something. Thanks for all the help.

                              Comment

                              Working...