SlideShare a Scribd company logo
COMPSCI 121: INTRODUCTION TO
PROGRAMMING
FALL 2019
WHAT ARE THE GOALS FOR TODAY’S CLASS
• Introduce you to some java
programming constructs.
• Introduce you to problem solving
techniques.
• Demo: using jGRASP
• Point out good programming practices.
2
LET’S PRACTICE T-P-S
Think
How do you learn best? (2 mins)
Pair
Share your thoughts with
your neighbor (2 mins)
Share
A few insights with the class (2 mins)
3
WHY ARE WE USING JAVA?
4
● Java is a widely used language in
industry.
● Knowledge of Java is in high demand.
● It is a mature language (1995) and is
frequently updated.
● Helps you learn other languages.
● Created as a “Software Engineering”
language so that large programs can be
more easily developed and maintained.
● It is designed to support software best
practices.
WHY ARE WE USING JAVA?
TIOBE Index for August 2019
https://www.tiobe.com/tiobe-index/
5
6
QUESTIONS?LET’S START PROGRAMMING
PROGRAMMING STEPS & PATTERN
7
Best to write a little
code, test it,
debug (find and
remove errors) if
test(s) failed, or
continue to write
more code if the
test(s) passed.
Note:
A computer can only
execute one step at a
time. Each step must be
clearly defined.
PROGRAMMING STEPS
All programming has this pattern:
8
Read and analyze all aspects of the
problem the program is to address.
Make sure you understand what the
program should do- its logic. What are
the inputs and outputs to the program?
Write a step-by-step process, an
algorithm, that will use the input to
produce the expected output.
Write code that will correctly
implement some or all of the
algorithm.
Test some or all of the program:
compare program output to correct
output given a set of inputs.
Continue to
develop the
program or
debug if any
tests did not
pass.
BAKING ANALOGY
• Problem: I want some banana bread.
• Input: The ingredients.
• Output: Warm banana bread to eat!
• Test: Must be edible and must look and taste like
banana bread.
• Algorithm: A step-by-step plan, like a recipe, for
getting from the Input to the Output.
• Implementation: in this case, ME!
9
AN ALGORITHM YOU CAN ENJOY!
10
1. Preheat oven to 350 degrees F (175 degrees C).
2. Lightly grease a 9x5 inch loaf pan.
3. In a large bowl, combine flour, baking soda and salt.
4. In a separate bowl, cream together butter and brown sugar.
5. Stir in eggs and mashed bananas until well blended.
6. Stir banana mixture into flour mixture; stir just to moisten.
7. Pour batter into prepared loaf pan.
8. Bake in preheated oven for 60 to 65 minutes.
9. Bake until a toothpick inserted into center of the loaf comes out
clean.
10. Let bread cool in pan for 10 minutes.
11. Turn out onto a wire rack.
12. Test the output!
In this case, there is no way to partially
implement the algorithm, so if the test
fails, you’ll have to start over!
HOW DOES PROGRAMMING IN JAVA WORK?
1. The problem is analyzed
and understood.
2. Inputs and outputs
defined.
3. An algorithm is
designed.
4. Then, Java code is
written in one or more
source code files.
5. This file is compiled and
run.
6. The output is generated.
7. Tests can be run on the
output.
11
DEMO IN zyBooks AND JGRASP
Saving files in folders
Compiling & running the Salary program (from zyBooks).
Printing in single/new lines.
Making/removing syntax errors.
12
PROBLEM STATEMENT & ANALYSIS
Write a program that calculates the annual salary
given the hourly wage.
1. What are the inputs?
2. What are the outputs?
THINK - PAIR - SHARE!
13
DESIGN THE ALGORITHM
Write a program that calculates the annual salary
given the hourly wage.
1. What are the inputs? hourly wage
2. What are the outputs? annual salary
3. What is the algorithm (steps) for the program?
THINK - PAIR - SHARE!
14
DESIGN THE ALGORITHM
3. What is the algorithm (steps) for the program?
1. Get the hourly rate (for example, from user).
hourlyWage = ?
2. Calculate the annual salary:
hoursPerWeek = 40, weeksPerYear = 50
annSalary = hourlyWage * hoursPerWeek * weeksPerYear
3. Print the result.
15
WRITE THE JAVA CODE - CHECK FOR ERRORS
16
Line numbers Class name
Variable assignment
Output statements
Semi-colons
DEMO IN
JGRASP
{ } braces for scope
Declare a variable
Note: Code in jGRASP is color-coded
CLICKER QUESTION #1
On which line does the program start when it runs?
A. On the first line of the program public class Salary
B. With the main statement public static void main
C. With the print statement System.out.print
17
ANSWER CLICKER QUESTION #1
On which line does the program start?
A. On the first line of the program public class Salary
B. With the main statement public static void main
C. With the print statement System.out.print
18
HOW DO WE ASSIGN VALUES TO VARIABLES?
Assignment statements: create a variable and assign a
value to it.
int num = 450;
• LHS has to be a variable, RHS has to resolve to a value.
19
value
assignment
operator
data
type variable
name
RHSLHS
“Declaration” of the variable
“num” with data type “int”.
It also assigns the value 450 to
that variable.
ASSIGNMENT IN JAVA: WHAT HAPPENS IN MEMORY
20
EXAMPLE:
After the execution of the last statement, the variable “num1” now
references an integer value of 3.
State diagram
ASSIGNMENT IN JAVA: WHAT HAPPENS IN MEMORY
21
Notice that you declare a variable only once,
but can use it as much as necessary once it’s
declared.
You cannot re-declare a variable:
int a = 450;
int a = 450;
int a = 22;
declaration OK
re-declaration Not OK
You can re-assign a variable:
int a = 450;
a = 22; re-assignment OK
CLICKER QUESTION 2
Which variable holds the highest number?
1. int num = 14;
2. int num2 = (num + 1);
3. int num3 = num2;
4. num2 = num3 + 3;
5. num = 17;
A. num
B. num2
C. num3 22
ANSWER CLICKER QUESTION 2
Which variable holds the highest number?
1. int num = 14;
2. int num2 = (num + 1);
3. int num3 = num2;
4. num2 = num3 + 3;
5. num = 17;
A. num = 17
B. num2 = 18
C. num3 = 15 23
HOW DID WE SHOW THE OUTPUT TO THE USER?
24
Uses "" to output a string literal.
Multiple output statements continue printing
on the same output line.
Uses "" to output a string literal.
Starts a new output line after the
outputted values, called a newline.
CLICKER QUESTION 3
Given this code (assume variables have been declared):
hourlyWage = 20, hoursPerWeek = 40, weeksPerYear = 50;
annSalary = hourlyWage * hoursPerWeek * weeksPerYear;
Which one does not print 40000?
A. System.out.print(annSalary);
B. System.out.print(hourlyWage * hoursPerWeek *
weeksPerYear);
C. System.out.print(“annSalary”);
D. System.out.print(20 * 40 * 50);
25
CLICKER QUESTION 3
Given this code (assume variables have been declared):
hourlyWage = 20, hoursPerWeek = 40, weeksPerYear = 50;
annSalary = hourlyWage * hoursPerWeek * weeksPerYear;
Which one does not print 40000?
A. System.out.print(annSalary);
B. System.out.print(hourlyWage * hoursPerWeek *
weeksPerYear);
C. System.out.print(“annSalary”);
D. System.out.print(20 * 40 * 50); 26
PROBLEM: HOW DO WE GET USER INPUT FOR OUR PROGRAM?
For our Salary program, we want the user to input the wage from
the keyboard.
Solution: we use the special Java text parser called Scanner.
Steps:
1. Create a Scanner object: Scanner scnr = new Scanner(System.in);
System.in corresponds to keyboard input.
2. Given Scanner object scnr, get an input value and assign to a
variable with scnr.nextInt()
scnr.nextInt()is a function (method) that gets the input
value of type integer (int)
27
USING SCANNER TO GET USER INPUT FROM KEYBOARD
28
Import the Scanner class
Create instance
Scanner method
TESTING FOR LOGIC ERRORS
29
Now let’s calculate the monthly salary.
What if our program works but the output is wrong?
GRADING CRITERIA OF PROGRAMMING PROJECTS
Code Style: follows good style (as defined in the course
material). This means your code is readable to you and to
others. This is professional “best practice”.
Code Compiles: A program that doesn't compile cannot run and
is not a successful program. This program does not get credit.
Logical Correctness: The code is logically correct if it runs and
produces the correct output.
30
CLICKER QUESTION 4
A. All that glittersis not gold.
B. All that glitters is not gold.
C. All that glitters isnot gold.
D. All that glitters is not gold.
What is the output of running this file?
31
CLICKER QUESTION 4 ANSWER
A. All that glittersis not gold.
B. All that glitters is not gold.
C. All that glitters isnot gold.
D. All that glitters is not gold.
What is the output of running this file?
Watch
white
spaces!
32
SOME GOOD PROGRAMMING PRACTICES
1. Create a good solution (algorithm) first, before you start coding.
2. Compile after writing only a few lines of code, rather than writing
tens of lines and then compiling.
3. Make incremental changes— Don't try to change everything at
once.
4. Focus on fixing just the first error reported by the compiler, and
then re-compiling.
5. When you don't find an error at a specified line, look to previous
lines.
6. Be careful not type the number "1" or a capital I in
System.out.println.
7. Do not type numbers directly in the output statements; use the
variables.
33
Week 1 TODO List:
1. Register your iClicker in Moodle.
2. Install OpenJDK and jGRASP.
3. Complete zyBook chapter 1 exercises.
4. Attend lab on Friday with your laptop.
5. Read the course documents (Moodle).
6.Give your consent to use Gradescope.
7.Complete the Orientation Quiz in Moodle.
8.Ask questions in Piazza.
34

