Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSE114 Spring 2016
Lab Exercise 2 of Week 1
Values, Expressions, Types, and Variables
Chen-Wei Wang
Task: Create a new Java project CSE114 S16 Week01 Lab2.
1 Values
In this lab session we focus on two kinds of values in Java: strings and numbers (i.e., integers and
fractional numbers).
1.1 String Values
Each string value is a sequence of characters, where each character is a key stroke from your keyboard,
enclosed within a pair of double quotes (i.e., "). For example, "a" is a string containing a single character,
"SUNY" is a string containing four characters, and etc. Note that each white space, if appearing in a string,
is also considered as a character in that string. For example, "SUNY Korea" is a string containing 10
characters (including the white space between the two words). There is also a special string: an empty
string "" that contains no characters at all.
String values need not stand alone! You may concatenate two or more string values together, meaning
that you put them one after another. To concatenate, we use the plus (+) operator. For example, you
write "SUNY" + " " + "Korea" to mean the concatenation of three string values. The result from con-
catenating a list of string values is simply another string value, whose components are ordered according
to how you concatenate them. For example, writing "SUNY" + " " + "Korea" will result in a string
value "SUNY Korea".
When you write a System.out.println(. . . ) statement in Java, where . . . is any string value or a
concatenation of string values, then that string value will be output to the console verbatim.
Task: Create a new Java class StringValues. Let us try some examples:
public class StringValues {
public static void main(String[] args) {
/* a single string */
System.out.println("SUNY Korea 1");
/* concatenations of two strings */
System.out.println("SUNY " + "Korea 2");
System.out.println("SUNY" + " Korea 3");
/* a concatenation of three strings */
System.out.println("SUNY" + " " + "Korea 4");
/* print "SUNY " and "Korea" on the same line */
System.out.print("SUNY ");
System.out.println("Korea 5");
1
/* any later string will start with a new line */
/* print "SUNY " and "Korea" on two different lines */
System.out.println("SUNY ");
System.out.println("Korea 6");
}
}
Running/Executing the above Java program on Eclipse will produce the following output to the
console:
SUNY Korea 1
SUNY Korea 2
SUNY Korea 3
SUNY Korea 4
SUNY Korea 5
SUNY
Korea 6
Notice that there is a subtle, yet useful, difference between the two print statements System.out.print
and System.out.println: think of the latter (println) as the former (print) appended with a new line
(i.e., by hitting the return/enter key).
1.2 Numerical Values
Each numerical value can either be an integer (without a fractional part) or a real number (with a
fractional part). How you write a number in Java is exactly the same as how you would write it in
mathematics!
Task: Create a new Java class NumericalValues. Let us try some examples:
public class NumericalValues {
public static void main(String[] args) {
System.out.println("Number one is " + 1);
System.out.println("Number two is " + 2);
System.out.println("Number one point two is " + 1.2);
System.out.println("Pi is " + 3.1415926);
System.out.println();
System.out.println("Number two followed by number two is " + 1 + 2);
System.out.println("Number one point two followed by pi is " + 1.2 + 3.1415926);
System.out.println();
System.out.println("Result of one plus two is " + (1 + 2));
System.out.println("Result of one point two plus pi is " + (1.2 + 3.1415926));
}
}
Running/Executing the above Java program on Eclipse will produce the following output to the
console:
Number one is 1
Number two is 2
Number one point two is 1.2
Pi is 3.1415926
2
Number two followed by number two is 12
Number one point two followed by pi is 1.23.1415926
Result of one plus two is 3
Result of one point two plus pi is 4.3415926
2 Types and Variables
Task: Create a new Java class Variables. Try the examples below within the main method of this class.
In the above programs of NumericalValues and StringValues, you may find it tedious and incon-
venient to repeatedly write out the same string values (i.e., "SUNY", "Korea", and " ") and numerical
values (i.e., 1, 2, 1.2, 3.1415926, etc.).
We may store each of these recurring values in a variable (i.e., a place holder of value). Once a value
is stored in a variable, we may only refer to the name of that variable, without explicitly writing out that
value. To store a value in a variable in Java, there are two steps:
1. Declare the name of a variable, as well as what type of value (string or number) it is going to
hold/store. It is important to note that: 1) the name and type of a variable, once declared,
cannot be changed; and 2) the value that a variable holds/stores may change at any time. As an
example, let us declare a string variable s, an integer variable i, and a real number variable r:
String s; /* declaring a variable named s that stores a string value */
int i; /* declaring a variable named i that stores an integer value */
double r; /* declaring a variable named r that stores a real number value */
This step of declaration can only be done once, after which a variable’s name and type are fixed
(and cannot be re-declared).
2. Assign a value to the variable. In Java, we use the equal sign = (called the assignment operator) for
variable assignments. It is critical that the value that you assign to a variable must be consistent
with its declared type; otherwise, Eclipse will report a type error. For example, from the above
declarations of variables s, i, and r, the following assignments are considered as not type correct :
s = 2.3;
i = "SUNY";
r = "Korea";
On the other hand, the following assignments are considered as type correct :
s = "SUNY Korea";
i = 30;
r = 3.14;
The value of a variable may change for as many times as you wish. Each change of a variable’s
value is called a re-assignment to it. This means that after each re-assignment to the same variable,
a print out of its value might just give you a different result! For example:
String name = "Jackie";
System.out.println("Name is " + name); /* output is Jackie */
String name = "Jonathan";
System.out.println("Name is " + name); /* output is Jonathan */
String name = "Jim";
System.out.println("Name is " + name); /* output is Jim */
3
Task: Create a new Java class StringVariables. Let us write a program, using variables, that
produces the same output as does the StringValues program:
public class StringVariables {
public static void main(String[] args) {
String suny = "SUNY";
String korea = "Korea";
String space = " ";
int counter = 1;
System.out.println(suny + space + korea + space + counter);
counter = 2; /* re-assignment to counter */
System.out.println(suny + space + korea + space + counter);
counter = 3; /* re-assignment to counter */
System.out.println(suny + space + korea + space + counter);
counter = 4; /* re-assignment to counter */
System.out.println(suny + space + korea + space + counter);
counter = 5; /* re-assignment to counter */
System.out.print(suny + space);
System.out.println(korea + space + counter);
counter = 6; /* re-assignment to counter */
System.out.println(suny + space);
System.out.println(korea + space + counter);
}
}
Exercise: Create a new Java class NumericalVariables. Write a program, using variables, that
produces the same output as does the NumericalValues program.
3 Expressions
Like in mathematics, numerical values and variables may be combined, using numerical operators, to form
complicated numerical expressions. All standard numerical operators are supported in Java: addition
(+), subtraction (-), multiplication (*), and division (/). You can change the sign of a value or variable
(from positive to negative, or vice versa) using the negation operator (-). You can also calculate the
remainder from a division using the modulo operator (%), e.g., 10 % 3 will give you the remainder 1. You
may also use parentheses () to force a particular order of evaluation, e.g., (2 + 3) * 6.
Exercise: Create a new Java class NumericalExpressions. In its main method, declare numerical
variables, (re-)assign values to them, form complicated expressions from these variables, and print out
the values of these expressions. For example:
public class NumericalExpressions {
public static void main(String[] args) {
int i = 5;
int j = 3;
int k = 2;
System.out.println("((i + j) * k) % i is " + ((i + j) * k) % i);
}
}
Write more example on your own!
4