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

Thread: Simple question about a ticketmachine assignment

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Simple question about a ticketmachine assignment

    /**
     * TicketMachine models a naive ticket machine that issues
     * flat-fare tickets.
     * The price of a ticket is specified via the constructor.
     * It is a naive machine in the sense that it trusts its users
     * to insert enough money before trying to print a ticket.
     * It also assumes that users enter sensible amounts.
     *
     * @author David J. Barnes and Michael Kölling
     * @version 2011.07.31
     */
    public class TicketMachine
    {
        // The price of a ticket from this machine.
        private int price;
        // The amount of money entered by a customer so far.
        private int balance;
        // The total amount of money collected by this machine.
        private int total;
     
        /**
         * Create a machine that issues tickets of the given price.
         * Note that the price must be greater than zero, and there
         * are no checks to ensure this.
         */
        public TicketMachine(int ticketcost)
        {
            price = ticketcost;
            balance = 0;
            total = 0;
        }
     
        /**
         * blablabla
         */
        public int getTotal()
        {
            return total;
        }
     
        /**
         * hallon
         */
        public void setPrice(int ticketCost)
        {
     
        }
     
           /**
         * Return the price of a ticket.
         */
        public int getPrice()
        {
            return price;
        }
     
        /**
         * Return the amount of money already inserted for the
         * next ticket.
         */
        public int getBalance()
        {
            return balance;
        }
     
        /**
         * Receive an amount of money from a customer.
         */
        public void insertMoney(int amount)
        {
            balance = balance + amount;
        }
     
        /**
         * Print a ticket.
         * Update the total collected and
         * reduce the balance to zero.
         */
        public void printTicket()
        {
            // Simulate the printing of a ticket.
            System.out.println("##################");
            System.out.println("# The BlueJ Line");
            System.out.println("# Ticket");
            System.out.println("# " + price + " cents.");
            System.out.println("##################");
            System.out.println();
     
            // Update the total collected with the balance.
            total = total + balance;
            // Clear the balance.
            balance = 0;
        }
    }

    Excercise 2.30: Complete the body of the setPrice method so that it assigns the value of its parameter to the price field.

    Does this mean i have to fill in price = ticketcost in the body? I doubt it, since that's already in the constructor. Can anyone help me out?


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Simple question about a ticketmachine assignment

    Hello Ypmaha!
    Quote Originally Posted by Ypmaha View Post
    [code]
    Does this mean i have to fill in price = ticketcost in the body? I doubt it, since that's already in the constructor. Can anyone help me out?
    I think that yes, this is what it means. Instead of doing this assignment in your constructor you have to do it in your setPrice() method. And in your constructor invoke the setPrice() method with the parameter you pass in it (int ticketcost).

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple question about a ticketmachine assignment

    Now i need to add an object to give a discount on a ticket:

    /**
         * Reduce price by the given amount
         */
        public void discount(int amount)
        {
            amount = ticketcost - discount;
    }

    Now, why do i get an error message showing: "cannot find symbol - variable ticketcost"?

    EDIT: nevermind, didn't know i had to create a discount object.
    Last edited by Ypmaha; March 18th, 2012 at 04:36 PM.

  4. #4
    Junior Member andolasoft's Avatar
    Join Date
    Feb 2012
    Location
    USA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple question about a ticketmachine assignment

    This is the Ticket Machine for the train system in Sweden, or at least in Uppsala and Stockholm.

    The first time that you fall on it, and you don’t understand Swedish it’s almost impossible to print a ticket.

    The device is quite simple, they tried to make it the more cheap and small as possible.

    The display don’t show anything until you pass your Credit Card in the slot. So if you don’t have a Credit Card is not possible to do a ticket and you have to go to the shelter and make it the old way, usually there is a long queue waiting there.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple question about a ticketmachine assignment

    really? a creditcard to buy a trainticket? I don't think that's very safe

    Anyway, i've tried every possible i could think of, but i can't solve this one:
    I need to implement a method to change the price of a ticket. i came up with this, but it's not working:

    [code]
    /**
    * TicketMachine models a naive ticket machine that issues
    * flat-fare tickets.
    * The price of a ticket is specified via the constructor.
    * It is a naive machine in the sense that it trusts its users
    * to insert enough money before trying to print a ticket.
    * It also assumes that users enter sensible amounts.
    *
    * @author David J. Barnes and Michael Kölling
    * @version 2011.07.31
    */
    public class TicketMachine
    {
    // The price of a ticket from this machine.
    private int price;
    // The amount of money entered by a customer so far.
    private int balance;
    // The total amount of money collected by this machine.
    private int total;
    // The score
    private int score;
    // a discount
    private int discount;
    // new price
    private int newprice;


    /**
    * Create a machine that issues tickets of the given price.
    * Note that the price must be greater than zero, and there
    * are no checks to ensure this.
    */
    public TicketMachine(int ticketcost)
    {
    price = ticketcost;
    balance = 0;
    total = 0;
    discount = 50;
    newprice = price;
    }

    /*
    * Remove all money from the machine
    */
    public void empty()
    {
    total = 0;
    }

    /**
    * Shows a message that asks to insert the right amount of money
    */
    public void prompt()
    {
    System.out.println("Please insert the correct amount of money");
    System.out.println();
    }

    /**
    * Shows the price of a ticket
    */
    public void showPrice()
    {
    System.out.println("The price of a ticket is " + price + " cents.");
    }

    /**
    * Get the total amount of money inserted in the Ticketmachine
    */
    public int getTotal()
    {
    return total;
    }

    /**
    *
    */
    public void setPrice(int ticketCost)
    {
    ticketCost = price;
    }

    /**
    * Set a new price for a ticket
    */
    public void setnewprice(int newprice)
    {
    newprice = price;
    }


    /**
    * Return the price of a ticket.
    */
    public int getPrice()
    {
    return price;
    }

    /**
    * Reduce price by the given amount
    */
    public void discount(int amount)
    {
    amount = price - discount;
    }

    /**
    * Increase score by the given number of points
    */
    public void increase(int points)
    {
    points = score + points;
    }

    /**
    * Return the amount of money already inserted for the
    * next ticket.
    */
    public int getBalance()
    {
    return balance;
    }

    /**
    * Receive an amount of money from a customer.
    */
    public void insertMoney(int amount)
    {
    balance = balance + amount;
    }

    /**
    * Print a ticket.
    * Update the total collected and
    * reduce the balance to zero.
    */
    public void printTicket()
    {
    // Simulate the printing of a ticket.
    System.out.println("##################");
    System.out.println("# The BlueJ Line");
    System.out.println("# Ticket");
    System.out.println("# " + price + " cents.");
    System.out.println("##################");
    System.out.println();

    // Update the total collected with the balance.
    total = total + balance;
    // Clear the balance.
    balance = 0;
    }
    }

    the highlighted area's are the things i have added. i have also tried things like
    setnewPrice(int ticketCost)
    {
                ticketCost= newprice
    }

    or

    price = newprice

    but it's not working, and i don't know why
    Last edited by Ypmaha; March 20th, 2012 at 07:34 AM.

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Simple question about a ticketmachine assignment

    Quote Originally Posted by Ypmaha View Post
    Anyway, i've tried every possible i could think of, but i can't solve this one:
    I need to implement a method to change the price of a ticket.
    Your setPrice() method is supposed to do what you want. If you implement it as i suggested you in post #2, then you can call it to change the price of a ticket.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple question about a ticketmachine assignment

    Excercise 2.49: Write an assignment statement that will store the result of mulitplying two variables: price and discount, into a third variable, saving. How is price a saving? and how can i only write an assignment statement without creating a method?

  8. #8
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Simple question about a ticketmachine assignment

    Quote Originally Posted by Ypmaha View Post
    how can i only write an assignment statement without creating a method?
    You can declare the new variable (saving) as class member like price and discount and then do the assignment inside your constructor.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple question about a ticketmachine assignment

    what if i want to add multiple tickets with each a different price to my machine?

  10. #10
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Simple question about a ticketmachine assignment

    Quote Originally Posted by Ypmaha View Post
    what if i want to add multiple tickets with each a different price to my machine?
    Can you explain what exactly you are you trying to do?
    You can create more instances of your TicketMachine and give them the ticketcost you want.
    Also, it would help if you posted your current code.

  11. #11
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple question about a ticketmachine assignment

    Quote Originally Posted by andreas90 View Post
    Can you explain what exactly you are you trying to do?
    You can create more instances of your TicketMachine and give them the ticketcost you want.
    Also, it would help if you posted your current code.
    I skipped that assignment, because it was incomplete.

    Thanks alot for the help so far, everyone.

    I have another one:

    Add conditional statements to the constructor of Student to print an error message if either the length of the fullname parameter is less than four characters or the length of studentID parameter is less than three characters. However the constructor should still use those parameters to set the name and id fields, even if the error message is printed. Hint: Use if statements of the following form (that is, having no else part) to print the error messages.

    if(perform a test on one of the parameters ) {
                   Print an error message if the test gave a true result
                     }

    This is the complete code:

     
    /**
     * The Student class represents a student in a student administration system.
     * It holds the student details relevant in our context.
     * 
     * @author Michael Kölling and David Barnes
     * @version 2011.07.31
     */
    public class Student
    {
        // the student's full name
        private String name;
        // the student ID
        private String id;
        // the amount of credits for study taken so far
        private int credits;
     
        /**
         * Create a new student with a given name and ID number.
         */
        public Student(String fullName, String studentID)
        {
            name = fullName;
            id = studentID;
            credits = 0;                    
        }
     
        /**
         * Return the number of characters in this string
         */
        public int length()
        {
            if(name =< 4) {
                System.out.println("That not enough characters, brah");
                System.out.println();
            }
        }
     
        /**
         * Return the full name of this student.
         */
        public String getName()
        {
            return name;
        }
     
        /**
         * Set a new name for this student.
         */
        public void changeName(String replacementName)
        {
            name = replacementName;
        }
     
        /**
         * Return the student ID of this student.
         */
        public String getStudentID()
        {
            return id;
        }
     
        /**
         * Add some credit points to the student's accumulated credits.
         */
        public void addCredits(int additionalPoints)
        {
            credits += additionalPoints;
        }
     
        /**
         * Return the number of credit points this student has accumulated.
         */
        public int getCredits()
        {
            return credits;
        }
     
        /**
         * Return the login name of this student. The login name is a combination
         * of the first four characters of the student's name and the first three
         * characters of the student's ID number.
         */
        public String getLoginName()
        {
            return name.substring(0,4) + id.substring(0,3);
        }
     
        /**
         * Print the student's name and ID number to the output terminal.
         */
        public void print()
        {
            System.out.println(name + ", student ID: " + id + ", credits: " + credits);
        }
    }

    This is the object which i've added (which you can also find in the complete code):
    /**
         * Return the number of characters in this string
         */
        public int length()
        {
            if(name =< 4) {
                System.out.println("That not enough characters, brah");
                System.out.println();
            }
        }

    I didn't write anything for studentID, because i want to keep it simple. What is it that im doing wrong?

  12. #12
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Simple question about a ticketmachine assignment

    Quote Originally Posted by Ypmaha View Post
    This is the object which i've added (which you can also find in the complete code):
    /**
         * Return the number of characters in this string
         */
        public int length()
        {
            if(name =< 4) {
                System.out.println("That not enough characters, brah");
                System.out.println();
            }
        }

    I didn't write anything for studentID, because i want to keep it simple. What is it that im doing wrong?
    This is not an object, it's a method.
    Your length() method has errors. By writing int when you define it, the compiler expects it to return an int value. If the only thing you want it to do is just print a message you can declare it void (which means it returns nothing). Another thing is that in your if statement you compare a String with an int value (name =< 4). The String class has a length() method the returned value of which you can compare with the int value you want. Therefore it would be better if you gave a different name to your method.
    I would recommend you reading the Java API - especially for methods.

  13. The Following User Says Thank You to andreas90 For This Useful Post:

    Ypmaha (March 29th, 2012)

  14. #13
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple question about a ticketmachine assignment

    Quote Originally Posted by andreas90 View Post
    This is not an object, it's a method.
    Your length() method has errors. By writing int when you define it, the compiler expects it to return an int value. If the only thing you want it to do is just print a message you can declare it void (which means it returns nothing). Another thing is that in your if statement you compare a String with an int value (name =< 4). The String class has a length() method the returned value of which you can compare with the int value you want. Therefore it would be better if you gave a different name to your method.
    I would recommend you reading the Java API - especially for methods.
    I get it. I guess the main question is: "how can Ticketmachine tell me how much characters a string has? i copied the signature of the length method, so i doubt if i need to replace int with void. as you can see the getLoginName method uses name.substring(0,4). i tried to do something like that as well in the length method, but it only gave me errors (mostly illegal start of type)

  15. #14
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Simple question about a ticketmachine assignment

    Quote Originally Posted by Ypmaha View Post
    "how can Ticketmachine tell me how much characters a string has?
    The String.length() method does this.
    Quote Originally Posted by Ypmaha View Post
    i copied the signature of the length method, so i doubt if i need to replace int with void. as you can see the getLoginName method uses name.substring(0,4). i tried to do something like that as well in the length method, but it only gave me errors (mostly illegal start of type)
    What exactly does YOUR length() method do? Because if it does the same as String.length() then you don't need it. Can you post it and explain what you tried and what are the exact errors?

  16. #15
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple question about a ticketmachine assignment

    Quote Originally Posted by andreas90 View Post
    The String.length() method does this.

    What exactly does YOUR length() method do? Because if it does the same as String.length() then you don't need it. Can you post it and explain what you tried and what are the exact errors?
    i understand it a little better.

    I came up with this:

    if (fullName.length < 4) studentID.length <3) {
                System.out.println("That not enough characters, brah");
                System.out.println();}

    But it gave me the error: "not a statement"
    When i removed studentID.length <3 it gave me the error: "cannot find symbol- variable length"
    Last edited by Ypmaha; March 29th, 2012 at 05:38 AM.

  17. #16
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple question about a ticketmachine assignment

    nevermind i got it fixed:

    if (fullName.length() < 4 | studentID.length() < 3) {

  18. #17
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Simple question about a ticketmachine assignment

    Glad you got it work.
    Just a little note: i would suggest using the logical OR || instead of the bitwise inclusive OR |.
    Check the javadoc for Operators to see the differencies between them.

  19. #18
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple question about a ticketmachine assignment

    Thanks did that,

    class Book
    {
        // The fields.
        private String author;
        private String title;
        private String pages;
     
        /**
         * Set the author and title fields when this object
         * is constructed.
         */
        public Book(String bookAuthor, String bookTitle, bookPages)
        {
            author = bookAuthor;
            title = bookTitle;
            pages = bookPages;

    Can anyone tell me why i get the error: "identifier expected" between 'bookPages' and ')' in the method header? Isn't ')' the identifier?

    EDIT: NEVERMIND, SOMETIMES IM BLIND
    Last edited by Ypmaha; March 30th, 2012 at 11:30 AM.

Similar Threads

  1. Help with assignment about a board game (enum and array question)
    By Kranti1992 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2012, 07:47 AM
  2. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 06:44 AM
  3. Simple question
    By white97 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 11th, 2011, 06:30 PM
  4. Beginner needs help with simple java assignment.
    By joachim89 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2010, 06:53 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 02:47 AM