5 Replies - 401 Views - Last Post: 28 March 2010 - 10:15 PM Rate Topic: -----

Topic Sponsor:

#1 ElCam  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 39
  • Joined: 16-April 09

Class Design(Arrays in java)

Posted 28 March 2010 - 08:35 PM

My project is to design a "Month" Class and I don't even know how to begin.

My constructor should be a no args constructor..what is that? My constructor should also accept an integer and it should set it to its corresponding month(1-12).
It should also accept the name of the month and make its corresponding match(Jan-Dec).
How do I begin? My main question is how do I input my months so they match with the integers??

public class GetMonthName
{
 public static void main(String[] args)
  {
        String[] monthName = {"January", "February",
            "March", "April", "May", "June", "July",
            "August", "September", "October", "November",
            "December"};
 
        Calendar cal = Calendar.getInstance();
        String month = monthName[cal.get(Calendar.MONTH)];
 
        System.out.println("Month name: " + month);   
  }
}






Where do I add the integers?? Or is this even right?
Thanks you guys! You guys are amazing programmers and I really appreciate your help!!

Is This A Good Question/Topic? 0
  • +

Replies To: Class Design(Arrays in java)

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7493
  • View blog
  • Posts: 28,833
  • Joined: 27-December 08

Re: Class Design(Arrays in java)

Posted 28 March 2010 - 09:02 PM

You are on the right track using your String[] to hold the month names, but that is about it. A class should be a blueprint for an Object, and therefore does not need a main() method. Remember- the main() method should be used to run your program; in this case, you probably want to use it to demonstrate the functionality of your class.

Quote

My constructor should be a no args constructor..what is that?

A no-args constructor is a constructor method that does not accept any parameters. It sounds like from your instructions you will have overloaded constructors- a no-args and a one-arg constructor.

As for matching the month name to an int, I'll give you a hint with a method, here:
//given monthName your array of Strings that you defined in your main() method 
//@param month- an int from 1-12 representing the month
//@return- a String representation of the month
public String getMonthName(int month){
   return monthName[month-1];
}



You might also want to check out Locke's tutorial on class design.
Link: http://www.dreaminco...showtopic=59043
Was This Post Helpful? 0
  • +
  • -

#3 ElCam  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 39
  • Joined: 16-April 09

Re: Class Design(Arrays in java)

Posted 28 March 2010 - 09:27 PM

I am still very confused as to how the last line of code relates to the months, I am sorry I am new to java/programming. And how would a no-args constructor look like in code? Is my code ok?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7493
  • View blog
  • Posts: 28,833
  • Joined: 27-December 08

Re: Class Design(Arrays in java)

Posted 28 March 2010 - 09:40 PM

I'm sorry to say, but your code does not meet requirements for your assignment, as you should not be using a main() method to do anything more than create an instance of your class, and demonstrate that Object's functionality.

Are you familiar with what a constructor method is? Basically, it is used to create an instance of your class, and is defined using only access modifiers (public, private, protected, or none of these) and named the same as the class. A no-args constructor is defined without any parameters, as I demonstrate below.
class MyClass{
 
   //this is a no-args constructors
   //as it takes no arguments, or 
   //accepts no parameters
    public MyClass(){

    } 
}



As for your confusion, did you check out Locke's tutorial? It is really good for demonstrating class design.
Was This Post Helpful? 0
  • +
  • -

#5 ElCam  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 39
  • Joined: 16-April 09

Re: Class Design(Arrays in java)

Posted 28 March 2010 - 09:49 PM

Thank you! See as you said in the above post...I didn't know that you couldn't use the main() function in an instance of a class. BUT I am still confused, my class should accept both strings of months and integers of both right? Don't I need a scanner method? DOn't I need arrays? Or would that all go in my main? And if it does then what happens in the class? Thank you for being patient with me. I have so many questions lol.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7493
  • View blog
  • Posts: 28,833
  • Joined: 27-December 08

Re: Class Design(Arrays in java)

Posted 28 March 2010 - 10:15 PM

Getting input is not something your Month class should be responsible for. It should have parameters in appropriate methods to accept values for its attributes/instance variables. However, getting input from the user should be left to another class or the main() method.

Quote

See as you said in the above post...I didn't know that you couldn't use the main() function in an instance of a class.

As the main() method is static, it cannot interact with the instance variables and non-static methods for an Object.

Quote

And if it does then what happens in the class?

Basically, your class should model a single month. I'll get you started with a skeleton, and leave you to fill in the gaps:
//a class to model a Month
public class Month{
   private String name;
   private int index; //ie., 1 for Jan, 12 for Dec

   //fill in the ... here for the other months I omitted
   private static final MONTH_NAMES = {"January","February","March",..."December"};

   //this is your no-args constructor
   //since it does not accept params
   //you should use it to initialize
   //month and index to appropriate default values
   public Month(){
         //code
   }

   //this constructor accepts an int param
   //and assigns name the corresponding month name
   //think back to earlier in this thread where I 
   //showed you how to get a value from the array
   //based on the passed index
   public Month(int index){
 
        //since the param index has the same name
        //as your instance variable index
        //you have to use this keyword to 
        //assign param to instance variable
        this.index = index;

        //rest of implementation for you to write
   }

   //accessors and mutators, also called getters and setters
   public void setMonth(int index){
        //code
   }

   public String getMonth(){
         //code
   }
}



Notice how in your class, I do not handle getting user input or outputting any data. Rather, I have my accessors and mutators, along with my constructors. It is up to some other class or the main() method to handle this I/O.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1