Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create...

56
Java Names and Variables COMP163 Introduction to Computer Programming

Transcript of Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create...

Page 1: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Java Names and Variables

COMP163Introduction to

Computer Programming

Page 2: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

“What’s in a name? That which we call a rose

by any other name would smell as sweet.”

William Shakespeare

Page 3: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Textbook

1. Sign in or create an account at

learn.zybooks.com

2. Enter zyBook code

NCATCOMP163WilliamsFall2019

3. Subscribe

A subscription is $58 and will

last until Dec 30, 2019

Available at the Bookstore or online

Page 4: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

ClickersClicking will be required starting Monday, August 28

Instead of using a clicker, you can

purchase the Android or iPhone appat iclicker.com

Register your clicker on Blackboard

Page 5: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Java Source Code

• What you type is called the source code of your program

• The source code is not directly run by the computer

• The source code must be compiled into an executable form before the computer can execute it

Page 6: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Traditional Java Programs

Source Code

Compiler

Bytecodes

Java Virtual Machine

Bytecode Libraries

Page 7: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Errors

• When programming you will make mistakes

• There are three types of programming errors

– Compile errors – When you compile your program, the compiler might detect an error (i.e. missing semicolon)

– Run time errors – An error can occur when your program is running (i.e. division by zero)

– Logic errors – Your program might not produce the correct results

Page 8: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Software Development Process

• Requirements – high level description of what the system is supposed to do

• Specifications – detailed description

• Design – how will the system do this

• Coding – writing the program

• Integration – putting the pieces together

• Testing – make sure it works correctly

• Maintenance – updates and corrections

Page 9: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Class Programs

• Requirements – faculty will define briefly

• Specifications – faculty will define

• Design – student responsibility

• Coding – student responsibility

• Testing – informal student responsibility

Page 10: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Programming Assignments

• Programming assignments will define a program you are to create

• The assignment will explicitly define what the program is supposed to do

• Sample data and the expected results for that data is often provided

• Your program should work with any data, not just the sample data

Page 11: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Example Assignment

• Write a program to input two numbers and display the average of the numbers

• Sample input

2 6

• Sample output

Average is 4.0

Page 12: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

General Algorithm

• Tell the user to enter the first number

• Read a number into first

• Tell the user to enter the second number

• Read a number into second

• average is (first + second) / 2.0

• Display the result

Page 13: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Java Program/* Simple program to average two numbers.

written by Ken Williams */

public class Average {

public static void main(String[] unused) {

double first, second; // input numbers

double avg; // result average

java.util.Scanner keyboard = new

java.util.Scanner(System.in);

System.out.print("Enter a number ");

first = keyboard.nextDouble();

System.out.print("Enter another number ");

second = keyboard.nextDouble();

avg = (first + second) / 2.0;

System.out.println("Average is " + avg);

}

}

Page 14: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Steps in the Right Order

• In computer programming, you must write the steps in the correct order

• The program is executed sequentially

• You must read or calculate a data value before you use it

• You cannot display the answer until you calculate it

Page 15: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Put the steps in order to bake a cake

1. Bake for 30 minutes

2. Mix the ingredients in the mixing bowl

3. Pour batter from mixing bowl to cooking pan

4. Preheat oven

5. Put all ingredients in the mixing bowl

6. Put the cooking pan in the oven

7. Remove the cooked cake from the pan

8. Remove the cooking pan from the oven

Page 16: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Possible Solution

4. Preheat oven

5. Put all ingredients in the mixing bowl

2. Mix the ingredients in the mixing bowl

3. Pour batter from mixing bowl to cooking pan

6. Put the cooking pan in the oven

1. Bake for 30 minutes

8. Remove the cooking pan from the oven

7. Remove the cooked cake from the pan

Page 17: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Writing and Running a Program

Writing a program Running a program

Done first Run after it is written

Written by you Run by anyone

Left alone once complete

May be run many times

Data values are unknown

May use any data values

Page 18: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Structure of a Java Program

import package.class;

