Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Reusable classes.

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reusable classes.

    I am doing an exercise on reusable classes, but I can't seem to get it to work right. Here is the code
    Time3.java
    //Jeremiah A.Walker
    /**
    Steps for Declaring a Reusable Class
    Before a class can be imported into multiple applications, it must be placed in a package
    to make it reusable. Figure 8.15 shows how to specify the package in which a class should
    be placed. Figure 8.16 shows how to import our packaged class so that it can be used in
    an application. The steps for creating a reusable class are:
     
    1. Declare a public class. If the class is not public, it can be used only by other
    classes in the same package.
     
    2. Choose a unique package name and add a package declaration to the source-code
    file for the reusable class declaration. In each Java source-code file there can be
    only one package declaration, and it must precede all other declarations and
    statements. Comments are not statements, so comments can be placed before a
    package statement in a file. [Note: If no package statement is provided, the class
    is placed in the so-called default package and is accessible only to other classes in
    the default package that are located in the same directory. All prior programs in
    this book having two or more classes have used this default package.]
     
    3. Compile the class so that it’s placed in the appropriate package directory.
     
    4. Import the reusable class into a program and use the class.
    We’ll now discuss each of these steps in detail.**/
     
    //Time3 class declaration maintains the time in 24 format.
    package com.walker.jhtp.reucla;
     
    public class Time3
    {
    	private int hour; //0 - 23
    	private int minute; //0 - 59
    	private int second; //0 - 59
     
     
    	/**
    	Setting a new time value using universal time; 
    	This will throw an exception if the hour, minute or second is invalid
    	**/
    	public void setTime(int h, int m, int s)
    	{
    		//validate hour, minute and second
    		if((h >= 0 && h < 24) && (m >= 0 && m < 60 ) && (s >= 0 && s < 60) )
    		{
    			hour = h;
    			minute = m; 
    			second = s;
    		}//end if
    		else
    			throw new IllegalArgumentException("Hour, minute, and/or second was out of range");
    	}//end setTime
     
    	//convert to String in universal-time format (hh:mm:ss)
    	public String toUniversalString()
    	{
    		return String.format("%02d:%02d:%02d", hour, minute, second);
    	}//end method toUniversalString
     
    	//convert to String in stangdard-time format (H:MM:SS AM or PM)
    	public String toString()
    	{
    		return String.format("%d:%02:%02d %s", ((hour == 0 || hour == 12) ? 12 : hour % 12 ), minute, second, (hour < 12 ? "AM" : "PM"));
    	}//end method toString
    }//end class Time3

    and its tester
    //Jeremiah A. Walker
    /**
    Once it's compiled and stored in its package, the class can be imported into programs.
    In this Tester, we specify that class Time3 should be imported for use in class Time3Test.
    This class is in the default package because its .java file does not contain a package declaration.  Since the
    classes are in different package, the import at the beginninf is required so that our tester can use Time3.
    **/
     
    import com.walker.jhtp.reucla.Time3;//import class Time 3
     
    public class Time3Test
    {
    	public static void main(String[] args)
    	{
    		//Create and initialize a Time3 object
    		Time3 time = new Time3();//invokes Time3 constructor
     
    		//output string representation of the time
    		System.out.print("The initial universal time is: ");
    		System.out.println(time.toUniversalString());
    		System.out.print("The initial standard time is: ");
    		System.out.println(time.toString());
    		System.out.println();//output a blank line
     
    		//change time and output updated time
    		time.setTime(13, 14, 15);
                    System.out.print("The initial universal time is: ");
                    System.out.println(time.toUniversalString());
                    System.out.print("The initial standard time is: ");
                    System.out.println(time.toString()); 
                    System.out.println();//output a blank line
     
    		//attempt to set time with invalid values
    		try
    		{
    			time.setTime(99,99,99); //all values out of range
    		}//end try
    		catch (IllegalArgumentException e)
    		{
    			System.out.printf("Exception:  %s\n\n", e.getMessage());
    		}//end catch
     
    		//display time after attempt to set invalid values
    		System.out.println("After attempting invalid settings: ");
                    System.out.print("The initial universal time is: ");
                    System.out.println(time.toUniversalString());
                    System.out.print("The initial standard time is: ");
                    System.out.println(time.toString()); 
                    System.out.println();//output a blank line
    	}//end main
    }//end class Time3Test

    I am following the directions to the letter, but I am getting an error saying that the directory doesn't exist. What am I doing wrong.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Reusable classes.

    I am getting an error
    Always copy and paste the full text of your error messages. You'll stand a much better chance of getting a reply.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reusable classes.

    Time3Test.java:9: package com.walker.jhtp.reucla does not exist
    import com.walker.jhtp.reucla.Time3;//import class Time 3
    ^

    Time3Test.java:16: cannot access Time3
    bad class file: RegularFileObject[Time3.class]
    class file contains wrong class: com.walker.jhtp.reucla.Time3
    Please remove or make sure it appears in the correct subdirectory of the classpath.
    Time3 time = new Time3();//invokes Time3 constructor

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reusable classes.

    Is the package or folder containing the classes you are trying to implement on the classpath? The compiler needs to know explicitly where to find imported classes
    PATH and CLASSPATH (The Java™ Tutorials > Essential Classes > The Platform Environment)

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reusable classes.

    I was able to get this to work. Thanks for your help.

Similar Threads

  1. Need Help with using classes [HELP]
    By dragon40226 in forum Java Theory & Questions
    Replies: 4
    Last Post: May 19th, 2011, 01:59 PM
  2. Using values in different classes
    By The_Mexican in forum Java Theory & Questions
    Replies: 2
    Last Post: November 19th, 2010, 08:08 PM
  3. cant run the program with 2 classes
    By clone_hiryu in forum Object Oriented Programming
    Replies: 5
    Last Post: November 18th, 2010, 12:28 AM
  4. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM