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

Thread: how to take array input from main method and use it in one of the classes

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    14
    My Mood
    Worried
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default how to take array input from main method and use it in one of the classes

    QUESTION
    I have to find total number of different books in method getdiffCount().

    package module4c;
     
    class Book{
    	String title;
    	String author;
    	int count;
     
    	public Book(){
     
    	}
    	public Book(String title,String author,int count){
    		this.title=title;
    		this.author = author;
    		this.count=count;
    	}
     
    	public void increment(int i){
    		i=i+1;
    	}
     
    	public void decrement(int i){
    		if(i>0){
    			i=i-1;
    		}
    	}
     
    }
     
    class Library{
    	Book [] books;
     
    	public Library() {
    		// TODO Auto-generated constructor stub
    	}
    	public Library(Book[] books){
    		this.books= books;
    	}
     
    public int getdiffCount(){
    	int bs=0;
     
       bs = bs +books.length;
     
    		System.out.println(bs);
    		return bs;
    		}
     
     }
    public class TestLibrary {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Library lib = new Library();
          Book book0 = new Book("A","AB",10);
          Book book1 = new Book("B","CD",20);
          Book book2 = new Book("C","EF",30);
          Book book3 = new Book("D","GH",40);
          Book [] books ={book0,book1,book2,book3};
     
     
          int b3 = lib.getdiffCount();


  2. #2
    Junior Member
    Join Date
    Aug 2014
    Posts
    14
    My Mood
    Worried
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default how to take array input from main method and use it in one of the classes

    QUESTION
    I have to find total number of different books in method getdiffCount().
     
    package module4c;
     
    class Book{
    	String title;
    	String author;
    	int count;
     
    	public Book(){
     
    	}
    	public Book(String title,String author,int count){
    		this.title=title;
    		this.author = author;
    		this.count=count;
    	}
     
    	public void increment(int i){
    		i=i+1;
    	}
     
    	public void decrement(int i){
    		if(i>0){
    			i=i-1;
    		}
    	}
     
    }
     
    class Library{
    	Book [] books;
     
    	public Library() {
    		// TODO Auto-generated constructor stub
    	}
    	public Library(Book[] books){
    		this.books= books;
    	}
     
    public int getdiffCount(){
    	int bs=0;
     
       bs = bs +books.length;
     
    		System.out.println(bs);
    		return bs;
    		}
     
     }
    public class TestLibrary {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Library lib = new Library();
          Book book0 = new Book("A","AB",10);
          Book book1 = new Book("B","CD",20);
          Book book2 = new Book("C","EF",30);
          Book book3 = new Book("D","GH",40);
          Book [] books ={book0,book1,book2,book3};
     
     
          int b3 = lib.getdiffCount();

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to take array input from main method and use it in one of the classes

    Threads merged. Please don't post the same topic multiple times.

    Perhaps you could declare 'count' to be a class variable and increment that each time a book is created.

    Your increment() and decrement() methods are pointless.

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    adit (August 26th, 2014)

  5. #4
    Junior Member
    Join Date
    Aug 2014
    Posts
    14
    My Mood
    Worried
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: how to take array input from main method and use it in one of the classes

    thanks...
    i was trying to find out the total no of different books (ie 4 is the answer).so , i thought that if i could find the length of the Book [] books, i would get my answer. Is my approach correct???. if so how would i do it.

    If that way of approach is not correct , then please suggest the correct approach.

    --- Update ---

    i was also wondering if the concept of interface would do some good???

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to take array input from main method and use it in one of the classes

    i thought that if i could find the length of the Book [] books, i would get my answer. Is my approach correct???
    There are multiple approaches, all of them 'correct'. If books[] has no duplicates, yes, that should work. Will you be certain that books[] has only unique entries? Arrays in Java have an attribute 'length', so myArray.length will give you the number of elements in the array myArray. You can discover these interesting and useful facts yourself by referring to the appropriate API pages.

    Also, a class variable 'count' or 'uniqueCount' that is incremented each time a unique book is added to the array should also provide quick access to the needed value.

    Why do you ask about an interface? Is your program supposed to include one? Other than a requirement or chance for extra credit, I don't see value in squeezing one in to accomplish what you've described so far.

  7. #6
    Junior Member
    Join Date
    Aug 2014
    Posts
    14
    My Mood
    Worried
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: how to take array input from main method and use it in one of the classes

    Doubt
    i tried using attribute 'length' but in (books.length) 'books' corresponds to Book[].module 4c but it should correspond to my input Book[] books.
    i am giving the code plz help.

    package module4c;
     
    class Book{
    	String title;
    	String author;
    	int count;
     
    	public Book(){
     
    	}
    	public Book(String title,String author,int count){
    		this.title=title;
    		this.author = author;
    		this.count=count;
    	}
    class Library{
    	Book [] books;
     
    	public Library() {
    		// TODO Auto-generated constructor stub
    	}
    	public Library(Book[] books){
    		this.books= books;
     
    	}
    public int getdiffCount(){
    	int bs=0;
     
       bs = bs +books.length;
     
    		System.out.println(bs);
    		return bs;
    		}
    public class TestLibrary {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Library lib = new Library();
          Book book0 = new Book("A","AB",10);
          Book book1 = new Book("B","CD",20);
          Book book2 = new Book("C","EF",30);
          Book book3 = new Book("D","GH",40);
          Book [] books ={book0,book1,book2,book3};
     
          int b1 = lib.getCount(book1);
          int b2 = lib.getCount("A");
          int b3 = lib.getdiffCount();
          int b4 = lib.addBook(book3);
     
          System.out.println();
    	}


    --- Update ---

    can it be achieved using get or set method???

  8. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to take array input from main method and use it in one of the classes

    It's hard to tell what's going on with your code the way you've posted it. For example, the TestLibrary class is a Library nested class. Reorganize your code and indent it properly so that TestLibrary is THE top-level public class and all other classes are appropriately organized. Post your updated code when able.

  9. #8
    Junior Member
    Join Date
    Aug 2014
    Posts
    14
    My Mood
    Worried
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: how to take array input from main method and use it in one of the classes

    is it correct now



     
    package module4c;
     
    class Book{
    	String title;
    	String author;
    	int count;
     
    	public Book(){
     
    	}
    	public Book(String title,String author,int count){
    		this.title=title;
    		this.author = author;
    		this.count=count;
    	}
    class Library{
    	Book [] books;
     
    	public Library() {
    		// TODO Auto-generated constructor stub
    	}
    	public Library(Book[] books){
    		this.books= books;
     
    	}
    public int getdiffCount(){
    	int bs=0;
     
       bs = bs +books.length;
     
    		System.out.println(bs);
    		return bs;
    		}
    }
    public class TestLibrary {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Library lib = new Library();
          Book book0 = new Book("A","AB",10);
          Book book1 = new Book("B","CD",20);
          Book book2 = new Book("C","EF",30);
          Book book3 = new Book("D","GH",40);
          Book [] books ={book0,book1,book2,book3};
     
          int b1 = lib.getCount(book1);
          int b2 = lib.getCount("A");
          int b3 = lib.getdiffCount();
          int b4 = lib.addBook(book3);
     
          System.out.println();
    	}

  10. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to take array input from main method and use it in one of the classes

    No. Now TestLibrary is a Book nested class. I will repost your code indented correctly but still incorrectly organized. See if you can reorganize the code correctly while preserving correct indentation. Post your results.
    class Book{
        String title;
        String author;
        int count;
     
        public Book(){
     
        }
        public Book(String title,String author,int count){
            this.title=title;
            this.author = author;
            this.count=count;
        }
        class Library{
            Book [] books;
     
            public Library() {
                // TODO Auto-generated constructor stub
            }
            public Library(Book[] books){
                this.books= books;
     
            }
            public int getdiffCount(){
                int bs=0;
     
                bs = bs +books.length;
     
                System.out.println(bs);
                return bs;
            }
        }
        public class TestLibrary {
     
            public static void main(String[] args) {
                // TODO Auto-generated method stub
                Library lib = new Library();
                Book book0 = new Book("A","AB",10);
                Book book1 = new Book("B","CD",20);
                Book book2 = new Book("C","EF",30);
                Book book3 = new Book("D","GH",40);
                Book [] books ={book0,book1,book2,book3};
     
                int b1 = lib.getCount(book1);
                int b2 = lib.getCount("A");
                int b3 = lib.getdiffCount();
                int b4 = lib.addBook(book3);
     
                System.out.println();
            }
        }
    }
    What are you using for a source code editor or IDE?

  11. #10
    Junior Member
    Join Date
    Aug 2014
    Posts
    14
    My Mood
    Worried
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: how to take array input from main method and use it in one of the classes

    i am using eclipse as IDE

    i hope its correct now...

     
    class Book{
        String title;
        String author;
        int count;
     
        public Book(){
     
        }
        public Book(String title,String author,int count){
            this.title=title;
            this.author = author;
            this.count=count;
        }
        class Library{
            Book [] books;
     
            public Library() {
                // TODO Auto-generated constructor stub
            }
            public Library(Book[] books){
                this.books= books;
     
            }
            public int getdiffCount(){
                int bs=0;
     
                bs = bs +books.length;
     
                System.out.println(bs);
                return bs;
            }
        }
    }
        public class TestLibrary {
     
            public static void main(String[] args) {
                // TODO Auto-generated method stub
                Library lib = new Library();
                Book book0 = new Book("A","AB",10);
                Book book1 = new Book("B","CD",20);
                Book book2 = new Book("C","EF",30);
                Book book3 = new Book("D","GH",40);
                Book [] books ={book0,book1,book2,book3};
     
                int b1 = lib.getCount(book1);
                int b2 = lib.getCount("A");
                int b3 = lib.getdiffCount();
                int b4 = lib.addBook(book3);
     
                System.out.println();
            }
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: May 27th, 2014, 07:39 PM
  2. Asking for user input in the main method?
    By HeroProtagonist in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 19th, 2012, 07:00 AM
  3. Why cant I call on the array I returned in the main method?
    By ColeTrain in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 04:08 PM
  4. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  5. Paint program adding classes to main method class
    By Maxfmc in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 15th, 2011, 07:01 PM