Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2010, 06:07 PM
Member
 
Join Date: Mar 2010
Posts: 2
Rep Power: 0
yogesara is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-20-2010, 06:21 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,388
Rep Power: 11
Fubarable is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-20-2010, 06:31 PM
Member
 
Join Date: Mar 2010
Posts: 2
Rep Power: 0
yogesara is on a distinguished road
Question im beginner for java
my coding for the question tht i pass it just now.

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 07:58 PM. Reason: Moderator Edit: Code tags added
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-20-2010, 08:01 PM
Senior Member
 
Join Date: Mar 2010
Posts: 605
Rep Power: 1
gcalvin is on a distinguished road
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-
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-20-2010, 08:03 PM
Senior Member
 
Join Date: Mar 2010
Posts: 605
Rep Power: 1
gcalvin is on a distinguished road
Default
Oh, and Console.Write() won't help you much here -- this is Java, not C#.

-Gary-
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
E:\IT 215 Java Programming\GUI1.java:125: class, interface, or enum expected tlouvierre New To Java 1 05-31-2009 10:57 PM
E:\IT 215 Java Programming\Inventory.java:211: reached end of file while parsing tlouvierre New To Java 1 05-31-2009 06:48 PM
E:\IT 215 Java Programming\GUI1.java:125: class, interface, or enum expected tlouvierre New To Java 1 05-31-2009 02:17 PM
E:\IT 215 Java Programming\Inventory.java:36: class, interface, or enum expected tlouvierre Advanced Java 16 05-28-2009 03:41 PM
Java Programming JavaForums Java Tutorials 0 07-28-2007 11:10 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:34 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org