Tuesday, 15 June 2010

java - Boolean return statement (To make it not print true/false) -



java - Boolean return statement (To make it not print true/false) -

my programme runs , compiles. however, not desired output. outputs of needed information, prints out things not like. along patron, author , books, lot of true/false statements printed:

/*true (current out print statement) true true true name: ken lambert title: cider house rules author: john irving title: perfect storm author: sebastian junger title: illiad author: homer title: hamlet author: william shakespeare true false false name: ken lambert title: perfect storm author: sebastian junger title: illiad author: homer title: hamlet author: william shakespeare (current print statement) name: ken lambert (desired print statement) title: cider house rules author: john irving title: perfect storm author: sebastian junger title: illiad author: homer title: hamlet author: william shakespeare name: ken lambert title: perfect storm author: sebastian junger title: illiad author: homer title: hamlet author: william shakespeare*/ public class book { private string author, title; public book(string t, string a){ author = a; title = t; } public string getauthor(){ homecoming author; } public string gettitle(){ homecoming title; } public string tostring(){ homecoming "title: " + title + "\n" + "author: " + author; } } public class librarytester{ public static void main (string[] args){ patron p = new patron("ken lambert"); book b1 = new book("cider house rules", "john irving"); book b2 = new book("the perfect storm", "sebastian junger"); book b3 = new book("the illiad", "homer"); book b4 = new book("hamlet", "william shakespeare"); system.out.println(p.borrowbook(b1)); system.out.println(p.borrowbook(b2)); system.out.println(p.borrowbook(b3)); system.out.println(p.borrowbook(b4)); system.out.println(p); system.out.println(p.returnbook("cider house rules")); system.out.println(p.hasbook("cider house rules")); system.out.println(p.hasbook("the perfect storm")); system.out.println(p); } } public class patron{ book b1, b2, b3, b4; private string name; public patron(string nm){ name = nm; b1 = null; b2 = null; b3 = null; b4 = null; } public string getname(string nm){ if(name.equals(null)) system.out.println("you must set in name!"); else name = nm; homecoming name; } public boolean borrowbook(book bbook){ boolean test = false; if (b1 == null){ b1 = bbook; test = true; } else if (b2 == null){ b2 = bbook; test = true; } else if(b3 == null){ b3 = bbook; test = true; } else if(b4 == null){ b4 = bbook; test = true; } else { test = false; } homecoming test; } public boolean hasbook(string title){ if(b1 != null) if(b1.gettitle().equals(title)){ homecoming true; } else if(b2 != null) if(b2.gettitle().equals(title)){ homecoming true; } else if(b3 != null) if(b3.gettitle().equals(title)){ homecoming true; } else if(b4 != null) if(b4.gettitle().equals(title)){ homecoming true; } homecoming false; } public boolean returnbook(string b){ if(b1.gettitle().equals(b)){ b1 = null; homecoming true; } else if(b2.gettitle().equals(b)){ b2 = null; homecoming true; } else if(b3.gettitle().equals(b)){ b3 = null; homecoming true; } else if (b4.gettitle().equals(b)){ b4 = null; homecoming true; } else { homecoming false; } } public string tostring(){ string str = "name: " + name; if(b1 != null) str = str + "\n" + b1; if(b2 != null) str = str + "\n" + b2; if(b3 != null) str = str + "\n" + b3; if(b4 != null) str = str + "\n" + b4; homecoming str; } }//class

the method public boolean borrowbook(book bbook) returns boolean. since print homecoming of method, prints... booleans !

system.out.println(p.borrowbook(b1)); system.out.println(p.borrowbook(b2)); system.out.println(p.borrowbook(b3)); system.out.println(p.borrowbook(b4));

java boolean

No comments:

Post a Comment