Computer Programming I - Unit 2 Lecture 1 of 13

Similar documents
AP Programming - Chapter 3 Lecture. An Introduction

The Math Class. Using various math class methods. Formatting the values.

AP Programming - Chapter 6 Lecture

Turing - Java Dictionary A Concise Guide for Translating Turing Constructs and Routines into Java

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes

AP CS Unit 3: Control Structures Notes

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

12. Numbers. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Full file at

Important Java terminology

Lecture 6: While Loops and the Math Class

AP Computer Science A. Return values

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

CS110: PROGRAMMING LANGUAGE I

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

CIS133J. Working with Numbers in Java

Java Classes: Math, Integer A C S L E C T U R E 8

Building Java Programs

A variable is a name for a location in memory A variable must be declared

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

4. Java Project Design, Input Methods

UNIT- 3 Introduction to C++

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long


Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Building Java Programs

Program Fundamentals

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Building Java Programs

Research Group. 2: More types, Methods, Conditionals

Lecture Set 4: More About Methods and More About Operators

Building Java Programs

Building Java Programs

Chapter 3. Numeric Types, Expressions, and Output

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

Excel for Algebra 1 Lesson 1: Basic Concepts and Button-ology

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

DATA TYPES AND EXPRESSIONS

1001ICT Introduction To Programming Lecture Notes

CS 302: Introduction to Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

MODULE 02: BASIC COMPUTATION IN JAVA

Outline. Data and Operations. Data Types. Integral Types

Pace University. Fundamental Concepts of CS121 1

Chapter 3: Operators, Expressions and Type Conversion

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Calculations, Formatting and Conversions

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Lecture Set 4: More About Methods and More About Operators

CHAPTER 3 Expressions, Functions, Output

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

CT 229 Java Syntax Continued

Expressions, Input, Output and Data Type Conversions

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Using Java Classes Fall 2018 Margaret Reid-Miller

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Introduction to Computer Science and Object-Oriented Programming

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

egrapher Language Reference Manual

Arithmetic and IO. 25 August 2017

Introduction to JAVA

Python Programming Exercises 1

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

CHAPTER 3: CORE PROGRAMMING ELEMENTS

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Programming Exercise 7: Static Methods

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

Downloaded from Chapter 2. Functions

Types and Expressions. Chapter 3

FUNDAMENTAL DATA TYPES

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

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

CS111: PROGRAMMING LANGUAGE II

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Full file at

Basics of Java Programming

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

Variables, Types, Operations on Numbers

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Built-in Types of Data

Building Java Programs

A Balanced Introduction to Computer Science, 3/E

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Advanced Object Concepts

A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University 2011 Pearson Prentice Hall ISBN

CS 335 Lecture 02 Java Programming

Zheng-Liang Lu Java Programming 45 / 79

JAVA Programming Fundamentals

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Transcription:

1 of 13 Precedence, Mixed Expressions, Math Methods, and Output Fmatting I. Precedence Rules - der of operations Introduction example: 4 + 2 X 3 =? A) Parenthesis, Division Multiplication (from left to right), Addition Subtraction (from left to right) ex.1) 2-5 + 1 ex.2) 4 / 2 * 2 ex.3) 3 + 6 / 3 * 2-1 ex.4) (3 + 6) / 3 * 2-1 + 2 B) Convert each of the following mathematical expressions to a Java arithmetical expression. Use parenthesis only where needed. ex.1) y = 5 a (integer quotient) ex.2) x = B + 2AC ex.3) x = 2 (m + n) ex.4) x = 3 - b r + 4 (floating point result)

2 of 13 II. Data Conversion with Mixed Data Types Recall: 1) If x is an integer type, then after the statement x = 5; x has the value of 5 sted in its memy location. 2) If x is a double float type, then after the statement x = 5.0; x has the value of 5.0 sted in its memy location. Note: You can only ste an integer in an integer variable and a floating point decimal in a float double. But what if we tried to ste an integer in a double a decimal in an integer variable?? A) Assignment conversion ( also called widening, promotion, type coercion) - the automatic conversion of a data type to another type due to mixing types in an expression. 1) Sting an integer in a decimal type: ex.1) If x is a double type, then after the statement x = 5; x has the value of 5.0 sted in its memy location. *When an integer is assigned to a decimal (double float type) variable the integer is automatically converted to a number with a decimal point and a zero after it! 2) Sting a decimal in an integer: ex.2) If x is an integer type, then the statement x = 5.9; is illegal in Java (but not in C++)! *When a decimal (double float type) can not directly be assigned to an integer variable.

3 of 13 B) Type Casting - the explicit conversion of a value from one data type to another, by using a typecast operat which is really just the name of the desired data type in parenthesis ex.1) int x; double y = 5.9; x = y; // illegal but you can fce the value of y into x by casting it: x = (int) y; x = (int) (y); but not x = int (y); **But the value in x is not 5.9 but 5. *When a decimal (double float type) is assigned to an integer variable by type casting the decimal part is truncated (dropped off)!

