09-20-2013

posted Sep 20, 2013, 7:56 AM by Samuel Konstantinovich   [ updated Sep 20, 2013, 8:15 PM ]
Goal: Types and Classes

TYPES

Java differs from Scheme, Netlogo, and Python because it is a typed language.
A typed language means that every variable may only store one type of data. (e.g. Number/word/boolean)

Types can either be classes or primitive data types.

Primitive data types are NOT objects.

Int and double are the most commonly used of the primative types. 

 Type Name Size (Bits)Range
 Integers byte 8 Bits -128 --> 127
  short 16 Bits -215 --> 215 -1
  int 32 Bits  -231 -->  231 - 1
  long 64 Bits -263 --> 263 - 1
 Floating point numbers (contain Decimals) float 32 Bits No range since it measures digits. Therefore:
  double 64 Bits 64 Bit can be more precise but no definitive range. 
 Characters char 16 Bits 
 Boolean boolean *         True/False
 


 
*Size that booleans use is dependent on the JVM.

METHODS

Method Header: (we will ignore static for now)

<Protection> <Return Type> <Name> (Parameters) 
public     void     foo ( int a, foat test)
{
 //code block follows the header
}

protection:  public private protected or default (leave it blank). 
return type: any type or the name of a class.

Homework:
1 Make a simple 'coordinate' class. 
a) It should include instance variables for x and y.
b) It should include methods to access and modify x and y individually. (4 methods)
c) It should include a method setxy() which allows you to modify both x and y at the same time.
d) It should include a method toString() which returns the coordinate as a string in the format:   (x,y)  e.g. "(2,13)" 

2)Make a 'demo' class to test it by accessing, modifying, and printing. This should be used as you write the code for the coordinate class, not after you finish it!

3. Place the two  .java files, and the two  .class files in your dropbox share under a directory named HW2.

Comments