Download You must write a program that will monitor the temperature and

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COMP120 – Computer Science II
Tim Kington
(H)659-0365
(C)560-1708
[email protected]
Office Hours: 30 minutes before class, or by appointment
Syllabus Error: Participation Points
Grading Summary:
4 Programming Assignments X 100 pts
2 Midterm Exams X 100 pts
Final Exam
3 Module Assessments X 30 pts
Activities (20 pts/module)
Stacks Assignment
Total
Basic Characteristics of Java
 Platform Independent
o Java Virtual Machine
 Object Oriented
o Encapsulation
o Polymorphism
o Inheritance
 Secure Environment
o Sandbox
Types of Applications
 Standalone
o Command line
o GUI
 Applets
 Servlets
 Enterprise Java Beans
400
200
160
90
120
30
1000
Java Virtual Machine
Java is platform independent because all Java programs run inside of a virtual
machine. This means that any Java program will run on any platform for which a virtual
machine exists.
Installing Java
http://java.sun.com/j2se/1.4.2/download.html
Download J2SE v 1.4.2_01
J2SE v 1.4.2 Documentation
Download NetBeans IDE v 3.5.1 with J2SE v 1.4.2 COBUNDLE
Installation
1. Install JDK
2. Install docs in same directory
3. Add bin directory to PATH
The JDK documentation is HTML. The start page is index.html in the docs subdirectory.
When looking for documentation on the standard classes, you want the Java 2 Platform
API Specification.
Writing a program
 Editor is all you need
 .java => .class => .jar
 javac MyProg.java
 java MyProg
 Classpath
