Java Answer

Help This Answer

Page 1 of 1

9 Replies - 506 Views - Last Post: 25 February 2010 - 07:00 PM Rate Topic: -----

#1 aceng1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-February 10

Java Answer

Posted 25 February 2010 - 09:27 AM

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) an input function that reads the month as an integer,
(B) an input function that reads the month as the first three letters in the name of the month,
Test your class in a new program which has the main() method.
Why i can not displayed this input program?

Answer a
public class month {

public static void main (String[] args) {
{
int month
month [ ] month = new month [12];
month[0] = new month ( "1")
month[1] = new month ( "2")
month[2] = new month ( "3")
month[3] = new month ( "4")
month[4] = new month ( "5")
month[5] = new month ( "6")
month[6] = new month ( "7")
month[7] = new month ( "8")
month[8] = new month ( "9")
month[9] = new month ( "10")
month[10] = new month ( "11")
month[11] = new month ( "12")
System.out.printIn("Month Of Integer:");
System ( int counter=0; <= 12; counter== ){
month [ counter ] .
}

}//main

}//class

}

Answer b

public class month {

public static void main (String[] args) {
{
int month
month [ ] month = new month [12];
month[0] = new month ( "JAN")
month[1] = new month ( "FEB")
month[2] = new month ( "MAC")
month[3] = new month ( "APR")
month[4] = new month ( "MEI")
month[5] = new month ( "JUN")
month[6] = new month ( "JUL")
month[7] = new month ( "OGO")
month[8] = new month ( "SEP")
month[9] = new month ( "OKT")
month[10] = new month ( "NOW")
month[11] = new month ( "DIS")
System.out.printIn("Name Of Month:");
System ( int counter=0; <= 12; counter== ){
month [ counter ] .
}

}//main

}//class

}

Is This A Good Question/Topic? 0
  • +

Replies To: Java Answer

#2 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 83
  • View blog
  • Posts: 505
  • Joined: 14-January 09

Re: Java Answer

Posted 25 February 2010 - 09:33 AM

Firstly; Put your code into
[code]
[/code] please, and secondly.


System ( int counter=0; <= 12; counter== ){
month [ counter ] .
}



What exactly is that..?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9041
  • View blog
  • Posts: 33,540
  • Joined: 27-December 08

Re: Java Answer

Posted 25 February 2010 - 09:34 AM

Please, :code:

The reason you aren't getting the desired output is b/c of these lines below. Where you have System, you want for instead. Also, instead of counter==, you probably want counter++ instead. Next, the segment <= 12 should be counter < 12 or counter < month.length to prevent an IndexOutOfBoundsException from being thrown. Lastly, the line month [ counter ] . should probably be a print statement if you want to output it. Even if you changed the period to a semi-colon, it would be illegal b/c it's not a statement.

System ( int counter=0; <= 12; counter== ){
month [ counter ] .
}


Was This Post Helpful? 0
  • +
  • -

#4 aceng1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-February 10

Re: Java Answer

Posted 25 February 2010 - 09:42 AM

ok thanks you your explain...!!

public class month {

public static void main (String[] args) {
{
int month
month [ ] month = new month [12];
month[0] = new month ( "1")
month[1] = new month ( "2")
month[2] = new month ( "3")
month[3] = new month ( "4")
month[4] = new month ( "5")
month[5] = new month ( "6")
month[6] = new month ( "7")
month[7] = new month ( "8")
month[8] = new month ( "9")
month[9] = new month ( "10")
month[10] = new month ( "11")
month[11] = new month ( "12")
System.out.printIn("Month Of Integer:");
System ( int counter=0; <= 12; counter== ){
month [ counter ] .
}

}//main

}//class

}

This post has been edited by aceng1981: 25 February 2010 - 09:44 AM

Was This Post Helpful? 0
  • +
  • -

#5 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 83
  • View blog
  • Posts: 505
  • Joined: 14-January 09

Re: Java Answer

Posted 25 February 2010 - 09:42 AM

for(int counter=0;counter <= 12; counter++ ){
System.out.println(month[counter];
}


Thats probably what you meant to write.. (Or what should be there at least.)
Was This Post Helpful? 0
  • +
  • -

#6 aceng1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-February 10

Re: Java Answer

Posted 25 February 2010 - 09:54 AM

ok thanks...
Was This Post Helpful? 0
  • +
  • -

#7 aceng1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-February 10

Re: Java Answer

Posted 25 February 2010 - 10:03 AM

Ok thanks your explain...

Now why i can display a default constructor..?
Can i take this statement.?
Was This Post Helpful? 0
  • +
  • -

#8 Simple_Condolences  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 138
  • Joined: 10-January 10

Re: Java Answer

Posted 25 February 2010 - 11:53 AM

Another thing...


System.out.printIn()

//should be 

System.out.println()


Also, why not just make an array and put ALL of the months in it, then just use substring and for loops to find the first three letters when you need to compare them?

like...


Scanner scan = new Scanner(System.in);
System.out.println("What month?: ");
String in = scan.nextLine();

String[] months = new String{"January", "February", "March", "April", ...};
for(int i = 0; i < months.length;i++){
    if(months[i].substring(0, 2).equalsIgnoreCase(in))
        System.out.println(months[i]);



Just seems a little redundant to iterate the array twice and then compare them to each other...
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9041
  • View blog
  • Posts: 33,540
  • Joined: 27-December 08

Re: Java Answer

Posted 25 February 2010 - 03:41 PM

View Postaceng1981, on 25 February 2010 - 01:03 PM, said:

Ok thanks your explain...

Now why i can display a default constructor..?
Can i take this statement.?


What exactly is your question? Are you asking how default constructors work? And what statement are you referring to?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,176
  • Joined: 06-March 08

Re: Java Answer

Posted 25 February 2010 - 07:00 PM

month [ ] month = new month [12]; 
month[0] = new month ( "1") 
month[1] = new month ( "2") 
month[2] = new month ( "3") 
month[3] = new month ( "4") 
month[4] = new month ( "5") 
month[5] = new month ( "6") 
month[6] = new month ( "7") 
month[7] = new month ( "8") 
month[8] = new month ( "9") 
month[9] = new month ( "10") 
month[10] = new month ( "11") 
month[11] = new month ( "12") 


ouf !!! Q chance you do not use the Aztek calendar with 60 months :bigsmile:
month [ ] month = new month [12]; 
for(int i = 0; i < month.length; i++)
   month[i] = new month("" + (i+1));


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1