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

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

AP Computer Science Unit 1. Programs

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

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

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

4. Java Project Design, Input Methods

Midterm Examination (MTA)

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

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

CS110: PROGRAMMING LANGUAGE I

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.

Important Java terminology

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

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

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

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

Practice Midterm 1 Answer Key

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

2.2 - Making Decisions

Practice Midterm 1. Problem Points Score TOTAL 50

NATIONAL UNIVERSITY OF SINGAPORE

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

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

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

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

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

Computer Programming I - Unit 2 Lecture 1 of 13

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

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

JAVA PROGRAMMING (340)

What did we talk about last time? Examples switch statements

Supplementary Test 1

CMPS 11 Introduction to Programming Midterm 1 Review Problems

CS141 Programming Assignment #6

Object Oriented Programming. Java-Lecture 6 - Arrays

CIS133J. Working with Numbers in Java

Chapter 6 Methods. Dr. Hikmat Jaber

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

CT 229 Java Syntax Continued

Object-Based Programming. Programming with Objects

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

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class


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

AP Computer Science A. Return values

Formatting Output & Enumerated Types & Wrapper Classes

CS111: PROGRAMMING LANGUAGE II

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

Lecture 5: Methods CS2301

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

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

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

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

Programming with Java

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

Chapter 5 Methods / Functions

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

Methods CSC 121 Spring 2017 Howard Rosenthal

Building Java Programs

Welcome to the Primitives and Expressions Lab!

Data Structure and Programming Languages

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

CSC 1051 Data Structures and Algorithms I

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Lecture 6: While Loops and the Math Class

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

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

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

Pace University. Fundamental Concepts of CS121 1

Topics. Chapter 5. Equality Operators

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

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

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

Java Reference Card. 1. Classes. 2. Methods. 3. Conditionals. 4. Operators

Building Java Programs

A Foundation for Programming

CS 200 Using Objects. Jim Williams, PhD

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

JAVA OPERATORS GENERAL

Advanced Java Concept Unit 1. Mostly Review

Selection and Repetition Revisited

AP Computer Science A Unit 2. Exercises

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

Oct Decision Structures cont d

Chapter 5 Methods. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

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

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

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Chapter 7. Iteration. 7.1 Multiple assignment

Java I/O and Control Structures

CS110D: PROGRAMMING LANGUAGE I

Chapter 3. Selections

Problem Grade Total

Java I/O and Control Structures Algorithms in everyday life

Building Java Programs

Building Java Programs

ITERATION WEEK 4: EXMAPLES IN CLASS

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: 1. Java Compiler 2. 3. PC JVM Mac JVM Linux JVM machine language machine language machine language 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. 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? x > 10 num == 14 50 <= y Boolean expressions can be manipulated the same way inequalities in math can be manipulated to produce equivalent expressions. x > 20 + 5 * x Rewrite the expression so that all x terms are combined on the left and the coefficient is 1. 4. Are the two expressions equivalent? 5. Are the two expressions equivalent? 6. Are the two expressions equivalent? 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 n 5 n = n - 1 n = 3 * n 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 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(); 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 ); // a equals // a equals // equivalent to // c equals The sqrt and pow methods always return a 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(); // 1. a is a random decimal within these limits a < double a = 8 * Math.random(); 2. b is a random decimal within these limits 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 c int c = (int)( 12 * Math.random() ); 4. d is a random integer within these limits d int d = (int)( 3 * Math.random() ) + 2; 5. e is a random integer within these limits 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" 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(); IMPORTANT. Page 6