1 of 13

Welcome Back

Coding in Java

Getting Started kahoot:506996

Essential Questions:

Can you read, fix and write a program written in Java?

How do you get information from the user in Java?

2 of 13

Learning Targets

  • Be able to fix a program written in Java
  • Be able to get and show information from the user.
  • Be able to create a variable in Java
  • Be able to do simple Math in Java

3 of 13

Fix Go Titans

Log into OnlineGDB and open the Fix Go Titans program.

Using your notes, the Google slides from last class, … fix the program.

After the class has time to fix it, we will correct it as a class.

4 of 13

Fix It

5 of 13

What do you think this program does?

import java.util.Scanner;

public class Main

{

public static void main( String args[] )

{

Scanner input = new Scanner( System.in );

int number1;

int number2, sum=0;

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

number1 = input.nextInt();

System.out.println( "Enter second integer: " );

number2 = input.nextInt();

sum = number1 + number2;

System.out.println( "Sum is “ + sum );

}

}

6 of 13

import java.util.Scanner;

public class Main

{

public static void main( String args[] )

{

Scanner input = new Scanner( System.in );

int number1;

int number2, sum=0;

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

number1 = input.nextInt();

System.out.println( "Enter second integer: " );

number2 = input.nextInt();

sum = number1 + number2;

System.out.println( "Sum is “ + sum );

}

}

  • import java.util.Scanner;

2) Create a Scanner object:

3) Create variables to save the input

5) Get the value from the user..

4) Prompt: Ask the user for the value.

7 of 13

Integer Variables� int sum; �

  • Declares an integer variable.
  • -2,147,483,648 to 2, 147, 483, 647
  • Other types
    • double : real numbers
    • float : real numbers
    • char: characters
  • Variable name rules
    • Start with a character
    • Then you can have numbers and characters.
    • No spaces or punctuation
    • Cannot be a reserved word
    • Start the variables with a lowercase letter and describes what it is storing. (Style)

8 of 13

Valid? Good? Variable names

Variable name

Valid?

Good?

x

Hello world

total

score

r2d2

57 chevy

task

name

My first attempt at a variable

mySecondAttemptAtAVariable

Variable name rules

  • Start with a character
  • Then you can have numbers and characters.
  • No spaces or punctuation
  • Cannot be a reserved word
  • Start the variables with a lowercase letter and describes what it is storing. (Style)

9 of 13

More variable declaring options.

int first, second; // More than one at a time

int sum = 0; //Declare it and give it a starting value

int number = input.nextInt();

//Declare while getting the starting value from

// the user.

10 of 13

number1 = input.nextInt();

  • 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().

  • You can declare a variable on the same line of code where you get the value.

int num2 = input.nextInt();

11 of 13

sum = number1 + number2;

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

  • Java does the math on the right of the =, and saves the answer in the variable to the left of the =.

12 of 13

Math in Java

Math

Java

Example

+

+

ans = a + b;

-

-

ans = a – b;

Multiply

*

ans = a*b;

Divide (integer)

/

ans = a / b;

Mod (Remainder of dividing)

%

ans = a %b;

Square root

Math.sqrt()

ans = Math.sqrt(9);

Squaring

Math.pow(a,2);

ans = Math.pow(a,2);

Powers ab

Math.pow(a,b)

ans = Math.pow(a,b);

PI (π)

Math.PI

Ans = Math.PI *Math.pow(r,2);

13 of 13

Java Assignment Options:

Write the code for one of the following

  • Input three numbers and output their average. (Add them up and divide by 3)

  • Chirp Temp: Input the number of cricket chirps you count in one minute.
    • Process: Calculate the temperature

Temp = 50 + (chirps - 40) / 4 (Dolbear’s law)

    • Output: The temperature in F