public class ProgramName {

public static void main(String[] rabbit) {

// your simple program here

}

}

Page 19: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Comments

• Programs are written for both computers and humans to read

• Comments are notes for humans that the compiler ignores

• There are two formats for comments

// the rest of the line is a comment

/* everything is ignored

until */

• Javadoc comments start with /** more to come */

Page 20: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Programming Requirement

All programs in COMP163 MUST start with a

comment that gives:

• Brief description of the program

• Your name

• There is a 15 point penalty for no comments

• Exams do not require comments

Page 21: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

import

• If you use existing objects in your program, you need to tell Java where to find them

• import tells Java to look in this package for classes you are using

• The import statement allows you to use the short name of a class

• import is not required. You can always use the full name of an object.

Page 22: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

import example

import javax.swing.JOptionPane;

… other parts of the program ...

JOptionPane.showMessageDialog(…);

or without import

javax.swing.JOptionPane.showMessageDialog(…);

Page 23: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Class statement

• Java is an Object-Oriented language

• Objects are defined by classes

• All Java programs are an object

• Much more on objects to come later

Page 24: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

main method

• Methods are functions or programs that can be executed

• The method with the name “main” is always executed first

• main has an array of String parameters that can be used to pass command line arguments

– We will not be using these parameters

Page 25: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Names• Names or identifiers in Java (such as program names, class

names, variable names, etc.) can be as long as you like

• Names can contain letters, numbers, underscores (_) and dollar signs ($)

• Spaces are not allowed in a name

• Names cannot start with a number

• Java is case sensitive, upper and lower case letters are different

Page 26: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Java Reserved Words

abstract continue for new switchassert default goto package synchronizedboolean do if private thisbreak double implements protected throwbyte else import public throwscase enum instanceof return transient catch extends int short trychar final interface static voidclass finally long strictfp volatileconst float native super while

• Reserved words cannot be used for

program names

Page 27: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Meaningful Names

• Names should make sense to humans

• Variable names such as:i X v027 v028

are not very meaningful

• The variable name should describe the databalance numWidgets xCoordinate

Page 28: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Why Animals?

Page 29: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Java Conventions

While the Java language allows you to use upper and lower case letters as you please, tradition dictates you follow some rules:

• Variables and method names start with a lower case letter

• Class names start with an Upper Case letter

• Constants are all UPPER CASE

• When you combine two English words, upper case the first letter of the second word (i.e. firstPlace, mySchool, whoCares)

Page 30: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Which of these are valid names?

a) dog e) DOG i) a_1

b) m/h f) 25or6to4 j) studentNumber

c) main g) 1stTime k) Main

d) double h) first name l) ______

Page 31: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Which of these are valid names?

a) dog e) DOG i) a_1

b) m/h f) 25or6to4 j) studentNumber

c) main g) 1stTime k) Main

d) double h) first name l) ______

Page 32: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Variables

• Variables hold data. They represent a memory location.

• Variables have a:

– Name: so you can identify them in Java

– Type: the format of the data

– Value: assigned at run time and often changes

• You must set a variable's value before you can use it

Page 33: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Memory Locations

owner sum price

String int double

“Fred” 47 665.95

• When you create a variable in Java,

it reserves memory to hold the data

Page 34: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Simple Data Types

• int – integer whole numbers

• double – numbers with decimal points

• char – single characters

• boolean – true or false

Page 35: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Different sized data

• float – a decimal with 7 digit accuracy

• double – a decimal with 16 digit accuracy

• byte – a 1 byte int for numbers < 128

• short – a 2 byte int for number < 16384

• int – a 4 byte int for numbers < 2 billion

• long – a 8 byte int for numbers < 9 x1018

Page 36: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

A Not-So-Simple Data Type

• String – A string of characters

• A String can contain any character on the keyboard (and more)

• String is a Java class. (Note the first letter is capitalized.) We will talk more about classes and complex data types later.

Page 37: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Java is Strongly Typed

• In Java you must specify the type of the data to be stored in a variable

