Java Programming: EECS 484 Discussion 1 - Prof. Kristen R. Lefevre, Exams of Database Management Systems (DBMS)

An introduction to java programming for c++ programmers, including the differences between java and c++, java naming conventions, and exception handling. It also includes examples of method definitions, static and final variables, inheritance, and the use of streamtokenizer for reading input.

Typology: Exams

Pre 2010

Uploaded on 09/02/2009

koofers-user-ijl
koofers-user-ijl 🇺🇸

10 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EECS 484 Discussion 1
Jay Athalye
jathalye [at] eecs.umich.edu
Office Hours: Wednesday, 1:30-3:30 1637 CSE
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Java Programming: EECS 484 Discussion 1 - Prof. Kristen R. Lefevre and more Exams Database Management Systems (DBMS) in PDF only on Docsity!

EECS 484 Discussion 1

Jay Athalye jathalye [at] eecs.umich.edu Office Hours: Wednesday, 1:30-3:30 1637 CSE

Java Resources

Java for C++ Programmers –

MUST READ

http://triton.towson.edu/~mzimand/os/Lect2-java-tutorial.html Learning a New Programming Language: Java for C++ Programmers http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial Sun Java Tutorials http://java.sun.com/docs/books/tutorial/ Java 1.5 API Reference http://java.sun.com/j2se/1.5.0/docs/api/index.html

class Pair{int x,y;} class PairExample { void f() { int n = 1; Pair p = new Pair(); // this is the way to initialize p p.x = 2; p.y = 3; System.out.println(n); // prints 1 System.out.println(p.x); // prints 2 g(n,p); System.out.println(n); // still prints 1 System.out.println(p.x); // prints 100 } void g(int num, Pair ptr) { System.out.println(num); // prints 1 num =

// changes only the local copy System.out.println(num); // prints 17 System.out.println(ptr.x); // prints 2 ptr.x = 100; // changes x field of caller's Pair ptr = null; // changes only the local ptr } }

What you should know…

No stand-alone functions Method definitions are contained within the class definition main() must -

be inside a class

be

public

static

void

have one argument, a string array

Java Naming Conventions

classes

class Account class BankAccount

methods

void turnRight() int startLeftMotor()

variables

int headCount

constants

static final int MAX_VALUE = 10

Static, Final and Public…

Static – only one member shared across all instances – globals [Math.PI, Integer.parseInt()] Final – constant (similar to const) Public,Private,Protected – Similar to C++

Example of inheritance

class

BaseClass{

int

inheritableMember;

} interface

Move{

void

goStraight();

void

turnLeft();

} class

DerivedClass

extends

BaseClass

implements

Move

void

goStraight(){

mandatory

implementations

of

//functions

void

turnLeft()

these

may

use

inheritableMember

Exception Handling

try { ... foo.bar(); ... a[i] = 17; ... }catch (IndexOutOfBoundsException e) {System.err.println(“oops: " + e);}

import java.io.; /

import

is

like

#include,

except

it's

not

a

literal

copy

and

paste.

Allows

the

use

of

other

classes

in

other

files.

java.io

is

a

special

class

that

comes

with

the

language

(much

like

) */

class WordLocatorTokenizer { //

Members

(variables

and

functions)

of

class

go

here

Functions

are

defined

inside

class

definition.

(unlike

C++)

import java.io.; class WordLocatorTokenizer { public static void main(String[] args) { // Main definition goes here } / public – means same thing as in C++ static – same as in C++ (one instance across all instances of class) void – same as in C++ main() - called when WordLocatorTokenizer is started as a program String[] args – like *argv[] in c++ main definition. argc is not needed because string objects have a .length attribute.

import java.io.*; class WordLocatorTokenizer { public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer st = new StreamTokenizer(in); while (true) { int nexttoken = st.nextToken(); if (nexttoken == StreamTokenizer.TT_WORD) { System.out.println("Word: " + st.sval); } else if (nexttoken == StreamTokenizer.TT_NUMBER) { System.out.println("Number: " + st.nval); } else if (nexttoken == StreamTokenizer.TT_EOF) { System.out.println("All tokens Read"); return; } } } }

Compiling…

javac

WordLocatorTokenizer.java

WordLocatorTokenizer.java:10: unreported exception java.io.IOException; must

be

caught

or

declared

to

be

thrown int

nexttoken

st.nextToken();

^ 1

error

What will happen on compiling?

class ClassName {

void functionCall() {

} public

static

void

main(String[]

args)

... functionCall(); ...

Some things to go over…

String methods - Arrays - Collections - List - Vector - Map