4 of 13 Mixing data types can cause problems in a program. ex.1) double x; int a = 30, b = 40; x = a / b; What is x after the last line? Answer: 0.0!!!!!! We can fix this result a couple of different ways: ex.1) double x; int a = 30, b = 40; x = (double)a/b; But NOT x = a/(double)b; x = (double)a/(double)b; (a and/ b could also be in parenthesis but are usually not in any of the above) x = (double)(a/b); Why? Because, the integers are divided first resulting in an integer result and when the result is converted the decimal part is truncated again to 0.0 as a final value in x. C) Arithmetic Promotion - the explicit conversion of a value from one data type to another due to an arithmetic operation involving mixed data types. 1) In a calculation involving a mixture of integers and decimals the integers are temparily converted to a decimal and the answer is a decimal. ex.1) y = 2; x = y + 3.5; a) What is x if x is an integer type? Answer: illegal statement b) What is x if x is a double float type? Answer: 5.5 c) What is y after the last line? Answer: 2 (not 2.0)

5 of 13 III. Math methods - predefined ( prewritten) methods that are in the Math class that is included in every Java program because the Math class is found in the java.lang package which is automatically impted into all Java programs A) Basic Methods to Know: (Note: random will covered in two pages) Type of Type of value Name Description argument returned Example Result sqrt square root double double Math.sqrt(4.0) 2.0 int double Math.sqrt(9) 3.0 int/double none Math.sqrt(-9.0) err pow powers double double Math.pow(2.0,3.0) 8.0 int double Math.pow(5,2) 25.0 abs absolute value int int Math.abs(-5) 5 int int Math.abs(5) 5 double double Math.abs(-5.3) 5.3 double double Math.abs(5.3) 5.3 ceil ceiling double double Math.ceil(5.2) 6.0 (round up) double double Math.ceil(5.9) 6.0 int double Math.ceil(5) 5.0 flo flo double double Math.flo(5.2) 5.0 (round down) double double Math.flo(5.9) 5.0 int double Math.flo(5) 5.0 round round to double double Math.round(5.2) 5.0 integer double double Math.round(5.9) 6.0 double double Math.round(-5.9) -6.0 int double Math.round(5) 5.0 (Note: All double's could also be floats.) Advanced topic: Math class is a static class therefe objects can not be created f it. The methods in Math class are static methods. We saw in Unit 1 that to use a method it needs to be accessed by connecting it to an object by the dot operat. Static methods are accessed by connecting them to the name of the static class by the dot operat so no object is needed (n could one even be created). ex.) Math.sqrt(x);

6 of 13 III. Math methods - continued 1) Determine the value of the following Java arithmetic expressions. (Note: Be careful of the answer's data type.) ex.1a) Math.sqrt(25.0) ex.1b) Math.sqrt(25) ex.2a) Math.pow(2.0, 5.0) ex.2b) Math.pow(2, 5) ex.3a) Math.abs(5) ex.3b) Math.abs(-5) ex.4a) Math.abs(3.9) ex.4b) Math.abs(-3.9) ex.5a) Math.ceil(5.1) ex.5b) Math.ceil(5.8) ex.6a) Math.flo(5.1) ex.6b) Math.flo(5.9) ex.7a) Math.round(5.5) ex.7b) Math.round(-5.9) ex.8) Math.sqrt(Math.abs(-36)) ex.9) Math.abs(Math.sqrt(pow(3,2))) 2) Convert the following mathematical expressions to a Java arithmetical expression ex.) x5 + y7 2 - d 3) Convert the following Java code into an algebraic expression. ex.) x = (Math.abs(Math.pow(b,2) - 4.0 * a * c))/(3 * a)

7 of 13 III. Math methods - continued A) random method (Note: Our blue book does not cover this one.) Math.random( ) produces a pseudandum random decimal number between 0 (inclusive) and n (exclusive) The following line would generate a random number between Big number and Small number inclusive: (int)(math.random( )*(Big - Small + 1)) + Small (int)(math.random( )*(Big - Small + 1) + Small) ex.1) Generate a random number between 5 and 10 inclusive Note 1: Big is the larger number, 10 in this example Note 2: Small is the smaller number, 5 in this example Note 3: the fmula needs a typecasting of int Answer: (int)(math.random( )*(10-5 + 1) + 5) (int)(math.random( )*(6) + 5) ex.2) generate a random number between 15 and 25 inclusive and ste in x Answer: int x = (int)(math.random( )*(11) + 15); ex.3) (int)(math.random( )*(100) + 10) will produce a random number between what two numbers? Answer: between 10 and 109 inclusive (wk: 100 = last - 10 + 1)

