Primitives in Java - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Primitives in Java

Description:

someNumber = 18; System.out.println('I just turned ' someNumber); Example 1, result ... someNumber = 18; someDecimal = 166.85; myEffortRating = B' ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 14
Provided by: SUPE106
Category:
Tags: java | just | primitives | turned

less

Transcript and Presenter's Notes

Title: Primitives in Java


1
Primitives in Java
  • Characters char a, b 16 bits 2 bytes
  • Smallest integers byte -128 to 127 8 bits 1
    byte
  • Small integers short -32768 to 32767 16 bits
    2 bytes
  • Integers int -2147483648 to 32 bits 4 bytes
  • 2147483647
  • Big integer long -9223372036854775808 to 64
    bits 8 bytes
  • 9223372036854775807
  • Decimals float -1.45E-45 to -3.4E38 32 bits
    4 bytes
  • Big decimals double -4.9E-324 to -1.79E308 64
    bits 8 bytes
  • Boolean boolean true or false 1 bit
  • Emptyness void - 0 bit

AAAAARGH !!!
2
The ones we will use
  • int integers
  • float decimals
  • double big decimals
  • boolean true or false
  • void emptyness
  • char characters

3
Example 1, using an integer
  • public class FirstTry
  • public static void main(String args)
  • System.out.println("Hello dude!!!")
  • int someNumber
  • someNumber 18
  • System.out.println("I just turned "
    someNumber)

4
Example 1, result
5
Example 2
  • public class FirstTry
  • public static void main(String args)
  • System.out.println("Hello dude!!!")
  • int someNumber
  • double someDecimal
  • char myEffortRating
  • someNumber 18
  • someDecimal 166.85
  • myEffortRating B
  • System.out.println("I just turned "
    someNumber)
  • System.out.println(My height is "
    someDecimal " cm")
  • System.out.println(My effort rating is "
    myEffortRating)

6
Example 2, result
7
Our first real program
  • Pseudo Code
  • Input n
  • Sum ? 0
  • i ? 1
  • Repeat the following 3 steps
  • while i lt n
  • Sq i i
  • Sum ? Sum Sq
  • i ? i 1
  • Output Sum
  • Actual Code
  • public class SumOfSq
  • public static void main(String args)
  • int n, sum, i, sq
  • sum 0
  • i 1
  • n 100
  • while (i lt n)
  • sq i i
  • sum sum sq
  • i i 1
  • System.out.println("the sum of the " n "
    first squares is "sum)

8
Result
9
Some Java rules
  • Put a after each statement but not after a
  • // is used for comments, these statements will
    not be considered by the compiler. These will
    appear in green
  • / / is for a bloc of comments
  • Things you want executed should be in
  • public static void main(String args)
  • You must declare all variables and give them a
    type Ex int n
  • You can declare many variables of the same type
    in one line Ex int n, sum, i, sq

10
First Assignment
  • Write a pseudo code as a Word Doc before writing
    your actual program
  • Write a Java program called InterestCalc
  • Write comments in a bloc stating your name, etc
    Ex
  • /
  • Assignment 1
  • Form 7, AP Java
  • Author Joe Scablowsky
  • version 1.00 04/09/09
  • /
  • The program should add a 10 interest a year on
    an initial amount of 5000 and this for 20 years.
  • Display the result on the console using
    System.out

11
Hints
  • After the first year, you could calculate your
    interests like this
  • amount amount amount 0.1
  • You will need to some doubles (decimals)
    Ex. for amount
  • You will need to use a while loop
  • For the while loop, see book p 298

12
Result
  • The result should look like this

13
Similaritieshomo-sapiens vs primates
Write a Comment
User Comments (0)
About PowerShow.com