Introduction to Computer Science Unit 2. Notes

Similar documents
Introduction to Computer Science Unit 2. Notes

AP CS Unit 3: Control Structures Notes

Introduction to Computer Science Unit 2. Exercises

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

AP Computer Science Unit 1. Programs

Important Java terminology

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

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

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

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

M e t h o d s a n d P a r a m e t e r s

4. Java Project Design, Input Methods

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Programming with Java

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Midterm Examination (MTA)

CIS133J. Working with Numbers in Java

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Supplementary Test 1

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

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

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Topics. Chapter 5. Equality Operators

AP Computer Science Java Mr. Clausen Program 6A, 6B

AP Computer Science A Unit 2. Exercises

NATIONAL UNIVERSITY OF SINGAPORE

CS110: PROGRAMMING LANGUAGE I

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

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

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Practice Midterm 1 Answer Key

Practice Midterm 1. Problem Points Score TOTAL 50

A Balanced Introduction to Computer Science, 3/E

Pace University. Fundamental Concepts of CS121 1

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

Advanced Java Concept Unit 1. Mostly Review

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

Lecture 6: While Loops and the Math Class

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled


Formatting Output & Enumerated Types & Wrapper Classes

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

What did we talk about last time? Examples switch statements

Chapter 3. Selections

DM503 Programming B. Peter Schneider-Kamp.

Oct Decision Structures cont d

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

CS141 Programming Assignment #6

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

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

CS111: PROGRAMMING LANGUAGE II

CT 229 Java Syntax Continued

Lecture Set 2: Starting Java

Research Group. 2: More types, Methods, Conditionals

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

JAVA OPERATORS GENERAL

AP Computer Science A. Return values

Welcome to the Primitives and Expressions Lab!

Lecture Set 2: Starting Java

Anatomy of a Java Program: Comments

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Following is the general form of a typical decision making structure found in most of the programming languages:

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Using Java Classes Fall 2018 Margaret Reid-Miller

DM550 Introduction to Programming part 2. Jan Baumbach.

Java I/O and Control Structures

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Java I/O and Control Structures Algorithms in everyday life

Operators. Java operators are classified into three categories:

Using API in Java. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

2.2 - Making Decisions

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

