CS1150 Principles of Computer Science Introduction (Part II)

Similar documents
CS1150 Principles of Computer Science Midterm Review

CS1150 Principles of Computer Science Introduction

CS1150 Principles of Computer Science Final Review

CS1150 Principles of Computer Science Methods

Lab 1 - Calculator. K&R All of Chapter 1, 7.4, and Appendix B1.2

Project #1 - Fraction Calculator

CS1150 Principles of Computer Science Loops

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture2 Functions

Lab 4. Name: Checked: Objectives:

CS1150 Principles of Computer Science Math Functions, Characters and Strings

CS1150 Principles of Computer Science Boolean, Selection Statements (Part II)

Lab 1 - Calculator. K&R All of Chapter 1, 7.4, and Appendix B1.2 Iterative Code Design handout Style Guidelines handout

COP2800 Homework #3 Assignment Spring 2013

CS1150 Principles of Computer Science Methods

Structure Query Language (SQL)

Relational Operators, and the If Statement. 9.1 Combined Assignments. Relational Operators (4.1) Last time we discovered combined assignments such as:

CS5530 Mobile/Wireless Systems Swift

CS1150 Principles of Computer Science Boolean, Selection Statements (Part II)

Preparation: Follow the instructions on the course website to install Java JDK and jgrasp on your laptop.

The Java if statement is used to test the condition. It checks Boolean condition: true or false. There are various types of if statement in java.

Programming Project: Building a Web Server

HOW-TO Use SAP SUIM OR RSUSR008_009_NEW to Analysing Critical Authorisations

C++ Reference Material Programming Style Conventions

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

Mathematical Functions, Characters, and Strings. APSC 160 Chapter 4

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Reading and writing data in files

1 Version Spaces. CS 478 Homework 1 SOLUTION

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

$ARCSIGHT_HOME/current/user/agent/map. The files are named in sequential order such as:

Ascii Art Capstone project in C

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

LAB 7 (June 29/July 4) Structures, Stream I/O, Self-referential structures (Linked list) in C

Whitepaper. Migrating External Specs to AutoCAD Plant 3D. Set Up the Required Folder Structure. Migrating External Specs to AutoCAD Plant 3D

Using the Swiftpage Connect List Manager

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

Computer Organization and Architecture

Computer Organization and Architecture

MOS Access 2013 Quick Reference


Using the Swiftpage Connect List Manager

Importing data. Import file format

Procurement Contract Portal. User Guide

1 Binary Trees and Adaptive Data Compression

Using the Menu: Explain cut, copy, and paste.

Using CppSim to Generate Neural Network Modules in Simulink using the simulink_neural_net_gen command

Using SPLAY Tree s for state-full packet classification

MIPS Architecture and Assembly Language Overview

MySqlWorkbench Tutorial: Creating Related Database Tables

McGill University School of Computer Science COMP-206. Software Systems. Due: September 29, 2008 on WEB CT at 23:55.

FIT 100. Lab 10: Creating the What s Your Sign, Dude? Application Spring 2002

Laboratory #13: Trigger

Municode Website Instructions

Enterprise Chat and Developer s Guide to Web Service APIs for Chat, Release 11.6(1)

It has hardware. It has application software.

Systems & Operating Systems

Exporting and Importing the Blackboard Vista Grade Book

Adverse Action Letters

Overview of OPC Alarms and Events

KNX integration for Project Designer

Integrating QuickBooks with TimePro

Contents: Module. Objectives. Lesson 1: Lesson 2: appropriately. As benefit of good. with almost any planning. it places on the.

Contents. I. A Simple Java Program. Topic 02 - Java Fundamentals. VIII. Conversion and type casting

History of Java. VM (Java Virtual Machine) What is JVM. What it does. 1. Brief history of Java 2. Java Version History

Copyrights and Trademarks

B ERKELEY. Homework 7: Homework 7 JavaScript and jquery: An Introduction. Part 1:

The following screens show some of the extra features provided by the Extended Order Entry screen:

Proper Document Usage and Document Distribution. TIP! How to Use the Guide. Managing the News Page

PAGE NAMING STRATEGIES

UML : MODELS, VIEWS, AND DIAGRAMS

SmartPass User Guide Page 1 of 50

Create Your Own Report Connector

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Importing an Excel Worksheet into SAS (commands=import_excel.sas)

Relius Documents ASP Checklist Entry

Scroll down to New and another menu will appear. Select Folder and a new

Primitive Types and Methods. Reference Types and Methods. Review: Methods and Reference Types

The programming for this lab is done in Java and requires the use of Java datagrams.

Access 2000 Queries Tips & Techniques

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

C pointers. (Reek, Ch. 6) 1 CS 3090: Safety Critical Programming in C

CSCI L Topics in Computing Fall 2018 Web Page Project 50 points

