?
Solved

using a boolean in creating a class

Posted on 2004-10-19
16
Medium Priority
?
325 Views
Last Modified: 2010-03-31
I'm trying to create a class that stores a name, id number and the number for a library book borrowed by a student. I also want to give three methods to checkout a book and return a book.

The methods I'm supposed to be using resemble:

public boolean bkBorrow(long CN)

public boolean bkReturn(long CN)

I want them to change catNumber (defined as a long) to the CN of the book but if the student already has a book out (ie: catNumber != -1) it wont let them do it.

Similarly if the student tries to return a book that does not match the one they borrowed it would return an error.

I'm not sure how to work with booleans at all =(
0
Comment
Question by:Wraith10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 2
16 Comments
 

Author Comment

by:Wraith10
ID: 12353816
What I have so far:

public class LibraryAccount {
private String name;
private int studentId;
private long catNumber;

public LibraryAccount(string n, int id){
n=name;
id=123456789;
}
public string getName(){
return name;
}
public string getStudentID {
return StudentID + "";
}
public double getCatNumber(){
return catNumber;
}

public String toString()
String s;
if (catNumber = -1){
s=name + "has no book on loan."
else
s = name + "has catalogue number" + catNumber + "on loan."
}
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12353859
try this:


public class test {
      public String name;
      public long id;
      public long catNumber;
      
      public test(String thisname,long thisid){
            name=thisname;
            id=thisid;
            catNumber=-1;
      }
      
      public boolean bkBorrow(long CN){
            if(catNumber!=-1){
                  System.out.println("Already borrowed book!");
                  return false;
            }else{
                  catNumber=CN;
                  System.out.println("borrowed book!");
                  return true;
            }                  
      }

      public boolean bkReturn(long CN){
            if(catNumber!=CN){
                  System.out.println("You do not have this book!");
                  return false;
            }else{
                  catNumber=-1;
                  System.out.println("returned book!");
                  return true;
            }
      }
      
      public static void main(String[] args){
            test student1=new test("a",1L);            
            //borrow
            boolean value=false;
            value=student1.bkBorrow(1L);
            System.out.println(value);
            //error here
            value=student1.bkBorrow(1L);
            System.out.println(value);
            value=student1.bkReturn(2L);
            System.out.println(value);
            //return
            value=student1.bkReturn(1L);            
            System.out.println(value);
      }
}
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12354187
Well,

I think that what fredwangus gave you is sufficient. But why do you have CN as long? I think that for the book, normally, the ID contains 0 in the front such as 000019232. I think that you should use String better.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:suprapto45
ID: 12354223
Hi,

Please tell me if you need some additional help. Good program fredwangus :).
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12354382
:)
0
 

Author Comment

by:Wraith10
ID: 12354449
The output is formatted correctly except I do not think it is assigning the account names properly I get output like:

First LibraryAccount: LibraryAccount@f4a24a
when it should be
First LibraryAccount: Ryan Smith

I'm using a separate file to turn the data I need called SmLib.java

public class SmLib {
   public static void main(String[] args) {
      LibraryAccount ryan = new LibraryAccount("Ryan Smith", 94000);
      LibraryAccount cassie = new LibraryAccount("Cassie Cambell", 77000);

      System.out.println("\nFirst LibraryAccount: " + ryan);
      System.out.println("Second LibraryAccount: " + cassie);

      ryan.bookBorrow(111111111);
      System.out.println("\nFirst LibraryAccount after bookBorrow:\n" + ryan + "\n");

      ryan.bookReturn(111111111);
      System.out.println("First LibraryAccount after bookReturn:\n" + ryan);

      cassie.bookBorrow(444444444);
      System.out.println("\nSecond LibraryAccount after bookBorrow:\n" + cassie);

      if(cassie.bookBorrow(111111111)) System.out.println("\nCassie successfully borrowed a second book.");
      else System.out.println("\nCassie tried to borrow a second book. - DENIED");
      System.out.println("\nSecond LibraryAccount:\n" + cassie);

      if(cassie.bookReturn(111111111)) System.out.println("\nCassie successfully returned an unborrowed book.");
      else System.out.println("\nCassie tried to return a book she did not check out. - DENIED");
      System.out.println("\nSecond LibraryAccount:\n" + cassie + "\n");

   } // main method
} // SmLib
0
 