More Related Content

PPTX
An Interactive Guide to Creating a Simple LabVIEW Program
englercm
 
DOC
Comp 122 lab 4 lab report and source code
pradesigali1
 
PDF
Loops
Learn By Watch
 
PDF
Java Basics.pdf
EdFeranil
 
PPTX
Introduction to computer science
umardanjumamaiwada
 
PPTX
lecture 6
umardanjumamaiwada
 
PPT
a basic java programming and data type.ppt
GevitaChinnaiah
 
An Interactive Guide to Creating a Simple LabVIEW Program
englercm
 
Comp 122 lab 4 lab report and source code
pradesigali1
 
Java Basics.pdf
EdFeranil
 
Introduction to computer science
umardanjumamaiwada
 
lecture 6
umardanjumamaiwada
 
a basic java programming and data type.ppt
GevitaChinnaiah
 

Similar to Week 1 (20)

PPTX
Java basics-includes java classes, methods ,OOPs concepts
susmigeorge93
 
PPTX
Intro to programing with java-lecture 3
Mohamed Essam
 
PPTX
Java basics
Hoang Nguyen
 
PDF
Week 2
EasyStudy3
 
PDF
(eTextbook PDF) for Starting Out with Java: From Control Structures through O...
gandheassoah
 
PPT
Unit 1: Primitive Types - Variables and Datatypes
agautham211
 
