Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Help with class assignment

  1. #1
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Help with class assignment

    Feel like I am beating my head against a brick wall with this class assignment. Java is hard for me to grasp and is a real struggle for me. My class compiles fine, but when I compile the tester I get an error I do not understand...any help or direction would be appreciated.
    The arrow is suppose to be pointing to the dot between newMonth.getMonth, I do not know why it moved when I posted....
    Error:
    MonthTester.java:19: error: cannot find symbol
    System.out.print("The name for month number " + (monthNumber = keyboard.nextInt()) + " is " + newMonth.getMonth() + ".");
    ^
    symbol: method getMonth()
    location: variable newMonth of type Month
    1 error



    Class Assignment:
    1. Write a class called Month whose job it is to transform numbers 1, 2, 3, . . ., 12 into the corresponding month names January, February, March, . . ., December.
    2. Implement the class Month
    1. One Instance Variable called monthNumber that is an int representing the month ( 1, 2, 3 ... 12).
    2. Default constructor parameter that sets the monthNumber to 1.
    3. Parameterized constructor has one parameter an int that sets monthNumber
    4. An accessor and mutatator method for monthNumber
    5. A getName method
    1. Returns a String representing the month name.
    2. Hint: Make a very long string constant that contains "January February March . . . ", in which you add spaces such that each month name has the same length. Then use substring to extract the month you want.
    3. The returned String should not have any extra spaces, look in the Java String API class for a method that would remove extra spaces from the front of the String.
    4. DO NOT USE AN IF STATEMENT TO SOLVE THIS PROBLEM
    6. A constant int containing the number of months in a year may be helpful.
    3. Implement the class MonthTester
    1. Prompts the user to enter an int representing a month (1 - 12) no error checking required. (use the Scanner class)
    2. Print out a nicely formated String such as: Name for month 2 is February
    3. Repeat the code to allow the user to ask for 2 more month names, no looping required.


    What I have so far:
    Java Code:
     public class Month
       {	//Instance Variables
       
       /**
       Number of the month
       */
          private int monthNumber;
       
       //Constructors
       /**
       Default construtor that sets the instance fields to a default value.
       */
          public Month()
          {
             
          }
       	
       /**
       Constructs parameterized constructor that sets the monthNumber   	
       @param newMonthNumber the months number
       */
          public Month(int initMonthNumber)
          {
                  }	
       
       //methods
       /**
       Sets the month number
       @param newMonthNumber the months number
       */  	
          public void setMonthNumber(int newMonthNumber)
          {
            
          }
          
       /**
       Gets the month number
       @return returns month number
       */
       	 public int getMonthNumber()
          {
          
          }
       	
       	
       /**
       This method returns the name of the month to the corresponding number.
       @param beginIndex beginning of the substring
       @param endIndex ending index of the substring
       @return returns the month name
       */	
          public String getName()
          {
             
          }	
       
       }
    and the tester:

    Java Code:
    import java.util.Scanner;  // Needed for the Scanner class
    
    /*
       This program test the input and output of the Month class.
    */
    
    public class MonthTester
    {
       public static void main(String[] args)
       {
          // Create a Scanner object to read input.
          Scanner keyboard = new Scanner(System.in);
          int monthNumber; // To hold the user's month number
          System.out.print("Please enter a month number 1 - 12: ");
          monthNumber = keyboard.nextInt();
    		Month newMonth = new Month();
          
          // Get the cooresponding name for the month number
          System.out.print("The name for month number " + (monthNumber = keyboard.nextInt()) + " is " + newMonth.getMonth() + ".");
          }
    }
    Last edited by jwood; 06-22-2013 at 10:29 PM.

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Help with class assignment

    You do not have defined any method getMonth() , that is why it is not recognized.

  3. #3
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Help with class assignment

    BLEH....nevermind on that issue....should be getName not getMonth in the tester. Now that it is compiling though, I have a new issue when I run it.

    Every number I enter responds with the month as January......

  4. #4
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Help with class assignment

    Quote Originally Posted by superhaNds View Post
    You do not have defined any method getMonth() , that is why it is not recognized.
    Yeah, I found that and feel stupid for missing it....thanks

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Help with class assignment

    consider using a switch statement to return the name of the month, here is the tutorial in case you haven't used it The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    but it's weird because it gives you solution, but not my fault. :)

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Help with class assignment

    2. Hint: Make a very long string constant that contains "January February March . . . ", in which you add spaces such that each month name has the same length. Then use substring to extract the month you want.

    OMG!

    I take it you have not learnt arrays yet. The reason you get January every time is because you use the default constructor of Month which sets the monthNumber to 1.

  7. #7
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Help with class assignment

    How do I tell the parameterized constructor that I want the parameter to be the user input? I have been trying, but can't seem to get it working...everything goes to the default constructor. No we have not learned arrays yet. We are only in CH 4 of Big Java

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Help with class assignment

    Java Code:
    Month newMonth = new Month();
    That calls the default no argument constructor. So call the constructor that takes a single int parameter. You do know how to call methods don't you and pass parameters? Well constructors work the same way.

  9. #9
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Help with class assignment

    Yes I do know call the paramatized constructor, what I do not know is how to tell it that the single int parameter is the user input...

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

    Default Re: Help with class assignment

    Quote Originally Posted by jwood View Post
    Yes I do know call the paramatized constructor, what I do not know is how to tell it that the single int parameter is the user input...
    The constructor doesn't care where the input comes from; all it cares about is that you're passing an int into it. What you need to do is 1) get the int input from the user, and then 2) call the correct Month constructor, the one that takes an int parameter, passing in the input you received. Let's see you try this.

  11. #11
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Help with class assignment

    OK, I feel really stupid...cause I do not really understand what you just said. In past projects when we called the parameterized constructor we input the paramater when we called it (like this: Month newMonth = new Month(4); . I do not know what to put in the perins that tells the constructor to read the user input. Am I getting closer? This compiles, but does nothing after the user input /enter when it runs....

    Java Code:
    import java.util.Scanner;  // Needed for the Scanner class
    
    /*
       This program test the input and output of the Month class.
    */
    
    public class MonthTester3
    {
       public static void main(String[] args)
       {
          
    		int number;//To hold the users month number
    		
    		
    		// Create a Scanner object to read input.
          Scanner keyboard = new Scanner(System.in);
    		
          System.out.print("Please enter a month number 1 - 12: ");
    		number = keyboard.nextInt();
    		Month newMonth = new Month(number);
    		
    		// Get the cooresponding name for the month number
          System.out.println("The name for month number " + (number = keyboard.nextInt()) + " is " + newMonth.getName() + ".");
          }
    }

  12. #12
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Help with class assignment

    some of the original class changed from my original post, but that part is working
    Last edited by jwood; 06-22-2013 at 10:31 PM.

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

    Default Re: Help with class assignment

    Quote Originally Posted by jwood View Post
    OK, I feel really stupid...cause I do not really understand what you just said. In past projects when we called the parameterized constructor we input the paramater when we called it (like this: Month newMonth = new Month(4); . I do not know what to put in the perins that tells the constructor to read the user input. Am I getting closer? This compiles, but does nothing after the user input /enter when it runs....
    It looks closer to me. Please check comments in the code:



    Java Code:
       System.out.print("Please enter a month number 1 - 12: ");
       number = keyboard.nextInt();  // good. you're getting user input and saving it.
       Month newMonth = new Month(number); // good. you're using the input appropriately.
    		
       // why are you calling keyboard.nextInt() below? 
       // You already have your correct number assigned to the number variable.
       System.out.println("The name for month number " + (number = keyboard.nextInt()) + 
               " is " + newMonth.getName() + ".");
    As an aside: please pay attention to your code formatting and indentation. Appropriate, regular consistent indentation and code format are not to help your code compile or run, but to help you better understand your own code, and more importantly for *us* to understand your code and be able to help you.

  14. #14
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Help with class assignment

    Thankyou so much....it sorta makes sense to me now. My assignment ask 3. Repeat the code to allow the user to ask for 2 more month names, no looping required. Would I recall the constructor each time as a new constructor (newMonth2, newMonth3, etc...) or is that inappropriate coding? ALso, I do not know why, but when I copy and paste the code over from jGrasp, it shifts. I actually have everything from int number;//To hold the users month number down lined up. That is how all the tester programs are formatted in our examples. If that is incorrect, what would be the proper format? Also, I apologize for my ignorance, I am a graphic design major/ eBusiness minor...not sure why I am required to take this class. It is completely alien to me, and I m having a really hard time with the lingo despite how much studying I am doing. I am not use to a subject being so difficult for me to grasp.
    Last edited by jwood; 06-22-2013 at 10:31 PM.

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

    Default Re: Help with class assignment

    Quote Originally Posted by jwood View Post
    Thankyou so much....it sorta makes sense to me now.
    You're welcome.

    My assignment ask 3. Repeat the code to allow the user to ask for 2 more month names, no looping required. Would I recall the constructor each time as a new constructor (newMonth2, newMonth3, etc...) or is that inappropriate coding?
    Check out the Month class -- does it have some mechanism (a method perhaps) that would allow one to change a Month object's current month after the object has been created?

    ALso, I do not know why, but when I copy and paste the code over from jGrasp, it shifts. I actually have everything from int number;//To hold the users month number down lined up. That is how all the tester programs are formatted in our examples. If that is incorrect, what would be the proper format?
    Make sure you're not mixing tabs with spaces. I do not use JGrasp and so cannot speak to its settings, but I think that the forum software works better if you can set indentations to be spaces, not tabs. Also, before submitting a post on this site, you can preview by pressing the "Preview Post" button, allowing you to review your code formatting before submission.

    Also, I apologize for my ignorance, I am a graphic design major/ eBusiness minor...not sure why I am required to take this class. It is completely alien to me, and I m having a really hard time with the lingo despite how much studying I am doing. I am not use to a subject being so difficult for me to grasp.
    This is true for most all of us trying to learn a completely new subject. Even if you never use this again the intellectual exercise will be worth it.
    jwood likes this.

Similar Threads

  1. Help With class assignment - recursion
    By Ivanniki in forum New To Java
    Replies: 1
    Last Post: 12-10-2012, 04:48 PM
  2. Need help with class Month assignment please!!
    By ndsmith20 in forum New To Java
    Replies: 5
    Last Post: 10-17-2012, 10:26 PM
  3. Help needed with class assignment.
    By DigDug in forum New To Java
    Replies: 9
    Last Post: 01-24-2012, 04:52 AM
  4. Need help with Instrument Class assignment
    By Kinney.j in forum New To Java
    Replies: 0
    Last Post: 11-02-2011, 04:42 AM
  5. Replies: 1
    Last Post: 10-02-2009, 01:35 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
  •