Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with else statments please

    Hey Guys, (first post!)

    I have an assignment on a Library class i think i'm almost there except for the fact I have four constructors for four methorods and only two are required.

    I have been fiddling around with if else for the last week with little luck below are my objectives and the program (libraryitem) and tester (libraryitemtest).

    I am still getting confused between constructors and methods i have read a few online books and asked my teacher also for help but i still don't get it.

    The code below works, I tried to use if else but made a mess of it and it was a little too confusing so i have left it out.

    Below is are the requirements and code:

    a) Attributes title, author, media, status,cost . Choose Appropriate data types.

    b) Code a constructor to handle 5 inputs.

    c) Code a constructor to handle title and author only, Otherwise default values.

    d) Code methods to set media. status and cost.

    e) Code a method to display author and title of all overdue items.

    f) Code a method to display all data of an object.
    -----------------------------------------------------------------
    public class libraryitem {

    String title;
    String author;
    String media;
    String status;
    Double cost;


    libraryitem (String tit, String aut, String med, String sta, Double cos) {

    title=tit;
    author=aut;
    media=med;
    status=sta;
    cost=cos;
    }


    libraryitem (String tit, String aut) {

    title=tit;
    author=aut;
    }


    libraryitem (String med, String sta, Double cos) {


    media=med;
    status=sta;
    cost=cos;

    }

    libraryitem (String med, String sta, String tit) {

    media=med;
    status=sta;
    title=tit;
    }

    public String geteverycat() {
    return title+ ", " + author + ", "+media+ ", " +status+ ", "+cost;

    }

    public String gettitleauthonly() {
    return title+ ", " +author;


    }


    public String getmedstacosonly() {
    return media+ ", " +status+ ", "+cost;


    }

    public String getmedstatitonly() {
    return media+ ", " +status+ ", "+title;

    }


    }

    -----------------------------------------------------------------
    public class libraryitemtest {
    public static void main ( String [] args){

    libraryitem libraryitemtestone = new libraryitem ("Java Programming","Vikki","Book","Onloan",35.95);
    libraryitem libraryitemtesttwo = new libraryitem ("Visual Basic Programming","Bob Down");
    libraryitem libraryitemtestthree = new libraryitem ("DVD","Overdue",23.45);
    libraryitem libraryitemtestfour = new libraryitem ("Magazine","OnShelf","C++ Tips");

    String allcat;
    String titaut;
    String medstacos;
    String medstatit;

    allcat=libraryitemtestone.geteverycat();
    titaut=libraryitemtesttwo.gettitleauthonly();
    medstacos=libraryitemtestthree.getmedstacosonly();
    medstatit=libraryitemtestfour.getmedstatitonly();


    System.out.println("TITLE AUTHOR MEDIA STATUS AND COST = "+allcat);
    System.out.println("TITLE AUTHOR = "+titaut);
    System.out.println("MEDIA STATUS AND COST = "+medstacos);
    System.out.println("MEDIA STATUS TITLE = "+medstatit);
    }
    }

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Java and javascript are entirely different. Request a moderator to move this thread to the right sub forum.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    ok, moved to the Java Forum
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by Kor View Post
    ok, moved to the Java Forum
    But a little late! Now there are two threads
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I have no power within this Forum, I can't delete this one nor the duplicate.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry about the double post :/

    So can someone help me with trimming my constructors from four down to two ?

  • #7
    New Coder
    Join Date
    May 2007
    Location
    SF, CA
    Posts
    55
    Thanks
    0
    Thanked 4 Times in 4 Posts
    You need to identify the constructors. Constructors in java are pretty easy to identify as they have the same name as the class. So your class is named libraryitem, so you should start there. You should find five different classes.

    After you've identified the constructors you can look at the requirements for b and c.

    b) Code a constructor to handle 5 inputs.

    c) Code a constructor to handle title and author only, Otherwise default values.

    So of the two constructors that you want to keep, you want one that has five parameters (the parameters are in the parentheses) and the other has two (a title and author).

    Hope this helps.

    semper fi...

    javabits


  •  

    Posting Permissions

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