java 9-10-2015 variables, types, and math getting started complete and turn in address/poem

22
Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Upload: tracy-webster

Post on 21-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 2: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Learning Objectives

• Understand how to declare and use primitives variables.

• Be able to read a simple Java program.• Be able to incorporate math expressions

into a Java program• Be able to find and use JavaDocs

Page 3: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Review• //• public static void main (String [] args)• {

– System.out.println(“Hello world”);– System.out.print(“Hello world”);– int num;– num = 30;– double num2;– num = input.nextInt();– double num2 = input.nextDouble();– System.out.printf( “The second number is %5.2f\n", num2 );

• }

Page 4: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Primitive Types (Not objects)

• byte • short• int (4 bytes) -2,147,483,648 to

2,1147,483,647• long +/-9,223,372,036,854,775,808• float• double (8 bytes)

10 e +/-308 with 15 significant digits• char• boolean true or false

Page 5: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

public class Interest {

public static void main(String[] args) { /* Declare the variables. */

double principal=0; // The value of the investment.double rate, interest; // The annual interest rate.

// Interest earned in one year./* Do the computations. */principal = 17000;rate = 0.07;interest = principal * rate; // Compute the interest.principal = principal + interest;// Compute value of investment after one year, with interest.// (Note: The new value replaces the old value of principal.)/* Output the results. */System.out.print("The interest earned is $");System.out.println(interest);// Can also use //System.out.print("The interest earned is $“ + interest);System.out.print("The value of the investment after one year is $");System.out.println(principal);

} // end of main()} // end of class Interest

The variables exist while main is running.

Page 6: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Sort of primitive type, but it’s an object

• String A sequence of characters.– The type declarations does start with an

uppercase letter.

Page 7: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Variables in Programs

• Declare the variables inside the main subroutine. (For now)

• You can also declare variables where you need them. (Declare a looping variable at the loop)– int numberOfStudents;– String name;– double x,y;– boolean isFinished;

Page 8: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Simple input and

math

import java.util.Scanner; // program uses class Scanner

public class Addition { // main method begins execution of Java application public static void main( String args[] ) { // create Scanner to obtain input from command window Scanner input = new Scanner( System.in );

int number1; // first number to add int number2, sum; // second number to add String name;

System.out.print( "Enter first integer: " ); // prompt number1 = input.nextInt(); // read first number from user

System.out.print( "Enter second integer: " ); // prompt number2 = input.nextInt(); // read second number from user System.out.print("Enter your name "); name = input.nextLine(); sum = number1 + number2; // add numbers

System.out.printf( "Sum is %d\n", sum ); // display sum System.out.println("Thanks " + name);// Note: Uses the + for concatenating } // end method main

} // end class Addition

Use input.nextDouble(); for getting a double

value

Page 9: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

// Addition.java// Addition program that displays the sum of two

numbers.

• Displays the file name and a brief description of the program

Page 10: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

import java.util.Scanner;

• Import declaration• This is a predefined class this program will use• Package: A collection of classes.• Java Class Library or Java Application Programming

Interface (API): Collection of Packages• This will use the Scanner class from the java.util

package• Note: All import declarations MUST appear before the

first class declaration in the file. Placing an import declaration inside a class declaration’s body or after a class declaration is a syntax error.

• Note: “cannot resolve symbol” error->Forgetting to include an import declaration for a class used in your program.

Page 11: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

public class Addition

• Begins the declaration of the Addition class.

• The file name must be Addition.java (case sensitive)

Page 12: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Scanner input = new Scanner( System.in );

• Variable declaration• Declares the variable (object) input is of type Scanner.

• Scanner enables the program to read data (numbers) for use in the program.

• = Declares that the Scanner variable input should be initialized in its declaration with the result of the expression newScanner(System.in) to the right side of the =.

• Creates a Scanner Object that can be used to read data entered through the keyboard.

Page 13: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

int number1; // first number to add int number2; // second number to add

int sum; // sum of number1 and number2

• Declares three integer variables.• -2,147,483,648 to 2, 147, 483, 647• Other types

– float like real numbers– char: characters

• Primitive or built in types• Variables follow identifier rules

– Start the variables with a lowercase letter. (Style)

Page 14: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

System.out.print( "Enter first integer: " );

• The prompt for user input

• Need to include a prompt for every value input

Page 15: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

number1 = input.nextInt();

• Uses the Scanner object input.nextInt() method to obtain an integer from the user at the keyboard. At this point the program waits for the user to type the number and press enter.

• This line is read “number1 is assigned the value of input.nextInt().”

Page 16: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

sum = number1 + number2;

• Calculates the sum of number1 and number2 and places the result in the integer variable sum.

Page 17: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

System.out.printf( "Sum is %d\n", sum );

• Displays the sum.

• Could also use• System.out.printf( "Sum is %d\n", number1+number2 );• Or• System.out.println(“Sum is “ + sum);• But this will give a result you might not expect.• System.out.println(“Sum is “ + number1 + number2);

Page 18: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Simple Integer Math Operations

()• * Multiply• / Integer Division• % MOD, The remainder of integer division.• +, - • Order of Operation

– ()– *, /, %– +, -

Page 19: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Show java doc

• On the left lists the different classes.

• We’ll look at the Math class.– sqrt– pow

• Using the math you’ve seen so far and the java docs, translate the following into java

• http://download.oracle.com/javase/6/docs/api/

Page 20: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

Translate to Java

Math Java

2(a+c) Ans=2*(a+c);

3x + 5y

(1/2)bhπr2

a

acbb

2

42

ca

yx

5

32

22 ba

Page 21: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

public class DryRun9_15_09 {

public static void main(String[] args) {

int a, b, c = 20;double d = 1.0;a = c + 10;b = a + c;System.out.println("Numbers " + a +" " + b + " " + c);System.out.println(a+b+c);c = a % 7;b = a / 7;a = a + 2*b + c % 3;System.out.println("Numbers " + a +" " + b + " " + c);System.out.println(a+b+c);d = 1.0/3;System.out.println(d);System.out.printf("%5.2f \n", d);d = Math.sqrt(a-b);System.out.println(d);System.out.printf("%5.2f \n", d);

}

}

Page 22: Java 9-10-2015 Variables, Types, and Math Getting Started Complete and Turn in Address/Poem

First Input Program(9/13/2011)• Complete one of the following

– Quadratic formula• Input: a, b and c (of type double)• Output: The positive root (-b + sqrt(b2 – 4ac))/(2a)

– Distance formula• Input: The x and y coordinates for two points• Output the distance between the points:

– Distance = sqrt((y1-y2)2 + (x1-x2)2)

• Push: Calculate the distance between two points that are on a 3-D grid. That is they have an x, y, and z value.

– Given the lengths of three sides of a triangle, find the area of the triangle.

• Find a way to calculate this even if it is not a right triangle. (Do a little research)

Complete additional programs as pushes.