Anatomy of a Java Program: Comments

Similar documents
Professor: Alvin Chao

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Chapter 6 Lab Classes and Objects

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Chapter 6 Lab Classes and Objects

AP Computer Science Unit 1. Programs

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction to Computer Science Unit 2. Notes

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Input. Scanner keyboard = new Scanner(System.in); String name;

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Programming with Java

Supplementary Test 1

CS 302: Introduction to Programming

Welcome to the Primitives and Expressions Lab!

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java

St. Edmund Preparatory High School Brooklyn, NY

Chapter 1 Lab Algorithms, Errors, and Testing

Chapter 1 Introduction to Computers, Programs, and Java. What is a Computer? A Bit of History

CS110: PROGRAMMING LANGUAGE I

Eng. Mohammed S. Abdualal

Lesson 7 Part 2 Flags

CS11 Java. Fall Lecture 1

Object-oriented programming in...

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

2.8. Decision Making: Equality and Relational Operators

Introduction to Java Applications; Input/Output and Operators

Fundamentals of Programming Data Types & Methods

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Chapter 4: Conditionals and Recursion

Chapter 2: Basic Elements of Java

Full file at

Loops. CSE 114, Computer Science 1 Stony Brook University

CS111: PROGRAMMING LANGUAGE II

CS 11 java track: lecture 1

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Chapter 5 Lab Methods

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

Basic Computation. Chapter 2

Computational Expression

Object Communication

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Introduction to Computer Science Unit 2. Exercises

C Sc 227 Project 1: Three Main Methods

Section 2.2 Your First Program in Java: Printing a Line of Text

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

Introduction to Computer Science Unit 2. Notes

Chapter 1 Introduction to Java

CSIS 10A Assignment 4 SOLUTIONS

A token is a sequence of characters not including any whitespace.

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter Two Bonus Lesson: JavaDoc

What did we talk about last time? Examples switch statements

IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar

Topics. The Development Process

Character Stream : It provides a convenient means for handling input and output of characters.

Chapter 5 Lab Methods

CMSC131. Introduction to your Introduction to Java. Why Java?

Array. Array Declaration:

AP Computer Science A Unit 2. Exercises

Text User Interfaces. Keyboard IO plus

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Getting Started With Java

Peer Instruction 1. Elementary Programming

Evaluating the Style of your programs

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Introduction to Computers, Programs, and Java. CSE 114, Computer Science 1 Stony Brook University

AP Computer Science Unit 1. Writing Programs Using BlueJ

ITERATION WEEK 4: EXMAPLES IN CLASS

Problem Solving With Loops

Midterms Save the Dates!

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Basic Programming Elements

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Lecture 8 " INPUT " Instructor: Craig Duckett

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Eng. Mohammed Alokshiya

Chapter 02: Using Data

Object Oriented Programming. Java-Lecture 1

CSE Module 1. A Programming Primer 1/23/19 CSE 1321 MODULE 1 1

Lesson 10: Quiz #1 and Getting User Input (W03D2)

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Basic Computation. Chapter 2

Over and Over Again GEEN163

4. Java language basics: Function. Minhaeng Lee

PROGRAMMING STYLE. Fundamentals of Computer Science I

Section 2.2 Your First Program in Java: Printing a Line of Text

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Program Control Flow

Program Control Flow

JAVA Programming Concepts

Transcription:

CS149

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

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

Anatomy of a Java Program: Classes 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 { 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 Braces { and enclose blocks of code

Anatomy of a Java Program: Methods Method named collection of Java statements: public class ConvertInches { public static void main(string[] args) { Late r

Anatomy of a Java Program: Methods Method named collection of Java statements: public class ConvertInches { public static void main(string[] args) { Late r return type (void means nothing is returned)

Anatomy of a Java Program: Methods Method named collection of Java statements: public class ConvertInches { public static void main(string[] args) { Late r return type (void means nothing is returned) method name main is the starting point for all Java programs

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

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

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

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

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

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

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; // Create a scanner for standard input. Scanner keyboard; keyboard = new Scanner(System.in); // 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 "); multiplication + joins strings (or adds numbers)

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

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

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.");

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.");

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.");