Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Chapter 6: User-Defined Functions I
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects.
CSC Programming I Lecture 5 August 30, 2002.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Exposure Java-A 2006 Chapter 4 Slides Using Methods and Parameters
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 7: Return values, Math, and double reading: 3.2,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson All rights reserved.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
The Math Class Methods Utilizing the Important Math Operations of Java!
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Math Class Mrs. C. Furman September 2, Math frequently used methods NameUse floor()rounds down ceil()rounds up pow(x,y)returns x to the power of.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Java Statements –Java Expressions –Postfix Expressions –Prefix Expressions –Evaluating.
Java Keywords Java has special keywords that have meaning in Java. You have already seen a fair amount of keywords. Examples are: public, main, System,
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
Java Keywords Java has special keywords that have meaning in Java. You have already seen a fair amount of keywords. Examples are: public, main, System,
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
Object Oriented Programming one of the main reasons we now teach Java instead of C++. C++ was designed to be backwardly compatible with the original.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
Lecture 6: While Loops and the Math Class
Introduction to Modular Programming
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 Mathematical Functions, Characters, and Strings
Math Methods that return values
Building Java Programs
Object Oriented Systems Lecture 03 Method
PowerPoint Presentation Authors of Exposure Java
Chapter 3 Methods.
First Programs CSE 1310 – Introduction to Computers and Programming
Static and non-Static Chapter 5.
Chapter 4: Mathematical Functions, Characters, and Strings
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Building Java Programs
Chapter 5 Methods.
Lecture 6: While Loops and the Math Class
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Chapter 6: User-Defined Functions I
Building Java Programs
Building Java Programs
Using java libraries CGS3416 spring 2019.
Chapter 2: Java Fundamentals cont’d
Introduction to Computer Science and Object-Oriented Programming
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

Java Program Components

Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples are: public, main, System, int, double, print, void - and there will be many more you will learn during this course.

Java Program Statements - Java keywords make up Java program statements. - You have already seen, and written, a fair amount of program statements. Examples are: int x; //Declaration Statement x = 5; //Initialization Statement int y = 10; //Declaration & Initialization System.out.println(y); //Print statement int z = x + y; //Math expression statement

Java Methods - Java program statements make up methods. -You have only seen one example of a method in the programs you have written, and that’s the main method. -Every Java program has to have a main method, but could contain more than one method that perform different functions/tasks. -The main method is called the “driver” method because it drives the execution of the program. In this next unit, you will learn how to call and use Java methods other than the main method.

Java Classes - Java methods make up Java classes. -You have only seen one example of a class in the Java programs you have written, and that’s the public class. -Every Java program has to have a public class, and can contain more than one class, but only one “public” class. -The main method in the public class may contain “calls” to methods other than the main method. In this next unit, you will learn how to call and use pre- written Java methods other than the main method, which perform or accomplish certain tasks.

