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

    Help with else statments please.

    (please delete my post under javascript moderator)


    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
    New Coder
    Join Date
    Jun 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts
    hm okay. First of all, does status mean out or in? Because if so, then it should probably a boolean. I don't see any need to have if or else statements besides maybe for the

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

    step.

    You would do if (status.equals("overdue")) return author + ", " + title; (or w/e)

    Also, you dont have any mutators (methods to set variable). These are really easy. For example, to set media, you just need a method

    public void setMedia(String media) {
    media = this.media;
    }

    The "this." is used for shadowing, so when you have the same name for a variable in the parameter and in the class, you can use this. to refer to the variable in the parameter.

    Okay, hope that helps

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This:
    Code:
    public void setMedia(String media) {
    media = this.media;
    }
    Is incorrect. The variable 'media' exists only on the stack, and when you attempt to apply this.media to it, it will then be crushed, regardless of what this.media contains it will lose all reference to the correct value. Reverse those:
    Code:
    public void setMedia(String media)
    {
       this.media = media;
    }
    Assignment is always from right into left, except where you use multiple: a = b = c = 1 for example.
    Constructors in java can be chained so you can save yourself a lot of work:
    Code:
    public class MyClass
    {
        public MyClass(int id)
        {
             this(id, "");
        }
        public MyClass(String name)
        {
             this(0, name);
        }
        public MyClass(int id, String name)
        {
             this.id = id;
             this.name = name;
        }
    }
    for example. A constructor is called when the class is instantiated through the use of new. They have no return method.
    A method is used as a call to an object or class to perform some code functionality. For example:
    Code:
    public class Math
    {
        public static double divide(double d1, double d2) 
        {
             if (d2 == 0)
             {
                 throw new IllegalArgumentException("Cannot divide by zero!");
             }
             return d1 / d2;
        }
    }
    // Call with:
    Math.divide(4, 2);
    Above is a static example, which is simply a method that can be called without the use of an object, just a class. So you cannot do:
    Code:
    Math m = new Math();
    m.divide(1, 2);
    for example.
    To display the data in an object, overload the object ToString method. This way you can just do a system.out.println(obj) and it will display what you want . Maybe add another method that simply returns the tostring value for it.
    This one here
    e) Code a method to display author and title of all overdue items.
    Is impossible to do within the objects themselves. What you would need to do is add a static function that takes an array of libraryItem, and loop through each checking on the status to determine what to display. Prototype would be:
    public static libraryitem[] getOverdueItems(libraryitem[] items);
    I would put that in the class, it makes sense to keep it together.

    I hope that helps you, I didn't put too much by way of information (we have strict rules about helping with homework), but enough detail you should be able to find what you are looking for. Good luck mate!

    Edit:
    One other thing to mention. If you're allowed to do it in your course, you may want to use an enum for your status, especially if its only say three or four possible value. This would make checking the status extremely simple.
    Last edited by Fou-Lu; 06-18-2008 at 12:02 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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