Download COMP120 – Week 1

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
COMP313 – Software Design and Construction
Tim Kington
(H)659-0365
(C)560-1708
[email protected]
Office Hours: 30 minutes before class, or by appointment
Grading Summary:
4 Programming Assignments X 100 pts
2 Assignments X 50 pts
Object-Oriented Design Project
Midterm Exam
Final Exam
Total
400
100
100
150
250
1000
Compiling in C++ and Java
C++
MyClass.cpp => MyClass.o => a.out/MyApp.exe
Java
MyClass.java => MyClass.class => MyApp.jar
Data Types
Java
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
C++
Data Type:
bool
char
unsigned char
short
unsigned short
int
unsigned
long
unsigned long
float
double
long double
Arrays in Java
Declaration:
int[] intArray;
int intArray[];
Data Size:
1 byte
2 bytes
4 bytes
8 bytes
4 bytes
8 bytes
2 bytes
Initialization:
intArray = new int[10];
int[] intArray = new int[10];
intArray[0] = 1;
intArray[3] = 5;
intArray[x + y] = 17;
intArray[10] = 0;
// Error
intArray = new int[10];
intArray = new int[50];
int[] a = {1, 2, 3, 4};
Copying arrays:
int[] a = new int[10];
int[] b = a;
a[2] = 5;
// b[2] is now 5
int[] a = new int[10];
int[] b = new int[10];
for(int x = 0; x < 10; x++)
b[x] = a[x];
Array length:
// Calculate the average of the values in an array
int sum = 0;
for(int x = 0; x < a.length; x++)
sum += a[x];
double avg = (double)sum / a.length;
Two-dimensional Arrays in Java
Declaration:
int[][] array2d;
array2d = new int[3][5];
Initialization:
array2d[1][2] = 3;
int[][] a = {{1, 2, 3}, {4, 5, 6}};
Arrays in C++
Declaration
int c[10];
int *c;
Initialization
int
int
int
int
*c = new int[5];
c[3] = {1, 5, 25};
c[] = {1, 2};
c[2] = {1, 2, 3};
// Syntax Error
Two-dimensional Arrays in C++
Declaration
int a[2][3];
Initialization
int a[2][2] = {{0, 1}, {2, 3}};
int a[2][2] = {1, 2, 3};
// 2x3 array
Strings
Java
String s = “Hi”;
String t = “ there!”;
String u = s + t;
System.out.println(u);
String he = t.substring(2,4);
int len = t.length();
C++
char color[] = “blue”;
char color[] = {‘b’, ‘l’, ‘u’, ‘e’, ‘\0’};
char *color = “blue”;
char s[] = “Hi”;
char t[] = “ there!”;
char u[20];
strcpy(u,s);
strcat(u,t);
cout << u << endl;
char he[3];
strncpy(he, t + 2, 2);
int len = strlen(t);
Classes in Java
class Date {
private int day;
private int month;
private int year;
private static int numDatesCreated = 0;
Date(int d, int m, int y) {
numDates++;
day = d;
month = m;
year = y;
}
public
public
public
public
public
public
int getDay() { return day; }
void setDay(int d) { day = d; }
int getMonth() { return month; }
void setMonth(int m) { month = m; }
int getYear() { return year; }
void setYear(int y) { year = y; }
public String toString() {
return month + “/” + day + “/” + year;
}
public static int getNumDatesCreated() {
return numDatesCreated;
}
public static void main(String[] args) {
Date d1 = new Date(1,1,2004);
Date d2 = new Date(7,4,1776);
System.out.println(d1 + “\n” + d2);
System.out.println(“Num dates:” +
Date.numDatesCreated());
}
}
Access Specifiers in Java
public
Allows access from anywhere
protected Allows access from anywhere in the same package, and access in derived
classes
default
Allows access from anywhere in the same package
private
Can only be accessed from the class in which it is declared
Classes in C++
Structure Definitions
struct Date {
int day;
int month;
int year;
};
Date d;
Date &dateRef = d;
Date *datePtr = &d;
Accessing Structure Members
d.day = 5;
dateRef.day = 5;
datePtr->day = 5;
Classes
class Date {
public:
Date(int, int, int);
int getDay();
void setDay(int);
int getMonth();
void setMonth(int);
int getYear();
void setYear(int);
void print();
private:
int day;
int month;
int year;
}; // Don’t forget the ;
Date::Date(int d, int m, int y) {
day = d;
month = m;
year = y;
}
int Date::getDay() { return day; }
void Date::setDay(int d) { day = d; }
int Date::getMonth() { return month; }
void Date::setMonth(int m) { month = m; }
int Date::getYear() { return year; }
void Date::setYear(int y) { year = y; }
void Date::print() {
cout << month << “/” << day << “/” << year << endl;
}
int main() {
Date d(1, 1, 2004);
Date &dRef = d;
Date *dPtr = &d;
d.print();
dRef.setMonth(2);
dRef.print();
dPtr->setYear(2003);
dPtr->print();
}
Access Specifiers in C++
public
private
protected
Can be accessed from anywhere in the program
Can only be accessed in same class, and by friends
Can be accessed by members and friends of the class, and
members and friends of any derived classes
Classes default to private, structs to public.
Integrated Development Environments (IDEs)
Advantages:
 Editor, compiler, and debugger run in a seamless tool
 Graphical editors for use in building GUIs
 Automated building of an application
 Integrated help systems
 Language-Specific Editing/Syntax Highlighting
 Packaging Assistance (Building DLLs and JARs)
 Command Completion
Disadvantages:
 Source management system may not have an interface to the IDE
 Moving build to an automated system may require extra work
 GUI editors may destroy code when GUI is changed
 GUI editor may not generate the same code you would have written
 Cost
 Resource usage
 Startup time
An “expert” often likes a more stripped-down tools interface with fewer help features.
An able computer science student should know how to work with an IDE and with a
basic editor and compiler system. We often move back and forth between the
environments.
Installing Sun One Studio/Netbeans
1. Install JDK 1.4 from java.sun.com (J2SE)
2. Download and unzip docs into 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.
Using the On-Campus Labs
1. Create a directory on the PC, or on a floppy.
2. In a command window, type runide –userdir <mydir>
Assignment #1
Next Week
 Debugging
 I/O