Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
1Programming Fundamentals I
CS 110, Fall 2016, Central Washington University
Q) Why was the computer cold?
Warm-up “Question”
A) It left its Windows open.
10/4/2016
Lab 2
 Due date is this coming-up October 5. Submit your lab files to Canvas zipped.
Homework Assignment #1
 Was due last night, at 11:59pm
 End-of-the-chapter questions: submit your answers via Canvas: upload a single file 
(.doc, or .pdf (preferred)) and .java files all zipped together.
 Details abut the homework are always posted on the course website
Open Lab Hours
 Monday through Thursday: 8:00 a.m. to 10:00 p.m.
 Friday: 8:00 a.m. to 5:00 p.m.
 Sunday: 1:00 p.m. to 10:00 p.m.
 5pm onward, Monday-Thursday, and 1-10pm on Sunday, the student TA in the labs 
(look for “TA on duty” sign) will be able to answer your questions
JGrasp is available for download at http://www.jgrasp.org
10/4/20162
 Counts for 15% of your grade
 Must be a non-trivial Java program
 Open-ended: you decide
 Assigned after the midterm; due before the final
I'll display several sample programs throughout the quarter to give you ideas 
of the types of programs that you can create.
Sample final project: word Jumble DEMO
10/4/20163
A. The value of result is 4
B. The value of result is 4.0
C. The value of result is 2.5
D. The value of result is 5
E. The value of result is 5.0
The following code will output what to the screen?
variable1 = 25
variable2 = 10
Declare two variables, 
both of type integer, and 
assign their values
10/4/20164
A. The value of result is 4
B. The value of result is 4.0
C. The value of result is 2.5
D. The value of result is 5
E. The value of result is 5.0
The following code will output what to the screen?
variable1 = 25
variable2 = 10
result = ?
Declare a third variable, of type double, and assign 
it the value that is the result of the integer division 
calculation of 25/10 = 2.5
10/4/20165
A. The value of result is 4
B. The value of result is 4.0
C. The value of result is 2.5
D. The value of result is 5
E. The value of result is 5.0
The following code will output what to the screen?
variable1 = 25
variable2 = 10
result = ?
… recall integer division “drops” the 
decimal place, so:
25/10 = 2.5 → 2
10/4/2016
6
Poll Question … from last time ...
A. The value of result is 4
B. The value of result is 4.0
C. The value of result is 2.5
D. The value of result is 5
E. The value of result is 5.0
The following code will output what to the screen?
variable1 = 25
variable2 = 10
result = 2.0
… but the variable result is a double, so 
the value stored “in” the variable result is 
a decimal number … 2.0
10/4/20167
Poll Question … from last time ...
A. The value of result is 4
B. The value of result is 4.0
C. The value of result is 2.5
D. The value of result is 5
E. The value of result is 5.0
The following code will output what to the screen?
variable1 = 25
variable2 = 10
result = 2.0
Next up you cast the value of result (which is 
declared of type double) to a type integer
10/4/2016
8
Poll Question … from last time ...
A. The value of result is 4
B. The value of result is 4.0
C. The value of result is 2.5
D. The value of result is 5
E. The value of result is 5.0
The following code will output what to the screen?
variable1 = 25
variable2 = 10
result = 2.0
… which makes this integer division:
10 / 2 = 5
10/4/20169
Poll Question … from last time ...
A. The value of result is 4
B. The value of result is 4.0
C. The value of result is 2.5
D. The value of result is 5
E. The value of result is 5.0
The following code will output what to the screen?
result = 2.0
variable2 = 10
result = 5
But because result is of type double, then 
the decimal form of 5 is saved into the 
variable result
10/4/201610
Warm up Question …
A. I only F. I and II
B. II only G. I and III
C. III only H. II and IV
D. IV only I. II, IV and V
E. V only J. II, III and V
I. x += 2;
II. x -= 10;
III. x -= -10;
IV. x += -10;
V. x %= 5;
x = 14
x = 2
x = 22
x = 2
x = 2
Assuming that int x = 12; then which of the following will update 
the value of x so that its value is 2?
x = x + 2;
x = x - 10;
x = x - (-10);
x = x + (-10);
x = x % 5;
10/4/201611
Named constants using final
The String class (brief introduction)
Scope
Comments
Conventions
10/4/201612
What if as a programmer, you are given a piece of code, and you come 
upon the following statement:
double amount = originalValue * 0.15;
Although it may be clear what calculation the statement performs, it is not clear WHY. 
You'd have to read the rest of the code, to figure out why originalValue is being multiplied by 
0.15.
Also, the number 0.15 may appear multiple times throughout the code, so if ever that value 
needs to be changed, it needs to be changed in all places where it appears.
To make things easier, a named constant can be used, to designate a variable that is 
assigned, and which cannot be changed. By convention, all such variables are named 
with all capital letters, and are designated final:
final double INTEREST_RATE = 0.15;
Then the statement is much easier to read:
double amount = originalValue * INTEREST_RATE;
10/4/201613
Can you think of a named constants that you may need to refer to on a constant 
basis (especially if you are performing geometric mathematical calculations)?
double areaOfCircle = radius * radius * 3.141592653;
It would be tedious to have to write out the value of Pi each time that you need to use 
it. Luckily, the Math class provides a predefined named constant, Math.PI, which is 
assigned the value 3.14159265358979323846. Then, performing calculations that 
involve many instances of the value of Pi becomes easier:
double areaOfCircle = radius * radius * Math.PI;
And what if you require calculations that are precise to the 18th decimal digit? 
Then you'd have to write:
double areaOfCircle = radius * radius * 3.141592653589793238;
You used Math.PI in the last lab.
10/4/201614
String univName = “Central Washington University”;
The String class allows you to create objects for holding strings. It has various methods 
for manipulating and working with objects of type String.
A Class is used to specify attributes and methods that a 
particular type of object may have.
Section 2.9 in the textbook
(an introduction … MUCH more on Strings later)
The name of the variable
The type of data that the 
variable will hold
The assignment operator A String literal
10/4/201615
The String class allows you to create objects for holding strings. It has various methods 
for manipulating and working with objects of type String.
Why are all of these variables that holds strings, different?
String univName1 = "Central Washington University ";
String univName2 = "central Washington University";
String univName3 = "Central washington University";
String univName4 = "Central Washington university";
String univName5 = "Central Washington University.";
An empty space IS a character
Just like an empty space, a period is a character.
10/4/201616
The String class allows you to create objects for holding strings. It has various 
methods for manipulating and working with objects of type String.
Why are all of these variables that holds strings, different?
String univName1 = “Central Washington University ”;
String univName2 = “central Washington University”;
String univName3 = “Central washington University”;
String univName4 = “Central Washington university”;
String univName5 = “Central Washington University.”;
Remember that Java is case-sensitive, so an upper-case letter is considered
a different letter than the lower-case equivalent
10/4/201617
The String class allows you to create objects for holding strings. It has various methods 
for manipulating and working with objects of type String.
So, these strings are all different…
String univName1 = “Central Washington University ”;
String univName2 = “central Washington University”;
String univName3 = “Central washington University”;
String univName4 = “Central Washington university”;
String univName5 = “Central Washington University.”;
Thus, yes, all of these String variables are different, 
hence they must all have unique variable names.
10/4/201618
The String class is NOT a primitive data type.
It has many methods for working with strings.
Assuming that the following has been declared:
String myString = “Hello ”;Methods will be covered in 
more depth in Chapter 5.
Method Description            Example                 Result
length() Determine length
To invoke the length method on the 
object myString, you first need to 
inform the computer which object 
you want to manipulate. In this case 
we say: “Using the object 
myString”
myString.length();   6
To invoke a method on an object, you use “dot”.
To invoke a method, you give the name of the 
method after the “dot”.
The length method takes zero arguments, and outputs an 
object of type int, which is the length (number of 
characters) in the string. A space IS a character!
How are you supped to know all of the possible (50+) methods that you can use to 
manipulate objects of type String?
Memorize?
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html
API = Application Programming InterfaceVersion Number
10/4/201619
The name of the method (clickable, for more information)
This is what follows the “dot” in the code
The dataType of the output of the method.
If int, it means that the method outputs an integer.
10/4/201620
The parameter(s) that the input needs.
If the parameters is () it means the method requires NO parameter
For the method matches, the parameter is regex, 
which is of type String
An explanation of the method.
10/4/201621
What do you think that the method charAt outputs?
Method Description               Example                      Result
length()             Determine length
charAt(index)    Get character at 
position index
To method charAt takes as input a single integer value, and 
outputs the character at that position in the string.
myString.length();  6
myString.charAt(1);
myString.charAt(5);
“e”
A string's FIRST character is at position 0.
“Hello “
Position: 0      1 2      3 4      5
A position index can be the blank character in a string.
“ ”
10/4/2016
22
substring(index
1,index2) 
The toLowerCase() and toUpperCase() methods take no arguments, and return Strings that 
are the all lower case and all upper case equivalents of the original string
Carrying on with our
String myString = “Hello ”;
Method Description               Example                         Result
length()               Determine length
charAt(index)      Get character at 
position index
myString.length();  6
myString.charAt(1);
myString.charAt(5);
“e”
“ ”
toLowerCase() Convert to lower
case myString.toLowerCase();  “hello “
toUpperCase() Convert to upper
case myString.toUpperCase(); “HELLO “
myString.substring(3,5); “lo“Return substring that 
begins at position 
index1 and continues 
until (but doesn't 
include) position 
index2
The space is not output, because the 
space is at position 5
10/4/2016
23
String myNewString = myString.toUpperCase(); The variable 
myNewString is
Unless there's a good reason to do otherwise, it's a good practice to assign a value to a 
variable as soon as you declare the variable … that way there's no chance you'll forget.
declared and assigned the
value “02 OCTOBER 2012 “.
The original string myString
Is NOT modified.
Remember, that you can assign AND 
declare a variable all at once, or using two 
separate commands:
Sample invocation of a few methods from the String Class.
Assuming String myString = “02 October 2012 ”;
String myNewString;
myNewString = myString.toUpperCase();
10/4/201624
Sample invocation of a few methods from the String Class.
Assuming String  myString = “02 October 2012 ”;
String myNewString = myString.toUpperCase();
The variable
myNewString is
declared and assigned the
value “02 OCTOBER 2012 “.
String myNewString = myString.toLowerCase();
The variable myNewString is
declared and assigned the
value “02 october 2012 “.
You can also invoke methods “on the fly,” without having to assign the
output of a method to a variable.
System.out.println(“Length of myString : “ + myString.length());
Q: What will the above code output to the screen?
Remember, in both methods the original string myString is NOT modified.
Length of myString : 16 Remember that a space 
is a character!
10/4/201625
Poll Question
A. The specialLetter is i D. The specialLetter is F
B. The specialLetter is I E. The specialLetter is e
C. The specialLetter is f F. The specialLetter is E
The following piece of code will output what to the screen?
Remember that the first character 
in an object of type String is at 
position 0.
length = 73, so
lengthOfString -3 = 70
The . is at position 72
The e is at position 71
10/4/201626
A variable's scope is the part of the program that has access to the variable
1. The variable declaration must be written before the variable is used
public static void main(String[] args) {
System.out.println(myVariable);
}
2. You cannot have two local variables with the same name
public static void main(String[] args) {
int myNumber = 47;
int myNumber = 56;
}
Much more on this later in the course
myVariable has not yet been declared,
which will cause an error because the 
variable has NO scope
myNumber is defined twice.
10/4/201627
Comments are notes of explanation, written by the 
programmer, that document the code.
 The compiler ignores them
 They should be succinct (briefly and clearly expressed) and to the point
 The goal is to enable somebody else to look at your code and to see what you were 
