section 0006 instructor alvin chao professor alvin chao
play

Section 0006 Instructor: Alvin Chao Professor: Alvin Chao Anatomy - PowerPoint PPT Presentation

Welcome to CS 149 Section 0006 Instructor: Alvin Chao Professor: Alvin Chao Anatomy of a Java Program: Comments l Javadoc comments: /** l * Application that converts inches to centimeters. * l * @author Chris Mayfield l * @version


  1. Welcome to CS 149 Section 0006 Instructor: Alvin Chao

  2. Professor: Alvin Chao

  3. Anatomy of a Java Program: Comments l Javadoc comments: /** l * Application that converts inches to centimeters. * l * @author Chris Mayfield l * @version 01/21/2014 */ l l Everything between /** and */ ignored by compiler l Used to generate code documentation l

  4. Anatomy of a Java Program: Comments l Block comments are used for text that should not be part of the published documentation: l /* Permission is hereby granted, free of charge, to any l person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the l Software without restriction. */ l l In-line comments are used for short clarifying statements: l // Create a scanner for standard input.

  5. Anatomy of a Java Program: Classes l Java is an object-oriented language (OO) − Java classes tie together instructions and data − All Java code must exist within some class public class ConvertInches { l l } l l public and class are keywords: Words that have a special meaning for Java. − public – (more later) − class – Create a class with the following name. (Must match the file name) − Class names are always captalized l Braces { and } enclose blocks of code l

  6. Anatomy of a Java Program: Methods l Method – named collection of Java statements: l l public class ConvertInches { l public static void main(String[] args) { l } l } l Later

  7. Anatomy of a Java Program: Methods l Method – named collection of Java statements: l l public class ConvertInches { l public static void main(String[] args) { l } l } l Later return type ( void means nothing is returned)

  8. Anatomy of a Java Program: Methods l Method – named collection of Java statements: l l public class ConvertInches { l public static void main(String[] args) { l } l } l return type method name Later ( void means “main” is the starting nothing is point for all Java returned) programs

  9. Anatomy of a Java Program: Methods l Method – named collection of Java statements: l l public class ConvertInches { l public static void main(String[] args) { l } l } l return type method name argument type Later String[] means that ( void means “main” is the starting this method takes an nothing is point for all Java array of Strings. returned) programs

  10. Anatomy of a Java Program: Methods l Method – named collection argument name args will be an array of of Java statements: Strings from the command line. a rgs[0], args[1] , etc. l l public class ConvertInches { l public static void main(String[] args) { l } l } l return type method name argument type Later String[] means that ( void means “main” is the starting this method takes an nothing is point for all Java array of Strings. returned) programs

  11. Anatomy of a Java Program: Declaring and Assigning Variables l variable – named box for storing data: l name type Should always be Defines what the l informative. “x” is not OK. variable can hold l l int inch; l double cent; final double CENT_PER_INCH; l CENT_PER_INCH = 2.54;

  12. Anatomy of a Java Program: Declaring and Assigning Variables l variable – named box for storing data: l name type Should always be Defines what the l informative. “x” is not OK. variable can hold l l int inch; l double cent; final double CENT_PER_INCH; l CENT_PER_INCH = 2.54; assignment literal value Puts the value on the right into the variable on the left. ALWAYS RIGHT TO LEFT!

  13. Anatomy of a Java Program: Declaring and Assigning Variables l variable – named box for storing data: l name type Should always be Defines what the l informative. “x” is not OK. variable can hold l l int inch; final l double cent; makes this final double CENT_PER_INCH; variable a l constant CENT_PER_INCH = 2.54; assignment literal value Puts the value on the right into the variable on the left. ALWAYS RIGHT TO LEFT!

  14. Anatomy of a Java Program: Standard Library and Keyboard Input import import java.util.Scanner; “Brings in” external classes /** * Application that converts inches to centimeters. * * @author Chris Mayfield * @version 01/21/2014 */ public class ConvertInches { public static void main(String[] args) { int inch; double cent; final double CENT_PER_INCH; The Scanner class, along CENT_PER_INCH = 2.54; with System.in are used to // Create a scanner for standard input. read user input from the Scanner keyboard; terminal keyboard = new Scanner(System.in); // Prompt the user and get the value. System.out.print("How many inches? "); inch = keyboard.nextInt();

  15. Putting it all together... import java.util.Scanner; /** * Application that converts inches to centimeters. * * @author Chris Mayfield * @version 01/21/2014 */ public class ConvertInches { public static void main(String[] args) { int inch; double cent; final double CENT_PER_INCH; CENT_PER_INCH = 2.54; multiplication // Create a scanner for standard input. Scanner keyboard; keyboard = new Scanner(System.in); + joins strings (or adds numbers) // Prompt the user and get the value. System.out.print("How many inches? "); inch = keyboard.nextInt(); // Convert and output the result. cent = inch * CENT_PER_INCH; System.out.print(inch + "in = "); System.out.println(cent + "cm "); } }

  16. Reminder: Portability l Most “high-level” languages are considered portable because they can be compiled into machine code for any computer: x86 Program x86 Compiler C Program ARM Compiler ARM Program

  17. Java Compilation l Byte Code Files are portable because there are JVM's that run on most machines l The same compiled byte code works on any JVM

  18. Which is Syntactically Correct? public static void main(String[] args) { System.out.println("Hello " + args[0] + "!"); System.out.println("Welcome to CS139."); } public class Personal { public static void main(String[] args) { System.out.println("Hello " + args[0] + "!"); System.out.println("Welcome to CS139."); } } public class Personal { // public static void main(String[] args) { System.out.println("Hello " + args[0] + "!"); System.out.println("Welcome to CS139."); } }

  19. Which is Syntactically Correct? (File name is Good.java) public class Welcome { public static void main(String[] args) { String name; name = "Bob"; System.out.println("Hello " + name + "!"); System.out.println("Welcome to CS139."); } } public class Good { public static void main(String[] args) { String name; "Bob" = name; System.out.println("Hello " + name + "!"); System.out.println("Welcome to CS139."); } } public class Good { public static void main(String[] args) { String name; name = "Bob"; System.out.println("Hello " + name + "!"); System.out.println("Welcome to CS139."); } }

  20. Which is Syntactically Correct? public class Good public static void main(String[] args) { String name; name = "Bob"; System.out.println("Hello " + name + "!"); System.out.println("Welcome to CS139."); } } public class Good { public static void main(String[] args) { String name; name = "Bob"; System.out.println("Hello " + name + "!") System.out.println("Welcome to CS139."); } } public class Good { public static void main(String[] args){ String name; name = "Bob"; System.out.println("Hello " + name + "!"); System.out.println("Welcome to CS139.");} }

  21. </end>

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend