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 4 of 4

Thread: not sure how to pass this value

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default not sure how to pass this value

    im making a program a class file with two method a check in and a checkout method. the program calls for this: the balance on your account is 0 then you check out 10 books and then check in 6 the balance should be printed after each situation. after my object i made goes through the checked out method how do i take that number and use it as the balance for the next method? Im stuck!! heres my code:

    public class LibraryAccount {
     
     
    	private int bookBal;
    	  private int bookIn;// books checked in variable
    	   private int bookOut;// books checked out variable
     
     
    	    // constructor
    	    public LibraryAccount(int bookBal, int bookIn, int bookOut) {
    	    	this.bookBal=bookBal;
    	    	this.bookIn=bookIn;
    	    	this.bookOut=bookOut;
     
    	    }
    	 // Get bookBal
    	    public int getBookBal(){
    	       return bookBal;
     
    	    }
     
    	    // Set bookBal
    	    public void setBookBal(int tempBookBal){
    	      bookBal = tempBookBal;
     
    	       }    	    
     
     
    	 // Get bookIn 
    	    public int getBookIn(){
    	       return bookIn;
     
    	    }
     
    	    // Set bookIn
    	    public void setBookIn(int tempBookIn){
    	      bookIn = tempBookIn;
     
    	       }    	    
    	 // Get bookOut
    	    public int getBookOut(){
    	       return bookOut;
     
    	    }
    	 // Set bookOut
    	    public void setBookOut(int tempBookOut){
    	       bookOut = tempBookOut;
     
    	    }
    	    public int checkIn(){
    	    int	yourBal=this.bookBal+this.bookIn;
     
    	    	return yourBal;
    }
     
    	    public int checkOut(){
    	    	int yourBal2=bookBal-bookOut;
     
    	    	return yourBal2;
    }}


    main class:
     
    public class LibraryAccountDemo {
     
     
    	public static void main(String[] args) {
    		int bookBal=0;
    		 int bookIn=6;// books checked in variable
    		 int bookOut=10;// books checked out variable
     
    		LibraryAccount myAccount = new LibraryAccount(bookBal,bookIn, bookOut);
    		System.out.println("Your initially have a balance of " + bookBal);
    		System.out.println("After checking out "+ bookOut+ " books you have "  + myAccount.checkOut());
    		System.out.println("After checking in "+ bookIn+ " books you have "  + (myAccount.checkOut()+myAccount.checkIn()));
     
    	}
     
    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: not sure how to pass this value

    Instead of passing values, in your checkout method you could set your balance to the desired number during the checkout rather than having to pass around variables.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: not sure how to pass this value

    found it! i think this is right
     
    public class LibraryAccount {
     
     
    	private int bookBal;
    	  private int bookIn;// books checked in variable
    	   private int bookOut;// books checked out variable
     
     
    	    // constructor
    	    public LibraryAccount(int bookBal, int bookIn, int bookOut) {
    	    	this.bookBal=bookBal;
    	    	this.bookIn=bookIn;
    	    	this.bookOut=bookOut;
     
    	    }
    	 // Get bookBal
    	    public int getBookBal(){
    	       return bookBal;
     
    	    }
     
    	    // Set bookBal
    	    public void setBookBal(int tempBookBal){
    	      bookBal = tempBookBal;
     
    	       }    	    
     
     
    	 // Get bookIn 
    	    public int getBookIn(){
    	       return bookIn;
     
    	    }
     
    	    // Set bookIn
    	    public void setBookIn(int tempBookIn){
    	      bookIn = tempBookIn;
     
    	       }    	    
    	 // Get bookOut
    	    public int getBookOut(){
    	       return bookOut;
     
    	    }
    	 // Set bookOut
    	    public void setBookOut(int tempBookOut){
    	       bookOut = tempBookOut;
     
    	    }
    	    public int checkIn(int passBal){
    	    	int newBal=passBal;
    	    int	yourBal=newBal+this.bookIn;
     
    	    	return yourBal;
    }
     
    	    public int checkOut(){
     
    	    	int yourBal=bookBal-bookOut;
     
    	    	return yourBal;
    }}

     
    public class LibraryAccountDemo {
     
     
    	public static void main(String[] args) {
    		int bookBal=0;
    		 int bookIn=6;// books checked in variable
    		 int bookOut=10;// books checked out variable
     
    		LibraryAccount myAccount = new LibraryAccount(bookBal,bookIn, bookOut);
    		int checkOut=myAccount.checkOut();
    		int checkIn=myAccount.checkIn(checkOut);
    		System.out.println("Your initially have a balance of " + bookBal);
    		System.out.println("After checking out "+ bookOut+ " books you have "  + checkOut);
    		System.out.println("After checking in "+ bookIn+ " books you have "  + checkIn);
     
    	}
     
    }
    i added a parameter to the check in method and called that that method using the variable i assigned to the value of the first method. i think this is correct. Any thoughts?

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: not sure how to pass this value

    int booksInPosession = 0;

    public void checkOut() { booksInPosession++; }

    public void checkIn() { booksInPosession--; }

    In this way booksInPosession will keep the number of books currently checked out

Similar Threads

  1. Cannot pass variables?
    By simpson_121919 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 7th, 2013, 02:33 PM
  2. should i pass arrays or bytebuffers?
    By deepthought in forum Java Native Interface
    Replies: 1
    Last Post: December 15th, 2011, 08:21 AM
  3. 'pass by value' and 'pass by reference'
    By pokuri in forum Object Oriented Programming
    Replies: 5
    Last Post: January 26th, 2011, 11:30 AM
  4. Can't pass to the other method
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2010, 09:44 AM
  5. How do you pass an Array as a Parameter?
    By Arius in forum Java Theory & Questions
    Replies: 1
    Last Post: January 23rd, 2010, 09:36 PM