trying to do
 Single-line comments are designated using //
// this part of the code performs a calculation
someVariable = radius * diameter * lengthOfChord;
 Single-line comments can also be at the end of a line
int earthSunDistance = 149; // rounded integer value, in Mkm
 Multiple-line comments start with /* and end with */
/* File name: solarSimulation.java
author: Tatiana Harrison
Last modified: 04 October 2014
*/
public class SolarSystemSimulation { ...
… and don’t forget:
Comments are powerful tool in 
problem solving!
10/4/201628
We've already learned about proper naming of variables.
We've also learned that commenting makes code human readable.
… and that indenting is good … why?
What if I had given you this clicker question:
Which is the value of String called specialLetter?
A. The specialLetter is i D. The specialLetter is F
B. The specialLetter is I E. The specialLetter is e
C. The specialLetter is f F. The specialLetter is E
Case and point…
10/4/201629
To make code human-readable ...
All code lines inside a set of braces should be indented.
Compare:
public class ExampleClass{
public static void main(String[] args){
int someVariable = 43;
double anotherVariable = 45.3467;
}
}
With:
public class ExampleClass{
public static void main(String[] args){
int someVariable = 43;
double anotherVariable = 45.3467;
}
}
Which one is easier to work with?
The indentation makes it easier to see that all 
of the indented code
is part of the code for the class ExampleClass
Remember,
Following coding 
conventions is powerful tool 
in problem solving!
10/4/201630
For statements that span multiple lines, extra spaces are inserted at the beginning of 
the statement's second, third, etc. lines, which indicates that those statements are 
continuations.
public class ExampleClass {
public static void main(String[] args) {
int someVariable = 43;
double anotherVariable = 45.3467;
String aSentence = “Q: What do you call a deer “ +
“   with no eyes?” +
“A: No eye-dear”;
String sentence2 = “Q: What do you call a deer “ +
“   with no eyes?” +
“A: No eye-dear”;
}
}
This makes it 
easy to see that 
the variable
aSentence spans 
multiple lines
This makes it difficulty to see that the variable
aSentence spans multiple lines
10/4/2016
31
Reading Keyboard Input
Dialog Boxes
Start of Decision Structures (Chapter 3)
10/4/201632