Comparing English & Java EnglishJava ComponentExampleComponentExample wordtigerkeywordpublic sentenceThe tiger is big.program statement System.out.print("The tiger is big."); paragraphMy sister and I went to the zoo. We saw many animals. The tigers were very scary. They were large, very loud and they smelled bad. We liked the funny monkeys better. methodpublic static void main(String args[]) { int a = 100; int b = 200; int sum = a + b; System.out.println(sum); } chapter or essay Our Trip to the Zoo Opening paragraph Middle paragraphs Closing paragraph classpublic class Demo { public static void main(String args[]) { System.out.println("Hello"); }

Grammar & Syntax Rules  As in any other language, Java has “grammar” rules that must be followed in order for the Java compiler to understand and interpret Java program statements as valid, clear and unambiguous commands.  These rules are called “syntax” rules in Java.  If these rules are violated in a Java program, the program will not compile.

Fundamental Java Syntax Rules  All program statements end with a semi-colon.  All program statements are contained in a method.  All methods are contained in a class.  Each method must have a heading.  Each class must have a heading.  Class and method containers start with a { brace.  Class and method containers end with a } brace.  Method headings and class heading are not program statements and they do not get a semi-colon.  All keywords in Java are case-sensitive. This means that System and system are two different words.  Comments, which are not program statements, start with two slashes and do not require a semi-colon. Examples of these rules are shown on the next slide.

Fundamental Java Syntax Rules Program Example: public class Example// class, called Example, heading {// start of the Example class container public static void main (String args[])//method, called main, heading {//start of the main method container int a = 10;//program statement int b = 25;//program statement System.out.println();//program statement System.out.println(a);//program statement System.out.println(b);//program statement System.out.println();//program statement }//end of the main method container }//end of the Example class container

Math Class Methods NamePurpose sqrt Returns the square root of the number/parameter passed. floor Returns the number/parameter passed rounded up. ceil Returns the number/parameter passed rounded down. round Returns the number/parameter passed rounded with conventional rules. max Returns the larger of the two numbers/parameters passed. min Returns the smaller of the two numbers/parameters passed. abs Returns the absolute value of the number/parameter passed. pow Returns the result of raising the base number (1 st parameter) to the power of the exponent (2 nd parameter).

Method Arguments or Parameters The information, which is passed to a method is called an argument or a parameter. Parameters are placed between parentheses immediately following the method identifier. Parameters can be constants, variables, expressions or they can be methods. The only requirement is that the correct data type value is passed to the method. In other words, Math.sqrt(x) can compute the square root of x, if x is any correct number, but not if x equals "aardvark".

Class Method Syntax Math.sqrt(n1) 1. Math is the class identifier, which contains the methods you call. 2.  separates the class identifier from the method identifier 3. sqrt is the method identifier 4. (n1) n1 is the argument or parameter passed to the method

// Java0401.java // This program shows how to use the method of the Math // class. The Math class is part of the java.lang package, which is // automatically loaded (imported) by the compiler. // Math.sqrt returns the square root of the argument. public class Java0401 { public static void main (String args[]) { System.out.println("\nJAVA0401.JAVA\n"); int n1 = 625; double n2 = 6.25; System.out.println("Square root of " + n1 + ": " + Math.sqrt(n1)); System.out.println("Square root of " + n2 + ": " + Math.sqrt(n2)); System.out.println(); }

// Java0402.java // This program shows different arguments that can be used with the // method. Note how a method call can be the argument of another method call. public class Java0402 { public static void main (String args[]) { System.out.println("\nJAVA0402.JAVA\n"); double n1, n2, n3, n4; n1 = Math.sqrt(1024);// constant argument n2 = Math.sqrt(n1);// variable argument n3 = Math.sqrt(n1 + n2); // expression argument n4 = Math.sqrt(Math.sqrt(256)); // method argument System.out.println("n1: " + n1); System.out.println("n2: " + n2); System.out.println("n3: " + n3); System.out.println("n4: " + n4); System.out.println(); }

// Java0403.java // This program demonstrates the and methods. // The method returns the truncation down to the next lower integer. // The method returns the next higher integer. // The method rounds the argument and returns the closest integer. public class Java0403 { public static void main (String args[]) { System.out.println("\nJAVA0403.JAVA\n"); System.out.println("Math.floor(5.001): " + Math.floor(5.001)); System.out.println("Math.floor(5.999): " + Math.floor(5.999)); System.out.println("Math.floor(5.5) : " + Math.floor(5.5)); System.out.println("Math.floor(5.499): " + Math.floor(5.499)); System.out.println(); System.out.println("Math.ceil(5.001) : " + Math.ceil(5.001)); System.out.println("Math.ceil(5.999) : " + Math.ceil(5.999)); System.out.println("Math.ceil(5.5) : " + Math.ceil(5.5)); System.out.println("Math.ceil(5.499) : " + Math.ceil(5.499)); System.out.println(); System.out.println("Math.round(5.001): " + Math.round(5.001)); System.out.println("Math.round(5.999): " + Math.round(5.999)); System.out.println("Math.round(5.5) : " + Math.round(5.5)); System.out.println("Math.round(5.499): " + Math.round(5.499)); System.out.println(); }

// Java0404.java // This program demonstrates the and methods. // Math.max returns the largest value of the two arguments. // Math.min returns the smallest value of the two arguments. public class Java0404 { public static void main (String args[]) { System.out.println("\nJAVA0404.JAVA\n"); System.out.println("Math.max(100,200): " + Math.max(100,200)); System.out.println("Math.max(-10,-20): " + Math.max(-10,-20)); System.out.println("Math.max(500,500): " + Math.max(500,500)); System.out.println(); System.out.println("Math.min(100,200): " + Math.min(100,200)); System.out.println("Math.min(-10,-20): " + Math.min(-10,-20)); System.out.println("Math.min(500,500): " + Math.min(500,500)); System.out.println(); }

// Java0405.java // This program demonstrates the and methods. // Math.abs returns the absolute value of the argument. // Math.pow returns the first argument raised to the power // of the second argument. public class Java0405 { public static void main (String args[]) { System.out.println("\nJAVA0405.JAVA\n"); System.out.println("Math.abs(-25): " + Math.abs(-25)); System.out.println("Math.abs(100): " + Math.abs(100)); System.out.println("Math.abs(0) : " + Math.abs(0)); System.out.println(); System.out.println("Math.pow(3,4) : " + Math.pow(3,4)); System.out.println("Math.pow(-2,2): " + Math.pow(-2,2)); System.out.println("Math.pow(2,-2): " + Math.pow(2,-2)); System.out.println(); }

// Java0406.java // This program demonstrates the and fields of the // Math class. // Both and are "final" attributes of the class. // and are not methods. Note there are no parentheses. public class Java0406 { public static void main (String args[]) { System.out.println("\nJAVA0406.JAVA\n"); System.out.println("Math.PI: " + Math.PI); System.out.println("Math.E : " + Math.E); System.out.println(); }