• In some other programming languages, you can put any kind of data in any variable

• Java carefully restricts the type of data that can be stored in a variable

• Strong typing helps prevent subtle errors

Page 38: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Constants

• int 1 2 3 -6 123456

• double -1.234 0.000123 1.23e-4

• String "inside double quotes"

• boolean true false

• char 'x' // a single character in single quotes

Page 39: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Declaring Variables

• All variables must be declared before they are used in the program

• A variable is declared by writing the data type followed by the variable name

• More than one variable may be declared on the same line as the same type by separating the variable names by commas

Page 40: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Example Declarations

double interestRate;

int numPenguins;

String myName;

boolean doit;

int first, second;

Page 41: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Assigning Values

• You can set a variable to a value during execution by putting the name of variable, an equals sign followed by a value and semicolon

numPenguins = 6;

first = 3;

interestRate = 4.75;

• The type of the variable and the value must match.

Page 42: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Compile Time vs Run Time

• You can set a variable to a value, such as

dog = 47 + 5; // set to an equation

or

dog = keyboard.nextInt(); // read from keyboard

• When you write a program, you do not know what values the user of the program will enter

Page 43: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Not All Are Equal

• The equals sign is used to set a variable to a value. It does not mean equal in the mathematical sense.

int cat, dog;

cat = 3;

dog = 5;

cat = dog; // cat has the value 5

• An old computer language used the arrow character to indicate assignment.

cat ← 3; dog ← 5; cat ← dog ; // not Java

Page 44: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Sequential Execution

• Java programs are executed sequentially one line at a time

int cat, dog;

dog = 5;

cat = dog; // cat has the value 5

dog = 7;

• cat still has the value 5 while dog now has the value 7

Page 45: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Moving Data

cat dog

? ?

• When you first create a variable, it

has an undefined value

Page 46: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Moving Data

cat dog

? 5

dog = 5;

5

Page 47: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Moving Data

cat dog

5 5

cat = dog;

Page 48: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Moving Data

cat dog

5 7

dog = 7;

7

Page 49: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Variable Initialization

• When you declare a variable, you can give it an initial value

• If you don’t give a variable an initial value, it will have some unknown random value

• In the declaration after the variable name, put an equals sign followed by the value

• The type of the variable and the value must match

Page 50: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Example Initializations

double interestRate = 0.075;

int numPenguins = 7;

String myName = "Ken";

boolean doit = false;

int first = 1, second = 2;

int bad = "This is wrong";

Page 51: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Which are valid statements?

double cow = 47.5;

int goat = 14.2;

String right = "wrong";

double bull = 2.94e14;

boolean bird = "owl";

char pony = 'x';

String horse = 'x';

Page 52: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Which are valid statements?

double cow = 47.5;

int goat = 14.2; no decimal point

String right = "wrong";

double bull = 2.94e14;

boolean bird = "owl"; only true or false

char pony = 'x';

String horse = 'x'; use double quotes

Page 53: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

First Programming Assignment

• The first COMP163 programming assignment will be posted on Blackboard over the weekend

• Programs are due by midnight on Friday

• The tutors, lab TAs and your instructor can help

• Upload your completed program to Blackboard

Page 54: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Tutors

Starting next week, there will be tutors available in

Cherry 124

from 11:00am to 8:00pm

Monday through Friday

Page 55: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

Textbook

1. Sign in or create an account at

learn.zybooks.com

2. Enter zyBook code

NCATCOMP163WilliamsFall2019

3. Subscribe

A subscription is $58 and will

last until Dec 30, 2019

Available at the Bookstore or online

Page 56: Java Names and Variableswilliams.comp.ncat.edu/COMP163/JavaBasics.pdfTextbook 1. Sign in or create an account at learn.zybooks.com 2. Enter zyBook code NCATCOMP163WilliamsFall2019

ClickersClicking will be required starting Monday, August 28

Instead of using a clicker, you can

purchase the Android or iPhone appat iclicker.com

Register your clicker on Blackboard