CS5530 Mobile/Wireless Systems Using Google Map in Android

3 AXIS STAGE CONTROLLER

escreen Setup and Usage Instructions

User Guide. ACE Data Source. OnCommand Workflow Automation (WFA) Abstract PROFESSIONAL SERVICES

Enabling Your Personal Web Page on the SacLink

Data Requirements. File Types. Timeclock

Data Structure Interview Questions

These tasks can now be performed by a special program called FTP clients.

ME Week 5 Project 2 ilogic Part 1

Assignment #5: Rootkit. ECE 650 Fall 2018

Tips & Tricks Data Entry Tool How to import files from Excel or Access into the DET

Campuses that access the SFS nvision Windows-based client need to allow outbound traffic to:

Kaltura Video Extension for IBM Connections User Guide. Version: 1.0

Tips and Tricks in Word 2000 Part II. Presented by Carla Torgerson

CSE 361S Intro to Systems Software Lab #2

FedVTE Training Advisor Guide

Report Writing Guidelines Writing Support Services

Transcription:

Principles f Cmputer Science Intrductin (Part II) Yanyan Zhuang Department f Cmputer Science http://www.cs.uccs.edu/~yzhuang UC. Clrad Springs

Review Terminlgy Class } Every Java prgram must have at least 1 class (same name as Java file) Methd } Java prgrams cntains a special methd called main public static vid main (String[] args) {..} The main methd is where a Java prgram starts executin Statements } Statements represent actins } Statements must end with a ; UC. Clrad Springs

Review Terminlgy Reserved wrds (keywrds) } Wrds that have specific meaning/cannt be used fr ther purpses } public, class, byte, int, lng, flat, duble, Blcks } Class blck and methd blck, using {} Cmments } // This is a line f cmment } /* This cmment can span acrss several lines */ UC. Clrad Springs

Review Identifier A name chsen by the prgrammer fr: classes, methds, variables, and cnstants Can start with letter, _ r $ (_ and $ are nt used much) UC. Clrad Springs

Overview Numerical Data Types Variables and cnstants Packages Data frmatting Data casting UC. Clrad Springs

Numerical Data Types Name Range Strage Size byte 2 7 t 2 7 1 (-128 t 127) 8-bit signed shrt 2 15 t 2 15 1 (-32768 t 32767) 16-bit signed int 2 31 t 2 31 1 (-2147483648 t 2147483647) 32-bit signed lng 2 63 t 2 63 1 64-bit signed (i.e., -9223372036854775808 t 9223372036854775807) flat Negative range: 32-bit flating pint -3.4028235E+38 t -1.4E-45 Psitive range: 1.4E-45 t 3.4028235E+38 duble Negative range: 64-bit flating pint -1.7976931348623157E+308 t -4.9E-324 Reading fr primitive data types: Psitive range: 4.9E-324 t 1.7976931348623157E+308 https://dcs.racle.cm/javase/tutrial/java/nutsandblts/datatypes.html UC. Clrad Springs

Numeric Operatrs Name Meaning Example Result + Additin 34 + 1 35 - Subtractin 34.0 0.1 33.9 * Multiplicatin 300 * 30 9000 / Divisin 1.0 / 2.0 0.5 % Remainder 20 % 3 2 UC. Clrad Springs

Integer Divisin +, -, *, /, and % 5 / 2 yields an integer 2 5.0 / 2 yields a duble value 2.5 5 % 2 yields 1 (the remainder f the divisin) ften called mdularperatin UC. Clrad Springs

Remainder Operatr % Remainder is very useful in prgramming Fr example, an even number % 2 is always 0 and an dd number % 2 is always 1 S yu can use this prperty t determine whether a number is even r dd Example: CmputeExpressin.java System.ut.println() prints nly strings, but cnverts number t string t print UC. Clrad Springs

Java Identifiers An identifier Rule } A sequence f characters that cnsist f letters, digits, underscres (_), and dllar signs ($), NO SPACES } Must start with a letter, _ r $, CANNOT start with a digit } CANNOT be a reserved wrd, true, false, r null } Can be f any length Example } Class/methd/variable/cnstant name (can refer by name later) Cnventin } Class names start with an upper case letter, variable/methd names start with a lwer case letter, cnstants all caps UC. Clrad Springs

Variables and Cnstants Variable Used t stre a value that may change E.g., duble length; length = 3.5; //length can change duble length = 3.5; // an (almst) equivalent way as abve Hw t use: Declare à assign a value à d smething t it } A variable can nly be declared nce } A variable must have a value (be initialized) befre we use it } Can mdify their value, display their value, use them in frmulas } Example: Arithmetic.java (hw t declare/use variables) UC. Clrad Springs