( &% class MyClass { }

Computer Programming I - Unit 2 Lecture 1 of 13

Lecture 5: Methods CS2301

ICSE Class 10 Computer Applications ( Java ) 2014 Solved Question Paper

double float char In a method: final typename variablename = expression ;

Chapter 6 Methods. Dr. Hikmat Jaber

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

Programming Practice (vs Principles)

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

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

APCS Semester #1 Final Exam Practice Problems

Chapter 3: Operators, Expressions and Type Conversion

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Advanced Object Concepts

Building Java Programs

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

CS 200 Using Objects. Jim Williams, PhD

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

AP Computer Science Unit 1. Writing Programs Using BlueJ

Calculations, Formatting and Conversions

Transcription:

Introduction to Computer Science Unit 2. Notes Name: Objectives: By the completion of this packet, students should be able to describe the difference between.java and.class files and the JVM. create and use flow charts evaluate and use Boolean expressions create and use if and if-else statements use the && (AND) and ( OR ) operators use some methods of the Math class. Compiling Code in Java A compiler is a program that translates code to machine language that the central processing unit (the computer's "brain") can understand. In many languages, the source code must be customized for different operating systems and then compiled for certain types of processors. Java works as follows:.java files The source code is saved in text files that end in.java 1..class files Java Compiler The compiler translates the text into byte code. Byte code is machine language for a virtual machine (e.g. computer or phone). 2. PC JVM machine language Mac JVM machine language Linux JVM machine language The Java Virtual Machine (JVM) is software that translates Byte code into local machine code. The JVM also runs the code. 3. Programmers need the compiler and JVM to develop software. A finished program is delivered as a collection of class files. End users need only the JVM to run the class files. Page 1

Boolean Expressions A Boolean expression is an expression that is either or. For example: x >= 8 The above expression compares x to 8. If x is greater than or equal to 8, then the expression is. Otherwise the expression is. Java uses 6 relational operators in its Boolean expressions. > < >= <= ==!= (means not equal) 1. If x is an int with a value of 12, what is the value of this expression? 2. If num is an int with a value of 22, what is the value of this expression? 3. If y is an int with a value of 7, what is the value of this expression? Boolean expressions can be manipulated the same way inequalities in math can be manipulated to produce equivalent expressions. Rewrite the expression so that all x terms are combined on the left and the coefficient is 1. x > 20 + 5 * x -5 *x - 5* x -4 * x > 20-4 * x > 20-4 -4 x < -5 x > 10 num == 14 50 <= y 4. Are the two expressions equivalent? 5. Are the two expressions equivalent? 6. Are the two expressions equivalent? no yes yes num <= k k <= num h!= 20 20!= h 20 - y > 5 y < 15 Page 2

The if and if-else Statements These statements are sometimes called control statements because they control the flow of the program. Some texts refer to them as conditionals because they represent a condition that may be or. Flowchart Java Code x > 8 x = x + 2 if ( x > 8 ) { x = x + 2; The boolean expression must be enclosed by parentheses. The curly braces define the block of code to be executed if the boolean expression is. n = n - 1 n 5 n = 3 * n if ( n >= 5 ) { n = 3 * n; else { n = n - 1; Logical Operators. More complicated Boolean expressions can be composed through the use of logical operators such as AND and OR. The following table shows how certain expressions can be written. English Mathematically Java If the number is greater than 0 and less than or equal to 5. If the number is less than 12 or greater than 65 0 < x 5 x > 0 && x <= 5 The expressions to the left and right of && must be complete boolean expressions. x< 12 or x>65 x < 12 x > 65 Page 3

Example. Complete the code to the right based on the flowchart below. k++ k = k + 2 3 < k < 7 k = k - 3 k 5 display k k-- k = 2 * k import java.util.scanner; public class Example { public static void main(string[] args) { Scanner x = new Scanner( System.in ); System.out.print("Enter an integer "); int k = x.nextint(); k = k + 2; if ( k > 3 && k < 7 ) k--; else The curly braces { k++; are not required if there is only one statement associated k = k - 3; with the if or else. if ( k!= 5 ) k = 2 * k; System.out.println( k ); The Math Class. The Math Class contains a collection of standard math functions. Here are some examples on how to use them. To take the square root of a number: double a = Math.sqrt( 16 ); a = Math.sqrt( a ); To raise a number to nth power: double b = Math.pow( 2, 3 ); double c = Math.pow( b, 2.0 ); 4.0 // a equals 2.0 // a equals 2 3 // equivalent to 64.0 // c equals The sqrt and pow methods always return a double Note: to square a number you can always multiply it by itself. int e = 7; int f = e * e; // or int f = (int) Math.pow( e, 2 ); Page 4

Find the absolute value: int x = Math.abs( -5 ); // if the argument is an int, the method returns an int double y = Math.abs( -7.3 ); // if the argument is a double, the method returns a double To generate a random decimal between 0 (inclusive) and 1 (exclusive): double g = Math.random(); double h = 5 * Math.random(); 0.0 g < 1.0 0.0 h < 5.0 // // 1. a is a random decimal within these limits 0.0 8.0 a < double a = 8 * Math.random(); 2. b is a random decimal within these limits -3.0 7.0 b < double b = 10 * Math.random() - 3; To generate a random integer from min (inclusive) and max (inclusive), use the following formula: int num = (int) ( range * Math.random() ) + min; // where range = max min + 1 For example, to generate a random integer between 3 and 6: int num = (int) ( 4 * Math.random() ) + 3; // num is equal to 3, 4, 5, or 6 3. c is a random integer within these limits 0 c 4. d is a random integer within these limits 2 int c = (int)( 9 * Math.random() ); d int d = (int)( 3 * Math.random() ) + 2; 5. e is a random integer within these limits -1 8 4 5 e int e = (int)( 7 * Math.random() ) - 1; Page 5

if-else if Statements. The basic if - else structure can be expanded to handle multiple conditionals through the use of else-if statements. For example, if you want to determine if a grade is an A, B, C, D, or E, a simple if - else statement won't work. Flowchart Java Code 0 age<13 age<20 age<120 print "???" print "child" print "teen" print "adult" if ( age >= 0 && age < 13 ) System.out.println( "child" ); else if ( age < 20 ) System.out.println( "teen" ); else if ( age < 120 ) System.out.println( "adult" ); else System.out.println( "???" ); done get cost get id cost > 100 id = 13 print cost reduce cost by 10% reduce cost by 5% import java.util.scanner; public class Example { public static void main(string[] args) { Scanner sc = new Scanner( System.in ); System.out.print("Enter cost "); double cost = sc.nextdouble(); System.out.print("Enter id "); int id = sc.nextint(); if ( cost > 100 ) cost = 0.9 * cost; else if ( id == 13 ) cost = 0.95 * cost; System.out.println( cost ); Only the statements associated with the first expression are executed. The others are ignored. IMPORTANT. Page 6