Data Types & Operations 1 Last Edited 1/10/04CPS4: Java for Video Games Data Types.

Slides:



Advertisements
Similar presentations
Method Parameters and Overloading. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
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.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
CompSci Data Types & Operations. CompSci The Plan  Overview  Data Types  Operations  Code Samples  ChangeMaker.java  PolarCoordinate.java.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSC Programming I Lecture 5 August 30, 2002.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
Mathematical Calculations in Java Mrs. G. Chapman.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
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,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables.
Chapter 2 Basic Computation
Lecture 3 Java Operators.
Chapter 4 – Fundamental Data Types
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Data Types & Operations
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
IFS410 Advanced Analysis and Design
Introduction to Java, and DrJava
Expressions and Assignment
In this class, we will cover:
Primitive Types and Expressions
Names of variables, functions, classes
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Presentation transcript:

Data Types & Operations 1 Last Edited 1/10/04CPS4: Java for Video Games Data Types & Operations

Data Types & Operations 2 Last Edited 1/10/04CPS4: Java for Video Games The Plan Overview –Data Types –Operations Code Samples –ChangeMaker.java –PolarCoordinate.java

Data Types & Operations 3 Last Edited 1/10/04CPS4: Java for Video Games Overview Data type – structure of information –Existing/User-defined Existing include int, String, double, boolean, Rectangle User defined are user written classes whose variables and methods describe the structure –Primitives/Reference primitives store only a single value (and no methods) References can store many values and methods

Data Types & Operations 4 Last Edited 1/10/04CPS4: Java for Video Games Data Types Primitives –double – real numbers –int – integers –boolean – true or false –char – single letter, number, space, or punctuation –long – larger range of integers –float – smaller precision real numbers References (Objects) –String – in java.lang –Rectangle – in java.awt –Greeter – in Big Java –Purse – in Big Java –ChangeMaker – going over this today –PolarCoordinate – going over this today

Data Types & Operations 5 Last Edited 1/10/04CPS4: Java for Video Games Operations Operators –Most are binary (having two operands) –Some are unary (having one operand) –compute a value Methods –static (do not require an instance, also known as class methods) –instance –may or may not compute a value (non-void or void) –can have zero or more parameters

Data Types & Operations 6 Last Edited 1/10/04CPS4: Java for Video Games Operations Operators –+ add –- subtract/negate –/ divide –* multiply –% mod –= assignment –== equivalence Methods –Static Math.sin – sine Math.cos – cosine Math.abs – absolute value –Instance sayHello of Greeter getDollars of ChangeMaker getMagnitude of PolarCoordinate equals of PolarCoordinate

Data Types & Operations 7 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker Converts dollars and cents to actual bills and coins. Example: $37.43 can be 1 $20 bill 1 $10 bill 1 $5 bill 2 $1 bills 1 quarter 1 dime 1 nickel 3 pennies