PPTX
Object oriented programming1 Week 1.pptx
MirHazarKhan1
 
PDF
CSC111-Chap_02.pdf
2b75fd3051
 
PPT
Introduction to Java Programming Part 2
university of education,Lahore
 
PPT
Mobile computing for Bsc Computer Science
ReshmiGopinath4
 
PPT
java programming for engineering students_Ch1.ppt
RasheedaAmeen
 
PPT
Programming with Java - Essentials to program
leaderHilali1
 
PPT
Java Concepts with object oriented programming
KalpeshM7
 
PPT
Basic elements of java
Ahmad Idrees
 
PPT
Introduction to java programming with Fundamentals
rajipe1
 
PPT
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
PPT
Programming with Java by Faizan Ahmed & Team
FaizanAhmed272398
 
PDF
Test bank for Big Java: Early Objects 6th Edition by Horstmann
mariksmarcas
 
PDF
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
RahulKumar342376
 
PPT
Presentation on programming language on java.ppt
HimambashaShaik
 
Java basics-includes java classes, methods ,OOPs concepts
susmigeorge93
 
Intro to programing with java-lecture 3
Mohamed Essam
 
Java basics
Hoang Nguyen
 
Week 2
EasyStudy3
 
(eTextbook PDF) for Starting Out with Java: From Control Structures through O...
gandheassoah
 
Unit 1: Primitive Types - Variables and Datatypes
agautham211
 
Object oriented programming1 Week 1.pptx
MirHazarKhan1
 
