CS 180 Recitation 9 / {06, 07} / 2007. Reminders Assignment 1 was due last night. Assignment 2 is available & due in 1 week. 10:00pm, Wednesday, September.

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Numerical Data.
Advertisements

Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
Numerical Data Recitation – 01/30/2009
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
Introduction to Python
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
CS 180 Recitation 8 / {30, 31} / Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146.
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Basic Input/Output and Variables Ethan Cerami New York
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data Animated Version.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3 Numerical Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Topics Class Basics and Benefits Creating Objects Using Constructors Calling Methods Using Object References Calling Static Methods and Using Static Class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Variables, Expressions and Statements
Chapter 2 Variables.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Lecture 3 Expressions, Type Conversion, Math and String
Arithmetic Expressions
Intro to OOP with Java, C. Thomas Wu Numerical Data
Object Oriented Systems Lecture 03 Method
Java Programming: From Problem Analysis to Program Design, 4e
CS170 ygao JAVA, C7.
Chapter 2: Basic Elements of Java
MSIS 655 Advanced Business Applications Programming
Chapter 3 Numerical Data
Programming Assignment #1 12-Month Calendar—
CS2011 Introduction to Programming I Elementary Programming
An Introduction to Programming with C++ Fifth Edition
Chapter 6.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

CS 180 Recitation 9 / {06, 07} / 2007

Reminders Assignment 1 was due last night. Assignment 2 is available & due in 1 week. 10:00pm, Wednesday, September 12 Make use of newsgroup Mail to Armand Navabi for Mentor session mailing list

Use of numeric data Use Parentheses –They help reduce errors –Make code more readable Eg: / 3 or 6 + (15/3) or (6 + 15)/3

Operator Overloading When a symbol is used to represent more than one operation – “+” symbol – = 9 – String output = “hello”+”kitty” Result will be “hellokitty” –“+” symbol is evaluated from left to right

Operator Overloading int x = 1; int y = 2; String output = “test” + x + y; output will be test12 String output = x + y + “test” output will be 3test How do we get test3 as the result?

Use of Constants Used when value of a variable remains constant Standard Java Convention –Use only capital letters and underscores Makes code more readable Allows user to make changes easily Constants defined in Java API: –Example: PI in Math class (package java.lang)

DecimalFormat Class Use DecimalFormat class (package is java.text) to format for numerical outputs double num = ; DecimalFormat df = new DecimalFormat(“0.000”); //three decimal places System.out.print(num); System.out.print(df.format(num));

Escape Characters Use of control characters –Tab \t –Newline \n

Math Class Available in java.lang package Make sure you get into the habit of using API for Java 5 Will find methods defined for a wide variety of mathematical and other programming needs

Math Class continued MethodInput type Output type Description exp(a) double Return e raised to power a. log(a) double Return natural log of a. floor(a) double Return largest whole number smaller than a. max(a,b) int double … int double … Return larger of a or b. pow(a,b) double Return a raised to power b. sqrt(a) double Return square root of a. sin(a) double Return sine of a(in radians).

Math Class Examples: –Formula: s = ut + 1/2 at 2 Code: s = u*t + a*t*t/2.0; –Formula: Code: v = Math.sqrt(3*k*t/(m*p)); –Formula: a = 2 π r π r h Code: a = 2*Math.PI*r*r + 2*Math.PI*r*h;

GregorianCalendar Class Part of java.util.Date package Used to manipulate calendar information GregorianCalendar today, independenceDay; today = new GregorianCalendar(); independenceDay = new GregorianCalendar(1776, 6, 4); //month 6 means July; 0 means January

Constants in GregorianCalendar ConstantDescription YEARThe year portion of the calendar date MONTHThe month portion of the calendar date DATEThe day of the month DAY_OF_MONTHSame as DATE DAY_OF_YEARThe day number within the year DAY_OF_MONTHThe day number within the month DAY_OF_WEEKThe day number within the week (Sun --1, Mon -- 2, etc.) WEEK_OF_YEARThe week number within the year WEEK_OF_MONTHThe week number within the month AM_PMThe indicator for AM or PM (AM -- 0, PM -- 1) HOURThe hour in the 12-hour notation HOUR_OF_DAYThe hour in 24-hour notation MINUTEThe minute within the hour

Sample Calendar Retrieval GregorianCalendar cal = new GregorianCalendar(); //Assume today is Nov 9, 2003 System.out.print(“Today is ” + (cal.get(Calendar.MONTH)+1) + “/” + cal.get(Calendar.DATE) + “/” + cal.get(Calendar.YEAR)); Today is 11/9/2003 Output

Revisit JOption Pane Does this work? age is not a String. It is of type int. How do we handle this? int age; age = JOptionPane.showInputDialog(null, “Enter your age”);

Wrapper Classes Wrapper classes are used to perform necessary type conversions, such as converting a String object to a numerical value. int age; String inputStr; inputStr = JOptionPane.showInputDialog( null, “Enter your age”); age = Integer.parseInt(inputStr);

Other Conversion Methods Look in Java API to convert from –String to Double –String to Float –String to Long –Hint: You will find conversion from String to Double in the Double class of java.lang package in the API

One more time Use the newsgroup Start with Project #2 Mentoring