File - AP Computer Science
Download
Report
Transcript File - AP Computer Science
Agenda
•Hierarchy of computer
•1st, 2nd, 3rd generation of programming
languages
•Compiler, executable
•Java jargons – JDK, IDE, JRE, JVM
•Statement, Identifier, variable, declaration,
initialization, keywords
•What types do you know?
•What’s type casting?
•Arithmetic, logical, rational, assignment
operators
statements
• A set of instructions
• A semicolon is required to indicate the end
of a statement.
• Related statements are enclosed by curly
braces ({ }) to begin and end the
instructions.
Topics
• Conditional control structure
if –else
if – else if
nested if –else
switch
• Object scanner, Math, Random
Quiz example
int
aNumber;
?
?
aNumber = 10;
?
• In two statements, declare a variable
named numBeads and assign it the value
5.
• What is the final value of yourNumber after
the last statement executes?
int myNumber = 5;
int yourNumber = 4;
myNumber = yourNumber * 2;
yourNumber = myNumber + 5;
• The java.util package contains a class
named Vector . Write a statement that
makes the Vector class accessible to an
application.
• Determine the appropriate data type for
each of the following values:
a) the number of basketballs in a department
store.
b) the price of a basketball.
c) whether a basketball player has received a
jersey or not.
• What package is loaded automatically?
• Why we can use System.out.println
without using import
• What are the 2 ways to write comments?
• What is the entry point(function/method)
for the program to execute?
• What is the difference between
System.out.print and System.out.println.
• What primitive data type do you know?
Int num1 = 5;
Int num2 = 3;
Int result;
double doubleNum1 = 5;
double doubleNum2 = 3;
double doubleResult;
result = num1 / num2;
doubleResult = num1 / num2;
System.out.println("num1 / num2: " + result);
• Using the following declarations, rewrite
the statements to include the appropriate
type casting, rounding where necessary. If
type casting is not necessary, explain why:
int j = 5;
double k = 1.6;
int y;
double z;
a) y = j * k;
b) z = j * k;
c) z= k * k;
Consider the code segment
if (n == 1)
k++;
else if (n == 4 )
k += 4;
If the given segment is rewritten in the form
if(/*condition*/)
1
/* assignment statement */
a)(1) n == 1 && n == 4
(2) k += n
b)(1) n == 1 && n == 4
(2) k += 4
c)(1) n == 1 || n == 4
(2) k += 4
d)(1) n == 1 || n == 4
(2) k += n
e)(1) n == 1 || n == 4
(2) k = n - k
2
• Explain why the following names are illegal?
139
139Abc
fast One
class
Slow.Sally
double
gold;Nugget
hopper-gee
Quiz exercises
Write a switch structure that uses the
character myChar. It should increment
the integer variable y if myChar is
either a capital or small letter G. It
should decrement y if myChar is either
a capital or a small letter M. If myChar
is anything else, add 100 to y.
Assume num1 and num2 contain integer
values. Write an if-else if statement that
displays one of the following messages as
appropriate:
First number is larger.
Second number is larger.
Numbers are equal.