Gregory M. Kapfhammer
Key Concept
Corresponding Diagram
In-Class Discussion
In-Class Activity
Details in the Textbook
Compiler
Java virtual machine
What are the inputs and outputs of these programs?
Draw a picture showing how these are related!
Types
Declaration
Assignment
Constants
How should we organize the declarations in our programs?
Integers and floating point values
Bit-depth trade-offs
Resource-constrained devices
Character strings and booleans
What does this look like in memory?
Arithmetic operators: +, -, *, /, %
Wait, what does %
mean?
Operator precedence
What about parentheses?
Review Sections 1.4.1 and 1.4.2 for more details
Can we ever transform one data type into another?
We can use widening and shortening conversions
Converting from a byte
to a short
is safe! Why?
Narrowing conversions can lead to information loss. Why?
Refer to Section 1.4.3 for details about casting
Assignment conversion: money = dollars
...
... when dollars is an int
and money is a float
Promotion conversion: result = sum / count
...
... when sum is a float
and count is an int
Casting conversion: dollars = (int) money
...
... when dollars is an int
and money is a float
public class Lincoln
{
// prints a presidential quotation
public static void main (String[] args)
{
System.out.println ("A quote by Abraham Lincoln:");
System.out.println ("Whatever you are, be a good one.");
}
}
Can you find? ... The definition of a class
public class Lincoln
{
// prints a presidential quotation
public static void main (String[] args)
{
System.out.println ("A quote by Abraham Lincoln:");
System.out.println ("Whatever you are, be a good one.");
}
}
Can you find? ... The main method
public class Lincoln
{
// prints a presidential quotation
public static void main (String[] args)
{
System.out.println ("A quote by Abraham Lincoln:");
System.out.println ("Whatever you are, be a good one.");
}
}
Can you find? ... Java keywords
public class Lincoln
{
// prints a presidential quotation
public static void main (String[] args)
{
System.out.println ("A quote by Abraham Lincoln:");
System.out.println ("Whatever you are, be a good one.");
}
}
Can you find? ... A method invocation
public class Lincoln
{
// prints a presidential quotation
public static void main (String[] args)
{
System.out.println ("A quote by Abraham Lincoln:");
System.out.println ("Whatever you are, be a good one.");
}
}
Can you find? ... A character string
public class Lincoln
{
// prints a presidential quotation
public static void main (String[] args)
{
System.out.println ("A quote by Abraham Lincoln:");
System.out.println ("Whatever you are, be a good one.");
}
}
Can you find? ... A single line comment
public class Lincoln
{
// prints a presidential quotation
public static void main (String[] args)
{
System.out.println ("A quote by Abraham Lincoln:");
System.out.println ("Whatever you are, be a good one.");
}
}
Can you find? ... A static variable
Allows us to implement conditional logic
Consider if(hours>STANDARD)
Consider if(myCoin.isHeads())
Wait! We can also have an else component!
Any questions about the use of this construct?
Review Section 1.5 for additional details
Floats, characters, and objects
Different methods for different data
Boolean and int are easy!
Remember, characters are just numbers!
Comparing a floating point is hard! Why?
For objects, we look at the address in memory
Perform some action repeatedly
Looping constructs: for
, while
, do-while
What are the similarities and differences?
When would you decide to use a certain construct?
Loops can tax the limited resources of a computer
A class defines behavior and data
An object is an instance of a class
Methods define behavior and variables store the data
Visibility modifiers control access
Variables/methods can either be instance or static
Inheritance supports code reuse
Methods have a "signature"
Returns values and parameters
Parameter passing in the Java language
Any questions about using methods in Java?
Write a Java program that includes a method that can swap two values.
Real world examples of string processing?
Purpose of and ways to index a string?
See Section 1.3 for more details about Strings
Arrays allow you to store a fixed number of variables
All values in the array are of the same type
Indexing an array can lead to out of bounds exceptions
Compare and contrast arrays and ArrayLists
Any questions about using arrays in Java?
Let's keep our source code organized!
Place related Java classes into a package
Make sure that your directories are correctly organized
Create an example of a package and a directory structure
Any questions about using packages in Java?
Instance variables
Constructors
Accessor methods
Utility method
Main method
How would you test this Java class?
Write a Java program called SimpleInput.java
that reads an integer value from
the user with java.util.Scanner
, generates a random integer value with
java.util.Random
, sums these two integers, and then outputs the result of the addition.