…WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

16
…WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA

Transcript of …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

Page 1: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

… W H A T Y O U S H O U L D H A V E L E A R N E D I N E T E C 1 1 0 1 … I N J A V A

2. C => JAVA

Page 2: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

THE PLAN…

• I'll show you something you (hopefully) recognize from C.• We'll re-write it (together) in Java.• As we go, we'll discuss differences…• …and generate a (partial) list of them.

Page 3: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

(STANDARD) INPUT / OUTPUT

// In a file called whatever.c#include <stdlib.h>

int main(int argc, char ** argv){char name[256]; printf("Enter name: "); scanf("%s", name); printf("Hello, '%s'!\n", name);}

Page 4: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

ARRAYS (ETEC1101 LAB2)// In a file called lab2.c#include <stdio.h>#include <stdlib.h>

int main(){const int NUM = 50;int i;double avg = 0, var = 0;int values[NUM]; // NUM *must* be const (why?)

srand(time(NULL));

for (i=0; i<NUM; i++) { values[i] = rand() % 100 + 1; printf("%02d) %d\n", i, values[i]); avg += values[i]; } avg /= NUM; for (i=0; i<NUM; i++) var += (values[i] - avg) * (values[i] - avg); var /= NUM;

printf("Avg: %f\nVar: %f\n", avg, var); return 0;}

Page 5: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

FUNCTIONS (ETEC1101 LAB4-ISH)// In a file called blah_blah.c#include <stdio.h>#include <stdlib.h>#include <string.h>

unsigned int count_word(char * text, char letter){unsigned int count = 0; for (int i=0; i<strlen(text); i++) { if (text[i] == letter) count++; } return count;}

int main(){char test_text[256]="Now is the winter of our discontent\n";char test_letter = 's';

strcat(test_text,"Made glorious summer by this son of York"); printf("Text: \n\"%s\"\n", test_text); printf("\tOccurrences of '%c':", test_letter); printf("%u\n", count_word(test_text, test_letter)); return 0;}

Page 6: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

C STRUCTURES / JAVA CLASSES

• C's closest approximation to OOP is structures• No methods (associated functions), only attributes (variables)• Usually, functions are used to modify a structure• C separates the data (structures) from the code (functions / program).• It is up to the programmer to connect them

• Java (and C++ and …) combine data and code to manipulate it into objects.

• A class is a description of a specific category of object.• E.g. LinkedList, Spaceship, etc.

• An object is a variable of the category described by a class.• E.g. My linked list of students; Player1 and Player2's spaceship.• All objects share the same structure, but separate values from

other instances of that class.

Page 7: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

OOP IN JAVA

• Java is (almost) completely an OOP-based language.• Our first few programs will have this structure:

package xyz;// import statements

