Review
This presentation is the property of its rightful owner.
Sponsored Links
1 / 9

In the last lesson we discussed about: Objects and Instances Declaring Variables PowerPoint PPT Presentation


  • 50 Views
  • Uploaded on
  • Presentation posted in: General

Review. In the last lesson we discussed about: Objects and Instances Declaring Variables The “=“ used as an assignment operator We also looked at the ACM hierarchy. Casting and Precedence.

Download Presentation

In the last lesson we discussed about: Objects and Instances Declaring Variables

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


In the last lesson we discussed about objects and instances declaring variables

Review

  • In the last lesson we discussed about:

  • Objects and Instances

  • Declaring Variables

  • The “=“ used as an assignment operator

  • We also looked at the ACM hierarchy


In the last lesson we discussed about objects and instances declaring variables

Casting and Precedence

  • When performing arithmetic operations in Java we have to understand how precedence (priority) works.

  • First priority is given to parenthesis ()

  • Second *, /, %

  • Least +,-

  • Starting from left to right


In the last lesson we discussed about objects and instances declaring variables

For example:

  • X = 1 + 3 * 5 / 2;

  • Lets see how this will evaluate.

  • 3 * 5 = 15

  • 15 / 2 = 7 (Integer division)

  • Add 1

  • X=8


In the last lesson we discussed about objects and instances declaring variables

Divide integers but get real

  • int x = 5; (Integer division)

  • double y = x / 2;

  • Evaluates the right side first and then returns the answer to the left side

  • In order to get a double value we will use a process called CAST!


In the last lesson we discussed about objects and instances declaring variables

CAST is just like in a film!

  • Just like a film has a cast of characters who act temporarily to fulfill a role similarly in Java a cast is a conversion of one data type to another.

    For e.g.:

    int x=5;

    double y = (double) x/2 (x remains as an integer)

    y now contains 2.5 due to the casting on the right side of the equal sign


In the last lesson we discussed about objects and instances declaring variables

Calculate Average

  • import acm.program.*;

  • public class CalculateAverage extends ConsoleProgram {

    public void run() {

    println(“This program requires two numbers:”);

    int n1 = readInt(“Enter n1”);

    int n2 = readInt(“Enter n2”);

    double avg = n1 + n2 / 2; //BUGGY!

    println(“The average is:” + avg + “.”);

    }

  • } // Whats the problem here?


In the last lesson we discussed about objects and instances declaring variables

Short hands

  • Seems like x = x +1 is very commonly used in programming languages

  • There are short hands for doing this:

  • x+=1;

  • x++;

  • x-=1;

  • x--;

  • x*=2;

  • x/=2;


In the last lesson we discussed about objects and instances declaring variables

Constants

  • Lets have a look at an example of area of a circle.

  • double PI =3.14;

  • double A = PI * r * r;

  • PI is a variable and it can be changed which means the whole equation will change.

  • Defining:

  • private static final double PI=3.14;

  • You could come up with a constant called SEVENTY_TWO=72


In the last lesson we discussed about objects and instances declaring variables

Downloading ACM

  • Before beginning to use ACM (Association of Computer Machinery) library you need to download it from:

  • http://www-cs-faculty.stanford.edu/~eroberts/jtf/acm.jar


  • Login