Conventions in Java

•      Classes start with a capital letter

•      Variable names start with a lower case letter

 

Terminology

•      Class and object

•      Instance variable

•      Methods

•      Mutator and accessor methods

•      Arguments for a method

•      Return type for a method

•      Constructors

 

Classes and Objects

•      When we write a class, it is a template for the information we want to keep about an object

•      Once we create an object, the mutator methods  are one way to store information with the object

•      Another way to get information in to the object is to use a constructor with arguments.

•      The constructor puts information in the object at the same time it creates it

 

Constructors for SimpleGradeBook

•      The default constructor has no arguments

–   If you don’t write a constructor, Java furnishes this automatically

–   Whenyou do write the default constructor, the programmer decides what the instant variables should be initially

–   Write a default constructor for SimpleGradeBook on the whiteboard:
Two instance variables
String name;  double grade;

 

Constructors with arguments

•      Since SimpleGradeBook has two instance variables, it is common to write another constructor with two arguments

–    We want to give both instance variables an initial value

–    This initial value can be changed by a set method.

•      The type of the arguments must be the same as the type of the instance variables

–    But the variable names should be different

•      Write a two argument constructor for SimpleGradeBook on the whiteboard

 

Calling the constructors

•      A constructor is really a method that is called automatically by the new operator

•      It is called in the class with the main( ) method

•      First, we will “hard code” the arguments
SimpleGradeBook firstObj = new SimpleGradeBook(“Fred”, 89);

•      A better way is to get input from user and store in variables

           Scanner in = new Scanner(System.in);
      String str = in.next();
      double num = nextDouble();

•      Then use those variables as the arguments
   
SimpleGradeBook secObj = new SimpleGradeBook(str, num);

 

The toString method

•      Java likes to make it easy to treat your class objects just like an int or String

•      i.e. just use System.out.println(object) to print them

•      The toString method is a way for the programmer to easily output an entire object

•      public String toString( )
{  /* build a string to output object
        usually this is labels for the instance
        variables, then the variables themselves,
        concatenated together  */

•      Write a toString( ) method on the whiteboard

•      Look at SimpleGradeBook class and test in BlueJ

 

Out-Lab 2

•      This is a one week lab

•      It is not much different than the in-lab.

 

Floating-Point Numbers

•      Floating point numbers are those numbers with a fractional part

–    i.e. have a decimal point

•      They are stored in the computer differently than integers

–    Floating point calculations are much slower than integer calculations

•      Java has two floating point types

–    float

–    double—take twice as many bits to store the number as floats

•    This makes them twice as accurate.

•      There are almost always rounding errors when using floating point numbers

 

Basic or Primitive types

Four integers type

byte -- 8 bit signed integral value

short -- 16-bit signed

int-- 32 bit signed (most commonly used)

long -- 64 bits

Floats

float – 32 bits (6 to 7 significant decimal digits , exponent to 38)

double – 64 bits (15 significant  decimal digits, exponent to 308)

boolean - can  only be true or false

char --stored in 16 bit ASCII code

–    Java does not use 8-bit ASCII code