Variables and Cnstants Cnstant Used t stre a value that will NEVER change Cnstants fllw certain rules } Must have a name (a meaningful name, like variables) With all uppercase letters (Java cnventin) } Declared using the keywrd final Example: final duble PI = 3.14159; UC. Clrad Springs

Write pseudcde Nt real cde, but help rganize prgram lgic Can use a mix f prgramming language + natural language Example: Calculate the area f a circle (Circle.java) Input: radius Output: area f the circle area = pi * radius * radius Print area Als see Rectangle.java UC. Clrad Springs

Reading Input frm the Cnsle 1. Create a Scanner bject Scanner keybard = new Scanner(System.in); 2. Use the methd nextduble() t btain t a duble value. Fr example, System.ut.print("Enter a duble value:"); duble d = keybard.nextduble(); 3. After finishing the Scanner bject, clse it keybard.clse(); UC. Clrad Springs

Reading Input frm the Cnsle Scanner keybard = new Scanner(System.in); int value = keybard.nextint(); Example: Average.java, CmputeArea1.java, CmputeArea2.java Methd nextbyte() nextshrt() nextint() nextlng() nextflat() Descriptin reads an integer f the byte type. reads an integer f the shrt type. reads an integer f the int type. reads an integer f the lng type. reads a number f the flat type. nextduble() reads a number f the duble type. UC. Clrad Springs

Packages A bunch f related classes gruped tgether Mechanism fr rganizing Java classes Java cntains many predefined packages T access class/methd in a predefined package use imprt imprt java.util.scanner; UC. Clrad Springs

Packages T create yur wn package File (with prject selected) à New à Package First line in yur Java class: package yur_package_name Cnventin: package name lwer case Structure A prject cntains package(s) A package cntains Java file(s) A Java file cntains class(es) UC. Clrad Springs

Let s Practice! Please write a prgram CmputeRadius.java: input the area f a circle, calculate its radius Write pseudcde t guide yur lgic Use cnstant PI Use Scanner t get input value area (a duble variable) Print the value f the radius } radius = the square rt f (area/pi) } Hw t d square rt? Math.sqrt(area/PI) yu will use this in a hmewrk questin UC. Clrad Springs

Prblem: Cnverting Temperatures Write a prgram that cnverts a Fahrenheit degree (read frm keybard) t Celsius using the frmula: celsius 5 = ( 9 )( fahrenheit - 32) Print the resulting Celsius with tw decimal pints. 19

Cnverting Temperatures Pseudcde Input? Output? Hw t calculate Celsius given a Fahrenheit value? celsius 5 = ( 9 )( fahrenheit - 32) Nte: yu have t write celsius = (5.0 / 9) * (fahrenheit 32) Hw t print the result using tw decimal pints? } Data frmatting UC. Clrad Springs

Frmatting decimal utput Use DecimalFrmat class imprt java.text.decimalfrmat; DecimalFrmat df = new DecimalFrmat("000.##"); System.ut.println(df.frmat(celsius)); 0: a digit #: a digit, zer shws as absent } 72.5 is shwn as 072.5 } 21.6666.. is shwn as 021.67 Mre infrmatin https://dcs.racle.cm/javase/tutrial/i18n/frmat/decimalfrmat.html UC. Clrad Springs

Frmatting decimal utput Use System.ut.frmat System.ut.frmat("the %s jumped ver the %s, %d times", "cw", "mn", 2); } the cw jumped ver the mn, 2 times System.ut.frmat("%.1f", 10.3456); } 10.3 // ne decimal pint System.ut.frmat("%.2f", 10.3456); } 10.35 // tw decimal pints System.ut.frmat("%8.2f", 10.3456); } 10.35 // Eight-wide, twdecimal pints UC. Clrad Springs

Let s Practice! Please write a prgram: given the radius f a circle, calculate its perimeter, and print the result with 4 decimal pints Write pseudcde t guide yur lgic Use cnstant PI Use Scanner t get input value radius Print the value f the perimeter UC. Clrad Springs

Data Casting When yu explicitly tell Java t cnvert a variable frm ne data type t anther type Think f data types as cups f different sizes } Can put the cntents f a smaller variable (cup) int a larger variable (cup) } Cannt put the cntents frm a larger variable (cup) int a smaller variable (cup), withut lsing infrmatin } Cheat sheet: int (32 bits), lng (64 bits), flat (32 bits), duble (64 bits) UC. Clrad Springs

Cnversin Rules When perfrming a binary peratin invlving tw perands f different types, Java autmatically cnverts the perand based n the fllwing rules: 1. If ne f the perands is duble, the ther is cnverted int duble. 2. Otherwise, if ne f the perands is flat, the ther is cnverted int flat. 3. Otherwise, if ne f the perands is lng, the ther is cnverted int lng. 4. Otherwise, bth perands are cnverted int int. 25