Author Comment

by:Wraith10
ID: 12354540
Bah there is also some error with my:

public String toString()
String s;
if (catNumber = -1){
s=name + "has no book on loan."
else
s = name + "has catalogue number" + catNumber + "on loan."
}

It keeps telling me illegal start of type for the ip statement and missing method body for the toString part.
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12354566
public String toString() {
String s;
if (catNumber = -1){
s=name + "has no book on loan."
else
s = name + "has catalogue number" + catNumber + "on loan."
}

guess u missed the {
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12354577
    System.out.println("\nFirst LibraryAccount: " + ryan);
      System.out.println("Second LibraryAccount: " + cassie);

don't print the class Libraryaccount directly
use the member variables instead

eg, if the member is 'name'

use

System.out.println("\nFirst LibraryAccount: " + ryan.getName());
0
 

Author Comment

by:Wraith10
ID: 12354651
I missed a couple ; as well but once I put the bracket in the compiler actually found that.

I dont have a choice about the way the file is printed. That's a stock file I have to use as that is how my source code will be tested for correct output.

I now have:

public String toString(); {
  String s;
  if (catNumber = -1){
  s=getName() + " has no book on loan.";
  }
else {
  s=getName() + " has catalogue number" + getCatNumber() + " on loan.";
  }
}

I get a compile arrow pointing to "toString" saying missing method body or declare abstract and a second error on the if(catNumber) saying incompatible type found long required boolean
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12354674
public String toString() {
  String s;
  if (catNumber = -1){
  s=getName() + " has no book on loan.";
  }
else {
  s=getName() + " has catalogue number" + getCatNumber() + " on loan.";
  }
}

u added a ; after toString()
should remove it
0
 

Author Comment

by:Wraith10
ID: 12354801
Gah I feel like such an idiot.

I have that segment solved. In addition to removing the ; I need to add a return s; to the end.

The output for the code is now working nearly perfectly expect that it is now giving a value of null instead of the Library account names still AND it says they have no book on loan even when they should in fact have a book on loan.

For example I get an output of:

"null has no book on loan"

when it should be

"Ryan Smith has catalogue number 111111111 on loan"

I'm still not sure how to fix that. I'm going to post a full copy of my current code. If you see my comment after your ":)" post it has the output file that I am using (which I cannot modify).


public class LibraryAccount {
    private String name;
    private int studentID;
    private long catNumber;

public LibraryAccount(string n, int id){
   n=name;
   id=123456789;
   catNumber = -1;
}

public string getName(){
return name;
}
public string getStudentID {
return StudentID + "";
}
public double getCatNumber(){
return catNumber;
}

public boolean bookBorrow(long bookCN){
          if(catNumber!=-1){
               System.out.println(getName() + " tried to borrow a second book - DENIED");
               return false;
          }else{
               catNumber=bookCN;
               return true;
          }              
     }

     public boolean bookReturn(long bookCN){
          if(catNumber!= bookCN){
               System.out.println(getName() + " tried to return a book she did not check out. - DENIED");
               return false;
          }else{
               catNumber=-1;
               return true;
          }
     }


public String toString() {
  String s;
  if (true){
  s=getName() + " has no book on loan.";
  }
else {
  s=getName() + " has catalogue number" + getCatNumber() + " on loan.";
  }
return s;
}


}


Currently it is ouputing:
First LibraryAccount: null has no book on loan
Second LibraryAccount: null has no book on loan

First LibraryAccount after bookBorrow:
null has no book on loan

It should output:
First LibraryAccount: Ryan Smith has no book on loan
Second LibraryAccount: Cassie Cambell has no book on loan

First LibraryAccount after bookBorrow:
Ryan Smith has catalogue number 111111111 on loan
0
 

Author Comment

by:Wraith10
ID: 12354850
Ignore my last post a friend of mine found the name problem. It should have been name = n not n = name.

However I still cannot get it to register that a book has been borrowed. It says they have no book on loan no matter what.

public class LibraryAccount {
    private String name;
    private int studentID;
    private long catNumber;

public LibraryAccount(string n, int id){
   name=n;
   id=123456789;
   catNumber = -1;
}

public string getName(){
return name;
}
public string getStudentID {
return StudentID + "";
}
public double getCatNumber(){
return catNumber;
}

public boolean bookBorrow(long bookCN){
          if(catNumber!=-1){
               System.out.println(getName() + " tried to borrow a second book - DENIED");
               return false;
          }else{
               catNumber=bookCN;
               return true;
          }              
     }

     public boolean bookReturn(long bookCN){
          if(catNumber!= bookCN){
               System.out.println(getName() + " tried to return a book she did not check out. - DENIED");
               return false;
          }else{
               catNumber=-1;
               return true;
          }
     }


public String toString() {
  String s;
  if (true){
  s=getName() + " has no book on loan.";
  }
else {
  s=getName() + " has catalogue number" + getCatNumber() + " on loan.";
  }
return s;
}


}


Currently it is ouputing:
First LibraryAccount: Ryan Smith has no book on loan
Second LibraryAccount: Cassie Cambell has no book on loan

First LibraryAccount after bookBorrow:
Ryan Smith has no book on loan

It should output:
First LibraryAccount: Ryan Smith has no book on loan
Second LibraryAccount: Cassie Cambell has no book on loan

First LibraryAccount after bookBorrow:
Ryan Smith has catalogue number 111111111 on loan
0
 
LVL 2

Accepted Solution

by:
fredwangus earned 2000 total points
ID: 12354864
u made several small mistakes, try this and see whether the output is as expected:

public class LibraryAccount {
    private String name;
    private int studentID;
    private long catNumber;

public LibraryAccount(String n, int id){
   name=n;
   id=123456789;
   catNumber = -1;
}

public String getName(){
return name;
}
public String getStudentID() {
return studentID + "";
}

public double getCatNumber(){
return catNumber;
}

public boolean bookBorrow(long bookCN){
          if(catNumber!=-1){
               System.out.println(getName() + " tried to borrow a second book - DENIED");
               return false;
          }else{
               catNumber=bookCN;
               return true;
          }              
     }

     public boolean bookReturn(long bookCN){
          if(catNumber!= bookCN){
               System.out.println(getName() + " tried to return a book she did not check out. - DENIED");
               return false;
          }else{
               catNumber=-1;
               return true;
          }
     }


public String toString() {
  String s;
  if (catNumber==-1L){
  s=getName() + " has no book on loan.";
  }
else {
  s=getName() + " has catalogue number" + getCatNumber() + " on loan.";
  }
return s;
}


}


public class test {
        public static void main(String[] args) {
            LibraryAccount ryan = new LibraryAccount("Ryan Smith", 94000);
            LibraryAccount cassie = new LibraryAccount("Cassie Cambell", 77000);

            System.out.println("\nFirst LibraryAccount: " + ryan);
            System.out.println("Second LibraryAccount: " + cassie);

            ryan.bookBorrow(111111111L);
            System.out.println("\nFirst LibraryAccount after bookBorrow:\n" + ryan + "\n");

            ryan.bookReturn(111111111);
            System.out.println("First LibraryAccount after bookReturn:\n" + ryan);

            cassie.bookBorrow(444444444);
            System.out.println("\nSecond LibraryAccount after bookBorrow:\n" + cassie);

            if(cassie.bookBorrow(111111111)) System.out.println("\nCassie successfully borrowed a second book.");
            else System.out.println("\nCassie tried to borrow a second book. - DENIED");
            System.out.println("\nSecond LibraryAccount:\n" + cassie);

            if(cassie.bookReturn(111111111)) System.out.println("\nCassie successfully returned an unborrowed book.");
            else System.out.println("\nCassie tried to return a book she did not check out. - DENIED");
            System.out.println("\nSecond LibraryAccount:\n" + cassie + "\n");

         } // main method
}
0
 

Author Comment

by:Wraith10
ID: 12354970
Thanks for all your help (and patience). I just need to fine tune why I am getting a double output when she tries to borrow a second book and I should be all set.
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12355106
u r welcome :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question