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

Thread: Java Beginner: Simple Movie Ticket Program using Selection?

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Beginner: Simple Movie Ticket Program using Selection?

    Hello everyone,

    I'm a beginner at Java and have a question. I have an optional exercise I can do for uni to create a program and am stuck as to what to use to get the program to work. The question is asking me to do it using selection? I am pretty sure selection is like for sorting by a letter or something. Here is my code:

    package movieprogram;
    import java.util.Scanner;
     
    public class MovieProgram {
     
     
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner scanner1 = new Scanner(System.in);
            System.out.println("What is your name?");
            String name1 = scanner1.next();
            System.out.println("What is your mobile number?");
            int number = scanner1.nextInt();
            System.out.println("What is your age?");
            int age = scanner1.nextInt();
            System.out.println("What is the name of the movie?");
            String movie = scanner1.next();
            Scanner scanner2 = new Scanner(System.in);
            System.out.println("What is the time of the movie");
            int time = scanner2.nextInt();
     
            int ticketPrice = 20;
     
            if (age < 18)
                ticketPrice = 20/2;
            System.out.println("You are under 18 so you get 50% off!" + "Your price is: " + ticketPrice);
            else if (age >= 18 && age <= 65)
                ticketPrice = 20;
                    System.out.println("Your price is " + ticketPrice);
            else if (age > 65)
                ticketPrice = 15;
                        System.out.println("You are over 65. Your price is: " + ticketPrice);
        }
     
    }

    The question is
    1. MOVIE TICKETS: Describe the process of booking a movie ticket. The user must be asked for their name, mobile number and age. The user will be asked to provide a movie name and time. If the user is under 18, the movie ticket price is halved ($10). If the user is between 18 and 65, the movie ticket is normal ($20), if the user is over 65, the movie ticket is three quarters of the normal price ($15).

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    The question is asking me to do it using selection?
    I don't know what that means.
    Can you get a definition for what "selection" means?
    You need that to be able to design and write the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    Quote Originally Posted by Norm View Post
    I don't know what that means.
    Can you get a definition for what "selection" means?
    You need that to be able to design and write the code.
    Selection as in using a switch statement but I'm unsure how it would answer the question as it uses three conditions not one. It's just an example it can be done anyway I would like my teacher says.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    it can be done anyway I would like
    Ok, then would the use of if/else if statements the code uses satisfy that requirement?

    Note: The code should use {}s following each if and else statement to surround the code that is included.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    Quote Originally Posted by Norm View Post
    Ok, then would the use of if/else if statements the code uses satisfy that requirement?

    Note: The code should use {}s following each if and else statement to surround the code that is included.
    Yes it would. My code gives an error for some reason though

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    My code gives an error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package movieprogram;
    import java.util.Scanner;
     
    /**
     *
     * @author stefansandoval
     */
    public class MovieProgram {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner scanner1 = new Scanner(System.in);
            System.out.println("What is your name?");
            String name1 = scanner1.next();
            System.out.println("What is your mobile number?");
            int number = scanner1.nextInt();
            System.out.println("What is your age?");
            int age = scanner1.nextInt();
            System.out.println("What is the name of the movie?");
            String movie = scanner1.next();
            Scanner scanner2 = new Scanner(System.in);
            System.out.println("What is the time of the movie");
            int time = scanner2.nextInt();
     
            int ticketPrice = 20;
     
            if (age < 18) {
                ticketPrice = 20/2;
            System.out.println("You are under 18 so you get 50% off!" + "Your price is: " + ticketPrice);
            } else if (age >= 18 && age <= 65) {
                ticketPrice = 20;
                    System.out.println("Your price is " + ticketPrice);
            } else if (age > 65)
                ticketPrice = 15;
                        System.out.println("You are over 65. Your price is: " + ticketPrice);
        }
     
    }

    Here is my output:
    run:
    What is your name?
    BaldEagle
    What is your mobile number?
    0439291281
    What is your age?
    22
    What is the name of the movie?
    Batman
    What is the time of the movie
    8
    Your price is 20
    You are over 65. Your price is: 20


    So the problem now is that it tells me that my price is 20 as well as that I am over 65 and my price is 20. If I put an else instead of an else if on line 41 it says underlines it in red and says "not a statement. ';' expected"

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    Did you see this from post#4?
    Note: The code should use {}s following each if and else statement to surround the code that is included.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    Quote Originally Posted by Norm View Post
    Did you see this from post#4?
    Yes I did. It was missing the curly brackets in a few places. It works now thanks

  10. #10
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    Ok. Now I'd like to create a boolean IsVIP that discounts the ticketPrice which is 20 by 20%. I created these lines of code
    System.out.println("Are you a VIP?");
    Boolean isVIP = scanner2.nextBoolean();
     
           int ticketPrice = 20;
     
     
        if (age < 18) {
            ticketPrice = 20/2;
    System.out.println("Your price is " + ticketPrice);
    } else if (age >= 18 && age <= 65) {
    System.out.println("Your price is " + ticketPrice);
    } else if (age > 65) {
        ticketPrice = 15;
    System.out.println("Your price is " + ticketPrice);
    } else if (isVIP) {
        ticketPrice = ticketPrice - (0.2 * 20);
    }

    For that last line it gives the error "incompatible types: possible lossy conversion from double to int." Putting the ticketPrice as just 16 doesn't work it just uses the original price. I'm stuck as to how to fix this problem.

    --- Update ---

    Fixed the error now there is a problem with my curly brackets. On the last bracket it says reached end of file while parsing?
        if (age < 18) {
            ticketPrice = 20/2;
        System.out.println("Your price is " + ticketPrice);
        } else if (age >= 18 && age <= 65) {
        System.out.println("Your price is " + ticketPrice);
        } else if (age > 65) {
        ticketPrice = 15;
        System.out.println("Your price is " + ticketPrice);
        } else if (isVIP) {
        ticketPrice = (int)(20*(0.2/100.0f));
        }


    --- Update ---

    Added two more curly brackets on the end and the error disappeared. Now it still keeps the same price 20 for ticketPrice for isVIP when I run the program

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    it still keeps the same price 20 for ticketPrice for isVIP when I run the program
    Please post the complete code for the program
    and the full contents of the console that shows the input and output.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Beginner: Simple Movie Ticket Program using Selection?

    Fixed the problem Program is complete now!

Similar Threads

  1. Simple question pls help beginner learning java
    By DroidJava in forum Java Theory & Questions
    Replies: 2
    Last Post: November 12th, 2013, 01:04 AM
  2. Replies: 1
    Last Post: November 11th, 2011, 07:46 PM
  3. Simple beginner's password guessing program
    By edishuman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 12th, 2011, 03:59 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, 07:53 PM