1 / 21

Variable Declaration

Variable Declaration. It is possible to declare multiple variables of the same data type on the same line. Ex. double hours, rate, total; Variables may also be initialized during multiple declarations Ex. double hours = 35.5, rate = 8.0, total;. Variable and Constants.

erica
Download Presentation

Variable Declaration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variable Declaration • It is possible to declare multiple variables of the same data type on the same line. • Ex. double hours, rate, total; • Variables may also be initialized during multiple declarations • Ex. double hours = 35.5, rate = 8.0, total;

  2. Variable and Constants • It is possible to give a variable a starting value when it is declared. Ex. int x=7; float y=2.345; char initial = ‘K’; • Another variation is to create a constant (i.e. a value that cannot change throughout a program) Ex. final int MAXAGE = 65;

  3. Add Subtract Multiply Divide Modulus (remainder) + - * / % Arithmetic Operators

  4. Order of Operations • All operators within parentheses are performed first • If there are nested parentheses (parentheses within parentheses) the innermost operators are performed • The *,/,% operators are performed next, from left to right • The + and - are performed last from left to right.

  5. Assignment Statement • An assignment statement is used to assign a value on the left hand side of an equation to a variable on the right. • The command used to create an assignment statement is the equals sign(=). • The general form of an assignment statement is: <variable> = <expression>;

  6. Simple Assignment Compound Addition Compound Subtraction Compound Multiplication Compound Division Compound Remainder = += -= *= /= %= Compound Assignment Operations (integers only)

  7. x = x + 10 x = x – 10 x = x * 10 x = x / 10 x = x % 10 x += 10 x –= 10 x *= 10 x /= 10 x %= 10 Compound Assignment Equalities

  8. Increment and Decrement Operators • Increment and Decrement operators add one or subtract one to the value of the variable. • Can be applied to integer, floating point, or character variables. • INCREMENT ++ • DECREMENT --

  9. Numeric Type Conversion(TypeCasting) • When you perform arithmetic operations with operands of unlike types, the Java language chooses a unifying type for the result. • The Java programming language then converts nonconforming operands to the unifying type. This unifying type is the type of the involved operand that appears first in the following list: • double • float • long • int • short • byte

  10. Typecasting (cont.) • If you do not want this to happen you have to purposely override the unifying type (typecasting) Ex: int balance= 189; double weeklyBudget = (double)balance/4; //weeklyBudget will be 47.25 int dollars = (int) weeklyBudget; //dollars will be 47

  11. Strings and Object-Oriented Types • Simple data types are used when simple calculations need to be done. • For more advanced applications, there is a need for strings and object-oriented types. • These types can give the application the ability to (for example) “turn the power on”, “signal if a seat belt is not attached”, “regulate the fuel mixture in an engine” • These data type classes are called class wrappers or object wrappers.

  12. String Data Type • String is used to contain more than one character. • The stored characters must be placed in double quotes. • Many times, processing all input as strings improves reliability of a program because the computer can take in a series of characters, test them to see if they are numbers, and then work with them. Ex: String name=“Rusty”;

  13. String Concatenation • String concatenation is used to combine strings and strings or strings and variables together. Ex. String firstName, lastName, lastFirst; int age = 65; firstName = “John”; lastName = “Doe; lastFirst = lastName + “ “ + firstName + “ is age “ + age; System.out.println (lastFirst); System.out.println(firstName + “ is: \n” + age + “ years old”); Output: Doe John is age 65 John is 65 years old

  14. Methods in class KeyboardReader

  15. Errors • There are three types of errors in programming: syntax, run-time, and logical errors. • Syntax errors occur when you violate a syntax rule (i.e using system.Out.println instead of System.out.println). • Run-time errors occur when you ask a computer to do something it cannot do (i.e divide by zero). • Logic errors occur when you fail to express yourself accurately (i.e using greater than instead of less than)

  16. Example Program public class Example2 { public static void main (String[] args) { int oneint=10; int twoint=15; int sum,difference,product, modulus; float quotient; System.out.print("The first int is "); System.out.println(oneint); System.out.println("The second int is " + twoint); sum = oneint + twoint; difference = oneint - twoint; product = oneint * twoint; modulus = oneint % twoint; quotient = oneint / (float)twoint; System.out.println("The sum is " + sum); System.out.println("The difference is " + difference); System.out.println("The product is " + product); System.out.println("The quotient is " + quotient); System.out.println("The modulus is " + modulus); } }

  17. Math Class • The math class is quite extensive but we will concentrate a just a few of it’s properties:

  18. Examples of Math Class Methods int m; double x; m = Math.abs(-7) // m equals 7 x = Math.abs(-7.5) // x equals 7.5 x = Math.pow(3.0,2.0) // x equals 3.0^2.0 = 9.0 x = Math.pow(16.0, .25) // x equals 16.0 ^ .25 = 2.0 m = Math.max(20,40) // m equals 40 m = Math.min(20,40) // m equals 20 m = (int) Math.round(4.27) // m equals 4

  19. Math.random • In order to generate a random number between a certain range use the following formula. • X = Math.random() * (high # - low #) + low # • If you want the number to be an integer, you must use the round function. • X = Math.round(Math.random() * (high # - low #) + low #) • Or typecast the formula + 1 to an int • X = (int)(Math.random() * (high # - low # + 1) + low #)

  20. Random Class • Must include • import java.util.Random; • Must then create an object from Random class • Ex: Random name = new Random(); • Use object to call methods • Ex: name.method();

  21. Random Class Methods • nextInt(integer); • Generates a random whole number between 0-(integer -1) • nextDouble(); • Generates a random decimal between 0.0 – 1.0

More Related