Results 1 to 5 of 5
  1. #1
    Tykk is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default Constructors Objects and Classes

    Alright, I wanted to thank everyone for helping me with my previous question regarding loops. I got the program to run and everything worked out pretty well.

    I've moved on to working with separate classes now, and creating objects to help store the information received as input, as well as display the same information as before with output.

    I am getting several errors regarding finding the variables specified in the code, specifically name, days, and rate.

    I actually started out with 28 errors >.<' but I've worked it down to 8, but these 8 are giving me tons of problems.

    Here's the code:

    Java Code:
    import java.util.Scanner; // Program uses class scanner
    import java.io.InputStream; // Program uses scanner input
    
    public class LFC3	//start of class LFC
    {
    	
    	
    	
    	//begin method main
    	public static void main( String args[] )
    	{
    		Scanner input = new Scanner( System.in );
    		LibraryItem item = new LibraryItem();
    		String cmdEnd = "";
    		boolean end = true;
    		
    		
    		// Welcome user
    		System.out.println( "\nWelcome to the Library Fine Calculator.\n" );
    	
    		while ( end ) {
    			
    			System.out.println( "Do you have any more items to check in? (Yes or No)\n" );
    			cmdEnd = input.nextLine();
    			
    			if ( cmdEnd.equalsIgnoreCase("No") ) {
    				break;
    			}
    			
    			// Get book input
    			System.out.println( "Please enter the name of the Book being returned:" );
    			item.name = input.nextLine(); //Store bookName as string
    			
    			// Get days input
    			System.out.println( "\nPlease enter the number of days the book is late:" );
    			item.days = input.nextInt(); // Store into int daysLate
    			input.nextLine();
    			
    			while ( item.days < 0 )
    			{
    				System.out.println( "You must enter a positive amount for the overdue days." );
    				item.days = input.nextInt();
    				input.nextLine();
    			}
    			
    			// Get fine amount
    			System.out.println( "\nPlease enter the per day fine for the book:" );
    			item.rate = input.nextDouble(); // Store into fineAmount
    			input.nextLine();
    			
    			while ( item.rate < 0 )
    			{
    				System.out.println( "You must enter a positive amount for the daily fine." );
    				item.rate = input.nextInt();
    				input.nextLine();
    			}
    			
    			
    			
    			// Print all related information.
    			System.out.printf( item.toString() );
    			
    			System.out.println();
    			
    			
    			
    		}
    		
    		System.out.println( "The check in program is complete!" );
    		
    	} // end method main
    	
    } // end class LFC

    And here is the object class:

    Java Code:
    import java.util.Scanner;
    import java.io.InputStream;
    
    
    
    public class LibraryItem // Start class dataBook
    {
    	private String bookName;
    	private int daysLate;
    	private double dailyFine;
    	private double fineAmount;
    	
    	// Constructor initializes book return data
    	public LibraryItem( String name, int days, double rate )
    	{
    		this.bookName = name;
    		this.daysLate = days;
    		this.dailyFine = rate;
    		calcTotal();
    	} // End constructor
    	
    	public String getName() {
    		return bookName;
    	}
    	
    	public void setName( String name ) {
    		name = bookName;
    	}
    	
    	public int getDays() {
    		return daysLate;
    	}
    	
    	public void setDays( int days ) {
    		days = daysLate;
    	}
    	
    	public double getRate() {
    		return dailyFine;
    	}
    	
    	public void setRate( double rate ) {
    		rate = dailyFine;
    	}
    	
    	// Calculate total amount for the fine
    	public void calcTotal()	{
    		fineAmount = daysLate * dailyFine;
    	} // End method calcTotal
    	
    	// Retrieve total amount
    	public double getTotal() {
    		return fineAmount;
    	} // End method getTotal
    	
    	// Convert info to string
    	public String toString() {
    		return String.format( "\n   Returned Book: %s\n   This book was %d days overdue.\n   The amount owed is $%.2f\n" , bookName , daysLate , fineAmount );
    	} // End string method
    	
    	
    } // End class dataBook


    The specific errors are 'cannot find symbol' for the variables name, days, and rate, as well as the constructor LibraryItem()

    Thanks in advanced for any help.


    EDIT:

    Added the previous code for this program. The new program should run exactly the same, just with separate classes.

    Java Code:
    import java.util.Scanner; // Program uses class scanner
    import java.io.InputStream; // Program uses scanner input
    
    public class LFCtwo	//start of class LFC
    {
    	//begin method main
    	public static void main( String args[] )
    	{
    		Scanner input = new Scanner( System.in );
    		
    		String cmdEnd = "";
    		boolean end = true;
    		int  daysLate;		// Create integer for days
    		double dailyFine;	// Create float for daily charge
    		double fineAmount;	// Create float for fine
    		
    		// Welcome user
    		System.out.println( "\nWelcome to the Library Fine Calculator.\n" );
    	
    		while ( end ) {
    			
    			System.out.println( "Do you have any more items to check in? (Yes or No)\n" );
    			cmdEnd = input.nextLine();
    			
    			if ( cmdEnd.equalsIgnoreCase("No") ) {
    				break;
    			}
    			
    			// Get book input
    			System.out.println( "Please enter the name of the Book being returned:" );
    			String bookName = input.nextLine(); //Store bookName as string
    			
    			// Get days input
    			System.out.println( "\nPlease enter the number of days the book is late:" );
    			daysLate = input.nextInt(); // Store into int daysLate
    			input.nextLine();
    			
    			while ( daysLate < 0 )
    			{
    				System.out.println( "You must enter a positive amount for the overdue days." );
    				daysLate = input.nextInt();
    				input.nextLine();
    			}
    			
    			// Get fine amount
    			System.out.println( "\nPlease enter the per day fine for the book:" );
    			dailyFine = input.nextDouble(); // Store into fineAmount
    			input.nextLine();
    			
    			while ( dailyFine < 0 )
    			{
    				System.out.println( "You must enter a positive amount for the daily fine." );
    				dailyFine = input.nextInt();
    				input.nextLine();
    			}
    			
    			fineAmount = daysLate * dailyFine;
    			
    			// Print all related information.
    			System.out.printf( "\n   Returned Book: %s\n   This book was %d days overdue.\n   The amount owed is $%.2f\n" , bookName , daysLate , fineAmount );
    			
    			System.out.println();
    			
    			
    			
    		}
    		
    		System.out.println( "The check in program is complete!" );
    		
    	} // end method main
    	
    } // end class LFC

    EDIT2: Changed the second code to the LibraryItem class

    Thanks for pointing this out to me toymachine ^.^
    Last edited by Tykk; 10-10-2009 at 10:00 PM. Reason: added previous code

  2. #2
    Tykk is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Tried a bit more this morning, read some more about objects, but I still cant seem to find anything about storing user input.

  3. #3
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    In your original post you have the same code in their twice. Try posting your LibraryItem class

  4. #4
    Tykk is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Updated the post, changed out the mistake with the LibraryItem class.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    When you do

    Java Code:
    LibraryItem item = new LibraryItem();
    You are calling a constructor in the LibraryItem class. The constructor you are calling in this case is one that takes no arguments. You don't have such a constructor in your class so the compiler issues a polite message about it.
    Call a constructor that actually exists in the class and you will make the compiler happy.
    Also, when you create private fields is a class, those fields are not available in other classes. If you create an instance of the class where they are declared in, you can access them through their getters and setters.

Similar Threads

  1. classes as objects
    By kroiz in forum New To Java
    Replies: 4
    Last Post: 07-25-2009, 06:22 AM
  2. Passing objects into constructors
    By aaronfsimons in forum New To Java
    Replies: 8
    Last Post: 04-14-2009, 01:08 PM
  3. help with passing objects between classes
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-22-2009, 03:41 PM
  4. Replies: 1
    Last Post: 11-18-2008, 05:48 PM
  5. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 09:05 AM

Tags for this Thread

Posting Permissions

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