Hello World:
Hello.java
public class Hello
{
public static void main(String [] args)
{
System.out.println(“Hello, world!”);
}
}
Primitive Data Types:
Data Type:
byte
short
int
long
float
double
char
boolean
Range:
-128 to 127
-32768 to 32767
-2147483648 to 2147483647
-9223372036854775808 to 922372036854775807
-3.4E38 to 3.4E38 with 7 digits accuracy
-1.7E308 to 1.7E308 with 17 digits accuracy
0-65535
true, false
Variable Declarations:
int x;
int y = 1;
byte b = 115;
double d = 3.27;
boolean b = true;
char c = ‘A’;
long l = 15L;
float f = 1.25F;
double d = 2.56E10;
int x = 0xFF;
int y = 0356;
Data Size:
1 byte
2 bytes
4 bytes
8 bytes
4 bytes
8 bytes
2 bytes
Arithmetic Examples:
int x, y, z;
double a, b, c;
a = b;
x = y = z = 42;
x = y + z;
y = x – z;
a = b * c;
b = a / c;
x = y % 10;
x = 2.5 * 4;
x = (int)2.5 * 4;
x = (int)(2.5 * 4);
a = 5 / 2;
a = (double)5 / 2;
a = (double)(5 / 2);
x++;
++x;
x = 3;
y = x++;
x = 3;
y = ++x;
a = 3.56 % 1.25;
x
x
x
x
x
x
= 5;
+= 3;
-= 2;
*= 6 / 3;
/= 3;
%= 3;
//8
//6
//12
//4
//1
Arithmetic Exceptions:
int x = 5 / 0;
Output:
java.lang.ArithmeticException: / by zero
at MyProg.MyProg.main(MyProg.java:24)
Exception in thread "main"
byte b = 100 + 100;
System.out.println(b);
Output:
-56
double d = 5.0 / 0;
System.out.println(d);
Output:
Infinity
double d = 0.0 / 0;
System.out.println(d);
Output:
NaN
Math Functions and Constants:
double s = Math.sin(2.5);
int a = Math.abs(-3);
double c = Math.PI * 2 * r;
Character Data:
Character data in java uses two bytes per character.
char c = ‘A’;
c = c + 1;
Escape Sequences:
\b
\f
\n
\r
\t
\\
\u58
backspace
form feed
newline
carriage return
tab
backslash
Unicode character number
Bitwise Operators:
NOT(~)
x result
0 1
1 0
NOT toggles all bits in the input:
Result = ~Input;
Input:
Result:
0x35
0xCA
00110101
11001010
AND (&)
x y result
0 0 0
0 1 0
1 0 0
1 1 1
AND turns off bits that are 0 in the mask:
Result = Input & Mask;
Input:
Mask:
Result:
0x35
0xF0
0x30
00110101
11110000
00110000
OR(|)
x y
0 0
0 1
1 0
1 1
result
0
1
1
1
OR turns on bits that are 1 in the mask:
Result = Input | Mask;
Input:
Mask:
Result:
0x35
0xF0
0x35
00110101
11110000
11110101
XOR(^)
x y result
0 0 0
0 1 1
1 0 1
1 1 0
XOR toggles bits that are 1 in the mask:
Result = Input ^ Mask;
Input:
Mask:
Result:
0x35
0xF0
0xC5
00110101
11110000
11000101
Shift Operators:
<<
>>
>>>
Left Shift
Right Shift
Unsigned Right Shift
byte x = 0x35;
x = x << 2;
x <<= 1;
00110101
11010100
10101000
x >>= 1;
00011010
byte x = 0xF4;
byte y = x >> 1;
11110100
11111010
// -12
// -6
byte z = x >>> 1;
01111010
Operator Precedence:
// 122
Precedence Group:
(), [], ., postfix ++, postfix -unary +, unary -, prefix ++, prefix --, ~, !
(type), new
*, /, %
+, <<, >>, >>>
<, <=, >, >=, instanceof
==, !=
&
^
|
&&
||
?:
=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^=
Associativity:
left
right
left
left
left
left
left
left
left
left
left
left
left
left
right
int x = 3 + 4 * 5;
int y = (3 + 4) * 5;
Comments:
int x = 3;
int y = 17;
//
/*
Single-line comment
This is a multi-line
comment */
x += 2;
//
Add 2 to x
x += 2;
//
Skip the next 2 entries in the list
/**
*
*
*
*
*/
This is a JavaDoc comment
@author Tim Kington
Comparison Operators:
boolean b = true;
b = 5 > 3;
b = x >= 6;
b = y < x;
b = 6 <= z;
b = !b;
b = x != y;
b = x == y;
If statements:
if(x > 5)
y = 3;
if(x < 10)
{
y = 4;
b = true;
}
if(x > 6)
y = 2;
b = false;
if(x > 2)
y = 3;
else y = 4;
if(x > 3)
y = 1;
else if(x < 0)
y = 2;
else y = 3;
if(x > 5)
{
if(y > 3)
z = 7;
else z = 4;
}
else
{
if(y == 4)
z = 1;
else z = 2;
}
Logical operators:
if(x > 2 && y > 5)
z = 4;
if(x > 2 || y > 5)
z = 4;
if(!(x > 2))
z = 4;
if(((x > 2) && (y < 5)) ||
((x < 0) && (y > 17)))
do something;
Ternary Operator:
boolean b = true;
if(b)
x = 3;
else x = 5;
x = b ? 3 : 5;
Variable Scope:
public static void main(String args[])
{
int a = 1;
if(true)
{
int b = 2;
System.out.println(“a = “ + a + “, b = “ + b);
int c = 3;
//int a;
}
//System.out.println(“b = “ + b);
int c = 5;
System.out.println(“a = “ + a + “, c = “ + c);
}
Switch Statement:
int x;
switch(x)
{
case 0:
y = 1;
z = 2;
break;
case 1:
y = 3;
case 2:
z = 4;
break;
default:
z = -1;
break;
}
Loops:
int x = 0;
While:
while(x < 10)
{
System.out.println(x++);
}
Do..While:
do
{
System.out.println(x--);
} while(x > 0);
For:
for(x = 0; x < 10; x++)
System.out.println(x);
for(initialization; test; increment)
{
body;
}
initialization;
do
{
body;
increment;
} while(test);
for(x = 0; x < 10; x++)
{
for(y = 0; y < 10; y++)
System.out.println(“x = “ + x + “, y = “ + y);
}
Continue Statement:
//
Add some odd numbers
int x, sum = 0;
for(x = 0; x < 20; x++)
{
if(x % 2 == 0)
continue;
sum += x;
}
System.out.println(“Sum = “ + sum);
Break Statement:
//
Add numbers until the total is > 100
int x, sum = 0;
while(true)
{
sum += x;
if(sum > 100)
break;
x++;
}
System.out.println(“Adding “ + x + “ sent us over 100!”);
Labeled Break Statement:
int x, y;
outerLoop:
for(x = 0; x < 10; x++)
{
for(y = 0; y < 10; y++)
{
if(x * y == 42)
break outerLoop;
}
}
I/O in Java vs. C++:
#include <io stream>
import java.io.*;
Output:
cout << “x = “ << x << endl;
System.out.println(“x = “ + x);
Input:
cout << “Enter a number:”;
cin >> x;
int num = -1;
System.out.print(“Enter a number:”);
try
{
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
String line = reader.readLine();
x = Integer.parseInt(line);
}
catch(Exception e)
{
e.printStackTrace();
}
Translation Exercise
Problem Solving Example
You must write a program that will monitor the temperature and humidity in a
greenhouse. The program will run for a twenty-four hour period to keep the greenhouse
at a given target temperature and humidity. The program will measure the temperature
and humidity once every minute. The user may change the target temperature and
humidity at any time. If the temperature falls below the target temperature, the
greenhouse heater must be turned on until the temperature rises above the target
temperature. If the humidity falls below the target humidity, the humidifier must be
turned on until the humidity rises above the target humidity.
Learning Sessions
Assignment #1
Next Week
 Arrays
 Functions
 Files
One-minute Paper
 What was the most important thing learned?
 What are you still confused about?
Problem Solving Activity
Related documents