Results 1 to 7 of 7
  1. #1
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Create user-defined Classes & Methods

    Hello All.
    This is what I want to do. Create a program that allows a user to enter a date, print out the date if it is valid, or print out some type of invalid statement if the date is not valid.

    In this program I will have a default constructor (because it's part of the assignment), & a constructor with parameters. I have started the program & it is fraught with danger (a little humor) and many errors. I am posting what I have so far. I didn't want to code any more than what I have for fear of falling further into the Java briar patch (a little more humor: nervousness does that to me <smile>).
    Please remember the name of the forum, "New to Java". Here goes and Thanks In Advance, lala.

    Java Code:
    import java.util.*;
    
    public class Date 
    {
    	static Scanner console = new Scanner  System.in)	
    	public static void main(String[] args)
    	{
    	Date  = myDate(day, month, year); //Instantiate class. [COLOR="Red"](Error day month year Date cannot be resolved)[/COLOR]		
    	String inputLine;
    	
    	System.out.println(" This program is a date validator.\n"
     			
    [INDENT]+" When you enter a date, it will tell you"
    +" whether the date is a valid date.\n"
    +" You must enter a year greater than or equal to 1950. ");	
    	
    System.out.println("Please enter today's date (month, day, and year separated by spaces):");
    	inputLine = console.nextLine();
    				
    	public class Date [COLOR="Red"](Error: illegal modifier for the local class Date)[/COLOR]	{
    		private int day;
    		private int month;
    		private int year;
    	}
    	
    	public Date() //My default constructor [COLOR="red"](Error: syntax error on token public, "new" expected)[/COLOR]	{
    		setDate(0,0,0); [COLOR="Red"](Error: The method setDate is undefined for the type new Date())	[/COLOR]	}
       }
    }
    [/INDENT]

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi,
    I changed your code
    Java Code:
    import java.util.Scanner;
    
    public class Date {
        private int day;
        private int month;
        private int year;
    
        private static Scanner console = new Scanner(System.in);
    
        public Date() {
            this(0, 0, 0);
        }
    
        public Date(int day, int month, int year) {
            this.day = day;
            this.month = month;
            this.year = year;
        }
    
        public static void main(String[] args) {
            Date date = new Date(31, 1, 2011);
            String inputLine;
    
            System.out.println(" This program is a date validator.\n"
    
                    +" When you enter a date, it will tell you"
                    +" whether the date is a valid date.\n"
                    +" You must enter a year greater than or equal to 1950. ");
    
            System.out.println("Please enter today's date (month, day, and year separated by spaces):");
            inputLine = console.nextLine();
        }
    }
    However you must read any book about Java Core. I think you don't understand that you are doing
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I have been noticing a lot of people have been simply posting finished code lately, please try to push people in the correct direction without doing it for them, this way they can understand what they did wrong, even though there is a code snippet posted I will explain what you had wrong op.

    First you declared the date class inside of the "test" class.

    You want to first create the class, and after you create the needed class you can add a main method, inside the class. In main you declare a date object as
    Java Code:
    Date = MyDate(month, date, year);
    this is incorrect, you need a few things, create and define an object with a name with
    Java Code:
    Date aDate =
    This creates the beginning of a new object with the name aDate.
    The second part(after the = sign) is to actually create the object by calling the constructor.
    Java Code:
    Date aDate = new Date();
    This is a default constructor, which will always be available unless you define your own constructors. You want to create a constructor inside your date class which takes an argument.
    Java Code:
    Date(args){
      //set class variables
    }
    When this constructor is created you can successfully create an object of your new class.

    You can then use a scanner object to get some input, store the input in variables, then use that info to create an object with a constructor that takes the variables.

    Check this link for constructor information, you can find a lot more information on the same site and should definitely read up on it to help you get a stronger understand it.
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  4. #4
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Create user-defined Classes & Methods

    Petr & sunde87, thanks so very much for responding. I am going through your inputs diligently so that I can understand what I am doing.

    I read lots of articles and the textbook on this subject B4 I posted, but some of the information I do not understand. One of the reasons I am having problems is because, that except for this forum, I do not have anyone to answer my questions or bounce off ideas. (unfortunately it's hard to get my instructor involved).

    I do appreciate your help, & I really do want to learn. As I stated earlier, I am studying and analyzing both posts for a better understanding of the topic.

    Thanks again,
    lala

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Dont be afraid to use the tutorial website, it's useful, you can also get more than one book to read at the same time. If you are new the ideas can get confusing, but with time and practice this basic stuff will be easy, just keep at it, and don't be afraid to ask for help here. If you do ask for help here though, always give it an attempt first.

  6. #6
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default 'Create user-defined Classes & Methods'

    Hello again. I have some errors that I have not been able to figure out. Also in a program with user-defined classes & methods where does the main method belong, & by main method I mean this statement and its code:
    public static void main(String[] args)
    Thanks In Advance

    Java Code:
    import java.util.*;
    
    public class CDate
    {
    		private int day;
    		private int month;
    		private int year;
    		
    		public CDate() // default constructor
    		{
    			setDate(0,0,0);
    		}
    		
    		public CDate(int day, int month, int year) // constructor with parameters
    		{
    			setDate(day, month, year);
    		}
    		
    		public void setDate(int day, int month, int year)
    		{
    [COLOR="Blue"]if month = 1 && day < 32 (syntax err on token "if", invalid Type. - syntax err, insert Block statements for value "32")[/COLOR]				D = day;
    				M = month;
    				Y = year;
    			else [COLOR="Blue"](syntax err on token "else)[/COLOR]				D = 0;
    				M = 0;
    				Y = 0;
    					 
    			if month = 2 && day < 29
    				D = day;
    				M = month;
    				Y = year;
    			else 
    				D = 0;
    				M = 0;
    				Y = 0;
    				
    			if month = 3 && day < 32
    				D = day;
    				M = month;
    				Y = year;
    			else 
    				D = 0;
    				M = 0;
    				Y = 0;
    				
    			if month = 4 && day < 31
    				D = day;
    				M = month;
    				Y = year;
    			else 
    				D = 0;
    				M = 0;
    				Y = 0;
    	}
    	
    	public static void main(String[] args) 
    	{
    		Scanner console = new Scanner(System.in);	
    		/* CDate aDate = new CDate(); */ // I don't think I need this statement (?)
    		String inputLine; [COLOR="Blue"](local variable inputLine never read)[/COLOR]		
    		System.out.println(" This program is a date validator.\n"
    				+" When you enter a date, it will tell you"
    				+" whether the date is a valid date.\n"
    				+" You must enter a year greater than or equal to 1950. ");
    		
    		System.out.println("Please enter today's date (month, day, and year separated by spaces):");
    		inputLine = console.nextLine();
    				
    				
    	}
    }

  7. #7
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

Similar Threads

  1. Write & Call User-Defined Method
    By lala in forum New To Java
    Replies: 6
    Last Post: 01-23-2011, 05:40 PM
  2. draw shape with user defined values
    By cka in forum New To Java
    Replies: 4
    Last Post: 07-14-2010, 04:35 PM
  3. User Defined Method
    By overcranked in forum New To Java
    Replies: 6
    Last Post: 04-09-2010, 02:02 AM
  4. JSP with user-defined java classes
    By adammyth in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-05-2010, 07:13 PM
  5. RegardingSgould Enter in user defined inbox
    By Prashant.surwade in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-23-2009, 02:21 PM

Posting Permissions

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