Java Programming

Dr. Ernest J. Friedman-Hill

ejfried@herzberg.ca.sandia.gov

http://herzberg.ca.sandia.gov/JavaCourse

 

 

‘Tis well blown, lads.

This morning, like the spirit of a youth

That means to be of note, begins betimes.

So, so. Come, give me that: this way; well said.

Fare thee well, dame; whate’er becomes of me,

This is a soldier’s kiss. Rebukable

And worthy shameful check it were to stand

On more mechanic compliment. I’ll leave thee

Now like a man of steel. You that will fight,

Follow me close; I’ll bring you to’t. Adieu.

--Antony and Cleopatra, IV.iv.25-33.

 

If your development tools don’t suck, you’re not on the bleeding edge.

-- Overheard at JavaOne opening reception

Your Instructor

What We’ll Do Today

What We’ll Do Later

What We Won’t Do

What You’ll Do

What You’ll Need

David Flanagan, "Java in a Nutshell", O'Reilly, 1997 (2nd ed.), ISBN 1-56592-262-X

http://www.javasoft.com/products/jdk/

Grading System

Who You Are

What’s Wrong with (X)?

Toasters and Televisions

Introducing Java

The Java Virtual Machine

The Java Virtual Machine

The Java Virtual Machine

Java and the WWW

History of the WWW

Java and Microsoft

The Java Language

int i, sum=0;

for (i=1; i<=10; i++)

{

sum = sum + i;

}

 

A C Program

 

int

main(int argc, char** argv)

{

int i, sum=0;

for (i=1; i<=10; i++)

{

sum = sum + i;

}

printf("The sum of 1 to 10 is %d\n",

sum);

return 0;

}

 

A Java Program

public class SumOneToTen

{

public static void

main(String[] argv)

{

int i, sum=0;

for (i=1; i<=10; i++)

{

sum = sum + i;

}

System.out.println("The sum of 1 " +

"to 10 is " +

sum);

return;

}

}

A Java Program

public class SumOneToTen {...}

public static void main(String [] argv)

Running A Java Program

javac SumOneToTen.java

C:\> java SumOneToTen

The sum of 1 to 10 is 55

C:\>

Code Formatting

Java is "Object Oriented"

PROGRAM RECTANGLE

REAL*8 X, Y, H, W, A

X=10.0

Y=10.0

H=5.0

W=12.0

A = RAREA(H,W)

Java is "Object Oriented"

typedef struct rect {

double x,y,h,w;

} rect;

rect r = {10.0, 10.0, 5.0, 12.0};

double area = rectarea(r);

Java is "Object Oriented"

public class Rect

{

double m_x, m_y, m_h, m_w;

public Rect(double x, double y,

double h, double w)

{

m_x = x; m_y = y;

m_h = h; m_w = w;

}

double area() { return m_h*m_w; }

}

Rect r = new Rect(10, 10, 5, 12);

double area = r.area();

 

Circle c = new Circle(10,10,5);

float area = c.area();

Basic Java Syntax

int i = (int) 3.2; // OK

int i = (int) "Hello!"; // error

if (i)... // error

boolean fun = true;

if (fun) ...

Basic Java Syntax

int[] intArray = new int[20];

int[] intArray;

int i = intArray[3]; // runtime error

intArray = new int[20];

int j = intArray[3]; // OK

int length = intArray.length;

String[] sArray = new String[2];

sArray[0] = "A String";

sArray[1] = "Another String";

Basic Java Syntax

String aString = "A String"; // or…

String aString = new String("ABC");

if (aString.equals("Some String"))…

int i = aString.compareTo(bString);

char c = aString.charAt(13);

int len = "A lovely String".length();

String noString = null;

int i = noString.length(); // error!

String emptyString = "";

int j = emptyString.length(); // OK, 0

String ans = "The answer is " + 42;

Basic Java Syntax

System.out.print(...);

System.out.flush(...);

System.out.println(...);

import java.io.*; // top of file

 

public static void

main(String [] argv)

throws IOException // add this

{

BufferedReader stdin =

new BufferedReader(new

InputStreamReader(System.in), 1);

 

String s = stdin.readLine();

int i =

Integer.parseInt(stdin.readLine());

 

Java vs. C/C++

Some things removed: Pointers, addresses, unions, preprocessor, multiple inheritance, enums, unsigned types, operator overloading

Java vs. C/C++

Java vs. C/C++

Java vs. C/C++

Java API Documentation

Other Java Information

Your First Assignment

Write a program that prompts for input strings from the user, then sorts them and prints the sorted results. You can use any sorting method you’d like; you won’t be graded particularly on the efficiency of your program. The compareTo() method in the java.lang.String class should prove helpful. Use the API documentation to read about it. You’re going to have to use the documents a lot during the course, so take the time to familiarize yourself with their layout and contents. Don’t be afraid to browse around and read whatever looks interesting.

Your First Assignment