Data Types & Operations 8 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public class ChangeMaker { int dollars; int cents; public ChangeMaker() { this(0, 0); } public ChangeMaker(int d, int c) { dollars=d; cents=c; } Constructors initialize the instance variables dollars and cents Assignment operator int means integer valued (no fractional parts)

Data Types & Operations 9 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public class ChangeMaker { int dollars; int cents; public ChangeMaker() { this(0, 0); } public ChangeMaker(int d, int c) { dollars=d; cents=c; } Calls the second constructor from the first.

Data Types & Operations 10 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public void addCents(int c) { cents=cents+c; dollars=dollars+cents/100; cents=cents%100; } public void addDollars(int d) { dollars=dollars+d; } Mutator methods modify instance variables

Data Types & Operations 11 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public void addCents(int c) { cents=cents+c; dollars=dollars+cents/100; cents=cents%100; } public void addDollars(int d) { dollars=dollars+d; } + adds / does division In this case integer division because cents is an integer and 100 is an integer Order of operations gives / precedence over + Integer division truncates – by dropping the fractional part. 120/100 is 1 not 1.2

Data Types & Operations 12 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public int getDollars() { return dollars; } public int getCents() { return cents; } Accessor methods give access to the instance variables

Data Types & Operations 13 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public int get20s() { return dollars/20; } public int get10s() { return (dollars%20)/10; } Integer division because dollars and 20 are both integers. Integer division truncates – by dropping the fractional part. 50/20 is 2 not 2.5

Data Types & Operations 14 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public int get20s() { return dollars/20; } public int get10s() { return (dollars%20)/10; } % gets the remainder after integer division. For example 50%20 is 10 7%5 is 2 5%7 is 5 Parenthesis may be used to indicate the desired order of computation.

Data Types & Operations 15 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public int get20s() { return dollars/20; } public int get10s() { return (dollars%20)/10; } If dollars is 50, what is 50%20? What is (50%20)/10? How many $10 bills are needed when trying to give $50 in the largest denominations possible? (with $20 bills the largest)

Data Types & Operations 16 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public int get5s() { return (dollars%10)/5; } public int get1s() { return dollars%5; } If dollars is 50, what is The result of calling get5s()? If dollars is 37 what is The result of calling get5s()? Do the get5s() and get1s() instance methods Compute the correct answer? Why?

Data Types & Operations 17 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public String toString() { if(cents<10) return "$"+dollars+".0"+cents; else return "$"+dollars+"."+cents; } The toString() instance method converts instance variable values into a meaningful String. For example, if dollars is 5 and cents is 14 the toString() Method returns the String “$5.14”.

Data Types & Operations 18 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public String toString() { if(cents<10) return "$"+dollars+".0"+cents; else return "$"+dollars+"."+cents; } Why does computing the return value of the toString() method depend on the value of cents?

Data Types & Operations 19 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public static void main(String[] argv) { ChangeMaker money=new ChangeMaker(37, 43); System.out.println(money+" has:"); System.out.println(money.get20s()+" 20 dollar bills"); System.out.println(money.get10s()+" 10 dollar bills"); System.out.println(money.get5s()+" 5 dollar bills"); System.out.println(money.get1s()+" 1 dollar bills"); System.out.println(money.getQuarters()+" quarters"); System.out.println(money.getDimes()+" dimes"); System.out.println(money.getNickels()+" nickels"); System.out.println(money.getPennies()+" pennies"); } Driver for testing the ChangeMaker class

Data Types & Operations 20 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public static void main(String[] argv) { ChangeMaker money=new ChangeMaker(37, 43); System.out.println(money+" has:"); System.out.println(money.get20s()+" 20 dollar bills"); System.out.println(money.get10s()+" 10 dollar bills"); System.out.println(money.get5s()+" 5 dollar bills"); System.out.println(money.get1s()+" 1 dollar bills"); System.out.println(money.getQuarters()+" quarters"); System.out.println(money.getDimes()+" dimes"); System.out.println(money.getNickels()+" nickels"); System.out.println(money.getPennies()+" pennies"); } Calls the constructor. public ChangeMaker(int d, int c) { dollars=d; cents=c; }

Data Types & Operations 21 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public static void main(String[] argv) { ChangeMaker money=new ChangeMaker(37, 43); System.out.println(money+" has:"); System.out.println(money.get20s()+" 20 dollar bills"); System.out.println(money.get10s()+" 10 dollar bills"); System.out.println(money.get5s()+" 5 dollar bills"); System.out.println(money.get1s()+" 1 dollar bills"); System.out.println(money.getQuarters()+" quarters"); System.out.println(money.getDimes()+" dimes"); System.out.println(money.getNickels()+" nickels"); System.out.println(money.getPennies()+" pennies"); } Automatically calls the toString() method public String toString() { if(cents<10) return "$"+dollars+".0"+cents; else return "$"+dollars+"."+cents; }

Data Types & Operations 22 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public static void main(String[] argv) { ChangeMaker money=new ChangeMaker(37, 43); System.out.println(money+" has:"); System.out.println(money.get20s()+" 20 dollar bills"); System.out.println(money.get10s()+" 10 dollar bills"); System.out.println(money.get5s()+" 5 dollar bills"); System.out.println(money.get1s()+" 1 dollar bills"); System.out.println(money.getQuarters()+" quarters"); System.out.println(money.getDimes()+" dimes"); System.out.println(money.getNickels()+" nickels"); System.out.println(money.getPennies()+" pennies"); } calls the get20s() method public int get20s() { return dollars/20; }

Data Types & Operations 23 Last Edited 1/10/04CPS4: Java for Video Games ChangeMaker.java public static void main(String[] argv) { ChangeMaker money=new ChangeMaker(37, 43); System.out.println(money+" has:"); System.out.println(money.get20s()+" 20 dollar bills"); System.out.println(money.get10s()+" 10 dollar bills"); System.out.println(money.get5s()+" 5 dollar bills"); System.out.println(money.get1s()+" 1 dollar bills"); System.out.println(money.getQuarters()+" quarters"); System.out.println(money.getDimes()+" dimes"); System.out.println(money.getNickels()+" nickels"); System.out.println(money.getPennies()+" pennies"); } main outputs: $37.43 has: 1 20 dollar bills 1 10 dollar bills 1 5 dollar bills 2 1 dollar bills 1 quarters 1 dimes 1 nickels 3 pennies

Data Types & Operations 24 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate Rectangular coordinates are broken down into horizontal and vertical components pairs, or just (x, y) Polar coordinates are broken down into magnitude and angle from the origin or (r, theta) The conversion exists (r, theta) in polar is equivalent to (r cosine(theta), r sine(theta)) Sometimes polar coordinates are easier to deal with, especially when doing rotations. PolarCoordinate.java provides routines for converting between polar and cartesian coordinates

Data Types & Operations 25 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java import java.awt.*; import java.awt.geom.*; public class PolarCoordinate { private static final double EPSILON=1e-10; private double magnitude; private double angle; static final is used to denote constants Note the all capitals Shorthand for scientific notation 1x10 -10

Data Types & Operations 26 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java public PolarCoordinate() { this(0, 0); } public PolarCoordinate(double pm, double pa) { setMagnitude(pm); setAngle(pa); } Constructors can call instance methods

Data Types & Operations 27 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java public Point getPoint() { int x=(int)(magnitude*Math.cos(angle)); int y=(int)(magnitude*Math.sin(angle)); return new Point(x, y); } public Point2D.Double getPoint2D() { double x=magnitude*Math.cos(angle); double y=magnitude*Math.sin(angle); return new Point2D.Double(x, y); } Static methods of the Math class Casting the results which are doubles into ints which truncates

Data Types & Operations 28 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java public double getMagnitude() { return magnitude; } public double getAngle() { return angle; } public void setMagnitude(double pm) { magnitude=pm; } public void setAngle(double pa) { angle=pa; } Which are accessor methods? Which are mutator methods?

Data Types & Operations 29 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java public boolean equals(Object object) { if(object instanceof PolarCoordinate) { PolarCoordinate polar=(PolarCoordinate)object; if(Math.abs(polar.angle-angle)<EPSILON && Math.abs(polar.magnitude-magnitude)<EPSILON) { return true; } else { return false; } else { return false; } Focus on this part of the code Boolean means true or false

Data Types & Operations 30 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java if(Math.abs(polar.angle-angle)<EPSILON && Math.abs(polar.magnitude-magnitude)<EPSILON) { return true; } else { return false; } Floating point arithmetic incurs small errors which can cause two numbers which would theoretically be the same to be slightly different. If comparing floating point number for equivalence, check to see if they are roughly the same rather than exactly equal.

Data Types & Operations 31 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java if(Math.abs(polar.angle-angle)<EPSILON && Math.abs(polar.magnitude-magnitude)<EPSILON) { return true; } else { return false; } abs is for absolute value Why is the absolute difference needed? All arguments are computed before Calling the method.

Data Types & Operations 32 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java if(Math.abs(polar.angle-angle)<EPSILON && Math.abs(polar.magnitude-magnitude)<EPSILON) { return true; } else { return false; } Each PolarCoordinate object has its own angle and magnitude. If there is no object name before an instance variable, the assumed object is this (which means the current object). more explanation about this later.

Data Types & Operations 33 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java public static double convertToDegrees(double radians) { return radians*180/Math.PI; } public static double convertToRadians(double degrees) { return degrees*Math.PI/180; } static methods do not access or modify any instance variables Notice angle and magnitude are not used in these methods.

Data Types & Operations 34 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java public static double convertToDegrees(double radians) { return radians*180/Math.PI; } public static double convertToRadians(double degrees) { return degrees*Math.PI/180; } / does floating point division because Math.PI is a double precision floating point number (so are radians and degrees) Floating point division retains the fractional part of the division

Data Types & Operations 35 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java public static void main(String[] argv) { PolarCoordinate one=new PolarCoordinate(5, Math.PI/2); System.out.println(one+" is "+one.getPoint2D()); PolarCoordinate two=new PolarCoordinate(); two.setCartesian(0, 5); System.out.println(two+" is "+two.getPoint2D()); Driver for PolarCoordinate.java (continued on next slide)

Data Types & Operations 36 Last Edited 1/10/04CPS4: Java for Video Games PolarCoordinate.java if(one==two) { System.out.println("one and two reference the same object"); } else { System.out.println("one and two reference different objects"); } if(one.equals(two)) { System.out.println("one and two have the same contents"); } else { System.out.println("one and two have different contents"); } With Objects, == is true if the two sides reference the same Object With Objects, the equals method checks the Contents of the objects for equality

Data Types & Operations 37 Last Edited 1/10/04CPS4: Java for Video Games Summary Data types –Primitives –References Operations –Operators –Methods

Data Types & Operations 38 Last Edited 1/10/04CPS4: Java for Video Games Reminders Should have read Chapter 1-3 of Big Java Start reading Brackeen p. 3-7, Intro to Video Game Package Wednesday Graphics on Friday Homework 1 due on Friday All Homework 0 graded. Check Blackboard for grades.