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?
Learning Targets
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.
Fix It
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 );
}
}
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 );
}
}
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.
Integer Variables� int sum; �
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
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.
number1 = input.nextInt();
int num2 = input.nextInt();
sum = number1 + number2;
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); |
Java Assignment Options:
Write the code for one of the following
Temp = 50 + (chirps - 40) / 4 (Dolbear’s law)