Results 1 to 5 of 5
  1. #1
    yogesara is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Question im a new to java programming

    i have a problem to settle this question below cos im new to java just bening to learn. can any one help me in solve this problem.

    Define a class called month. Your class will have one attribute of type int to represent a month (1 for January, 2 for February, and so forth). Include all the following methods in this class:-

    a) a contractor to set month using the first three letter in the name of the
    month as the three argument(‘J’,’A’,’N’ for January, ‘F’,’E’,’B’ for February
    and so forth)

    b) a constructor to set the month using an integer as an argument (1 for
    January, 2 for February, and so forth)

    c) a default constructor

    d) an input function that reads the month as a integer

    e) an input function that reads the month as the first three letters in the
    name of the month

    f) an output function that outputs the month as an integer

    g) an output function that outputs that the month as the first three letter in
    the name of the month

    h) and a member function that returns the next month as a value of type
    Month.

    Test your class in a new program which has the main ( ) method


    i dont knw how to define them

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The instructions are pretty much self-explanatory. I think the best way to help you learn is to have you post your best attempt at a solution and have us work with that. This way we'll see where you need help. If posting code here, please look at my signature below so you can use code tags.

    Much luck!

  3. #3
    yogesara is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Question im beginner for java

    my coding for the question tht i pass it just now.

    Java Code:
    import java.io.*;
    import java.text.*;
    import java.util.*;
    import java.lang.*;
    
    
    public class Month
    {
    private int _value;
    
    
    public Month(String value)
    {
    SetValue(value);
    }
    
    
    public Month(int value)
    {
    
    SetValue(value);
    }
    
    
    public Month()
    {
    _value = 1;
    }
    
    
    public void SetValue(int value)
    {
    if (value < 1 || value > 12)
    throw new Exception("Value must be between 1-12.");
    
    _value = value;
    }
    
    
    
    
    
    
    public void SetValue(String value)
    {
    
    
    if (value == "JAN")
    _value = 1;
    else if (value == "FEB")
    _value = 2;
    else if (value == "MAC")
    _value = 3;
    else if (value == "APR")
    _value = 4;
    else if (value == "MAY")
    _value = 5;
    else if (value == "JUN")
    _value = 6;
    else if (value == "JUL")
    _value = 7;
    else if (value == "AUG")
    _value = 8;
    else if (value == "SEP")
    _value = 9;
    else if (value == "OCT")
    _value = 10;
    else if (value == "NOV")
    _value = 11;
    else if (value == "DEC")
    _value = 12;
    else
    throw new Exception("Value " + value + " is not a valid month.");
    }
    
    
    public void DisplayAsInt()
    {
    
    Console.Write(_value);
    
    
    }
    
    
    private static String[] _names = new String[] {
    "JAN", "FEB", "MAC", "APR", "MAY", "JUN",
    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
    };
    
    public void DisplayAsString()
    {
    
    
    Console.Write(_names[_value-1]);
    }
    
    
    public Month Next()
    {
    if (_value == 12)
    return new Month(1);
    else
    return new Month(_value+1);
    }
    
    */
    public Month(int mon)
    {
    intMonth = mon;
    }
    public String name()
    {
    switch (intMonth)
    {
    case 1:
    strMonth = "January";
    break;
    case 2:
    strMonth = "February";
    break;
    case 3:
    strMonth = "March";
    break;
    case 4:
    strMonth = "April";
    break;
    case 5:
    strMonth = "May";
    break;
    case 6:
    strMonth = "June";
    break;
    case 7:
    strMonth = "July";
    break;
    case 8:
    strMonth = "August";
    break;
    case 9:
    strMonth = "September";
    break;
    case 10:
    strMonth = "October";
    break;
    case 11:
    strMonth = "November";
    break;
    case 12:
    strMonth = "December";
    break;
    default:
    strMonth = "Invalid";
    break;
    }
    return strMonth;
    
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 03-20-2010 at 08:58 PM. Reason: Moderator Edit: Code tags added

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Please use CODE tags when posting code, and use proper indentation. Use the Preview Post button before submitting.

    A couple of style points:

    1. Start your method names with a lower-case letter, and capitalize subsequent words. So, displayAsString, rather than DisplayAsString, for example. (It's the other way around for class names -- your class name is fine.)

    2. What's with the leading underscore on your variable names? It's legal, but it's not common practice, and it will tend to confuse people who look at your code. They will be thinking "what is special about this variable that he's calling it _value instead of value?" Better to call it "monthNumber" or something similarly descriptive.

    3. You don't need "new String[]" for initializing your _names array. And since _names becomes an instance variable you should keep it with your other instance variable(s) at the top or the bottom of the class.

    Don't use '==' to compare Strings. You've probably been taught that already, but forgot?

    Since you have your _names array, why not use it in your setValue(String) method? Use a similar array for your name() method.

    All in all, you're not doing so badly. Choose better names for methods and variables. Implement and test one thing at a time. Ask specific questions here if you run into trouble.

    -Gary-

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Oh, and Console.Write() won't help you much here -- this is Java, not C#. :)

    -Gary-

Similar Threads

  1. Replies: 1
    Last Post: 05-31-2009, 11:57 PM
  2. Replies: 1
    Last Post: 05-31-2009, 07:48 PM
  3. Replies: 1
    Last Post: 05-31-2009, 03:17 PM
  4. Replies: 16
    Last Post: 05-28-2009, 04:41 PM
  5. Java Programming
    By JavaForums in forum Java Tutorial
    Replies: 0
    Last Post: 07-29-2007, 12:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •