Friday, September 6, 2013

JAVA Case Study #1: Movie Tickets

 CASE STUDY #1:  Movie Tickets

Let's look at this code for a movie ticket program:
import java.util.Scanner;
class TicketPrice {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int age;
double price = 0.00;
System.out.print("How old are you? ");
age = myScanner.nextInt();
if (age >= 12 && age < 65) {
price = 9.25;
}
if (age < 12 || age >= 65) {
price = 5.25;
}
System.out.print("Please pay $");
System.out.print(price);
System.out.print(". ");
System.out.println("Enjoy the show!");
}
}
 
  • First of all, what is the name of this Class?
  • What are we bringing into the Class from Java that is already built for us?
  • What does "Scanner" do?
  • What does "double" mean in Java?
  • What variables are we using in this code?
  • What could you substitute for, "if (age <12 | | age >= 65)  {"
  • What are some of the things that are missing in this code?
  • What would the ticket price be for an 8 year old?  A 14 year old?  A 66 year old?
  • Draw a flow chart indicating the flow of this program.
Next, let's update the source code:
  • Begin by formatting the source code to be more readable -- add line breaks, indent (Tab) some of the lines, etc.  (See the next case study for an idea of how to format it)
  • Add some comments to the code.  Remember that every Class needs a title, author(s), and version (number or dates) and maybe some comments describing what a formula or function does.

No comments:

Post a Comment