8 of 13 IV. Fmatting Output Recall: Given that a = 1, b = 2; find the Output of the following code fragment c.print("start" + a + b + "End"); c.print("start" + a + b + "End"); c.print("start"); c.print(a); c.print(b); c.print("end"); OUTPUT: Start12End A) Creating Blank Lines - use multiples of either: 1) println 2) \n enclosed in quotes (single double quotes if alone) Given that a = 1, b = 2; ex.1) c.println("start"); c.println( ); c.println( ); c.println(a); c.println(b); c.print("end"); ex.2) c.print("start\n\n\n"); c.print(a + '\n'); c.print(b + \n\n ); c.print("end"); OUTPUT: Start --> --> 1 2 --> End

9 of 13 IV. Fmatting Output - continued B) Inserting blanks within a line - use space(s), inside of quote marks, between items Note: Blank spaces are indicated by a ex.) Given that a = 1, b = 2; c.print( Start " + a + " " + " " + " " + b + " " + " " + "End"); c.print( Start " + a + " " + b + " End"); Output: Start 1 2 End

10 of 13 IV. Fmatting Output - continued D) Using Field Widths 1) Fmatting Strings - a number after the string will create a left justified partition ex.1) c.print( Bob A,7); c.print( G,5); Ans: Bob _ A G ex.2) c.print( Bob A,2); c.print( G,5); Ans: Bob _ AG (Note: If the width is too small it is igned) 2) Fmatting Integers - a number after the integer will create a right justified partition ex.1) c.print(123,7); c.print(56,4); Ans: _ Ans: 1 2 3 5 6 3) Fmatting Decimals - the first number after the integer will create a right justified partition and the second number is how many places after the decimal to round to ex.1) c.print(123.56,8,1); Ans: Ans: _ 1 2 3. 6 ex.2) c.print(123.56,8,4); Ans: (Note: If the width is too small it is igned) Ans: 1 2 3. 5 6 0 0

11 of 13 IV. Fmatting Output - continued ex.) c.println( 12345678901234567890 ); // already done below c.print( Hey,5); c.print( WhatsUp,12); c.println( Doc ); c.println(512, 5); c.print(67.8, 6, 1); c.println(765.987, 7, 2); c.println(765.987, 8, 1); c.println(765.987, 1, 3); c.println(765.987, 10, 5); Output: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 Answer: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 H e y W h a t s U p D o c 5 1 2 6 7. 8 7 6 5. 9 9 7 6 6. 0 7 6 5. 9 8 7 7 6 5. 9 8 7 0 0 *Note: You can not have a string and a number that is fmatted in the same print println statement ex1) c.println( Hi there + 67.89, 5, 1); is an err do: c.print( Hi there ); c.println(67.89, 5, 1); Output: Hi there 67.9

V. String Class Computer Programming I - Unit 2 Lecture A) Instantiating (declaring) a string *Note: A string is really an object of the String class Method 1: ex.1a) String x; x = new String( ); x = hello there ; 12 of 13 ex.1b) String x = new String( ); x = hello there ; Method 2: ex.2a) String x; x = hello there ; ex.2b) String x = hello there ; B) String Class Methods (see page 81 of the blue book f others) 1) tolowercase( ) - returns the string as all lower case letters 2) touppercase( ) - returns the string as all upper case letters 3) length( ) - returns the length of the string including spaces etc. 4) concat(stg2) - makes one longer string from the two ex.1) String string1 = WHEATON ; String string2 = warrenville ; String string3 = Wheaton Warrenville South ; string1.tolowercase( ) string2.touppercase( ) result: wheaton string3.length( ) result: 25 string1.concat(string2) result: WARRENVILLE result: WHEATONwarrenville ex.2) String z = string2.concat(string1); c.print(z); output: warrenvillewheaton Extra: System.out.print(5 + 2 + A ); output: 7A System.out.print( A + (5 + 2)); output: A7 System.out.print( A + 5 + 2); output: A52

VI. Graphics A) Graphics Computer Programming I - Unit 2 Lecture impt java.awt.*; // awt stands f the Abstract Window Toolkit /* The.* means that all Classes in this package will be impted and we will not have to list each one */ impt hsa.console; public class P1ch2lab7 { static Console c; public static void main(string[ ] args) { c = new Console( ); c.setcol(col.blue); c.drawrect(0, 0, 640, 450); c.fillrect(0, 0, 640, 450); c.setcol(col.red); c.drawoval(100, 100, 50, 50); c.filloval (100, 100, 50, 50); c.setcol(col.green); c.drawline(200, 10, 30, 300); } } 13 of 13 VI. Applets impt java.applet.*; //A package containing the Applet Class impt java.awt.*; public class P1ch2lab8 extends Applet // Needed whenever Applets are used { // so that it can run in a browser window public void paint (Graphics g) // Needed instead of main() when // doing Applets { setbackground(col.cyan); // Does not wk on Console screen g.setcol (Col.red); g.fillrect (0, 0, 100, 100); } }