public class ABC{ protected int mAttr; // attribute public ABC(____) { /* code */} // constructor public void qrz(___) { /* code */ } // method public static void main(String[] args) { // Main code goes here. Usually we'll create // instances of abc and test them somehow. }}

• We'll explore the meaning of each part as we go.• We'll also look at alternative organizations.

Page 8: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

"OOP" (ETEC1101 LAB6-ISH)

// In a file called student.h#ifndef _STUDENT_H_#define _STUDENT_H_

// A new "type" description. All students will share this structuretypedef struct{ char mFName[64]; char mLName[64]; unsigned int mID;}Student;

// Initializes a new student record with all relevant data.// IMPORTANT: s must point to a valid memory location OR ELSE!void init_student(Student * s, char * fname, char * lname, unsigned int id);

// Prints the student to stdoutvoid print_student(Student * s);

#endif

Page 9: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

"OOP" (ETEC1101 LAB6-ISH), CONT.

// In a file called student.c#include "student.h"#include <string.h>#include <stdio.h>#include <stdlib.h>

void init_student(Student * s, char * fname, char * lname, unsigned int id){ strcpy(s->mFName, fname); strcpy(s->mLName, lname); s->mID = id;}

void print_student(Student * s){ printf("[%u: %s %s]\n", s->mID, s->mFName, s->mLName);}

Page 10: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

"OOP" (ETEC1101 LAB6-ISH), CONT.// In a file called student_dbase.h#ifndef _STUDENT_DBASE_H_#define _STUDENT_DBASE_H_#include "student.h"

// A new "type" description. All StudentDBase's will share this structuretypedef struct{ Student * mRecords; // Will be dynamically allocated as needed. unsigned int mNumRecords;}StudentDBase;

// Initializes a student database.void init_dbase(StudentDBase * sd);// Destroys a student database. MUST BE CALLED to avoid a memory leak.// ALSO: init_dbase must've been called initially on this dbase.void destroy_dbase(StudentDBase * sd);// Adds a new student to the end of the database.void add_student(StudentDBase * sd, char * fname, char * lname, unsigned int id);// Removes a student for the database (no effect if not found)void remove_student(StudentDBase * sd, unsigned int id);// Returns the pointer to the student record with the given id, or NULL// if not found.Student * find_student(StudentDBase * sd, unsigned int id);// Prints all students to stdoutvoid print_all(StudentDBase * sd);

#endif

Page 11: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

"OOP" (ETEC1101 LAB6-ISH), CONT.// In a file called student_dbase.c#include "student_dbase.h"#include "student.h"#include <stdlib.h>#include <string.h>#include <stdio.h>

void init_dbase(StudentDBase * sd){ sd->mRecords = NULL; sd->mNumRecords = 0;}

void destroy_dbase(StudentDBase * sd){ if (sd->mRecords) free(sd->mRecords); sd->mRecords = NULL; sd->mNumRecords = 0;}

Page 12: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

"OOP" (ETEC1101 LAB6-ISH), CONT.// student_dbase.c, cont.void add_student(StudentDBase * sd, char * fname, char * lname, unsigned int id){Student * new_records; // Re-allocate the array new_records = (Student*)malloc(sizeof(Student) * (sd->mNumRecords + 1)); if (new_records) { // Copy the old records to the new, if any if (sd->mNumRecords > 0) memcpy(new_records, sd->mRecords, sizeof(Student) * sd->mNumRecords);

// Initialize the last one with the given data init_student(&new_records[sd->mNumRecords], fname, lname, id);

// Copy new_records to sd's mRecord and increment the student count if (sd->mRecords) free(sd->mRecords); sd->mRecords = new_records; sd->mNumRecords++; }}

Page 13: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

"OOP" (ETEC1101 LAB6-ISH), CONT.// student_dbase.c, cont.void remove_student(StudentDBase * sd, unsigned int id){int pos = 0;Student * new_records = NULL;

while (pos < sd->mNumRecords && sd->mRecords[pos].mID != id) pos++; if (pos != sd->mNumRecords) { if (sd->mNumRecords > 1) { // Re-allocate the array to be of a smaller size new_records = (Student*)malloc(sizeof(Student) * (sd->mNumRecords - 1)); if (new_records) { // Copy up to position pos from current records memcpy(new_records, sd->mRecords, sizeof(Student) * pos); // Copy everything after position pos from current records memcpy(&new_records[pos], &sd->mRecords[pos + 1], sizeof(Student) * (sd->mNumRecords – pos - 1)); } } // Copy new records to sd's mRecord and decrement the student count if (sd->mRecords) free(sd->mRecords); sd->mRecords = new_records; sd->mNumRecords--; }}

Page 14: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

"OOP" (ETEC1101 LAB6-ISH), CONT.

// student_dbase.c, finishedStudent * find_student(StudentDBase * sd, unsigned int id){int i; for (i=0; i<sd->mNumRecords; i++) { if (sd->mRecords[i].mID == id) return &sd->mRecords[i]; } return NULL;}

void print_all(StudentDBase * sd){int i; for (i = 0; i < sd->mNumRecords; i++) print_student(&sd->mRecords[i]);}

Page 15: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

"OOP" (ETEC1101 LAB6-ISH), CONT.

// In a file, main.c#include "student_dbase.h"#include <string.h>

int main(){StudentDBase my_dbase;Student * temp; init_dbase(&my_dbase); add_student(&my_dbase, "Bob", "Smith", 14); add_student(&my_dbase, "Jane", "Doe", 83); add_student(&my_dbase, "Jake", "Dog", 56); remove_student(&my_dbase, 16); remove_student(&my_dbase, 83); temp = find_student(&my_dbase, 56); if (temp) strcpy(temp->mLName, "Human"); // Hmmm... print_all(&my_dbase); return 0;}

Page 16: …WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.

MINI-LAB

• [Try it first without looking back at the slides]• Write a class called Foo• Should have a private String attribute called theWord.• Should have a public method named speakIt• One parameter: an integer, n• The method should output (on one line) theWord n times (no

spaces)

• The main program (in the class) should…• create two instances of Foo: one called Bob (his word is

"kitten") and one called Sue (her word is "dragon").• Make Bob speakIt 5 times, Sue speakIt 3 times, then Bob

speakIt 4 times.

• [A good example of a quiz question…]