CSC111-Chap_02.pdf
2b75fd3051
 
Introduction to Java Programming Part 2
university of education,Lahore
 
Mobile computing for Bsc Computer Science
ReshmiGopinath4
 
java programming for engineering students_Ch1.ppt
RasheedaAmeen
 
Programming with Java - Essentials to program
leaderHilali1
 
Java Concepts with object oriented programming
KalpeshM7
 
Basic elements of java
Ahmad Idrees
 
Introduction to java programming with Fundamentals
rajipe1
 
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
Programming with Java by Faizan Ahmed & Team
FaizanAhmed272398
 
Test bank for Big Java: Early Objects 6th Edition by Horstmann
mariksmarcas
 
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
RahulKumar342376
 
Presentation on programming language on java.ppt
HimambashaShaik
 
Ad

More from EasyStudy3 (20)

PDF
Week 7
EasyStudy3
 
PDF
Chapter 3
EasyStudy3
 
PDF
Week 6
EasyStudy3
 
PDF
2. polynomial interpolation
EasyStudy3
 
PDF
Chapter2 slides-part 2-harish complete
EasyStudy3
 
PDF
L6
EasyStudy3
 
PDF
Chapter 5
EasyStudy3
 
PDF
Lec#4
EasyStudy3
 
PDF
Chapter 12 vectors and the geometry of space merged
EasyStudy3
 
PDF
Week 5
EasyStudy3
 
PDF
Chpater 6
EasyStudy3
 
PDF
Chapter 5
EasyStudy3
 
PDF
Lec#3
EasyStudy3
 
PDF
Chapter 16 2
EasyStudy3
 
PDF
Chapter 5 gen chem
EasyStudy3
 
PDF
Topic 4 gen chem guobi
EasyStudy3
 
PDF
Gen chem topic 3 guobi
EasyStudy3
 
PDF
Chapter 2
EasyStudy3
 
PDF
Gen chem topic 1 guobi
EasyStudy3
 
Week 7
EasyStudy3
 
Chapter 3
EasyStudy3
 
Week 6
EasyStudy3
 
2. polynomial interpolation
EasyStudy3
 
Chapter2 slides-part 2-harish complete
EasyStudy3
 
Chapter 5
EasyStudy3
 
Lec#4
EasyStudy3
 
Chapter 12 vectors and the geometry of space merged
EasyStudy3
 
Week 5
EasyStudy3
 
Chpater 6
EasyStudy3
 
Chapter 5
EasyStudy3
 
Lec#3
EasyStudy3
 
Chapter 16 2
EasyStudy3
 
Chapter 5 gen chem
EasyStudy3
 
Topic 4 gen chem guobi
EasyStudy3
 
Gen chem topic 3 guobi
EasyStudy3
 
Chapter 2
EasyStudy3
 
Gen chem topic 1 guobi
EasyStudy3
 
Ad

Recently uploaded (20)

PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
CDH. pptx
AneetaSharma15
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 

