Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with java program
New Posts  All Forums:Forum Nav:

Help with java program

post #1 of 4
Thread Starter 
i have a program i need to write for my CS class and i have almost everything except for 2 different methods

here is my code i dont have a main method yet because i am just testing it in the interactions before i make the main method
Code:


   public class Month {
   
      private String monthNames;
      private int monthNumber;
      private static String [] MONTHS = {"January", "February", "March",
                            "April", "May", "June", "July",
                            "August", "September", "October",
                            "November", "December" };
                                                         
                                                                 
      public Month(int m) {
      
         if (m < 1 || m > 12) {
            throw new IllegalArgumentException("Illegal month number: " + m);
         }
         monthNumber = m;
         monthNames = MONTHS[monthNumber-1];
      }
   
      public Month(String s) {
         int index = -1;
        //  int m = Integer.parseInt(s);
         
         boolean valid = false;
         for (int i = 0; i < MONTHS.length && !valid; i++) {
            if ( MONTHS[i].equalsIgnoreCase(s)) {
               index = i;
               valid = true;
            }
         }
         if (valid) { 
            monthNames = s;
            monthNumber = index+1;
         } 
         else {
            throw new IllegalArgumentException("Invalid month name: " + monthNames);
         }
      }
      
      public void setMonthNumber(int m) {
      
         monthNumber = m;
      }
      
                        
      public void setMonthName(String name) {
        
         monthNames = name;
      }
      
      public int getMonthNumber() { 
         return monthNumber; }
         
   
         
      public String getMonthName() {
       
         return monthNames; 
      }
   
      public String toString() { 
         return monthNames; }
        
         
   
      public boolean equals(String month) { 
         boolean valid = false;
         if (monthNames.equalsIgnoreCase(month))
         { 
            valid = true;
         }
         else
            valid = false;
      
         return valid; }
         
    
      public Month next() {
      
         int index = -1;
        
         for (int i = 0; i < MONTHS.length;) {
          
            index = i + 1;
             
         
         }
     
         return ;
      
      
   }
i just need help getting the program to return the next month in the array MONTHS and i cant get it to accept an input like this for example "1" as a month number
here is a link to my schools website with the outline for the program if u have any questions
The Prodigy
(16 items)
 
 
Dream Rig Contest
(11 items)
 
CPUMotherboardGraphicsRAM
[AMD] Athlon II x2 255 [Gigabyte] GA-78LMT-SP2 [Onboard] ATI Radeon 3000 [Crucial] Ballistix 8GB (2x4GB) 
Hard DriveHard DriveHard DriveOptical Drive
[Western Digital] Blue 320GB [Maxtor] 320GB soon to be adding 4TB from WD Red [Lite-On] DVD Player/Burner 
CoolingOS
[Stock] [Mircrosoft] Windows 7/ Home server 2011 
  hide details  
Reply
The Prodigy
(16 items)
 
 
Dream Rig Contest
(11 items)
 
CPUMotherboardGraphicsRAM
[AMD] Athlon II x2 255 [Gigabyte] GA-78LMT-SP2 [Onboard] ATI Radeon 3000 [Crucial] Ballistix 8GB (2x4GB) 
Hard DriveHard DriveHard DriveOptical Drive
[Western Digital] Blue 320GB [Maxtor] 320GB soon to be adding 4TB from WD Red [Lite-On] DVD Player/Burner 
CoolingOS
[Stock] [Mircrosoft] Windows 7/ Home server 2011 
  hide details  
Reply
post #2 of 4
Code:
public Month next() {  // return type is of type "Month"
      
         // int index = -1;
         //         
         // for (int i = 0; i < MONTHS.length; ++i) {  // you had an infinite loop before, you need to remember to increment i
         //          
         //     index = i + 1;
         //      
         //  
         //  }

         // I have no idea why you need to iterate in the code above. This instance of Month knows what the 
         // current month is (monthNumber), so all you have to do is return the month that corresponds to monthNumber +1
     
         // Since the return type is of type "Month" you need to construct a new instance to return
         return new Month(monthNumber + 1); 
} // you forgot this closing brace

So in summary, this is all you need:
Code:
public Month next(){
   return new Month(monthNumber + 1);
}
post #3 of 4
this is what i got hope it helps

Code:
public class Month
   {
   
   // Instance Variable
   
      private int monthNumber;
      private static String monthNames[] = {"January", "February", "March", "April", "May", "June",
         "July", "August", "September", "October", "November","December"};
   
   
    //Constructor method for month number
       
      public Month(int m)
      {
      
        
         if (m >= 1 && m <= 12)
         {
            monthNumber = m;
            
         }
         else 
         { 
            throw new IllegalArgumentException("illegal month number: " + m);
         }
        
      }
   
        
      //Constructor method for month NAME
   
      public Month(String s)
      {
         try {
         
         
            int number = Integer.parseInt(s);
         
                
            if (number >= 1 && number <= 12)
            
            {
               monthNumber = number;
            }
            else
            {
               throw new IllegalArgumentException("Illegal month number: " + number);
            }
         }
                
            catch (NumberFormatException e)
            {
               s.equalsIgnoreCase(s);
            
               if (s.equalsIgnoreCase(monthNames[0]))
                  monthNumber=1;
               else if (s.equalsIgnoreCase(monthNames[1]))
                  monthNumber=2;
               else if (s.equalsIgnoreCase(monthNames[2]))
                  monthNumber=3;
               else if (s.equalsIgnoreCase(monthNames[3]))
                  monthNumber=4;
               else if (s.equalsIgnoreCase(monthNames[4]))
                  monthNumber=5;
               else if (s.equalsIgnoreCase(monthNames[5]))
                  monthNumber=6;
               else if (s.equalsIgnoreCase(monthNames[6]))
                  monthNumber=7;
               else if (s.equalsIgnoreCase(monthNames[7]))
                  monthNumber=8;
               else if (s.equalsIgnoreCase(monthNames[8]))
                  monthNumber=9;
               else if (s.equalsIgnoreCase(monthNames[9]))
                  monthNumber=10;
               else if (s.equalsIgnoreCase(monthNames[10]))
                  monthNumber=11;
               else if (s.equalsIgnoreCase(monthNames[11]))
                  monthNumber=12;
               else 
                  throw new IllegalArgumentException("Invalid month name: " + s);
            
            }
      }
   // Set month number method
      public void setMonthNumber(int m)
      {
      
         if (m <= 1 || m >= 12)
         {
            throw new IllegalArgumentException("Invalid month number: " + m);
         }
         else monthNumber = m;
      }
   // Set month name method
      public void setMonthName(String name)
      {
       
         if (name.equalsIgnoreCase(monthNames[0]))
            monthNumber=1;
         else if (name.equalsIgnoreCase(monthNames[1]))
            monthNumber=2;
         else if (name.equalsIgnoreCase(monthNames[2]))
            monthNumber=3;
         else if (name.equalsIgnoreCase(monthNames[3]))
            monthNumber=4;
         else if (name.equalsIgnoreCase(monthNames[4]))
            monthNumber=5;
         else if (name.equalsIgnoreCase(monthNames[5]))
            monthNumber=6;
         else if (name.equalsIgnoreCase(monthNames[6]))
            monthNumber=7;
         else if (name.equalsIgnoreCase(monthNames[7]))
            monthNumber=8;
         else if (name.equalsIgnoreCase(monthNames[8]))
            monthNumber=9;
         else if (name.equalsIgnoreCase(monthNames[9]))
            monthNumber=10;
         else if (name.equalsIgnoreCase(monthNames[10]))
            monthNumber=11;
         else if (name.equalsIgnoreCase(monthNames[11]))
            monthNumber=12;
         else
            throw new IllegalArgumentException("Invalid month name: " + name);
      
      }
   //Get month number method
   
      public int getMonthNumber()
      {
      
         return monthNumber;
      }
   
   //Get month name method
      public String getMonthName()
      {
       
         return monthNames[monthNumber-1];
      }
        
   //To string method
      public String toString()
      {
         return monthNames[monthNumber-1];
      }
        
   // Equals method
   
      public boolean equals(Month m)
      {
         if (m.getMonthNumber()==monthNumber)
         {
            return true;
         }
         else 
         {
            return false;
         }
      }
        
      //Next method
      public Month next() 
      {
         if(monthNumber!=12)
         {
            return new Month(monthNumber +1);
         }
         else 
         {
            return new Month(1);
         }
      
      }
        
        //Previous method
      public Month previous() 
      {
         if(monthNumber!=1)
         {
            return new Month(monthNumber -1);
         }
         else 
         {
            return new Month(12);
         }
      
      }
   }

thumb.gif
post #4 of 4
Thread Starter 
thanks man it helps alot smile.gifsmile.gifsmile.gifsmile.gifsmile.gifsmile.gifsmile.gifsmile.gifsmile.gif
The Prodigy
(16 items)
 
 
Dream Rig Contest
(11 items)
 
CPUMotherboardGraphicsRAM
[AMD] Athlon II x2 255 [Gigabyte] GA-78LMT-SP2 [Onboard] ATI Radeon 3000 [Crucial] Ballistix 8GB (2x4GB) 
Hard DriveHard DriveHard DriveOptical Drive
[Western Digital] Blue 320GB [Maxtor] 320GB soon to be adding 4TB from WD Red [Lite-On] DVD Player/Burner 
CoolingOS
[Stock] [Mircrosoft] Windows 7/ Home server 2011 
  hide details  
Reply
The Prodigy
(16 items)
 
 
Dream Rig Contest
(11 items)
 
CPUMotherboardGraphicsRAM
[AMD] Athlon II x2 255 [Gigabyte] GA-78LMT-SP2 [Onboard] ATI Radeon 3000 [Crucial] Ballistix 8GB (2x4GB) 
Hard DriveHard DriveHard DriveOptical Drive
[Western Digital] Blue 320GB [Maxtor] 320GB soon to be adding 4TB from WD Red [Lite-On] DVD Player/Burner 
CoolingOS
[Stock] [Mircrosoft] Windows 7/ Home server 2011 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with java program