Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

 Base “primitive” types: TypeDescriptionSize intThe integer type, with range -2,147,483, ,147,483,647 4 bytes byteThe type describing a single.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Chapter 4  Fundamental Data Types 1 Chapter 4 Fundamental Data Types.
Chapter 2 Using Objects. These slides for CSE 110 Sections are based in part on the textbook-authors ’ slides, which are copyright 2003 by Cay Horstmann.
03 Data types1June Data types CE : Fundamental Programming Techniques.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
Chapter 2 types, variables, methods Pages Horstmann.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
Computer Programming Fundamental Data Types Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons.
Integer Division What is x? int x = 1729 / 100; Answer: 17.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 4: Fundamental Data Types. To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware.
Chapter 4 Numeric types & arithmetic Strings: reading & writing.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 3 – Fundamental Data Types.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Unit 3: Java Data Types Math class and String class.
CHAPTER 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Fall 2006Slides adapted from Java Concepts companion slides1 Fundamental Data Types Advanced Programming ICOM 4015 Lecture 4 Reading: Java Concepts Chapter.
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Chapter 7: Characters, Strings, and the StringBuilder.
String and Scanner CS 21a: Introduction to Computing I First Semester,
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.
String class.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Using Objects.
Chapter 4 – Fundamental Data Types
Wednesday 09/23/13.
Lecture 10 Strings CSE /26/2018.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Computer Science
In this class, we will cover:
4.3 Arithmetic Operators and Mathematical Functions
Each object belongs to a class
Parameter: an input to a method
Presentation transcript:

Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from the call Math.round(x) ? 3.How do you round the double value x to the nearest int value, assuming that you know that it is less than 2 · 10 9 ?

Answers int and double When the fractional part of x is ≥ 0.5 By using a cast: (int) Math.round(x)

Self Check 4.What is the difference between the following two statements? and 5.What is stylistically wrong with the following statement? final double CM_PER_INCH = 2.54; public static final double CM_PER_INCH = 2.54; double circumference = 3.14 * diameter;

Answers 4.The first definition is used inside a method, the second inside a class 5.(1) You should use a named constant, not the "magic number" 3.14 (2) 3.14 is not an accurate representation of π

Self Check 6.What is the meaning of the following statement? 7.What is the value of n after the following sequence of statements? n--; n++; n--; balance = balance + amount;

Answers 6.The statement adds the amount value to the balance variable 7.One less than it was before

Self Check 8.What is the value of 1729 / 100 ? Of 1729 % 100 ? 9.Why doesn't the following statement compute the average of s1, s2, and s3 ? 10.What is the value of in mathematical notation? double average = s1 + s2 + s3 / 3; Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))

Answers and Only s3 is divided by 3. To get the correct result, use parentheses. Moreover, if s1, s2, and s3 are integers, you must divide by 3.0 to avoid integer division: 10. (s1 + s2 + s3) / 3.0

Self Check 11.Why can't you call x.pow(y) to compute x y ? 12.Is the call System.out.println(4) a static method call?

Answers 11. x is a number, not an object, and you cannot invoke methods on numbers Additionally, pow is static method of the Math class, and must be invoked by Math.pow(n) 12. No–the println method is called on the object System.out

Self Check 13.Assuming the String variable s holds the value "Agent", what is the effect of the assignment s = s + s.length() ? 14.Assuming the String variable river holds the value "Mississippi", what is the value of river.substring(1, 2) ? Of river.substring(2, river.length() - 3)?

Answers 13. s is set to the string Agent5 14. The strings "i" and "ssissi"

Strings A string is a sequence of characters Strings are objects of the String class String constants: String variables: String length: Empty string: "Hello, World!" String message = "Hello, World!"; int n = message.length(); ""

Concatenation Use the + operator: If one of the arguments of the + operator is a string, the other is converted to a string String name = "Dave"; String message = "Hello, " + name; // message is "Hello, Dave" String a = "Agent"; int n = 7; String bond = a + n; // bond is Agent7

Concatenation in Print Statements Useful to reduce the number of System.out.print instructions versus System.out.print("The total is "); System.out.println(total); System.out.println("The total is " + total);

Converting between Strings and Numbers Convert to number: Convert to string: int n = Integer.parseInt(str); double x = Double.parseDouble(str); String str = "" + n; str = Integer.toString(n);

Substrings Supply start and “past the end” position First position is at 0 Continued… Figure 3: String Positions String greeting = "Hello, World!"; String sub = greeting.substring(0, 5); // sub is "Hello"

Substrings Figure 4: Extracting a Substring Substring length is “past the end” - start

How do we find out the other methods which String objects provide??

Self Check 15.Why can't input be read directly from System.in ? 16.Suppose in is a Scanner object that reads from System.in, and your program calls String name = in.next(); What is the value of name if the user enters John Q. Public ?

Answers 15.The class only has a method to read a single byte. It would be very tedious to form characters, strings, and numbers from those bytes. 16.The value is "John". The next method reads the next word.

Self Check 6.How can you compute the length of the string "Mississippi" ? 7.How can you print out the uppercase version of "Hello, World!" ? 8.Is it legal to call river.println() ? Why or why not?

Answers It is not legal. The variable river has type String. The println method is not a method of the String class. river.length() or "Mississippi".length() System.out.println(greeting.toUpperCase());

Self Check String river = “Mississippi”; String greeting = “Hello World”; What are the implicit parameters, explicit parameters, and return values in the method call river.length() ? What is the result of the call river.replace("p", "s")? What is the result of the call greeting.replace("World", "Dave").length() ?

Answers 9. The implicit parameter is river. There is no explicit parameter. The return value is "Missississi"

Self Check String greeting = “Welcome”; String greeting2 = “How are you?”; What is the effect of the assignment greeting2 = greeting ? After executing the statement greeting2.toUpperCase(); what are the contents of greeting and greeting2 ?

Answers 24.Now greeting and greeting2 both refer to the same String object. 25.Both variables still refer to the same string, and the string has not been modified. Recall that the toUpperCase method constructs a new string that contains uppercase characters, leaving the original string unchanged.

The API Documentation API: Application Programming Interface Lists classes and methods in the Java library

The API Documentation of the Standard Java Library Figure 13: The API Documentation of the Standard Java Library

The API Documentation for the Rectangle Class Figure 14: The API Documentation of the Rectangle Class

Javadoc Method Summary Figure 15: The Method Summary for the Rectangle Class

translate Method Documentation Figure 16: The API Documentation of the translate Method