Week 1

  • 1. COMPSCI 121: INTRODUCTION TO PROGRAMMING FALL 2019
  • 2. WHAT ARE THE GOALS FOR TODAY’S CLASS • Introduce you to some java programming constructs. • Introduce you to problem solving techniques. • Demo: using jGRASP • Point out good programming practices. 2
  • 3. LET’S PRACTICE T-P-S Think How do you learn best? (2 mins) Pair Share your thoughts with your neighbor (2 mins) Share A few insights with the class (2 mins) 3
  • 4. WHY ARE WE USING JAVA? 4 ● Java is a widely used language in industry. ● Knowledge of Java is in high demand. ● It is a mature language (1995) and is frequently updated. ● Helps you learn other languages. ● Created as a “Software Engineering” language so that large programs can be more easily developed and maintained. ● It is designed to support software best practices.
  • 5. WHY ARE WE USING JAVA? TIOBE Index for August 2019 https://www.tiobe.com/tiobe-index/ 5
  • 7. PROGRAMMING STEPS & PATTERN 7 Best to write a little code, test it, debug (find and remove errors) if test(s) failed, or continue to write more code if the test(s) passed. Note: A computer can only execute one step at a time. Each step must be clearly defined.
  • 8. PROGRAMMING STEPS All programming has this pattern: 8 Read and analyze all aspects of the problem the program is to address. Make sure you understand what the program should do- its logic. What are the inputs and outputs to the program? Write a step-by-step process, an algorithm, that will use the input to produce the expected output. Write code that will correctly implement some or all of the algorithm. Test some or all of the program: compare program output to correct output given a set of inputs. Continue to develop the program or debug if any tests did not pass.
  • 9. BAKING ANALOGY • Problem: I want some banana bread. • Input: The ingredients. • Output: Warm banana bread to eat! • Test: Must be edible and must look and taste like banana bread. • Algorithm: A step-by-step plan, like a recipe, for getting from the Input to the Output. • Implementation: in this case, ME! 9
  • 10. AN ALGORITHM YOU CAN ENJOY! 10 1. Preheat oven to 350 degrees F (175 degrees C). 2. Lightly grease a 9x5 inch loaf pan. 3. In a large bowl, combine flour, baking soda and salt. 4. In a separate bowl, cream together butter and brown sugar. 5. Stir in eggs and mashed bananas until well blended. 6. Stir banana mixture into flour mixture; stir just to moisten. 7. Pour batter into prepared loaf pan. 8. Bake in preheated oven for 60 to 65 minutes. 9. Bake until a toothpick inserted into center of the loaf comes out clean. 10. Let bread cool in pan for 10 minutes. 11. Turn out onto a wire rack. 12. Test the output! In this case, there is no way to partially implement the algorithm, so if the test fails, you’ll have to start over!
  • 11. HOW DOES PROGRAMMING IN JAVA WORK? 1. The problem is analyzed and understood. 2. Inputs and outputs defined. 3. An algorithm is designed. 4. Then, Java code is written in one or more source code files. 5. This file is compiled and run. 6. The output is generated. 7. Tests can be run on the output. 11
  • 12. DEMO IN zyBooks AND JGRASP Saving files in folders Compiling & running the Salary program (from zyBooks). Printing in single/new lines. Making/removing syntax errors. 12
  • 13. PROBLEM STATEMENT & ANALYSIS Write a program that calculates the annual salary given the hourly wage. 1. What are the inputs? 2. What are the outputs? THINK - PAIR - SHARE! 13
  • 14. DESIGN THE ALGORITHM Write a program that calculates the annual salary given the hourly wage. 1. What are the inputs? hourly wage 2. What are the outputs? annual salary 3. What is the algorithm (steps) for the program? THINK - PAIR - SHARE! 14
  • 15. DESIGN THE ALGORITHM 3. What is the algorithm (steps) for the program? 1. Get the hourly rate (for example, from user). hourlyWage = ? 2. Calculate the annual salary: hoursPerWeek = 40, weeksPerYear = 50 annSalary = hourlyWage * hoursPerWeek * weeksPerYear 3. Print the result. 15
  • 16. WRITE THE JAVA CODE - CHECK FOR ERRORS 16 Line numbers Class name Variable assignment Output statements Semi-colons DEMO IN JGRASP { } braces for scope Declare a variable Note: Code in jGRASP is color-coded
  • 17. CLICKER QUESTION #1 On which line does the program start when it runs? A. On the first line of the program public class Salary B. With the main statement public static void main C. With the print statement System.out.print 17
  • 18. ANSWER CLICKER QUESTION #1 On which line does the program start? A. On the first line of the program public class Salary B. With the main statement public static void main C. With the print statement System.out.print 18
  • 19. HOW DO WE ASSIGN VALUES TO VARIABLES? Assignment statements: create a variable and assign a value to it. int num = 450; • LHS has to be a variable, RHS has to resolve to a value. 19 value assignment operator data type variable name RHSLHS “Declaration” of the variable “num” with data type “int”. It also assigns the value 450 to that variable.
  • 20. ASSIGNMENT IN JAVA: WHAT HAPPENS IN MEMORY 20 EXAMPLE: After the execution of the last statement, the variable “num1” now references an integer value of 3. State diagram
  • 21. ASSIGNMENT IN JAVA: WHAT HAPPENS IN MEMORY 21 Notice that you declare a variable only once, but can use it as much as necessary once it’s declared. You cannot re-declare a variable: int a = 450; int a = 450; int a = 22; declaration OK re-declaration Not OK You can re-assign a variable: int a = 450; a = 22; re-assignment OK
  • 22. CLICKER QUESTION 2 Which variable holds the highest number? 1. int num = 14; 2. int num2 = (num + 1); 3. int num3 = num2; 4. num2 = num3 + 3; 5. num = 17; A. num B. num2 C. num3 22
  • 23. ANSWER CLICKER QUESTION 2 Which variable holds the highest number? 1. int num = 14; 2. int num2 = (num + 1); 3. int num3 = num2; 4. num2 = num3 + 3; 5. num = 17; A. num = 17 B. num2 = 18 C. num3 = 15 23
  • 24. HOW DID WE SHOW THE OUTPUT TO THE USER? 24 Uses "" to output a string literal. Multiple output statements continue printing on the same output line. Uses "" to output a string literal. Starts a new output line after the outputted values, called a newline.
  • 25. CLICKER QUESTION 3 Given this code (assume variables have been declared): hourlyWage = 20, hoursPerWeek = 40, weeksPerYear = 50; annSalary = hourlyWage * hoursPerWeek * weeksPerYear; Which one does not print 40000? A. System.out.print(annSalary); B. System.out.print(hourlyWage * hoursPerWeek * weeksPerYear); C. System.out.print(“annSalary”); D. System.out.print(20 * 40 * 50); 25
  • 26. CLICKER QUESTION 3 Given this code (assume variables have been declared): hourlyWage = 20, hoursPerWeek = 40, weeksPerYear = 50; annSalary = hourlyWage * hoursPerWeek * weeksPerYear; Which one does not print 40000? A. System.out.print(annSalary); B. System.out.print(hourlyWage * hoursPerWeek * weeksPerYear); C. System.out.print(“annSalary”); D. System.out.print(20 * 40 * 50); 26
  • 27. PROBLEM: HOW DO WE GET USER INPUT FOR OUR PROGRAM? For our Salary program, we want the user to input the wage from the keyboard. Solution: we use the special Java text parser called Scanner. Steps: 1. Create a Scanner object: Scanner scnr = new Scanner(System.in); System.in corresponds to keyboard input. 2. Given Scanner object scnr, get an input value and assign to a variable with scnr.nextInt() scnr.nextInt()is a function (method) that gets the input value of type integer (int) 27
  • 28. USING SCANNER TO GET USER INPUT FROM KEYBOARD 28 Import the Scanner class Create instance Scanner method
  • 29. TESTING FOR LOGIC ERRORS 29 Now let’s calculate the monthly salary. What if our program works but the output is wrong?
  • 30. GRADING CRITERIA OF PROGRAMMING PROJECTS Code Style: follows good style (as defined in the course material). This means your code is readable to you and to others. This is professional “best practice”. Code Compiles: A program that doesn't compile cannot run and is not a successful program. This program does not get credit. Logical Correctness: The code is logically correct if it runs and produces the correct output. 30
  • 31. CLICKER QUESTION 4 A. All that glittersis not gold. B. All that glitters is not gold. C. All that glitters isnot gold. D. All that glitters is not gold. What is the output of running this file? 31
  • 32. CLICKER QUESTION 4 ANSWER A. All that glittersis not gold. B. All that glitters is not gold. C. All that glitters isnot gold. D. All that glitters is not gold. What is the output of running this file? Watch white spaces! 32
  • 33. SOME GOOD PROGRAMMING PRACTICES 1. Create a good solution (algorithm) first, before you start coding. 2. Compile after writing only a few lines of code, rather than writing tens of lines and then compiling. 3. Make incremental changes— Don't try to change everything at once. 4. Focus on fixing just the first error reported by the compiler, and then re-compiling. 5. When you don't find an error at a specified line, look to previous lines. 6. Be careful not type the number "1" or a capital I in System.out.println. 7. Do not type numbers directly in the output statements; use the variables. 33
  • 34. Week 1 TODO List: 1. Register your iClicker in Moodle. 2. Install OpenJDK and jGRASP. 3. Complete zyBook chapter 1 exercises. 4. Attend lab on Friday with your laptop. 5. Read the course documents (Moodle). 6.Give your consent to use Gradescope. 7.Complete the Orientation Quiz in Moodle. 8.Ask questions in Piazza. 34