Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Types and Arithmetic Operators
Chapter 2 JAVA FUNDAMENTALS CONT’D
Java Planning our Programs Flowcharts Arithmetic Operators.
Chapter 3 Fundamental Data Types Goals: To understand integer and floating-point numbers To recognize the limitations of the int and double types and the.
Chapter 2: Using Data.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Data types, declarations, and expressions in Java.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
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 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
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.
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,
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
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 4: Fundamental Data Types. To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware.
Chapter 4 Numeric types & arithmetic Strings: reading & writing.
Chapter 3 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the int and double types.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To recognize.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
CSC Programming I Lecture 5 August 30, 2002.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Mathematical Calculations in Java Mrs. G. Chapman.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Lecture 2 Objectives Learn about objects and reference variables.
Dialog Boxes.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Mathematical Calculations in Java Mrs. C. Furman.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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)
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
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.
Java Programming Lecture 2
Lecture 3 Java Operators.
Elementary Programming
2.5 Another Java Application: Adding Integers
Primitive and Reference Data Values
Dialogues and Wrapper Classes
Type Conversion, Constants, and the String Object
Chapter 3: Introduction to Objects and Input/Output
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
In this class, we will cover:
4.3 Arithmetic Operators and Mathematical Functions
Presentation transcript:

Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas; public Car (String col, String mk, String mod, double galofgas) { …….. } public void moveForward(double miles) { gas = gas - miles/18.0; } public void moveBackward(double miles) { gas = gas - miles/18.0; }

public class Car { private final double MPG = 18.0; //value cannot change //adds readablility to code private String make; private String model; private double gas; public Car (String col, String mk, String mod, double galofgas) { …….. } public void moveForward(double miles) { gas = gas - miles/MPG; } public void moveBackward(double miles) { gas = gas - miles/MPG; }

Static Methods If a method is declared as static, it may be called without using an object of the class. For example: we called all Car class methods in the form CarObject.methodName because none of these methods were static If a method is declared as static, it may be called without an object ClassName.methodName Example: Math.sqrt(4) The sqrt method is a static method defined in the Math class.

Static Variables If an instance variable is declared as static, it is shared by ALL OBJECTS of the class. ( eg. Each object created from that class DOES NOT have its own storage location allocated for the variable … all the objects use the one variable location.) When do you use a static instance variable?? * when information needs to be shared between objects and a method calls does not make design sense *class constants should be static. A constant memory location cannot be changed, so it makes sense to just allocate one

Class Constants should be static In a method: final typeName variableName= expression ; In a class: accessSpecifier static final typeName variableName = expression; Why?? Because, each object of class does not need it’s own copy of a constant (each Car object does not need it’s own memory location to store MPG)

Simple example public class Player{ private static int topScore = 0; private int myScore = 0; private static final int winScore = 21; public Player() { …………….} public void gotPoint () { myScore = myScore + 1; if (myScore > topScore) topScore = myScore; } public static boolean winner( ) { return (topScore >= winScore) ; }

public class Game{ public static void main (String [] args){ Player player1, player2, player3; player1 = new Player (); player2 = new Player (); while ( Player.winner() == false) { //code to play game }

Arithmetic Operators +, -, * are used for addition, subtraction and multiplication / is the division operator If both arguments are integers, the result is an integer. The remainder is discarded For example: int val1 = 7; double value = 7.0; value = value / 4; //assigns 1.75 val1 = val1 / 4; //assignes 1 value = val1 / 4; //assigns 1.0

Arithmetic Operators Get the remainder with % (pronounced "modulo") For example: int val1 = 7; val1 = val1 / 4; //assigns 1 val1 = val1 % 4; //assigns 3

Mathematical Functions Math.sqrt(x)square root Math.pow(x, y)power x y Math.exp(x)exex Math.log(x)natural log Math.sin(x), Math.cos(x), Math.tan(x) sine, cosine, tangent (x in radian) Math.round(x)closest integer to x The Math class is a class which contains static methods which perform mathmatical functions.

Type Conversion Java will only allow an assignment if NO VALUE will be lost and types are compatible double total = "a lot"; //not compatible int ww = 5.67; //value would be lost Use “cast” to force conversion from one type to another (if it is possible): int ww = (int) 5.67; //discards fractional part int ww = Math.round(ww); //rounds to 6.0, then //truncates to 6

Reading Input The simplest (and prettiest) way to read input into a Java program is to use a method provided by the JOptionPane class (java.lang package). The call: JOptionPane.showInputDialog(“prompt”) causes a dialog box to be displayed with the prompt and a textbox for the user to type his/her response. Whatever the user types into the box is returned to the program (in String form). Note: showInputDialog is a static method of the JOptionPane class

An Input Dialog

int val; String in; in = JOptionPane.showInputDialog(“Enter a positive number”); Suppose we want to add the user input to the value 45. we cannot say: val = in + 45; because in is NOT of a numeric type (such as int or double). We must convert our string to type int. The Integer class provides a static method called parseInt which accepts a String, and returns its int equivalent. int size = Integer.parseInt(in); val = size + 45;

When using Integer.parseInt (or Double.parseDouble): Conversion throws an exception if user doesn't supply a number (ie. Program will crash if user provides non numerical input) JOptionPane also provides a method for output JOptionPane.showMessageDialog(null,”output string”); Add System.exit(0) to the end of any main method of any program that uses JOptionPane method