Steven Castellucci
1. Create a new object using the keyword “new”
2. Along with “new”, call the constructor to
initialize object attributes
3. Typically, assign the object to a declared
reference variable
ClassName identifier = new ClassName(args);
E.g.: Blink blink = new Blink(true);
"True" represents whether the debug
statements are on or off.
EECS1021 W16 (Steven C.) 2
name
string
my name is steven
Sequence of characters
Non-primitive (i.e., object) data type MY NAME IS
STEVEN
Read-only objects (recreated but not modified)
◦ Any “changes” are actually new objects initialized with the
new value
Strings can be initialized like objects or primitives:
String name = new String(“My name is Steven”);
String name = “My name is Steven”;
◦ The compiler replace the “short form” with the proper
(i.e., object) initialization statement
EECS1021 W16 (Steven C.) 3
Strings can be concatenated (i.e., joined)
using “+” operator:
String s = “EECS” + “1021”;
String indexes are numbered 0 to length-1
String: EECS1021
Index: 01234567
EECS1021 W16 (Steven C.) 4
Added withdrawal amount check
Added toString method
Updated main method
Code demonstrated in lecture
EECS1021 W16 (Steven C.) 5
string tostring : allows you to add to existing
length(): the number of characters in this String
m strings.
charAt(index): the char at the passed index
substring(start, end): a new String containing only the
characters at the index from start (inclusive) to end (exclusive)
trim(): a new String with the same characters, but without
leading and trailing whitespace
equals(otherString): true iff the this and the other are identical
indexOf(otherString): the index of the first occurrence of
otherString in this String object
split(delimiter): an array of all Strings in this String that were
separated by delimiter
return: what you want to be returned to you
EECS1021 W16 (Steven C.) 6
Each primitive type has its own wrapper class
Wrapper classes store (“wrap”) its associated
value in an object and contain static methods
For example:
◦ double n = [Link](“8.3”); // n = 8.3
EECS1021 W16 (Steven C.) 7
outputs the sting as a single string/
line
[Link](“hello”);
[Link](“bye”);
◦ Outputs:
hellobye
outputs the string then outputs a new
line character
[Link](“hello”);
[Link](“bye”);
◦ Outputs:
hello
bye
EECS1021 W16 (Steven C.) 8
Use [Link](“format string”, args)
Similar to sprintf in Matlab
Format string represents the output, but with
placeholders and formatting options for the
passed arguments (see Formatter API)
two decimal places
Example:
◦ [Link](“PI to 2 decimal places is %.2f.”, [Link]);
Outputs: PI to 2 decimal places is 3.14.
amount of decimal
places wanted
EECS1021 W16 (Steven C.) 9
Use the PrintStream class to create an object
representing an output file
◦ The file will be created if it doesn’t already exist
◦ The file will be overwritten if it already exists, so be
careful when doing this
Use the print, println, printf methods as before
Example:
PrintStream output = new PrintStream(new File(“[Link]”));
[Link](“PI to 2 decimal places is %.2f.”, [Link]);
[Link]();
◦ This will create a file called “[Link]” with the contents
“PI to 2 decimal places is 3.14.”
EECS1021 W16 (Steven C.) 10
Use the Scanner class
Scanner has methods to read in the next…
◦ int, double, char, etc. (i.e., primitive value)
◦ String or an entire line of text as a String object
Also has methods to test if there is more
input to read-in
Example:
Scanner input = new Scanner([Link]); // from console
int value = [Link](); // user input
[Link]();
EECS1021 W16 (Steven C.) 11
Similar to console input, but you pass a File
object as an argument
Example:
Scanner fileInput = new Scanner(new File(“[Link]”));
String firstLine = [Link](); // first line from [Link]
[Link]();
Potential errors for file I/O:
◦ Missing file, file not readable, etc.
◦ Must add “throws” clause to method declaration
◦ More on handling these exceptions in two weeks
EECS1021 W16 (Steven C.) 12
Added console input and file output
Code demonstrated in lecture
to enter prompts
[Link]("Enter _____ : ");
double______= [Link]();
EECS1021 W16 (Steven C.) 13
Loop body
◦ Statements to be executed iteratively (i.e., to be
looped) where all of the statements are stored
Initialization statement (optional)
◦ Executed once, when the loop is first encountered
◦ Used to declare and/or initialize any variables use
within the loop body (be careful of variable scope)
Boolean condition to continue iteration (i.e.,
looping)
◦ Similar to the if-statement condition
◦ Loop body is executed if the condition holds (i.e., is
true)
Update statement (optional)
◦ Update variables/state at the end of each iteration
(i.e., loop)
EECS1021 W16 (Steven C.) 14
Flow: Syntax:
S Statement-S
for (initial; condition; bottom)
for {
body;
{ initial }
Statement-X
condition
false
Algorithm:
{ body } 1. Start the for scope
2. Execute initial
bottom
true
3. If condition is false go to 9
condition 4. Start the body scope {
5. Execute the body
}
6. End the body scope }
7. Execute bottom
X 8. If condition is true go to 4
9. End the for scope
EECS1021 W16 (Steven C.) 15
Output the numbers from 1..100
Sequential
[Link](“1”);
[Link](“2”);
[Link](“3”);
…
Iterative decrimentation
condition
for (int count = 1; count <= 100; count++)
{ intialization
[Link](count); gives value of count variable
}
EECS1021 W16 (Steven C.) 16
EECS1021 W16 (Steven C.) 17
Add non-negative ints, stop when a negative
integer is entered:
input
int sum = 0;
Scanner input = new Scanner([Link]);
for(int n = [Link](); n >= 0; n = [Link]())
{
sum += n;
}
[Link]();
EECS1021 W16 (Steven C.) 18
Read-in lines from a file and output them to the
console
Scanner fileInput = new Scanner(new File(“[Link]”));
for(String line; [Link](); line = [Link]())
{
line= [Link]()
[Link](line);
}
[Link]();
EECS1021 W16 (Steven C.) 19
Added file I/O
Code demonstrated in lecture
EECS1021 W16 (Steven C.) 20